10 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
22 #define INADDR_NONE 0xffffffff
25 int sock_connect(char *host, char *service, char *protocol)
30 struct sockaddr_in sin;
31 struct sockaddr_in egress_sin;
34 if (host == NULL) return(-1);
35 if (service == NULL) return(-1);
36 if (protocol == NULL) return(-1);
38 memset(&sin, 0, sizeof(sin));
39 sin.sin_family = AF_INET;
41 pse = getservbyname(service, protocol);
43 sin.sin_port = pse->s_port;
44 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
45 fprintf(stderr, "Can't get %s service entry: %s\n",
46 service, strerror(errno));
49 phe = gethostbyname(host);
51 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
52 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
53 fprintf(stderr, "Can't get %s host entry: %s\n",
54 host, strerror(errno));
57 if ((ppe = getprotobyname(protocol)) == 0) {
58 fprintf(stderr, "Can't get %s protocol entry: %s\n",
59 protocol, strerror(errno));
62 if (!strcmp(protocol, "udp")) {
68 s = socket(PF_INET, type, ppe->p_proto);
70 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
74 /* Now try to connect to the remote host. */
75 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
76 fprintf(stderr, "Can't connect to %s:%s: %s\n",
77 host, service, strerror(errno));
88 * sock_read_to() - input binary data from socket, with a settable timeout.
89 * Returns the number of bytes read, or -1 for error.
90 * If keep_reading_until_full is nonzero, we keep reading until we get the number of requested bytes
92 int sock_read_to(int sock, char *buf, int bytes, int timeout, int keep_reading_until_full)
106 retval = select(sock+1, &rfds, NULL, NULL, &tv);
108 if (FD_ISSET(sock, &rfds) == 0) { /* timed out */
109 fprintf(stderr, "sock_read() timed out.\n");
113 rlen = read(sock, &buf[len], bytes-len);
115 fprintf(stderr, "sock_read() failed: %s\n",
120 if (!keep_reading_until_full) return(len);
127 * sock_read() - input binary data from socket.
128 * Returns the number of bytes read, or -1 for error.
130 int sock_read(int sock, char *buf, int bytes, int keep_reading_until_full)
132 return sock_read_to(sock, buf, bytes, 30, keep_reading_until_full);
137 * sock_write() - send binary to server.
138 * Returns the number of bytes written, or -1 for error.
140 int sock_write(int sock, char *buf, int nbytes)
142 int bytes_written = 0;
144 while (bytes_written < nbytes) {
145 retval = write(sock, &buf[bytes_written],
146 nbytes - bytes_written);
150 bytes_written = bytes_written + retval;
152 return (bytes_written);
158 * Input string from socket - implemented in terms of sock_read()
161 int sock_getln(int sock, char *buf, int bufsize)
165 /* Read one character at a time.
168 if (sock_read(sock, &buf[i], 1, 1) < 0) return(-1);
169 if (buf[i] == '\n' || i == (bufsize-1))
173 /* If we got a long line, discard characters until the newline.
175 if (i == (bufsize-1))
176 while (buf[i] != '\n')
177 if (sock_read(sock, &buf[i], 1, 1) < 0) return(-1);
179 /* Strip any trailing CR and LF characters.
183 && ( (buf[i - 1]==13)
184 || ( buf[i - 1]==10)) ) {
194 * sock_puts() - send line to server - implemented in terms of serv_write()
195 * Returns the number of bytes written, or -1 for error.
197 int sock_puts(int sock, char *buf)
201 i = sock_write(sock, buf, strlen(buf));
203 j = sock_write(sock, "\n", 1);