* Temporary hack to ig_tcp_server() to listen on an arbitrary port if the
[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 /* FIX ... the alarm clock is a problem for multithreaded programs because all
80  * threads receive the signal.
81         signal(SIGALRM, timeout);
82         alarm(30);
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         alarm(0);
92         signal(SIGALRM, SIG_IGN);
93  */
94
95         return (s);
96 }
97
98 /*
99  * sock_read() - input binary data from socket.
100  * Returns the number of bytes read, or -1 for error.
101  */
102 int sock_read(int sock, char *buf, int bytes)
103 {
104         int len, rlen;
105
106         len = 0;
107         while (len < bytes) {
108                 rlen = read(sock, &buf[len], bytes - len);
109                 if (rlen < 1) {
110                         return (-1);
111                 }
112                 len = len + rlen;
113         }
114         return (len);
115 }
116
117
118 /*
119  * sock_write() - send binary to server.
120  * Returns the number of bytes written, or -1 for error.
121  */
122 int sock_write(int sock, char *buf, int nbytes)
123 {
124         int bytes_written = 0;
125         int retval;
126         while (bytes_written < nbytes) {
127                 retval = write(sock, &buf[bytes_written],
128                                nbytes - bytes_written);
129                 if (retval < 1) {
130                         return (-1);
131                 }
132                 bytes_written = bytes_written + retval;
133         }
134         return (bytes_written);
135 }
136
137
138 /*
139  * Input string from socket - implemented in terms of sock_read()
140  * 
141  */
142 int sock_gets(int sock, char *buf)
143 {
144         int i;
145
146         /* Read one character at a time.
147          */
148         for (i = 0;; i++) {
149                 if (sock_read(sock, &buf[i], 1) < 0) return(-1);
150                 if (buf[i] == '\n' || i == 255)
151                         break;
152         }
153
154         /* If we got a long line, discard characters until the newline.
155          */
156         if (i == 255)
157                 while (buf[i] != '\n')
158                         if (sock_read(sock, &buf[i], 1) < 0) return(-1);
159
160         /* Strip any trailing CR and LF characters.
161          */
162         while ( (strlen(buf)>0)
163               && ((buf[strlen(buf)-1]==13)
164               || (buf[strlen(buf)-1]==10)) ) {
165                 buf[strlen(buf)-1] = 0;
166         }
167         return(strlen(buf));
168 }
169
170
171 /*
172  * sock_puts() - send line to server - implemented in terms of serv_write()
173  * Returns the number of bytes written, or -1 for error.
174  */
175 int sock_puts(int sock, char *buf)
176 {
177         int i, j;
178
179         i = sock_write(sock, buf, strlen(buf));
180         if (i<0) return(i);
181         j = sock_write(sock, "\n", 1);
182         if (j<0) return(j);
183         return(i+j);
184 }