5 * \defgroup TcpSockets TCP client socket module for WebCit
6 * \ingroup CitadelCommunitacion
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 inline void _serv_read(char *buf, int bytes, struct wcsession *WCC)
131 while (len < bytes) {
132 rlen = read(WCC->serv_sock, &buf[len], bytes - len);
134 lprintf(1, "Server connection broken: %s\n",
137 close(WCC->serv_sock);
138 WCC->serv_sock = (-1);
141 memset(buf, 0, bytes);
148 void serv_read(char *buf, int bytes)
150 struct wcsession *WCC = WC;
151 _serv_read(buf, bytes, WCC);
155 * \brief input string from pipe
157 int serv_getln(char *strbuf, int bufsize)
159 struct wcsession *WCC = WC;
166 _serv_read(&buf[0], 1, WCC);
168 if ((ch != 13) && (ch != 10)) {
171 } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)));
174 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
182 * \brief send binary to server
183 * \param buf the buffer to write to citadel server
184 * \param nbytes how many bytes to send to citadel server
186 void serv_write(char *buf, int nbytes)
188 int bytes_written = 0;
190 while (bytes_written < nbytes) {
191 retval = write(WC->serv_sock, &buf[bytes_written],
192 nbytes - bytes_written);
194 lprintf(1, "Server connection broken: %s\n",
196 close(WC->serv_sock);
197 WC->serv_sock = (-1);
202 bytes_written = bytes_written + retval;
208 * \brief send line to server
209 * \param string the line to send to the citadel server
211 void serv_puts(char *string)
214 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
216 serv_write(string, strlen(string));
222 * \brief convenience function to send stuff to the server
223 * \param format the formatstring
224 * \param ... the entities to insert into format
226 void serv_printf(const char *format,...)
232 va_start(arg_ptr, format);
233 vsnprintf(buf, sizeof buf, format, arg_ptr);
239 serv_write(buf, len);
241 lprintf(9, "<%s", buf);