4 * TCP socket module for WebCit
16 #include <sys/types.h>
18 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
33 #define INADDR_NONE 0xffffffff
36 RETSIGTYPE timeout(int signum)
38 fprintf(stderr, "Connection timed out.\n");
42 int connectsock(char *host, char *service, char *protocol)
47 struct sockaddr_in sin;
50 bzero((char *) &sin, sizeof(sin));
51 sin.sin_family = AF_INET;
53 pse = getservbyname(service, protocol);
55 sin.sin_port = pse->s_port;
56 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
57 fprintf(stderr, "Can't get %s service entry\n", service);
60 phe = gethostbyname(host);
62 bcopy(phe->h_addr, (char *) &sin.sin_addr, phe->h_length);
63 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
64 fprintf(stderr, "Can't get %s host entry: %s\n",
65 host, strerror(errno));
68 if ((ppe = getprotobyname(protocol)) == 0) {
69 fprintf(stderr, "Can't get %s protocol entry: %s\n",
70 protocol, strerror(errno));
73 if (!strcmp(protocol, "udp"))
78 s = socket(PF_INET, type, ppe->p_proto);
80 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
83 signal(SIGALRM, timeout);
86 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
87 fprintf(stderr, "can't connect to %s.%s: %s\n",
88 host, service, strerror(errno));
92 signal(SIGALRM, SIG_IGN);
101 * Input binary data from socket
103 void serv_read(char *buf, int bytes)
108 while (len < bytes) {
109 rlen = read(WC->serv_sock, &buf[len], bytes - len);
111 fprintf(stderr, "Server connection broken: %s\n",
123 * input string from pipe
125 void serv_gets(char *strbuf)
133 serv_read(&buf[0], 1);
136 } while ((ch != 10) && (ch != 13) && (ch != 0) && (len < 255));
138 /* fprintf(stderr, ">%s\n", strbuf); */
144 * send binary to server
146 void serv_write(char *buf, int nbytes)
148 int bytes_written = 0;
150 while (bytes_written < nbytes) {
151 retval = write(WC->serv_sock, &buf[bytes_written],
152 nbytes - bytes_written);
154 fprintf(stderr, "Server connection broken: %s\n",
160 bytes_written = bytes_written + retval;
166 * send line to server
168 void serv_puts(char *string)
172 sprintf(buf, "%s\n", string);
173 serv_write(buf, strlen(buf));
178 * convenience function to send stuff to the server
180 void serv_printf(const char *format,...)
185 va_start(arg_ptr, format);
186 vsprintf(buf, format, arg_ptr);
190 serv_write(buf, strlen(buf));
191 /* fprintf(stderr, "<%s", buf); */