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