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