4 * TCP client socket module for WebCit
9 * Uncomment this to log all communications with the Citadel server
14 #include "webserver.h"
16 RETSIGTYPE timeout(int signum)
18 lprintf(1, "Connection timed out.\n");
24 * Connect a unix domain socket
26 int uds_connectsock(char *sockpath)
28 struct sockaddr_un addr;
31 memset(&addr, 0, sizeof(addr));
32 addr.sun_family = AF_UNIX;
33 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
35 s = socket(AF_UNIX, SOCK_STREAM, 0);
37 lprintf(1, "Can't create socket: %s\n",
42 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
43 lprintf(1, "Can't connect: %s\n",
54 * Connect a TCP/IP socket
56 int tcp_connectsock(char *host, char *service)
61 struct sockaddr_in sin;
64 memset(&sin, 0, sizeof(sin));
65 sin.sin_family = AF_INET;
67 pse = getservbyname(service, "tcp");
69 sin.sin_port = pse->s_port;
70 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
71 lprintf(1, "Can't get %s service entry\n", service);
74 phe = gethostbyname(host);
76 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
77 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
78 lprintf(1, "Can't get %s host entry: %s\n",
79 host, strerror(errno));
82 if ((ppe = getprotobyname("tcp")) == 0) {
83 lprintf(1, "Can't get TCP protocol entry: %s\n",
88 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
90 lprintf(1, "Can't create socket: %s\n", strerror(errno));
93 signal(SIGALRM, timeout);
96 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
97 lprintf(1, "Can't connect to %s.%s: %s\n",
98 host, service, strerror(errno));
103 signal(SIGALRM, SIG_IGN);
112 * Input binary data from socket
114 void serv_read(char *buf, int bytes)
119 while (len < bytes) {
120 rlen = read(WC->serv_sock, &buf[len], bytes - len);
122 lprintf(1, "Server connection broken: %s\n",
124 close(WC->serv_sock);
125 WC->serv_sock = (-1);
128 memset(buf, 0, bytes);
137 * input string from pipe
139 void serv_getln(char *strbuf, int bufsize)
147 serv_read(&buf[0], 1);
150 } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)));
151 if (strbuf[len-1] == 10) strbuf[--len] = 0;
152 if (strbuf[len-1] == 13) strbuf[--len] = 0;
154 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
161 * send binary to server
163 void serv_write(char *buf, int nbytes)
165 int bytes_written = 0;
167 while (bytes_written < nbytes) {
168 retval = write(WC->serv_sock, &buf[bytes_written],
169 nbytes - bytes_written);
171 lprintf(1, "Server connection broken: %s\n",
173 close(WC->serv_sock);
174 WC->serv_sock = (-1);
179 bytes_written = bytes_written + retval;
185 * send line to server
187 void serv_puts(char *string)
192 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
194 sprintf(buf, "%s\n", string);
195 serv_write(buf, strlen(buf));
200 * convenience function to send stuff to the server
202 void serv_printf(const char *format,...)
207 va_start(arg_ptr, format);
208 vsnprintf(buf, sizeof buf, format, arg_ptr);
212 serv_write(buf, strlen(buf));
214 lprintf(9, "<%s", buf);