resolve_CFLAGS = $(AM_CFLAGS)
rtspd_SOURCES = $(CURLX_SRCS) $(CURLX_HDRS) $(USEFUL) $(UTIL) \
+ server_sockaddr.h \
rtspd.c
rtspd_LDADD = @TEST_SERVER_LIBS@
rtspd_CFLAGS = $(AM_CFLAGS)
sockfilt_SOURCES = $(CURLX_SRCS) $(CURLX_HDRS) $(USEFUL) $(UTIL) \
+ server_sockaddr.h \
sockfilt.c \
$(top_srcdir)/lib/inet_pton.c
sockfilt_LDADD = @TEST_SERVER_LIBS@
sockfilt_CFLAGS = $(AM_CFLAGS)
sws_SOURCES = $(CURLX_SRCS) $(CURLX_HDRS) $(USEFUL) $(UTIL) \
+ server_sockaddr.h \
sws.c
sws_LDADD = @TEST_SERVER_LIBS@
sws_CFLAGS = $(AM_CFLAGS)
tftpd_SOURCES = $(CURLX_SRCS) $(CURLX_HDRS) $(USEFUL) $(UTIL) \
+ server_sockaddr.h \
tftpd.c \
tftp.h
tftpd_LDADD = @TEST_SERVER_LIBS@
#include "curlx.h" /* from the private lib dir */
#include "getpart.h"
#include "util.h"
+#include "server_sockaddr.h"
/* include memdebug.h last */
#include "memdebug.h"
int main(int argc, char *argv[])
{
- struct sockaddr_in me;
-#ifdef ENABLE_IPV6
- struct sockaddr_in6 me6;
-#endif /* ENABLE_IPV6 */
+ srvr_sockaddr_union_t me;
curl_socket_t sock = CURL_SOCKET_BAD;
curl_socket_t msgsock = CURL_SOCKET_BAD;
int wrotepidfile = 0;
#ifdef ENABLE_IPV6
if(!use_ipv6) {
#endif
- memset(&me, 0, sizeof(me));
- me.sin_family = AF_INET;
- me.sin_addr.s_addr = INADDR_ANY;
- me.sin_port = htons(port);
- rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
+ memset(&me.sa4, 0, sizeof(me.sa4));
+ me.sa4.sin_family = AF_INET;
+ me.sa4.sin_addr.s_addr = INADDR_ANY;
+ me.sa4.sin_port = htons(port);
+ rc = bind(sock, &me.sa, sizeof(me.sa4));
#ifdef ENABLE_IPV6
}
else {
- memset(&me6, 0, sizeof(me6));
- me6.sin6_family = AF_INET6;
- me6.sin6_addr = in6addr_any;
- me6.sin6_port = htons(port);
- rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
+ memset(&me.sa6, 0, sizeof(me.sa6));
+ me.sa6.sin6_family = AF_INET6;
+ me.sa6.sin6_addr = in6addr_any;
+ me.sa6.sin6_port = htons(port);
+ rc = bind(sock, &me.sa, sizeof(me.sa6));
}
#endif /* ENABLE_IPV6 */
if(0 != rc) {
--- /dev/null
+#ifndef HEADER_SERVER_SOCKADDR_H
+#define HEADER_SERVER_SOCKADDR_H
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+
+typedef union {
+ struct sockaddr sa;
+ struct sockaddr_in sa4;
+#ifdef ENABLE_IPV6
+ struct sockaddr_in6 sa6;
+#endif
+} srvr_sockaddr_union_t;
+
+#endif /* HEADER_SERVER_SOCKADDR_H */
#include "getpart.h"
#include "inet_pton.h"
#include "util.h"
+#include "server_sockaddr.h"
/* include memdebug.h last */
#include "memdebug.h"
unsigned short *listenport)
{
/* passive daemon style */
- struct sockaddr_in me;
-#ifdef ENABLE_IPV6
- struct sockaddr_in6 me6;
-#endif /* ENABLE_IPV6 */
+ srvr_sockaddr_union_t listener;
int flag;
int rc;
int totdelay = 0;
#ifdef ENABLE_IPV6
if(!use_ipv6) {
#endif
- memset(&me, 0, sizeof(me));
- me.sin_family = AF_INET;
- me.sin_addr.s_addr = INADDR_ANY;
- me.sin_port = htons(*listenport);
- rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
+ memset(&listener.sa4, 0, sizeof(listener.sa4));
+ listener.sa4.sin_family = AF_INET;
+ listener.sa4.sin_addr.s_addr = INADDR_ANY;
+ listener.sa4.sin_port = htons(*listenport);
+ rc = bind(sock, &listener.sa, sizeof(listener.sa4));
#ifdef ENABLE_IPV6
}
else {
- memset(&me6, 0, sizeof(me6));
- me6.sin6_family = AF_INET6;
- me6.sin6_addr = in6addr_any;
- me6.sin6_port = htons(*listenport);
- rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
+ memset(&listener.sa6, 0, sizeof(listener.sa6));
+ listener.sa6.sin6_family = AF_INET6;
+ listener.sa6.sin6_addr = in6addr_any;
+ listener.sa6.sin6_port = htons(*listenport);
+ rc = bind(sock, &listener.sa, sizeof(listener.sa6));
}
#endif /* ENABLE_IPV6 */
if(rc) {
/* The system was supposed to choose a port number, figure out which
port we actually got and update the listener port value with it. */
curl_socklen_t la_size;
- struct sockaddr *localaddr;
- struct sockaddr_in localaddr4;
+ srvr_sockaddr_union_t localaddr;
#ifdef ENABLE_IPV6
- struct sockaddr_in6 localaddr6;
- if(!use_ipv6) {
+ if(!use_ipv6)
#endif
- la_size = sizeof(localaddr4);
- localaddr = (struct sockaddr *)&localaddr4;
+ la_size = sizeof(localaddr.sa4);
#ifdef ENABLE_IPV6
- }
- else {
- la_size = sizeof(localaddr6);
- localaddr = (struct sockaddr *)&localaddr6;
- }
+ else
+ la_size = sizeof(localaddr.sa6);
#endif
- memset(localaddr, 0, (size_t)la_size);
- if(getsockname(sock, localaddr, &la_size) < 0) {
+ memset(&localaddr.sa, 0, (size_t)la_size);
+ if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
error = SOCKERRNO;
logmsg("getsockname() failed with error: (%d) %s",
error, strerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}
- switch (localaddr->sa_family) {
+ switch (localaddr.sa.sa_family) {
case AF_INET:
- *listenport = ntohs(localaddr4.sin_port);
+ *listenport = ntohs(localaddr.sa4.sin_port);
break;
#ifdef ENABLE_IPV6
case AF_INET6:
- *listenport = ntohs(localaddr6.sin6_port);
+ *listenport = ntohs(localaddr.sa6.sin6_port);
break;
#endif
default:
int main(int argc, char *argv[])
{
- struct sockaddr_in me;
-#ifdef ENABLE_IPV6
- struct sockaddr_in6 me6;
-#endif /* ENABLE_IPV6 */
+ srvr_sockaddr_union_t me;
curl_socket_t sock = CURL_SOCKET_BAD;
curl_socket_t msgsock = CURL_SOCKET_BAD;
int wrotepidfile = 0;
#ifdef ENABLE_IPV6
if(!use_ipv6) {
#endif
- memset(&me, 0, sizeof(me));
- me.sin_family = AF_INET;
- me.sin_port = htons(connectport);
- me.sin_addr.s_addr = INADDR_ANY;
+ memset(&me.sa4, 0, sizeof(me.sa4));
+ me.sa4.sin_family = AF_INET;
+ me.sa4.sin_port = htons(connectport);
+ me.sa4.sin_addr.s_addr = INADDR_ANY;
if (!addr)
addr = "127.0.0.1";
- Curl_inet_pton(AF_INET, addr, &me.sin_addr);
+ Curl_inet_pton(AF_INET, addr, &me.sa4.sin_addr);
- rc = connect(sock, (struct sockaddr *) &me, sizeof(me));
+ rc = connect(sock, &me.sa, sizeof(me.sa4));
#ifdef ENABLE_IPV6
}
else {
- memset(&me6, 0, sizeof(me6));
- me6.sin6_family = AF_INET6;
- me6.sin6_port = htons(connectport);
+ memset(&me.sa6, 0, sizeof(me.sa6));
+ me.sa6.sin6_family = AF_INET6;
+ me.sa6.sin6_port = htons(connectport);
if (!addr)
addr = "::1";
- Curl_inet_pton(AF_INET6, addr, &me6.sin6_addr);
+ Curl_inet_pton(AF_INET6, addr, &me.sa6.sin6_addr);
- rc = connect(sock, (struct sockaddr *) &me6, sizeof(me6));
+ rc = connect(sock, &me.sa, sizeof(me.sa6));
}
#endif /* ENABLE_IPV6 */
if(rc) {
#include "curlx.h" /* from the private lib dir */
#include "getpart.h"
#include "util.h"
+#include "server_sockaddr.h"
/* include memdebug.h last */
#include "memdebug.h"
int main(int argc, char *argv[])
{
- struct sockaddr_in me;
-#ifdef ENABLE_IPV6
- struct sockaddr_in6 me6;
-#endif /* ENABLE_IPV6 */
+ srvr_sockaddr_union_t me;
curl_socket_t sock = CURL_SOCKET_BAD;
curl_socket_t msgsock = CURL_SOCKET_BAD;
int wrotepidfile = 0;
#ifdef ENABLE_IPV6
if(!use_ipv6) {
#endif
- memset(&me, 0, sizeof(me));
- me.sin_family = AF_INET;
- me.sin_addr.s_addr = INADDR_ANY;
- me.sin_port = htons(port);
- rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
+ memset(&me.sa4, 0, sizeof(me.sa4));
+ me.sa4.sin_family = AF_INET;
+ me.sa4.sin_addr.s_addr = INADDR_ANY;
+ me.sa4.sin_port = htons(port);
+ rc = bind(sock, &me.sa, sizeof(me.sa4));
#ifdef ENABLE_IPV6
}
else {
- memset(&me6, 0, sizeof(me6));
- me6.sin6_family = AF_INET6;
- me6.sin6_addr = in6addr_any;
- me6.sin6_port = htons(port);
- rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
+ memset(&me.sa6, 0, sizeof(me.sa6));
+ me.sa6.sin6_family = AF_INET6;
+ me.sa6.sin6_addr = in6addr_any;
+ me.sa6.sin6_port = htons(port);
+ rc = bind(sock, &me.sa, sizeof(me.sa6));
}
#endif /* ENABLE_IPV6 */
if(0 != rc) {
#include "curlx.h" /* from the private lib dir */
#include "getpart.h"
#include "util.h"
+#include "server_sockaddr.h"
/* include memdebug.h last */
#include "memdebug.h"
const char *e_msg;
};
+typedef union {
+ struct tftphdr hdr;
+ char storage[PKTSIZE];
+} tftphdr_storage_t;
+
/*
* bf.counter values in range [-1 .. SEGSIZE] represents size of data in the
* bf.buf buffer. Additionally it can also hold flags BF_ALLOC or BF_FREE.
struct bf {
int counter; /* size of data in buffer, or flag */
- char buf[PKTSIZE]; /* room for data packet */
+ tftphdr_storage_t buf; /* room for data packet */
};
#define BF_ALLOC -3 /* alloc'd but not yet filled */
static int newline = 0; /* fillbuf: in middle of newline expansion */
static int prevchar = -1; /* putbuf: previous char (cr check) */
-static char buf[PKTSIZE];
-static char ackbuf[PKTSIZE];
+static tftphdr_storage_t buf;
+static tftphdr_storage_t ackbuf;
-static struct sockaddr_in from;
+static srvr_sockaddr_union_t from;
static curl_socklen_t fromlen;
static curl_socket_t peer = CURL_SOCKET_BAD;
current = 0;
bfs[1].counter = BF_FREE;
nextone = x; /* ahead or behind? */
- return (struct tftphdr *)bfs[0].buf;
+ return &bfs[0].buf.hdr;
}
static struct tftphdr *w_init(void)
if (b->counter == BF_FREE) /* if it's empty */
read_ahead(test, convert); /* fill it */
- *dpp = (struct tftphdr *)b->buf; /* set caller's ptr */
+ *dpp = &b->buf.hdr; /* set caller's ptr */
return b->counter;
}
return;
nextone = !nextone; /* "incr" next buffer ptr */
- dp = (struct tftphdr *)b->buf;
+ dp = &b->buf.hdr;
if (convert == 0) {
/* The former file reading code did this:
if (bfs[current].counter != BF_FREE) /* if not free */
write_behind(test, convert); /* flush it */
bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
- *dpp = (struct tftphdr *)bfs[current].buf;
+ *dpp = &bfs[current].buf.hdr;
return ct; /* this is a lie of course */
}
count = b->counter; /* remember byte count */
b->counter = BF_FREE; /* reset flag */
- dp = (struct tftphdr *)b->buf;
+ dp = &b->buf.hdr;
nextone = !nextone; /* incr for next time */
writebuf = dp->th_data;
#endif
int j = 0;
char rbuf[PKTSIZE];
- struct sockaddr_in fromaddr;
+ srvr_sockaddr_union_t fromaddr;
curl_socklen_t fromaddrlen;
for (;;) {
#endif
if (i) {
j++;
- fromaddrlen = sizeof(fromaddr);
- (void)recvfrom(f, rbuf, sizeof(rbuf), 0,
- (struct sockaddr *)&fromaddr, &fromaddrlen);
+#ifdef ENABLE_IPV6
+ if(!use_ipv6)
+#endif
+ fromaddrlen = sizeof(fromaddr.sa4);
+#ifdef ENABLE_IPV6
+ else
+ fromaddrlen = sizeof(fromaddr.sa6);
+#endif
+ (void) recvfrom(f, rbuf, sizeof(rbuf), 0,
+ &fromaddr.sa, &fromaddrlen);
}
else
break;
int main(int argc, char **argv)
{
- struct sockaddr_in me;
-#ifdef ENABLE_IPV6
- struct sockaddr_in6 me6;
-#endif /* ENABLE_IPV6 */
-
+ srvr_sockaddr_union_t me;
struct tftphdr *tp;
ssize_t n = 0;
int arg = 1;
#ifdef ENABLE_IPV6
if(!use_ipv6) {
#endif
- memset(&me, 0, sizeof(me));
- me.sin_family = AF_INET;
- me.sin_addr.s_addr = INADDR_ANY;
- me.sin_port = htons(port);
- rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
+ memset(&me.sa4, 0, sizeof(me.sa4));
+ me.sa4.sin_family = AF_INET;
+ me.sa4.sin_addr.s_addr = INADDR_ANY;
+ me.sa4.sin_port = htons(port);
+ rc = bind(sock, &me.sa, sizeof(me.sa4));
#ifdef ENABLE_IPV6
}
else {
- memset(&me6, 0, sizeof(me6));
- me6.sin6_family = AF_INET6;
- me6.sin6_addr = in6addr_any;
- me6.sin6_port = htons(port);
- rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
+ memset(&me.sa6, 0, sizeof(me.sa6));
+ me.sa6.sin6_family = AF_INET6;
+ me.sa6.sin6_addr = in6addr_any;
+ me.sa6.sin6_port = htons(port);
+ rc = bind(sock, &me.sa, sizeof(me.sa6));
}
#endif /* ENABLE_IPV6 */
if(0 != rc) {
for (;;) {
fromlen = sizeof(from);
- n = (ssize_t)recvfrom(sock, buf, sizeof(buf), 0,
- (struct sockaddr *)&from, &fromlen);
+#ifdef ENABLE_IPV6
+ if(!use_ipv6)
+#endif
+ fromlen = sizeof(from.sa4);
+#ifdef ENABLE_IPV6
+ else
+ fromlen = sizeof(from.sa6);
+#endif
+ n = (ssize_t)recvfrom(sock, &buf.storage[0], sizeof(buf.storage), 0,
+ &from.sa, &fromlen);
if(got_exit_signal)
break;
if (n < 0) {
set_advisor_read_lock(SERVERLOGS_LOCK);
serverlogslocked = 1;
- from.sin_family = AF_INET;
-
- peer = socket(AF_INET, SOCK_DGRAM, 0);
- if(CURL_SOCKET_BAD == peer) {
- logmsg("socket");
- result = 2;
- break;
+#ifdef ENABLE_IPV6
+ if(!use_ipv6) {
+#endif
+ from.sa4.sin_family = AF_INET;
+ peer = socket(AF_INET, SOCK_DGRAM, 0);
+ if(CURL_SOCKET_BAD == peer) {
+ logmsg("socket");
+ result = 2;
+ break;
+ }
+ if(connect(peer, &from.sa, sizeof(from.sa4)) < 0) {
+ logmsg("connect: fail");
+ result = 1;
+ break;
+ }
+#ifdef ENABLE_IPV6
}
-
- if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
- logmsg("connect: fail");
- result = 1;
- break;
+ else {
+ from.sa6.sin6_family = AF_INET6;
+ peer = socket(AF_INET6, SOCK_DGRAM, 0);
+ if(CURL_SOCKET_BAD == peer) {
+ logmsg("socket");
+ result = 2;
+ break;
+ }
+ if (connect(peer, &from.sa, sizeof(from.sa6)) < 0) {
+ logmsg("connect: fail");
+ result = 1;
+ break;
+ }
}
+#endif
+
maxtimeout = 5*TIMEOUT;
- tp = (struct tftphdr *)buf;
+ tp = &buf.hdr;
tp->th_opcode = ntohs(tp->th_opcode);
if (tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) {
memset(&test, 0, sizeof(test));
cp = (char *)&tp->th_stuff;
filename = cp;
again:
- while (cp < buf + size) {
+ while (cp < &buf.storage[size]) {
if (*cp == '\0')
break;
cp++;
mysignal(SIGALRM, timer);
#endif
sdp = r_init();
- sap = (struct tftphdr *)ackbuf;
+ sap = &ackbuf.hdr;
do {
size = readit(test, &sdp, pf->f_convert);
if (size < 0) {
#ifdef HAVE_ALARM
alarm(rexmtval); /* read the ack */
#endif
- n = sread(peer, ackbuf, sizeof (ackbuf));
+ n = sread(peer, &ackbuf.storage[0], sizeof(ackbuf.storage));
#ifdef HAVE_ALARM
alarm(0);
#endif
mysignal(SIGALRM, timer);
#endif
rdp = w_init();
- rap = (struct tftphdr *)ackbuf;
+ rap = &ackbuf.hdr;
do {
timeout = 0;
rap->th_opcode = htons((u_short)opcode_ACK);
(void) sigsetjmp(timeoutbuf, 1);
#endif
send_ack:
- if (swrite(peer, ackbuf, 4) != 4) {
+ if (swrite(peer, &ackbuf.storage[0], 4) != 4) {
logmsg("write: fail\n");
goto abort;
}
rap->th_opcode = htons((u_short)opcode_ACK); /* send the "final" ack */
rap->th_block = htons((u_short)recvblock);
- (void) swrite(peer, ackbuf, 4);
+ (void) swrite(peer, &ackbuf.storage[0], 4);
#if defined(HAVE_ALARM) && defined(SIGALRM)
mysignal(SIGALRM, justtimeout); /* just abort read on timeout */
alarm(rexmtval);
#endif
- n = sread(peer, buf, sizeof(buf)); /* normally times out and quits */
+ /* normally times out and quits */
+ n = sread(peer, &buf.storage[0], sizeof(buf.storage));
#ifdef HAVE_ALARM
alarm(0);
#endif
if(got_exit_signal)
goto abort;
- if (n >= 4 && /* if read some data */
- rdp->th_opcode == opcode_DATA && /* and got a data block */
- recvblock == rdp->th_block) { /* then my last ack was lost */
- (void) swrite(peer, ackbuf, 4); /* resend final ack */
+ if (n >= 4 && /* if read some data */
+ rdp->th_opcode == opcode_DATA && /* and got a data block */
+ recvblock == rdp->th_block) { /* then my last ack was lost */
+ (void) swrite(peer, &ackbuf.storage[0], 4); /* resend final ack */
}
abort:
return;
int length;
struct errmsg *pe;
- tp = (struct tftphdr *)buf;
+ tp = &buf.hdr;
tp->th_opcode = htons((u_short)opcode_ERROR);
tp->th_code = htons((u_short)error);
for (pe = errmsgs; pe->e_code >= 0; pe++)
length = (int)strlen(pe->e_msg);
tp->th_msg[length] = '\0';
length += 5;
- if (swrite(peer, buf, length) != length)
+ if (swrite(peer, &buf.storage[0], length) != length)
logmsg("nak: fail\n");
}