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\n",
46 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
47 lprintf(1, "Can't connect: %s\n",
58 * Connect a TCP/IP socket
59 * host the host to connect to
60 * service the service on the host to call
62 int tcp_connectsock(char *host, char *service)
67 struct sockaddr_in sin;
70 memset(&sin, 0, sizeof(sin));
71 sin.sin_family = AF_INET;
73 pse = getservbyname(service, "tcp");
75 sin.sin_port = pse->s_port;
76 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
77 lprintf(1, "Can't get %s service entry\n", service);
80 phe = gethostbyname(host);
82 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
83 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
84 lprintf(1, "Can't get %s host entry: %s\n",
85 host, strerror(errno));
88 if ((ppe = getprotobyname("tcp")) == 0) {
89 lprintf(1, "Can't get TCP protocol entry: %s\n",
94 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
96 lprintf(1, "Can't create socket: %s\n", strerror(errno));
99 signal(SIGALRM, timeout);
102 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
103 lprintf(1, "Can't connect to %s.%s: %s\n",
104 host, service, strerror(errno));
109 signal(SIGALRM, SIG_IGN);
118 * Input binary data from socket
119 * buf the buffer to get the input to
120 * bytes the maximal number of bytes to read
122 inline void _serv_read(char *buf, int bytes, wcsession *WCC)
127 while (len < bytes) {
128 rlen = read(WCC->serv_sock, &buf[len], bytes - len);
130 lprintf(1, "Server connection broken: %s\n",
133 close(WCC->serv_sock);
134 WCC->serv_sock = (-1);
137 memset(buf, 0, bytes);
144 void serv_read(char *buf, int bytes)
147 _serv_read(buf, bytes, WCC);
151 * input string from pipe
153 int serv_getln(char *strbuf, int bufsize)
162 _serv_read(&buf[0], 1, WCC);
164 if ((ch != 13) && (ch != 10)) {
167 } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)));
170 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
175 int StrBuf_ServGetln(StrBuf *buf)
180 rc = StrBufTCP_read_line(buf, &WC->serv_sock, 0, &ErrStr);
183 lprintf(1, "Server connection broken: %s\n",
186 WC->serv_sock = (-1);
193 int StrBuf_ServGetlnBuffered(StrBuf *buf)
199 rc = StrBufTCP_read_buffered_line(buf,
206 lprintf(1, "Server connection broken: %s\n",
209 WCC->serv_sock = (-1);
216 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
221 rc = StrBufReadBLOB(buf, &WC->serv_sock, 1, BlobSize, &Err);
224 lprintf(1, "Server connection broken: %s\n",
227 WC->serv_sock = (-1);
235 * send binary to server
236 * buf the buffer to write to citadel server
237 * nbytes how many bytes to send to citadel server
239 void serv_write(const char *buf, int nbytes)
241 int bytes_written = 0;
243 while (bytes_written < nbytes) {
244 retval = write(WC->serv_sock, &buf[bytes_written],
245 nbytes - bytes_written);
247 lprintf(1, "Server connection broken: %s\n",
249 close(WC->serv_sock);
250 WC->serv_sock = (-1);
255 bytes_written = bytes_written + retval;
261 * send line to server
262 * string the line to send to the citadel server
264 void serv_puts(const char *string)
267 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
269 serv_write(string, strlen(string));
274 * send line to server
275 * string the line to send to the citadel server
277 void serv_putbuf(const StrBuf *string)
280 lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
282 serv_write(ChrPtr(string), StrLength(string));
288 * convenience function to send stuff to the server
289 * format the formatstring
290 * ... the entities to insert into format
292 void serv_printf(const char *format,...)
298 va_start(arg_ptr, format);
299 vsnprintf(buf, sizeof buf, format, arg_ptr);
305 serv_write(buf, len);
307 lprintf(9, "<%s", buf);