5 * \defgroup TcpSockets TCP client socket module for WebCit
11 * Uncomment this to log all communications with the Citadel server
16 #include "webserver.h"
19 * \brief register the timeout
20 * \param signum signalhandler number
23 RETSIGTYPE timeout(int signum)
25 lprintf(1, "Connection timed out.\n");
31 * \brief Connect a unix domain socket
32 * \param sockpath where to open a unix domain socket
34 int uds_connectsock(char *sockpath)
36 struct sockaddr_un addr;
39 memset(&addr, 0, sizeof(addr));
40 addr.sun_family = AF_UNIX;
41 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
43 s = socket(AF_UNIX, SOCK_STREAM, 0);
45 lprintf(1, "Can't create socket: %s\n",
50 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
51 lprintf(1, "Can't connect: %s\n",
62 * \brief Connect a TCP/IP socket
63 * \param host the host to connect to
64 * \param service the service on the host to call
66 int tcp_connectsock(char *host, char *service)
71 struct sockaddr_in sin;
74 memset(&sin, 0, sizeof(sin));
75 sin.sin_family = AF_INET;
77 pse = getservbyname(service, "tcp");
79 sin.sin_port = pse->s_port;
80 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
81 lprintf(1, "Can't get %s service entry\n", service);
84 phe = gethostbyname(host);
86 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
87 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
88 lprintf(1, "Can't get %s host entry: %s\n",
89 host, strerror(errno));
92 if ((ppe = getprotobyname("tcp")) == 0) {
93 lprintf(1, "Can't get TCP protocol entry: %s\n",
98 s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
100 lprintf(1, "Can't create socket: %s\n", strerror(errno));
103 signal(SIGALRM, timeout);
106 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
107 lprintf(1, "Can't connect to %s.%s: %s\n",
108 host, service, strerror(errno));
113 signal(SIGALRM, SIG_IGN);
122 * \brief Input binary data from socket
123 * \param buf the buffer to get the input to
124 * \param bytes the maximal number of bytes to read
126 void serv_read(char *buf, int bytes)
131 while (len < bytes) {
132 rlen = read(WC->serv_sock, &buf[len], bytes - len);
134 lprintf(1, "Server connection broken: %s\n",
136 close(WC->serv_sock);
137 WC->serv_sock = (-1);
140 memset(buf, 0, bytes);
149 * \brief input string from pipe
151 void serv_getln(char *strbuf, int bufsize)
159 serv_read(&buf[0], 1);
162 } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)));
163 if (strbuf[len-1] == 10) strbuf[--len] = 0;
164 if (strbuf[len-1] == 13) strbuf[--len] = 0;
166 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
173 * \brief send binary to server
174 * \param buf the buffer to write to citadel server
175 * \param nbytes how many bytes to send to citadel server
177 void serv_write(char *buf, int nbytes)
179 int bytes_written = 0;
181 while (bytes_written < nbytes) {
182 retval = write(WC->serv_sock, &buf[bytes_written],
183 nbytes - bytes_written);
185 lprintf(1, "Server connection broken: %s\n",
187 close(WC->serv_sock);
188 WC->serv_sock = (-1);
193 bytes_written = bytes_written + retval;
199 * \brief send line to server
200 * \param string the line to send to the citadel server
202 void serv_puts(char *string)
207 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
209 sprintf(buf, "%s\n", string);
210 serv_write(buf, strlen(buf));
215 * \brief convenience function to send stuff to the server
216 * \param format the formatstring
217 * \param ... the entities to insert into format
219 void serv_printf(const char *format,...)
224 va_start(arg_ptr, format);
225 vsnprintf(buf, sizeof buf, format, arg_ptr);
229 serv_write(buf, strlen(buf));
231 lprintf(9, "<%s", buf);