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).
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
29 #include "sysdep_decls.h"
32 #define INADDR_NONE 0xffffffff
35 int sock_connect(char *host, char *service, char *protocol)
40 struct sockaddr_in sin;
43 memset(&sin, 0, sizeof(sin));
44 sin.sin_family = AF_INET;
46 pse = getservbyname(service, protocol);
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));
54 phe = gethostbyname(host);
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));
62 if ((ppe = getprotobyname(protocol)) == 0) {
63 lprintf(3, "Can't get %s protocol entry: %s\n",
64 protocol, strerror(errno));
67 if (!strcmp(protocol, "udp")) {
73 s = socket(PF_INET, type, ppe->p_proto);
75 lprintf(3, "Can't create socket: %s\n", strerror(errno));
79 /* FIX ... the alarm clock is a problem for multithreaded programs because all
80 * threads receive the signal.
81 signal(SIGALRM, timeout);
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));
92 signal(SIGALRM, SIG_IGN);
99 * sock_read() - input binary data from socket.
100 * Returns the number of bytes read, or -1 for error.
102 int sock_read(int sock, char *buf, int bytes)
107 while (len < bytes) {
108 rlen = read(sock, &buf[len], bytes - len);
119 * sock_write() - send binary to server.
120 * Returns the number of bytes written, or -1 for error.
122 int sock_write(int sock, char *buf, int nbytes)
124 int bytes_written = 0;
126 while (bytes_written < nbytes) {
127 retval = write(sock, &buf[bytes_written],
128 nbytes - bytes_written);
132 bytes_written = bytes_written + retval;
134 return (bytes_written);
139 * Input string from socket - implemented in terms of sock_read()
142 int sock_gets(int sock, char *buf)
146 /* Read one character at a time.
149 if (sock_read(sock, &buf[i], 1) < 0) return(-1);
150 if (buf[i] == '\n' || i == 255)
154 /* If we got a long line, discard characters until the newline.
157 while (buf[i] != '\n')
158 if (sock_read(sock, &buf[i], 1) < 0) return(-1);
160 /* Strip any trailing CR and LF characters.
162 while ( (strlen(buf)>0)
163 && ((buf[strlen(buf)-1]==13)
164 || (buf[strlen(buf)-1]==10)) ) {
165 buf[strlen(buf)-1] = 0;
172 * sock_puts() - send line to server - implemented in terms of serv_write()
173 * Returns the number of bytes written, or -1 for error.
175 int sock_puts(int sock, char *buf)
179 i = sock_write(sock, buf, strlen(buf));
181 j = sock_write(sock, "\n", 1);