6 * Uncomment this to log all communications with the Citadel server
12 #include "webserver.h"
15 * register the timeout
16 * signum signalhandler number
19 RETSIGTYPE timeout(int signum)
21 lprintf(1, "Connection timed out; unable to reach citserver\n");
22 /* no exit here, since we need to server the connection unreachable thing. exit(3); */
27 * Connect a unix domain socket
28 * sockpath where to open a unix domain socket
30 int uds_connectsock(char *sockpath)
32 struct sockaddr_un addr;
35 memset(&addr, 0, sizeof(addr));
36 addr.sun_family = AF_UNIX;
37 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
39 s = socket(AF_UNIX, SOCK_STREAM, 0);
41 lprintf(1, "Can't create socket[%s]: %s\n",
47 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
48 lprintf(1, "Can't connect [%s]: %s\n",
60 * Connect a TCP/IP socket
61 * host the host to connect to
62 * service the service on the host to call
64 int tcp_connectsock(char *host, char *service)
69 struct sockaddr_in sin;
72 memset(&sin, 0, sizeof(sin));
73 sin.sin_family = AF_INET;
75 pse = getservbyname(service, "tcp");
77 sin.sin_port = pse->s_port;
78 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
79 lprintf(1, "Can't get %s service entry\n", service);
82 phe = gethostbyname(host);
84 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
85 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
86 lprintf(1, "Can't get %s host entry: %s\n",
87 host, strerror(errno));
90 if ((ppe = getprotobyname("tcp")) == 0) {
91 lprintf(1, "Can't get TCP protocol entry: %s\n",
96 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
98 lprintf(1, "Can't create socket: %s\n", strerror(errno));
101 signal(SIGALRM, timeout);
104 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
105 lprintf(1, "Can't connect to %s.%s: %s\n",
106 host, service, strerror(errno));
111 signal(SIGALRM, SIG_IGN);
120 * Input binary data from socket
121 * buf the buffer to get the input to
122 * bytes the maximal number of bytes to read
124 inline void _serv_read(char *buf, int bytes, wcsession *WCC)
129 while ((len < bytes) && (WCC->serv_sock != -1)){
130 rlen = read(WCC->serv_sock, &buf[len], bytes - len);
132 lprintf(1, "Server connection broken: %s\n",
135 close(WCC->serv_sock);
136 WCC->serv_sock = (-1);
139 memset(buf, 0, bytes);
146 void serv_read(char *buf, int bytes)
152 _serv_read(buf, bytes, WCC);
156 * input string from pipe
158 int serv_getln(char *strbuf, int bufsize)
168 _serv_read(&buf[0], 1, WCC);
170 if ((ch != 13) && (ch != 10)) {
173 } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)) && (WCC->serv_sock != -1));
176 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
181 int StrBuf_ServGetln(StrBuf *buf)
188 rc = StrBufTCP_read_line(buf, &WCC->serv_sock, 0, &ErrStr);
191 lprintf(1, "Server connection broken: %s\n",
194 WCC->serv_sock = (-1);
201 int StrBuf_ServGetlnBuffered(StrBuf *buf)
207 rc = StrBufTCP_read_buffered_line_fast(buf,
215 lprintf(1, "Server connection broken: %s\n",
218 WCC->serv_sock = (-1);
225 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
231 rc = StrBufReadBLOBBuffered(buf,
241 lprintf(1, "Server connection broken: %s\n",
244 WCC->serv_sock = (-1);
251 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
258 rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &Err);
261 lprintf(1, "Server connection broken: %s\n",
264 WCC->serv_sock = (-1);
272 * send binary to server
273 * buf the buffer to write to citadel server
274 * nbytes how many bytes to send to citadel server
276 void serv_write(const char *buf, int nbytes)
278 int bytes_written = 0;
280 while (bytes_written < nbytes) {
281 retval = write(WC->serv_sock, &buf[bytes_written],
282 nbytes - bytes_written);
284 lprintf(1, "Server connection broken: %s\n",
286 close(WC->serv_sock);
287 WC->serv_sock = (-1);
292 bytes_written = bytes_written + retval;
298 * send line to server
299 * string the line to send to the citadel server
301 void serv_puts(const char *string)
305 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
307 FlushStrBuf(WCC->ReadBuf);
310 serv_write(string, strlen(string));
315 * send line to server
316 * string the line to send to the citadel server
318 void serv_putbuf(const StrBuf *string)
322 lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
324 FlushStrBuf(WCC->ReadBuf);
327 serv_write(ChrPtr(string), StrLength(string));
333 * convenience function to send stuff to the server
334 * format the formatstring
335 * ... the entities to insert into format
337 void serv_printf(const char *format,...)
344 FlushStrBuf(WCC->ReadBuf);
347 va_start(arg_ptr, format);
348 vsnprintf(buf, sizeof buf, format, arg_ptr);
354 serv_write(buf, len);
356 lprintf(9, "<%s", buf);