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);
118 int StrBuf_ServGetln(StrBuf *buf)
125 rc = StrBufTCP_read_line(buf, &WCC->serv_sock, 0, &ErrStr);
128 lprintf(1, "Server connection broken: %s\n",
131 WCC->serv_sock = (-1);
139 * input string from pipe
141 int serv_getln(char *strbuf, int bufsize)
148 StrBuf_ServGetln(WCC->MigrateReadLineBuf);
149 len = StrLength(WCC->MigrateReadLineBuf);
152 memcpy(strbuf, ChrPtr(WCC->MigrateReadLineBuf), len);
155 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
161 int StrBuf_ServGetlnBuffered(StrBuf *buf)
167 rc = StrBufTCP_read_buffered_line_fast(buf,
175 lprintf(1, "Server connection broken: %s\n",
178 WCC->serv_sock = (-1);
185 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
191 rc = StrBufReadBLOBBuffered(buf,
201 lprintf(1, "Server connection broken: %s\n",
204 WCC->serv_sock = (-1);
211 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
218 rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &Err);
221 lprintf(1, "Server connection broken: %s\n",
224 WCC->serv_sock = (-1);
232 * send binary to server
233 * buf the buffer to write to citadel server
234 * nbytes how many bytes to send to citadel server
236 void serv_write(const char *buf, int nbytes)
238 int bytes_written = 0;
240 while (bytes_written < nbytes) {
241 retval = write(WC->serv_sock, &buf[bytes_written],
242 nbytes - bytes_written);
244 lprintf(1, "Server connection broken: %s\n",
246 close(WC->serv_sock);
247 WC->serv_sock = (-1);
252 bytes_written = bytes_written + retval;
258 * send line to server
259 * string the line to send to the citadel server
261 void serv_puts(const char *string)
265 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
267 FlushStrBuf(WCC->ReadBuf);
270 serv_write(string, strlen(string));
275 * send line to server
276 * string the line to send to the citadel server
278 void serv_putbuf(const StrBuf *string)
282 lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
284 FlushStrBuf(WCC->ReadBuf);
287 serv_write(ChrPtr(string), StrLength(string));
293 * convenience function to send stuff to the server
294 * format the formatstring
295 * ... the entities to insert into format
297 void serv_printf(const char *format,...)
304 FlushStrBuf(WCC->ReadBuf);
307 va_start(arg_ptr, format);
308 vsnprintf(buf, sizeof buf, format, arg_ptr);
314 serv_write(buf, len);
316 lprintf(9, "<%s", buf);