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 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);
161 if ((ch != 13) && (ch != 10)) {
164 } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)));
167 lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
174 * \brief send binary to server
175 * \param buf the buffer to write to citadel server
176 * \param nbytes how many bytes to send to citadel server
178 void serv_write(char *buf, int nbytes)
180 int bytes_written = 0;
182 while (bytes_written < nbytes) {
183 retval = write(WC->serv_sock, &buf[bytes_written],
184 nbytes - bytes_written);
186 lprintf(1, "Server connection broken: %s\n",
188 close(WC->serv_sock);
189 WC->serv_sock = (-1);
194 bytes_written = bytes_written + retval;
200 * \brief send line to server
201 * \param string the line to send to the citadel server
203 void serv_puts(char *string)
208 lprintf(9, "%3d<%s\n", WC->serv_sock, string);
210 sprintf(buf, "%s\n", string);
211 serv_write(buf, strlen(buf));
216 * \brief convenience function to send stuff to the server
217 * \param format the formatstring
218 * \param ... the entities to insert into format
220 void serv_printf(const char *format,...)
225 va_start(arg_ptr, format);
226 vsnprintf(buf, sizeof buf, format, arg_ptr);
230 serv_write(buf, strlen(buf));
232 lprintf(9, "<%s", buf);