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>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
31 #include "dynloader.h"
35 #include "sysdep_decls.h"
38 #define INADDR_NONE 0xffffffff
41 int sock_connect(char *host, char *service, char *protocol)
46 struct sockaddr_in sin;
49 memset(&sin, 0, sizeof(sin));
50 sin.sin_family = AF_INET;
52 pse = getservbyname(service, protocol);
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));
60 phe = gethostbyname(host);
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));
68 if ((ppe = getprotobyname(protocol)) == 0) {
69 lprintf(3, "Can't get %s protocol entry: %s\n",
70 protocol, strerror(errno));
73 if (!strcmp(protocol, "udp")) {
79 s = socket(PF_INET, type, ppe->p_proto);
81 lprintf(3, "Can't create socket: %s\n", strerror(errno));
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));
95 * sock_read() - input binary data from socket.
96 * Returns the number of bytes read, or -1 for error.
98 int sock_read(int sock, char *buf, int bytes)
103 while (len < bytes) {
104 rlen = read(sock, &buf[len], bytes - len);
115 * sock_write() - send binary to server.
116 * Returns the number of bytes written, or -1 for error.
118 int sock_write(int sock, char *buf, int nbytes)
120 int bytes_written = 0;
122 while (bytes_written < nbytes) {
123 retval = write(sock, &buf[bytes_written],
124 nbytes - bytes_written);
128 bytes_written = bytes_written + retval;
130 return (bytes_written);
136 * Input string from socket - implemented in terms of sock_read()
139 int sock_gets(int sock, char *buf)
143 /* Read one character at a time.
146 if (sock_read(sock, &buf[i], 1) < 0) return(-1);
147 if (buf[i] == '\n' || i == (SIZ-1))
151 /* If we got a long line, discard characters until the newline.
154 while (buf[i] != '\n')
155 if (sock_read(sock, &buf[i], 1) < 0) return(-1);
157 /* Strip any trailing CR and LF characters.
160 while ( (strlen(buf)>0)
161 && ((buf[strlen(buf)-1]==13)
162 || (buf[strlen(buf)-1]==10)) ) {
163 buf[strlen(buf)-1] = 0;
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.
173 int ml_sock_gets(int sock, char *buf) {
177 g = sock_gets(sock, buf);
178 if (g < 4) return(g);
179 if (buf[3] != '-') return(g);
182 g = sock_gets(sock, bigbuf);
183 if (g < 0) return(g);
184 } while ( (g >= 4) && (bigbuf[3] == '-') );
191 * sock_puts() - send line to server - implemented in terms of serv_write()
192 * Returns the number of bytes written, or -1 for error.
194 int sock_puts(int sock, char *buf)
198 i = sock_write(sock, buf, strlen(buf));
200 j = sock_write(sock, "\n", 1);
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.
210 int sock_puts_crlf(int sock, char *buf)
214 i = sock_write(sock, buf, strlen(buf));
216 j = sock_write(sock, "\r\n", 2);