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