4 * TCP client socket module for WebCit
9 * Uncomment this to log all communications with the Citadel server
19 #include <sys/types.h>
21 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
35 #include "webserver.h"
39 #define INADDR_NONE 0xffffffff
42 RETSIGTYPE timeout(int signum)
44 lprintf(1, "Connection timed out.\n");
51 * Connect a unix domain socket
53 int uds_connectsock(char *sockpath)
55 struct sockaddr_un addr;
58 memset(&addr, 0, sizeof(addr));
59 addr.sun_family = AF_UNIX;
60 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
62 s = socket(AF_UNIX, SOCK_STREAM, 0);
64 lprintf(1, "Can't create socket: %s\n",
69 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
70 lprintf(1, "Can't connect: %s\n",
81 * Connect a TCP/IP socket
83 int tcp_connectsock(char *host, char *service)
88 struct sockaddr_in sin;
91 memset(&sin, 0, sizeof(sin));
92 sin.sin_family = AF_INET;
94 pse = getservbyname(service, "tcp");
96 sin.sin_port = pse->s_port;
97 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
98 lprintf(1, "Can't get %s service entry\n", service);
101 phe = gethostbyname(host);
103 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
104 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
105 lprintf(1, "Can't get %s host entry: %s\n",
106 host, strerror(errno));
109 if ((ppe = getprotobyname("tcp")) == 0) {
110 lprintf(1, "Can't get TCP protocol entry: %s\n",
115 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
117 lprintf(1, "Can't create socket: %s\n", strerror(errno));
120 signal(SIGALRM, timeout);
123 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
124 lprintf(1, "Can't connect to %s.%s: %s\n",
125 host, service, strerror(errno));
130 signal(SIGALRM, SIG_IGN);
139 * Input binary data from socket
141 void serv_read(char *buf, int bytes)
146 while (len < bytes) {
147 rlen = read(WC->serv_sock, &buf[len], bytes - len);
149 lprintf(1, "Server connection broken: %s\n",
151 close(WC->serv_sock);
152 WC->serv_sock = (-1);
155 memset(buf, 0, bytes);
164 * input string from pipe
166 void serv_gets(char *strbuf)
174 serv_read(&buf[0], 1);
177 } while ((ch != 10) && (ch != 0) && (len < (SIZ-1)));
178 if (strbuf[len-1] == 10) strbuf[--len] = 0;
179 if (strbuf[len-1] == 13) strbuf[--len] = 0;
181 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
188 * send binary to server
190 void serv_write(char *buf, int nbytes)
192 int bytes_written = 0;
194 while (bytes_written < nbytes) {
195 retval = write(WC->serv_sock, &buf[bytes_written],
196 nbytes - bytes_written);
198 lprintf(1, "Server connection broken: %s\n",
200 close(WC->serv_sock);
201 WC->serv_sock = (-1);
206 bytes_written = bytes_written + retval;
212 * send line to server
214 void serv_puts(char *string)
219 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
221 sprintf(buf, "%s\n", string);
222 serv_write(buf, strlen(buf));
227 * convenience function to send stuff to the server
229 void serv_printf(const char *format,...)
234 va_start(arg_ptr, format);
235 vsnprintf(buf, sizeof buf, format, arg_ptr);
239 serv_write(buf, strlen(buf));
241 lprintf(9, "<%s", buf);