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)
70 struct sockaddr_in sin;
73 memset(&sin, 0, sizeof(sin));
74 sin.sin_family = AF_INET;
76 pse = getservbyname(service, "tcp");
78 sin.sin_port = pse->s_port;
79 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
80 lprintf(1, "Can't get %s service entry\n", service);
83 phe = gethostbyname(host);
85 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
86 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
87 lprintf(1, "Can't get %s host entry: %s\n",
88 host, strerror(errno));
91 if ((ppe = getprotobyname("tcp")) == 0) {
92 lprintf(1, "Can't get TCP protocol entry: %s\n",
97 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
99 lprintf(1, "Can't create socket: %s\n", strerror(errno));
102 signal(SIGALRM, timeout);
105 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
106 lprintf(1, "Can't connect to %s.%s: %s\n",
107 host, service, strerror(errno));
112 signal(SIGALRM, SIG_IGN);
114 fdflags = fcntl(s, F_GETFL);
115 fdflags = fdflags | O_NONBLOCK;
116 fcntl(s, F_SETFD, fdflags);
123 * input string from pipe
125 int serv_getln(char *strbuf, int bufsize)
131 StrBuf_ServGetln(WCC->MigrateReadLineBuf);
132 len = StrLength(WCC->MigrateReadLineBuf);
135 memcpy(strbuf, ChrPtr(WCC->MigrateReadLineBuf), len);
136 FlushStrBuf(WCC->MigrateReadLineBuf);
139 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
145 int StrBuf_ServGetln(StrBuf *buf)
151 rc = StrBufTCP_read_buffered_line_fast(buf,
159 lprintf(1, "Server connection broken: %s\n",
162 WCC->serv_sock = (-1);
169 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
175 rc = StrBufReadBLOBBuffered(buf,
185 lprintf(1, "Server connection broken: %s\n",
188 WCC->serv_sock = (-1);
195 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
202 rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &Err);
205 lprintf(1, "Server connection broken: %s\n",
208 WCC->serv_sock = (-1);
216 * send binary to server
217 * buf the buffer to write to citadel server
218 * nbytes how many bytes to send to citadel server
220 void serv_write(const char *buf, int nbytes)
222 int bytes_written = 0;
224 while (bytes_written < nbytes) {
225 retval = write(WC->serv_sock, &buf[bytes_written],
226 nbytes - bytes_written);
228 lprintf(1, "Server connection broken: %s\n",
230 close(WC->serv_sock);
231 WC->serv_sock = (-1);
236 bytes_written = bytes_written + retval;
242 * send line to server
243 * string the line to send to the citadel server
245 void serv_puts(const char *string)
249 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
251 FlushStrBuf(WCC->ReadBuf);
254 serv_write(string, strlen(string));
259 * send line to server
260 * string the line to send to the citadel server
262 void serv_putbuf(const StrBuf *string)
266 lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
268 FlushStrBuf(WCC->ReadBuf);
271 serv_write(ChrPtr(string), StrLength(string));
277 * convenience function to send stuff to the server
278 * format the formatstring
279 * ... the entities to insert into format
281 void serv_printf(const char *format,...)
288 FlushStrBuf(WCC->ReadBuf);
291 va_start(arg_ptr, format);
292 vsnprintf(buf, sizeof buf, format, arg_ptr);
298 serv_write(buf, len);
300 lprintf(9, "<%s", buf);