summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile20
-rw-r--r--ipxrelay.c76
2 files changed, 62 insertions, 34 deletions
diff --git a/Makefile b/Makefile
index a7d65f4..873fb1b 100644
--- a/Makefile
+++ b/Makefile
@@ -4,27 +4,33 @@ COPT = -g -O2
CREQ =
CFLAGS = $(CWARN) $(COPT) $(CREQ)
LDFLAGS =
+LIBS =
+X =
+O = o
-.SUFFIXES: .c .o .S .s .i
+.SUFFIXES: .c .$(O) .S .s .i
-.c.o:
+.c.$(O):
$(CC) $(CFLAGS) -c -o $@ $<
.c.s:
$(CC) $(CFLAGS) -S -o $@ $<
.c.i:
$(CC) $(CFLAGS) -E -o $@ $<
-.S.o:
+.S.$(O):
$(CC) $(CFLAGS) -c -o $@ $<
.S.s:
$(CC) $(CFLAGS) -E -o $@ $<
-all: ipxrelay
+all: ipxrelay$(X)
-ipxrelay: ipxrelay.o
- $(CC) $(LDFLAGS) -o $@ $^
+ipxrelay$(X): ipxrelay.$(O)
+ $(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
clean:
- rm -f ipxrelay *.o
+ rm -f ipxrelay$(X) *.$(O)
spotless: clean
rm -f *~ \#*
+
+win32:
+ $(MAKE) CC=i686-pc-mingw32-gcc LIBS=-lws2_32 X=.exe O=obj all
diff --git a/ipxrelay.c b/ipxrelay.c
index 72eca97..5f6bef1 100644
--- a/ipxrelay.c
+++ b/ipxrelay.c
@@ -2,20 +2,31 @@
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
-#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
-#include <sysexits.h>
#include <inttypes.h>
#include <time.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
-#include <netdb.h>
+#ifdef __WIN32__
+# define _WIN32_WINNT 0x0501 /* Windows XP/2003 or higher */
+# include <winsock2.h>
+# include <ws2tcpip.h>
+# include "lib/sysexits.h"
+#else
+# include <sysexits.h>
+# include <unistd.h>
+# include <sys/socket.h>
+# include <sys/types.h>
+# include <sys/time.h>
+# include <arpa/inet.h>
+# include <netinet/in.h>
+# include <netdb.h>
+#endif
+
+#ifndef AI_ADDRCONFIG
+# define AI_ADDRCONFIG 0
+#endif
#define VERSION_STRING "0.2"
@@ -124,7 +135,7 @@ static void enable_timeout(int fd)
timeout.tv_sec = opt.timeout;
timeout.tv_usec = 0;
- setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
+ setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof timeout);
}
static void disable_timeout(int fd)
@@ -134,7 +145,7 @@ static void disable_timeout(int fd)
timeout.tv_sec = 0;
timeout.tv_usec = 0;
- setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
+ setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof timeout);
}
static void cleanup_timeouts(int fd, time_t now)
@@ -143,7 +154,8 @@ static void cleanup_timeouts(int fd, time_t now)
c = client_head.prev;
- while (c != &client_head && (now - c->last_seen) > opt.timeout) {
+ while (c != &client_head &&
+ (unsigned int)(now - c->last_seen) > opt.timeout) {
p = c->prev;
p->next = c->next;
c->next->prev = p;
@@ -251,10 +263,10 @@ static int ipx_relay_server(int fd)
time_t now;
union {
struct ipx_header hdr;
- uint8_t buf[65536];
+ char buf[65536];
} pkt;
struct sockaddr from;
- socklen_t fromlen;
+ int fromlen;
int len, hlen;
struct ipx_client *s, *d;
@@ -265,10 +277,10 @@ static int ipx_relay_server(int fd)
cleanup_timeouts(fd, now);
fromlen = sizeof from;
- len = recvfrom(fd, pkt.buf, sizeof pkt, 0, &from, &fromlen);
+ len = recvfrom(fd, (void *)pkt.buf, sizeof pkt, 0, &from, &fromlen);
now = time(NULL);
- if (fromlen < sizeof(struct sockaddr_in))
+ if ((size_t)fromlen < sizeof(struct sockaddr_in))
continue; /* WTF? */
if (len < (int)sizeof(struct ipx_header))
@@ -355,12 +367,32 @@ static int set_sock_addr(const char *host, union sock_addr *s, char **name)
return 0;
}
+/*
+ * Basic stuff for a daemon to do. Is there any equivalent for Windows?
+ */
+static void daemonize_and_write_pidfile(void)
+{
+#ifndef __WIN32__
+ FILE *pidfile = NULL;
+
+ if (opt.pidfile)
+ pidfile = fopen(opt.pidfile, "w");
+
+ if (!opt.foreground)
+ daemon(0, 0);
+
+ if (pidfile) {
+ fprintf(pidfile, "%lu\n", (unsigned long)getpid());
+ fclose(pidfile);
+ }
+#endif
+}
+
int main(int argc, char *argv[])
{
int c;
char *ep;
int fd = -1;
- FILE *pidfile;
_progname = argv[0];
@@ -447,17 +479,7 @@ int main(int argc, char *argv[])
}
}
- pidfile = NULL;
- if (opt.pidfile)
- pidfile = fopen(opt.pidfile, "w");
-
- if (!opt.foreground)
- daemon(0, 0);
-
- if (pidfile) {
- fprintf(pidfile, "%lu\n", (unsigned long)getpid());
- fclose(pidfile);
- }
+ daemonize_and_write_pidfile();
return ipx_relay_server(fd);
}