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