4 * TCP client socket module for WebCit
9 * Uncomment this to log all communications with the Citadel server
14 #include "webserver.h"
17 #define INADDR_NONE 0xffffffff
20 RETSIGTYPE timeout(int signum)
22 lprintf(1, "Connection timed out.\n");
29 * Connect a unix domain socket
31 int uds_connectsock(char *sockpath)
33 struct sockaddr_un addr;
36 memset(&addr, 0, sizeof(addr));
37 addr.sun_family = AF_UNIX;
38 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
40 s = socket(AF_UNIX, SOCK_STREAM, 0);
42 lprintf(1, "Can't create socket: %s\n",
47 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
48 lprintf(1, "Can't connect: %s\n",
59 * Connect a TCP/IP socket
61 int tcp_connectsock(char *host, char *service)
66 struct sockaddr_in sin;
69 memset(&sin, 0, sizeof(sin));
70 sin.sin_family = AF_INET;
72 pse = getservbyname(service, "tcp");
74 sin.sin_port = pse->s_port;
75 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
76 lprintf(1, "Can't get %s service entry\n", service);
79 phe = gethostbyname(host);
81 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
82 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
83 lprintf(1, "Can't get %s host entry: %s\n",
84 host, strerror(errno));
87 if ((ppe = getprotobyname("tcp")) == 0) {
88 lprintf(1, "Can't get TCP protocol entry: %s\n",
93 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
95 lprintf(1, "Can't create socket: %s\n", strerror(errno));
98 signal(SIGALRM, timeout);
101 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
102 lprintf(1, "Can't connect to %s.%s: %s\n",
103 host, service, strerror(errno));
108 signal(SIGALRM, SIG_IGN);
117 * Input binary data from socket
119 void serv_read(char *buf, int bytes)
124 while (len < bytes) {
125 rlen = read(WC->serv_sock, &buf[len], bytes - len);
127 lprintf(1, "Server connection broken: %s\n",
129 close(WC->serv_sock);
130 WC->serv_sock = (-1);
133 memset(buf, 0, bytes);
142 * input string from pipe
144 void serv_getln(char *strbuf, int bufsize)
152 serv_read(&buf[0], 1);
155 } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)));
156 if (strbuf[len-1] == 10) strbuf[--len] = 0;
157 if (strbuf[len-1] == 13) strbuf[--len] = 0;
159 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
166 * send binary to server
168 void serv_write(char *buf, int nbytes)
170 int bytes_written = 0;
172 while (bytes_written < nbytes) {
173 retval = write(WC->serv_sock, &buf[bytes_written],
174 nbytes - bytes_written);
176 lprintf(1, "Server connection broken: %s\n",
178 close(WC->serv_sock);
179 WC->serv_sock = (-1);
184 bytes_written = bytes_written + retval;
190 * send line to server
192 void serv_puts(char *string)
197 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
199 sprintf(buf, "%s\n", string);
200 serv_write(buf, strlen(buf));
205 * convenience function to send stuff to the server
207 void serv_printf(const char *format,...)
212 va_start(arg_ptr, format);
213 vsnprintf(buf, sizeof buf, format, arg_ptr);
217 serv_write(buf, strlen(buf));
219 lprintf(9, "<%s", buf);