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