/*
- * Connect a unix domain socket
- * sockpath where to open a unix domain socket
+ * Client side - connect to a unix domain socket
*/
int uds_connectsock(char *sockpath)
{
s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s < 0) {
- lprintf(1, "Can't create socket[%s]: %s\n",
- sockpath,
- strerror(errno));
+ lprintf(1, "Can't create socket[%s]: %s\n", sockpath, strerror(errno));
return(-1);
}
if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
- lprintf(1, "Can't connect [%s]: %s\n",
- sockpath,
- strerror(errno));
+ lprintf(1, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
close(s);
return(-1);
}
/*
- * Connect a TCP/IP socket
- * host the host to connect to
- * service the service on the host to call
+ * TCP client - connect to a host/port
*/
int tcp_connectsock(char *host, char *service)
{
- int fdflags;
- struct hostent *phe;
- struct servent *pse;
- struct protoent *ppe;
- struct sockaddr_in sin;
- int s;
-
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
-
- pse = getservbyname(service, "tcp");
- if (pse) {
- sin.sin_port = pse->s_port;
- } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
- lprintf(1, "Can't get %s service entry\n", service);
+ struct in6_addr serveraddr;
+ struct addrinfo hints;
+ struct addrinfo *res = NULL;
+ struct addrinfo *ai = NULL;
+ int rc = (-1);
+ int s = (-1);
+
+ if ((host == NULL) || IsEmptyStr(host))
return (-1);
- }
- phe = gethostbyname(host);
- if (phe) {
- memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
- } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
- lprintf(1, "Can't get %s host entry: %s\n",
- host, strerror(errno));
- return (-1);
- }
- if ((ppe = getprotobyname("tcp")) == 0) {
- lprintf(1, "Can't get TCP protocol entry: %s\n",
- strerror(errno));
+ if ((service == NULL) || IsEmptyStr(service))
return (-1);
+
+ lprintf(9, "tcp_connectsock(%s,%s)\n", host, service);
+
+ memset(&hints, 0x00, sizeof(hints));
+ hints.ai_flags = AI_NUMERICSERV;
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+
+ /*
+ * Handle numeric IPv4 and IPv6 addresses
+ */
+ rc = inet_pton(AF_INET, host, &serveraddr);
+ if (rc == 1) { /* dotted quad */
+ hints.ai_family = AF_INET;
+ hints.ai_flags |= AI_NUMERICHOST;
+ } else {
+ rc = inet_pton(AF_INET6, host, &serveraddr);
+ if (rc == 1) { /* IPv6 address */
+ hints.ai_family = AF_INET6;
+ hints.ai_flags |= AI_NUMERICHOST;
+ }
}
- s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
- if (s < 0) {
- lprintf(1, "Can't create socket: %s\n", strerror(errno));
- return (-1);
+ /* Begin the connection process */
+
+ rc = getaddrinfo(host, service, &hints, &res);
+ if (rc != 0) {
+ lprintf(1, "%s: %s\n", host, gai_strerror(rc));
+ return(-1);
}
- fdflags = fcntl(s, F_GETFL);
- if (fdflags < 0)
- lprintf(1, "unable to get socket flags! %s.%s: %s \n",
- host, service, strerror(errno));
- fdflags = fdflags | O_NONBLOCK;
- if (fcntl(s, F_SETFD, fdflags) < 0)
- lprintf(1, "unable to set socket nonblocking flags! %s.%s: %s \n",
- host, service, strerror(errno));
+ /*
+ * Try all available addresses until we connect to one or until we run out.
+ */
+ for (ai = res; ai != NULL; ai = ai->ai_next) {
- signal(SIGALRM, timeout);
- alarm(30);
+ if (ai->ai_family == AF_INET) lprintf(9, "Trying IPv4\n");
+ else if (ai->ai_family == AF_INET6) lprintf(9, "Trying IPv6\n");
+ else lprintf(9, "This is going to fail.\n");
- if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
- lprintf(1, "Can't connect to %s.%s: %s\n",
- host, service, strerror(errno));
- close(s);
- return (-1);
- }
- alarm(0);
- signal(SIGALRM, SIG_IGN);
- if (!is_https) {
- fdflags = fcntl(s, F_GETFL);
- if (fdflags < 0)
- lprintf(1, "unable to get socket flags! %s.%s: %s \n",
- host, service, strerror(errno));
- fdflags = fdflags | O_NONBLOCK;
- if (fcntl(s, F_SETFD, fdflags) < 0)
- lprintf(1, "unable to set socket nonblocking flags! %s.%s: %s \n",
- host, service, strerror(errno));
+ s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ if (s < 0) {
+ lprintf(1, "socket() failed: %s\n", strerror(errno));
+ return(-1);
+ }
+ rc = connect(s, ai->ai_addr, ai->ai_addrlen);
+ if (rc >= 0) {
+ return(s);
+ }
+ else {
+ lprintf(1, "connect() failed: %s\n", strerror(errno));
+ close(s);
+ }
}
- return (s);
-}
+ return(-1);
+}
/*
#ifdef SERV_TRACE
else
{
- long pos=0;
+ long pos = 0;
if (WCC->ReadPos != NULL)
- pos = WCC->ReadPos - ChrPtr(buf);
+ pos = WCC->ReadPos - ChrPtr(WCC->ReadBuf);
lprintf(9, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
}
#endif
return rc;
}
+
+void FlushReadBuf (void)
+{
+ long len;
+ const char *pch;
+ const char *pche;
+ wcsession *WCC = WC;
+
+ len = StrLength(WCC->ReadBuf);
+ if ((len > 0) &&
+ (WCC->ReadPos != NULL) &&
+ (WCC->ReadPos != StrBufNOTNULL))
+
+ {
+ pch = ChrPtr(WCC->ReadBuf);
+ pche = pch + len;
+ if (WCC->ReadPos != pche)
+ {
+ lprintf(1, "ERROR: somebody didn't eat his soup! Remaing Chars: %d [%s]\n",
+ pche - WCC->ReadPos, pche);
+ lprintf(1,
+ "--------------------------------------------------------------------------------\n"
+ "Whole buf: [%s]\n"
+ "--------------------------------------------------------------------------------\n",
+ pch);
+ AppendImportantMessage(HKEY("Suppenkasper alert! watch your webcit logfile and get connected to your favourite opensource Crew."));
+ }
+ }
+
+ FlushStrBuf(WCC->ReadBuf);
+ WCC->ReadPos = NULL;
+
+
+}
+
+
/*
* send binary to server
* buf the buffer to write to citadel server
int bytes_written = 0;
int retval;
- FlushStrBuf(WCC->ReadBuf);
- WCC->ReadPos = NULL;
+ FlushReadBuf();
while (bytes_written < nbytes) {
retval = write(WCC->serv_sock, &buf[bytes_written],
nbytes - bytes_written);
*/
void serv_puts(const char *string)
{
- wcsession *WCC = WC;
#ifdef SERV_TRACE
lprintf(9, "%3d>>>%s\n", WC->serv_sock, string);
#endif
- FlushStrBuf(WCC->ReadBuf);
- WCC->ReadPos = NULL;
+ FlushReadBuf();
serv_write(string, strlen(string));
serv_write("\n", 1);
*/
void serv_putbuf(const StrBuf *string)
{
- wcsession *WCC = WC;
#ifdef SERV_TRACE
lprintf(9, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
#endif
- FlushStrBuf(WCC->ReadBuf);
- WCC->ReadPos = NULL;
+ FlushReadBuf();
serv_write(ChrPtr(string), StrLength(string));
serv_write("\n", 1);
*/
void serv_printf(const char *format,...)
{
- wcsession *WCC = WC;
va_list arg_ptr;
char buf[SIZ];
size_t len;
- FlushStrBuf(WCC->ReadBuf);
- WCC->ReadPos = NULL;
+ FlushReadBuf();
va_start(arg_ptr, format);
vsnprintf(buf, sizeof buf, format, arg_ptr);
if (MaxRead == -1)
{
- serv_printf("READ %d|%d", 0, total_len);
+ serv_printf("READ %d|"SIZE_T_FMT, 0, total_len);
if (StrBuf_ServGetln(Buf) > 0)
{
long YetRead;
return -1;
}
- pch = ChrPtr(WCC->ReadBuf);
- YetRead = WCC->ReadPos - pch;
- if (YetRead > 0)
- {
- long StillThere;
-
- StillThere = StrLength(WCC->ReadBuf) -
- YetRead;
-
- StrBufPlain(Ret,
- WCC->ReadPos,
- StillThere);
- total_len -= StillThere;
- }
- FlushStrBuf(WCC->ReadBuf);
- WCC->ReadPos = NULL;
-
+ if (WCC->ReadPos != NULL) {
+ pch = ChrPtr(WCC->ReadBuf);
+
+ YetRead = WCC->ReadPos - pch;
+ if (YetRead > 0)
+ {
+ long StillThere;
+
+ StillThere = StrLength(WCC->ReadBuf) -
+ YetRead;
+
+ StrBufPlain(Ret,
+ WCC->ReadPos,
+ StillThere);
+ total_len -= StillThere;
+ }
+ FlushStrBuf(WCC->ReadBuf);
+ WCC->ReadPos = NULL;
+ }
if (total_len > 0)
{
rc = StrBufReadBLOB(Ret,
&Error);
}
-#ifdef CTDL_IPV6
/*
* This is a generic function to set up a master socket for listening on
* port_number port number to bind
* queue_len number of incoming connections to allow in the queue
*/
-int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
+int webcit_tcp_server(char *ip_addr, int port_number, int queue_len)
{
struct protoent *p;
- struct sockaddr_in6 sin;
- int s, i;
-
- memset(&sin, 0, sizeof(sin));
- sin.sin6_family = AF_INET6;
-
- if ((ip_addr == NULL) || (IsEmptyStr(ip_addr)) || (!strcmp(ip_addr, "0.0.0.0"))) {
- sin.sin6_addr = in6addr_any;
- } else {
- char bind_to[256];
- if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) {
- snprintf(bind_to, sizeof bind_to, "::ffff:%s", ip_addr);
- }
- else {
- safestrncpy(bind_to, ip_addr, sizeof bind_to);
+ struct sockaddr_in6 sin6;
+ struct sockaddr_in sin4;
+ int s, i, b;
+ int ip_version = 6;
+
+ memset(&sin6, 0, sizeof(sin6));
+ memset(&sin4, 0, sizeof(sin4));
+ sin6.sin6_family = AF_INET6;
+ sin4.sin_family = AF_INET;
+
+ if ( (ip_addr == NULL) /* any IPv6 */
+ || (IsEmptyStr(ip_addr))
+ || (!strcmp(ip_addr, "*"))
+ ) {
+ ip_version = 6;
+ sin6.sin6_addr = in6addr_any;
+ }
+ else if (!strcmp(ip_addr, "0.0.0.0")) /* any IPv4 */
+ {
+ ip_version = 4;
+ sin4.sin_addr.s_addr = INADDR_ANY;
+ }
+ else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) /* specific IPv4 */
+ {
+ ip_version = 4;
+ if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
+ lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
+ return (-WC_EXIT_BIND);
}
- if (inet_pton(AF_INET6, bind_to, &sin.sin6_addr) <= 0) {
+ }
+ else /* specific IPv6 */
+ {
+ ip_version = 6;
+ if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
lprintf(1, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
return (-WC_EXIT_BIND);
}
lprintf(1, "Cannot start: no port number specified.\n");
return (-WC_EXIT_BIND);
}
- sin.sin6_port = htons((u_short) port_number);
+ sin6.sin6_port = htons((u_short) port_number);
+ sin4.sin_port = htons((u_short) port_number);
p = getprotobyname("tcp");
- s = socket(PF_INET6, SOCK_STREAM, (p->p_proto));
+ s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
if (s < 0) {
- lprintf(1, "Can't create a socket: %s\n", strerror(errno));
+ lprintf(1, "Can't create a listening socket: %s\n", strerror(errno));
return (-WC_EXIT_BIND);
}
/* Set some socket options that make sense. */
i = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
- #ifndef __APPLE__
- fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
- there should be a preceding F_GETFL
- and a bitwise OR with the previous
- fd flags */
- #endif
-
- if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
- lprintf(1, "Can't bind: %s\n", strerror(errno));
- return (-WC_EXIT_BIND);
+ if (ip_version == 6) {
+ b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
}
- if (listen(s, queue_len) < 0) {
- lprintf(1, "Can't listen: %s\n", strerror(errno));
- return (-WC_EXIT_BIND);
+ else {
+ b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
}
- return (s);
-}
-
-#else /* CTDL_IPV6 */
-/*
- * This is a generic function to set up a master socket for listening on
- * a TCP port. The server shuts down if the bind fails.
- *
- * ip_addr IP address to bind
- * port_number port number to bind
- * queue_len number of incoming connections to allow in the queue
- */
-int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
-{
- struct protoent *p;
- struct sockaddr_in sin;
- int s, i;
-
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- if (ip_addr == NULL) {
- sin.sin_addr.s_addr = INADDR_ANY;
- } else {
- sin.sin_addr.s_addr = inet_addr(ip_addr);
- }
-
- if (sin.sin_addr.s_addr == INADDR_NONE) {
- sin.sin_addr.s_addr = INADDR_ANY;
- }
-
- if (port_number == 0) {
- lprintf(1, "Cannot start: no port number specified.\n");
- return (-WC_EXIT_BIND);
- }
- sin.sin_port = htons((u_short) port_number);
-
- p = getprotobyname("tcp");
-
- s = socket(PF_INET, SOCK_STREAM, (p->p_proto));
- if (s < 0) {
- lprintf(1, "Can't create a socket: %s\n", strerror(errno));
- return (-WC_EXIT_BIND);
- }
- /* Set some socket options that make sense. */
- i = 1;
- setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
-
- #ifndef __APPLE__
- fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
- there should be a preceding F_GETFL
- and a bitwise OR with the previous
- fd flags */
- #endif
-
- if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
+ if (b < 0) {
lprintf(1, "Can't bind: %s\n", strerror(errno));
return (-WC_EXIT_BIND);
}
+
if (listen(s, queue_len) < 0) {
lprintf(1, "Can't listen: %s\n", strerror(errno));
return (-WC_EXIT_BIND);
return (s);
}
-#endif /* CTDL_IPV6 */
-
/*
* Create a Unix domain socket and listen on it
* sockpath - file name of the unix domain socket
* queue_len - Number of incoming connections to allow in the queue
*/
-int ig_uds_server(char *sockpath, int queue_len)
+int webcit_uds_server(char *sockpath, int queue_len)
{
struct sockaddr_un addr;
int s;
s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s < 0) {
- lprintf(1, "webcit: Can't create a socket: %s\n",
- strerror(errno));
+ lprintf(1, "webcit: Can't create a unix domain socket: %s\n", strerror(errno));
return (-WC_EXIT_BIND);
}
if (retval < 0) {
lprintf(2, "client_read() failed: %s\n",
Error);
+ wc_backtrace();
return retval;
}
}
}
+ if (WCC->WFBuf != NULL) {
+ WildFireSerializePayload(WCC->WFBuf, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
+ FreeStrBuf(&WCC->WFBuf);
+ }
+
if (WCC->Hdr->HR.prohibit_caching)
hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));