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).
20 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
32 #include "dynloader.h"
36 #include "sysdep_decls.h"
37 #include <clientsocket.h>
40 #define INADDR_NONE 0xffffffff
43 int sock_connect(char *host, char *service, char *protocol)
48 struct sockaddr_in sin;
51 memset(&sin, 0, sizeof(sin));
52 sin.sin_family = AF_INET;
54 pse = getservbyname(service, protocol);
56 sin.sin_port = pse->s_port;
57 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
58 lprintf(3, "Can't get %s service entry: %s\n",
59 service, strerror(errno));
62 phe = gethostbyname(host);
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(3, "Can't get %s host entry: %s\n",
67 host, strerror(errno));
70 if ((ppe = getprotobyname(protocol)) == 0) {
71 lprintf(3, "Can't get %s protocol entry: %s\n",
72 protocol, strerror(errno));
75 if (!strcmp(protocol, "udp")) {
81 s = socket(PF_INET, type, ppe->p_proto);
83 lprintf(3, "Can't create socket: %s\n", strerror(errno));
87 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
88 lprintf(3, "can't connect to %s:%s: %s\n",
89 host, service, strerror(errno));
99 * sock_read_to() - input binary data from socket, with a settable timeout.
100 * Returns the number of bytes read, or -1 for error.
102 int sock_read_to(int sock, char *buf, int bytes, int timeout)
116 retval = select(sock+1, &rfds, NULL, NULL, &tv);
118 if (FD_ISSET(sock, &rfds) == 0) { /* timed out */
119 lprintf(9, "sock_read() timed out.\n");
123 rlen = read(sock, &buf[len], bytes-len);
125 lprintf(2, "sock_read() failed: %s\n",
136 * sock_read() - input binary data from socket.
137 * Returns the number of bytes read, or -1 for error.
139 inline int sock_read(int sock, char *buf, int bytes)
141 return sock_read_to(sock, buf, bytes, CLIENT_TIMEOUT);
146 * sock_write() - send binary to server.
147 * Returns the number of bytes written, or -1 for error.
149 int sock_write(int sock, char *buf, int nbytes)
151 int bytes_written = 0;
153 while (bytes_written < nbytes) {
154 retval = write(sock, &buf[bytes_written],
155 nbytes - bytes_written);
159 bytes_written = bytes_written + retval;
161 return (bytes_written);
167 * Input string from socket - implemented in terms of sock_read()
170 int sock_gets(int sock, char *buf)
174 /* Read one character at a time.
177 if (sock_read(sock, &buf[i], 1) < 0) return(-1);
178 if (buf[i] == '\n' || i == (SIZ-1))
182 /* If we got a long line, discard characters until the newline.
185 while (buf[i] != '\n')
186 if (sock_read(sock, &buf[i], 1) < 0) return(-1);
188 /* Strip any trailing CR and LF characters.
191 while ( (strlen(buf)>0)
192 && ((buf[strlen(buf)-1]==13)
193 || (buf[strlen(buf)-1]==10)) ) {
194 buf[strlen(buf)-1] = 0;
200 * Multiline version of sock_gets() ... this is a convenience function for
201 * client side protocol implementations. It only returns the first line of
202 * a multiline response, discarding the rest.
204 int ml_sock_gets(int sock, char *buf) {
208 g = sock_gets(sock, buf);
209 if (g < 4) return(g);
210 if (buf[3] != '-') return(g);
213 g = sock_gets(sock, bigbuf);
214 if (g < 0) return(g);
215 } while ( (g >= 4) && (bigbuf[3] == '-') );
222 * sock_puts() - send line to server - implemented in terms of serv_write()
223 * Returns the number of bytes written, or -1 for error.
225 int sock_puts(int sock, char *buf)
229 i = sock_write(sock, buf, strlen(buf));
231 j = sock_write(sock, "\n", 1);