summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2018-01-04 22:14:21 +0100
committerChristian Poessinger <christian@poessinger.com>2018-01-04 22:14:21 +0100
commit13127cbd62e69be2d186c9d76d112729ea95a1ef (patch)
tree4289a23dae784443ebc97aa8d149a781c7c313d6
parent5286c027794cec6759495ba15dcab999c5921ec6 (diff)
downloadudp-broadcast-relay-13127cbd62e69be2d186c9d76d112729ea95a1ef.tar.gz
udp-broadcast-relay-13127cbd62e69be2d186c9d76d112729ea95a1ef.zip
Minor adjustments regarding '-d' debug output
-rw-r--r--udp-broadcast-relay.c162
-rw-r--r--udp-broadcast-relay@.service2
2 files changed, 81 insertions, 83 deletions
diff --git a/udp-broadcast-relay.c b/udp-broadcast-relay.c
index f3df27b..4010cf2 100644
--- a/udp-broadcast-relay.c
+++ b/udp-broadcast-relay.c
@@ -1,44 +1,41 @@
/*
-******************************************************************
-udp-broadcast-relay
- Relays UDP broadcasts to other networks, forging
- the sender address.
-
-Copyright (c) 2003 Joachim Breitner <mail@joachim-breitner.de>
-
-Based upon:
-udp_broadcast_fw ; Forwards UDP broadcast packets to all local
- interfaces as though they originated from sender
-
-Copyright (C) 2002 Nathan O'Sullivan
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-******************************************************************
-
-Thanks:
-
-Arny <cs6171@scitsc.wlv.ac.uk>
-- public domain UDP spoofing code
-http://www.netfor2.com/ip.htm
-- IP/UDP packet formatting info
-
-*/
-
-#define MAXIFS 256
-#define DPRINT if (debug) printf
-#define IPHEADER_LEN 20
-#define UDPHEADER_LEN 8
-#define HEADER_LEN (IPHEADER_LEN + UDPHEADER_LEN)
-#define TTL_ID_OFFSET 64
-
+ * udp-broadcast-relay
+ * Relays UDP broadcasts to other networks, forging
+ * the sender address.
+ *
+ * Copyright (c) 2003 Joachim Breitner <mail@joachim-breitner.de>
+ *
+ * Based upon:
+ * udp_broadcast_fw ; Forwards UDP broadcast packets to all local
+ * interfaces as though they originated from sender
+ *
+ * Copyright (C) 2002 Nathan O'Sullivan
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+/*
+ * Thanks to:
+ * Arny <cs6171@scitsc.wlv.ac.uk> - public domain UDP spoofing code
+ * http://www.netfor2.com/ip.htm - IP/UDP packet formatting info
+ */
+
+#define MAXIFS 256
+#define DPRINT if (debug) printf
+#define IPHEADER_LEN 20
+#define UDPHEADER_LEN 8
+#define HEADER_LEN (IPHEADER_LEN + UDPHEADER_LEN)
+#define TTL_ID_OFFSET 64
+#define MAX_ID 99
+
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in_systm.h>
@@ -77,7 +74,7 @@ int main(int argc,char **argv)
{
/* Debugging, forking, other settings */
int debug, forking;
-
+
u_int16_t port;
u_char id;
u_char ttl;
@@ -89,13 +86,13 @@ int main(int argc,char **argv)
/* Structure holds info on local interfaces */
struct ifreq reqbuf;
int maxifs;
-
+
/* Address broadcast packet was sent from */
struct sockaddr_in rcv_addr;
/* Spoofing source address of outgoing packets */
in_addr_t spoof_addr = 0;
-
+
/* Incoming message read via rcvsmsg */
struct msghdr rcv_msg;
struct iovec iov;
@@ -103,44 +100,46 @@ int main(int argc,char **argv)
/* various variables */
int x=1, len;
-
+
struct cmsghdr *cmsg;
int *ttlptr=NULL;
int rcv_ifindex = 0;
- iov.iov_base = gram+ HEADER_LEN;
+ iov.iov_base = gram+ HEADER_LEN;
iov.iov_len = 4006 - HEADER_LEN - 1;
-
+
rcv_msg.msg_name = &rcv_addr;
rcv_msg.msg_namelen = sizeof(rcv_addr);
rcv_msg.msg_iov = &iov;
rcv_msg.msg_iovlen = 1;
rcv_msg.msg_control = pkt_infos;
rcv_msg.msg_controllen = sizeof(pkt_infos);
-
+
/* parsing the args */
if(argc < 5)
{
- fprintf(stderr,"usage: %s [-d] [-f] [-s IP] id udp-port dev1 dev2 ...\n\n",*argv);
- fprintf(stderr,"This program listens for broadcast packets on the specified UDP port\n"
- "and then forwards them to each other given interface. Packets are sent\n"
+ fprintf(stderr,"usage: %s [-d] [-f] [-s IP] id port dev1 dev2 ...\n\n",*argv);
+ fprintf(stderr,"This program listens for broadcast packets on the specified UDP port\n"
+ "and then forwards them to each other given interface. Packets are sent\n"
"such that they appear to have come from the original broadcaster, resp.\n"
- "from the spoofing IP in case -s is used. When using multiple instances\n"
+ "from the spoofing IP in case -s is used.\n"
+ "\n"
+ "One ID per UDP port is required. Maximum number of IDs: %i\n"
"for the same port on the same network, they must have a different id.\n\n"
" -d enables debugging\n"
" -f forces forking to background\n"
" -s IP sets the source IP of forwarded packets; otherwise the\n"
- " original sender's address is used\n\n");
+ " original sender's address is used\n\n", MAX_ID);
exit(1);
};
-
+
if ((debug = (strcmp(argv[1],"-d") == 0)))
{
argc--;
argv++;
DPRINT ("Debugging Mode enabled\n");
};
-
+
if ((forking = (strcmp(argv[1],"-f") == 0)))
{
argc--;
@@ -171,9 +170,9 @@ int main(int argc,char **argv)
argc--;
argv++;
- if (id < 1 || id > 99)
+ if (id < 1 || id > MAX_ID)
{
- fprintf (stderr,"ID argument %i not between 1 and 99\n",id);
+ fprintf (stderr,"ID argument %i not between 1 and %d\n", id, MAX_ID);
exit(1);
}
ttl = id+TTL_ID_OFFSET;
@@ -181,7 +180,7 @@ int main(int argc,char **argv)
/* The id is used to detect packets we just sent, and is stored in the "ttl" field,
* which is not used with broadcast packets. Beware when using this with
* non-broadcast-packets */
-
+
if ((port = atoi(argv[1])) == 0)
{
fprintf (stderr,"Port argument not valid\n");
@@ -189,7 +188,7 @@ int main(int argc,char **argv)
}
argc--;
argv++;
-
+
DPRINT ("ID: %i (ttl: %i), Port %i\n",id,ttl,port);
@@ -204,7 +203,7 @@ int main(int argc,char **argv)
/* For each interface on the command line */
for (maxifs=0;argc>1;argc--,argv++)
{
- int ioctl_request;
+ int ioctl_request;
strncpy(reqbuf.ifr_name,argv[1],IFNAMSIZ);
@@ -213,10 +212,10 @@ int main(int argc,char **argv)
perror("ioctl(SIOCGIFINDEX)");
exit(1);
}
-
- /* Save the index for later use */
+
+ /* Save the index for later use */
ifs[maxifs].ifindex = reqbuf.ifr_ifindex;
-
+
/* Request flags for this interface */
if (ioctl(fd,SIOCGIFFLAGS, &reqbuf) < 0) {
perror("ioctl(SIOCGIFFLAGS)");
@@ -231,9 +230,9 @@ int main(int argc,char **argv)
/* find the address type we need */
if (reqbuf.ifr_flags & IFF_BROADCAST)
ioctl_request = SIOCGIFBRDADDR;
- else
+ else
ioctl_request = SIOCGIFDSTADDR;
-
+
/* Request the broadcast/destination address for this interface */
if (ioctl(fd,ioctl_request, &reqbuf) < 0) {
@@ -287,7 +286,7 @@ int main(int argc,char **argv)
/* well, we want the max index, actually */
maxifs--;
DPRINT("found %i interfaces total\n",maxifs+1);
-
+
/* Free our allocated buffer and close the socket */
close(fd);
@@ -328,16 +327,16 @@ int main(int argc,char **argv)
*(u_short*)(gram+22)=(u_short)htons(port);
/* Fork to background */
- if (! debug) {
- if (forking && fork())
- exit(0);
+ if (! debug) {
+ if (forking && fork())
+ exit(0);
- fclose(stdin);
- fclose(stdout);
- fclose(stderr);
- }
+ fclose(stdin);
+ fclose(stdout);
+ fclose(stderr);
+ }
- DPRINT("Done Initializing\n\n");
+ DPRINT("Done Initializing\n\n");
for (;;) /* endless loop */
{
@@ -365,16 +364,15 @@ int main(int argc,char **argv)
DPRINT ("Got local package (TTL %i) on interface %i\n",*ttlptr,rcv_ifindex);
continue;
}
-
+
gram[HEADER_LEN + len] =0;
DPRINT("Got remote package:\n");
- // DPRINT("Content:\t%s\n",gram+HEADER_LEN);
- DPRINT("TTL:\t\t%i\n",*ttlptr);
- DPRINT("Interface:\t%i\n",rcv_ifindex);
- DPRINT("From:\t\t%s:%d\n",inet_ntoa(rcv_addr.sin_addr),rcv_addr.sin_port);
-
- /* copy sender's details into our datagram as the source addr */
+ DPRINT("Interface %i, TTL %3i, from %s:%d\n", rcv_ifindex, *ttlptr,
+ inet_ntoa(rcv_addr.sin_addr), rcv_addr.sin_port);
+ DPRINT("Content: %s\n", gram + HEADER_LEN);
+
+ /* copy sender's details into our datagram as the source addr */
if (spoof_addr)
rcv_addr.sin_addr.s_addr = spoof_addr;
bcopy(&(rcv_addr.sin_addr.s_addr),(gram+12),4);
@@ -390,13 +388,13 @@ int main(int argc,char **argv)
if (ifs[x].ifindex == rcv_ifindex) continue; /* no bounces, please */
/* Set destination addr ip - port is set already */
- bcopy(&(ifs[x].dstaddr.sin_addr.s_addr),(gram+16),4);
+ bcopy(&(ifs[x].dstaddr.sin_addr.s_addr),(gram+16),4);
DPRINT ("Sent to %s:%d on interface %i\n",
inet_ntoa(ifs[x].dstaddr.sin_addr), /* dst ip */
ntohs(*(u_short*)(gram+22)), /* dst port */
ifs[x].ifindex); /* interface number */
-
+
/* Send the packet */
if (sendto(ifs[x].raw_socket,
&gram,
diff --git a/udp-broadcast-relay@.service b/udp-broadcast-relay@.service
index 90b37b7..229748c 100644
--- a/udp-broadcast-relay@.service
+++ b/udp-broadcast-relay@.service
@@ -7,7 +7,7 @@ RequiresMountsFor=/run
Type=simple
NotifyAccess=main
EnvironmentFile=-/etc/default/udp-broadcast-relay%I
-ExecStart=/usr/sbin/udp-broadcast-relay %I $DAEMON_ARGS
+ExecStart=/usr/sbin/udp-broadcast-relay $DAEMON_ARGS
Restart=on-failure
[Install]