* misc bugfixes and cleanups
[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/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <netdb.h>
21 #include <string.h>
22 #include <pwd.h>
23 #include <errno.h>
24 #include <stdarg.h>
25 #include "citadel.h"
26 #ifndef HAVE_SNPRINTF
27 #include "snprintf.h"
28 #endif
29 #include "sysdep_decls.h"
30
31 #ifndef INADDR_NONE
32 #define INADDR_NONE 0xffffffff
33 #endif
34
35 int sock_connect(char *host, char *service, char *protocol)
36 {
37         struct hostent *phe;
38         struct servent *pse;
39         struct protoent *ppe;
40         struct sockaddr_in sin;
41         int s, type;
42
43         memset(&sin, 0, sizeof(sin));
44         sin.sin_family = AF_INET;
45
46         pse = getservbyname(service, protocol);
47         if (pse) {
48                 sin.sin_port = pse->s_port;
49         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
50                 lprintf(3, "Can't get %s service entry: %s\n",
51                         service, strerror(errno));
52                 return(-1);
53         }
54         phe = gethostbyname(host);
55         if (phe) {
56                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
57         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
58                 lprintf(3, "Can't get %s host entry: %s\n",
59                         host, strerror(errno));
60                 return(-1);
61         }
62         if ((ppe = getprotobyname(protocol)) == 0) {
63                 lprintf(3, "Can't get %s protocol entry: %s\n",
64                         protocol, strerror(errno));
65                 return(-1);
66         }
67         if (!strcmp(protocol, "udp")) {
68                 type = SOCK_DGRAM;
69         } else {
70                 type = SOCK_STREAM;
71         }
72
73         s = socket(PF_INET, type, ppe->p_proto);
74         if (s < 0) {
75                 lprintf(3, "Can't create socket: %s\n", strerror(errno));
76                 return(-1);
77         }
78
79         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
80                 lprintf(3, "can't connect to %s.%s: %s\n",
81                         host, service, strerror(errno));
82                 return(-1);
83         }
84
85         return (s);
86 }
87
88 /*
89  * sock_read() - input binary data from socket.
90  * Returns the number of bytes read, or -1 for error.
91  */
92 int sock_read(int sock, char *buf, int bytes)
93 {
94         int len, rlen;
95
96         len = 0;
97         while (len < bytes) {
98                 rlen = read(sock, &buf[len], bytes - len);
99                 if (rlen < 1) {
100                         return (-1);
101                 }
102                 len = len + rlen;
103         }
104         return (len);
105 }
106
107
108 /*
109  * sock_write() - send binary to server.
110  * Returns the number of bytes written, or -1 for error.
111  */
112 int sock_write(int sock, char *buf, int nbytes)
113 {
114         int bytes_written = 0;
115         int retval;
116         while (bytes_written < nbytes) {
117                 retval = write(sock, &buf[bytes_written],
118                                nbytes - bytes_written);
119                 if (retval < 1) {
120                         return (-1);
121                 }
122                 bytes_written = bytes_written + retval;
123         }
124         return (bytes_written);
125 }
126
127
128 /*
129  * Input string from socket - implemented in terms of sock_read()
130  * 
131  */
132 int sock_gets(int sock, char *buf)
133 {
134         int i;
135
136         /* Read one character at a time.
137          */
138         for (i = 0;; i++) {
139                 if (sock_read(sock, &buf[i], 1) < 0) return(-1);
140                 if (buf[i] == '\n' || i == 255)
141                         break;
142         }
143
144         /* If we got a long line, discard characters until the newline.
145          */
146         if (i == 255)
147                 while (buf[i] != '\n')
148                         if (sock_read(sock, &buf[i], 1) < 0) return(-1);
149
150         /* Strip any trailing CR and LF characters.
151          */
152         buf[i] = 0;
153         while ( (strlen(buf)>0)
154               && ((buf[strlen(buf)-1]==13)
155               || (buf[strlen(buf)-1]==10)) ) {
156                 buf[strlen(buf)-1] = 0;
157         }
158         return(strlen(buf));
159 }
160
161
162 /*
163  * sock_puts() - send line to server - implemented in terms of serv_write()
164  * Returns the number of bytes written, or -1 for error.
165  */
166 int sock_puts(int sock, char *buf)
167 {
168         int i, j;
169
170         i = sock_write(sock, buf, strlen(buf));
171         if (i<0) return(i);
172         j = sock_write(sock, "\n", 1);
173         if (j<0) return(j);
174         return(i+j);
175 }