4 * TCP socket module for WebCit
10 * Uncomment this to log all communications with the Citadel server
20 #include <sys/types.h>
22 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
36 #include "webserver.h"
40 #define INADDR_NONE 0xffffffff
43 RETSIGTYPE timeout(int signum)
45 lprintf(1, "Connection timed out.\n");
52 * Connect a unix domain socket
54 int uds_connectsock(char *sockpath)
56 struct sockaddr_un addr;
59 memset(&addr, 0, sizeof(addr));
60 addr.sun_family = AF_UNIX;
61 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
63 s = socket(AF_UNIX, SOCK_STREAM, 0);
65 lprintf(1, "Can't create socket: %s\n",
70 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
71 lprintf(1, "Can't connect: %s\n",
82 * Connect a TCP/IP socket
84 int tcp_connectsock(char *host, char *service)
89 struct sockaddr_in sin;
92 memset(&sin, 0, sizeof(sin));
93 sin.sin_family = AF_INET;
95 pse = getservbyname(service, "tcp");
97 sin.sin_port = pse->s_port;
98 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
99 lprintf(1, "Can't get %s service entry\n", service);
102 phe = gethostbyname(host);
104 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
105 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
106 lprintf(1, "Can't get %s host entry: %s\n",
107 host, strerror(errno));
110 if ((ppe = getprotobyname("tcp")) == 0) {
111 lprintf(1, "Can't get TCP protocol entry: %s\n",
116 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
118 lprintf(1, "Can't create socket: %s\n", strerror(errno));
121 signal(SIGALRM, timeout);
124 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
125 lprintf(1, "Can't connect to %s.%s: %s\n",
126 host, service, strerror(errno));
131 signal(SIGALRM, SIG_IGN);
140 * Input binary data from socket
142 void serv_read(char *buf, int bytes)
147 while (len < bytes) {
148 rlen = read(WC->serv_sock, &buf[len], bytes - len);
150 lprintf(1, "Server connection broken: %s\n",
152 close(WC->serv_sock);
153 WC->serv_sock = (-1);
156 memset(buf, 0, bytes);
165 * input string from pipe
167 void serv_gets(char *strbuf)
175 serv_read(&buf[0], 1);
178 } while ((ch != 10) && (ch != 0) && (len < (SIZ-1)));
179 if (strbuf[len-1] == 10) strbuf[--len] = 0;
180 if (strbuf[len-1] == 13) strbuf[--len] = 0;
182 lprintf(9, ">%s\n", strbuf);
189 * send binary to server
191 void serv_write(char *buf, int nbytes)
193 int bytes_written = 0;
195 while (bytes_written < nbytes) {
196 retval = write(WC->serv_sock, &buf[bytes_written],
197 nbytes - bytes_written);
199 lprintf(1, "Server connection broken: %s\n",
201 close(WC->serv_sock);
202 WC->serv_sock = (-1);
207 bytes_written = bytes_written + retval;
213 * send line to server
215 void serv_puts(char *string)
220 lprintf(9, "<%s\n", string);
222 sprintf(buf, "%s\n", string);
223 serv_write(buf, strlen(buf));
228 * convenience function to send stuff to the server
230 void serv_printf(const char *format,...)
235 va_start(arg_ptr, format);
236 vsnprintf(buf, sizeof buf, format, arg_ptr);
240 serv_write(buf, strlen(buf));
242 lprintf(9, "<%s", buf);