Moved to new module init structure.
[citadel.git] / citadel / clientsocket.c
1 /*
2  * $Id$
3  *
4  * This module handles client-side sockets opened by the Citadel server (for
5  * the client side of Internet protocols, etc.)   It does _not_ handle client
6  * sockets for the Citadel client; for that you must look in ipc_c_tcp.c
7  * (which, uncoincidentally, bears a striking similarity to this file).
8  *
9  */
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <signal.h>
16 #include <sys/types.h>
17 #include <sys/time.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <netdb.h>
22 #include <string.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include "citadel.h"
27 #include "server.h"
28 #ifndef HAVE_SNPRINTF
29 #include "snprintf.h"
30 #endif
31 #include "sysdep_decls.h"
32 #include "config.h"
33 #include "clientsocket.h"
34
35 #ifndef INADDR_NONE
36 #define INADDR_NONE 0xffffffff
37 #endif
38
39 int sock_connect(char *host, char *service, char *protocol)
40 {
41         struct hostent *phe;
42         struct servent *pse;
43         struct protoent *ppe;
44         struct sockaddr_in sin;
45         struct sockaddr_in egress_sin;
46         int s, type;
47
48         if (host == NULL) return(-1);
49         if (strlen(host) == 0) return(-1);
50         if (service == NULL) return(-1);
51         if (strlen(service) == 0) return(-1);
52         if (protocol == NULL) return(-1);
53         if (strlen(protocol) == 0) return(-1);
54
55         memset(&sin, 0, sizeof(sin));
56         sin.sin_family = AF_INET;
57
58         pse = getservbyname(service, protocol);
59         if (pse) {
60                 sin.sin_port = pse->s_port;
61         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
62                 lprintf(CTDL_CRIT, "Can't get %s service entry: %s\n",
63                         service, strerror(errno));
64                 return(-1);
65         }
66         phe = gethostbyname(host);
67         if (phe) {
68                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
69         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
70                 lprintf(CTDL_ERR, "Can't get %s host entry: %s\n",
71                         host, strerror(errno));
72                 return(-1);
73         }
74         if ((ppe = getprotobyname(protocol)) == 0) {
75                 lprintf(CTDL_CRIT, "Can't get %s protocol entry: %s\n",
76                         protocol, strerror(errno));
77                 return(-1);
78         }
79         if (!strcmp(protocol, "udp")) {
80                 type = SOCK_DGRAM;
81         } else {
82                 type = SOCK_STREAM;
83         }
84
85         s = socket(PF_INET, type, ppe->p_proto);
86         if (s < 0) {
87                 lprintf(CTDL_CRIT, "Can't create socket: %s\n", strerror(errno));
88                 return(-1);
89         }
90
91         /* If citserver is bound to a specific IP address on the host, make
92          * sure we use that address for outbound connections.
93          */
94         memset(&egress_sin, 0, sizeof(egress_sin));
95         egress_sin.sin_family = AF_INET;
96         if (strlen(config.c_ip_addr) > 0) {
97                 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
98                 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
99                         egress_sin.sin_addr.s_addr = INADDR_ANY;
100                 }
101
102                 /* If this bind fails, no problem; we can still use INADDR_ANY */
103                 bind(s, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
104         }
105
106         /* Now try to connect to the remote host. */
107         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
108                 lprintf(CTDL_ERR, "Can't connect to %s:%s: %s\n",
109                         host, service, strerror(errno));
110                 close(s);
111                 return(-1);
112         }
113
114         return (s);
115 }
116
117
118
119 /*
120  * sock_read_to() - input binary data from socket, with a settable timeout.
121  * Returns the number of bytes read, or -1 for error.
122  */
123 int sock_read_to(int sock, char *buf, int bytes, int timeout)
124 {
125         int len,rlen;
126         fd_set rfds;
127         struct timeval tv;
128         int retval;
129
130         len = 0;
131         while(len<bytes) {
132                 FD_ZERO(&rfds);
133                 FD_SET(sock, &rfds);
134                 tv.tv_sec = timeout;
135                 tv.tv_usec = 0;
136
137                 retval = select(sock+1, &rfds, NULL, NULL, &tv);
138
139                 if (FD_ISSET(sock, &rfds) == 0) {       /* timed out */
140                         lprintf(CTDL_ERR, "sock_read() timed out.\n");
141                         return(-1);
142                 }
143
144                 rlen = read(sock, &buf[len], bytes-len);
145                 if (rlen<1) {
146                         lprintf(CTDL_ERR, "sock_read() failed: %s\n",
147                                 strerror(errno));
148                         return(-1);
149                 }
150                 len = len + rlen;
151         }
152         return(bytes);
153 }
154
155
156 /*
157  * sock_read() - input binary data from socket.
158  * Returns the number of bytes read, or -1 for error.
159  */
160 INLINE int sock_read(int sock, char *buf, int bytes)
161 {
162         return sock_read_to(sock, buf, bytes, CLIENT_TIMEOUT);
163 }
164
165
166 /*
167  * sock_write() - send binary to server.
168  * Returns the number of bytes written, or -1 for error.
169  */
170 int sock_write(int sock, char *buf, int nbytes)
171 {
172         int bytes_written = 0;
173         int retval;
174         while (bytes_written < nbytes) {
175                 retval = write(sock, &buf[bytes_written],
176                                nbytes - bytes_written);
177                 if (retval < 1) {
178                         return (-1);
179                 }
180                 bytes_written = bytes_written + retval;
181         }
182         return (bytes_written);
183 }
184
185
186
187 /*
188  * Input string from socket - implemented in terms of sock_read()
189  * 
190  */
191 int sock_gets(int sock, char *buf)
192 {
193         int i;
194
195         /* Read one character at a time.
196          */
197         for (i = 0;; i++) {
198                 if (sock_read(sock, &buf[i], 1) < 0) return(-1);
199                 if (buf[i] == '\n' || i == (SIZ-1))
200                         break;
201         }
202
203         /* If we got a long line, discard characters until the newline.
204          */
205         if (i == (SIZ-1))
206                 while (buf[i] != '\n')
207                         if (sock_read(sock, &buf[i], 1) < 0) return(-1);
208
209         /* Strip any trailing CR and LF characters.
210          */
211         buf[i] = 0;
212         while ( (strlen(buf)>0)
213               && ((buf[strlen(buf)-1]==13)
214               || (buf[strlen(buf)-1]==10)) ) {
215                 buf[strlen(buf)-1] = 0;
216         }
217         return(strlen(buf));
218 }
219
220 /*
221  * Multiline version of sock_gets() ... this is a convenience function for
222  * client side protocol implementations.  It only returns the first line of
223  * a multiline response, discarding the rest.
224  */
225 int ml_sock_gets(int sock, char *buf) {
226         char bigbuf[1024];
227         int g;
228
229         g = sock_gets(sock, buf);
230         if (g < 4) return(g);
231         if (buf[3] != '-') return(g);
232
233         do {
234                 g = sock_gets(sock, bigbuf);
235                 if (g < 0) return(g);
236         } while ( (g >= 4) && (bigbuf[3] == '-') );
237
238         return(strlen(buf));
239 }
240
241
242 /*
243  * sock_puts() - send line to server - implemented in terms of serv_write()
244  * Returns the number of bytes written, or -1 for error.
245  */
246 int sock_puts(int sock, char *buf)
247 {
248         int i, j;
249
250         i = sock_write(sock, buf, strlen(buf));
251         if (i<0) return(i);
252         j = sock_write(sock, "\n", 1);
253         if (j<0) return(j);
254         return(i+j);
255 }