Skip to content

Commit

Permalink
Allowing set of destination port
Browse files Browse the repository at this point in the history
  • Loading branch information
atupone committed Oct 2, 2023
1 parent 44bfe43 commit bd2bfc4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
7 changes: 5 additions & 2 deletions udpCallback.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static int udpSocket;
struct sockaddr_in sockaddr;
static int start_loop = 1;
char *dvalue = NULL;
char *pvalue = NULL;
int flood = 0;
int oneByOne = 0;
long int countToFlood = 0;
Expand Down Expand Up @@ -238,11 +239,13 @@ static void callback_handler(u_char *user __attribute__((unused)),
/* Set destination */
if (!dvalue)
sockaddr.sin_addr.s_addr = ip_hdr.ip_dst.s_addr;
if (!pvalue) {
#ifdef HAVE_STRUCT_UDPHDR_UH_DPORT
sockaddr.sin_port = udp_hdr.uh_dport;
sockaddr.sin_port = udp_hdr.uh_dport;
#else
sockaddr.sin_port = udp_hdr.dest;
sockaddr.sin_port = udp_hdr.dest;
#endif
}

if (asterixTime)
{
Expand Down
1 change: 1 addition & 0 deletions udpCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
extern struct sockaddr_in sockaddr;
extern int rewriteDestination;
extern char *dvalue;
extern char *pvalue;
extern int flood;
extern int oneByOne;
extern int asterixTime;
Expand Down
3 changes: 3 additions & 0 deletions udpReplay.1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Adjust the Asterix Time Of Day to reflect the time the message is sent
.BR -d\ host | --dest\ host
Send the datagram to the specified host. It this option is not present, data are sent to the original host as recorded in the pcap file
.TP
.BR -p\ port | --port\ port
Send the datagram to the specified port. It this option is not present, data are sent to the original port as recorded in the pcap file
.TP
.BR --mttl\ ttlValue
Set the time to live for multicast packets to the value specified as argument. Otherwise is set to 1
.TP
Expand Down
18 changes: 17 additions & 1 deletion udpReplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ int main(int argc, char* argv[])
{"step", no_argument, 0, '1'},
{"flood", no_argument, 0, 'f'},
{"dest", required_argument, 0, 'd'},
{"port", required_argument, 0, 'p'},
{"astx", no_argument, 0, 4},
{"mttl", required_argument, 0, 1},
{0, 0, 0, 0}
};
int option_index;

while ((c = getopt_long(argc, argv, "1fd:", long_options, &option_index)) != -1)
while ((c = getopt_long(argc, argv, "1fd:p:", long_options, &option_index)) != -1)
switch (c) {
case 1:
setMulticastTTL = 1;
Expand All @@ -65,6 +66,9 @@ int main(int argc, char* argv[])
case 'd':
dvalue = optarg;
break;
case 'p':
pvalue = optarg;
break;
default:
break;
};
Expand Down Expand Up @@ -94,6 +98,18 @@ int main(int argc, char* argv[])
return -1;
}
}
if (pvalue) {
int port = atoi(pvalue);
if (port <= 0) {
printf("-p %s option is an invalid number or zero\n", pvalue);
return -1;
}
if (port > 65535) {
printf("-p %s option is greater then 65535\n", pvalue);
return -1;
}
sockaddr.sin_port = htons(port);
}

pcap = pcap_open_offline(pcapName, errbuf);

Expand Down

0 comments on commit bd2bfc4

Please sign in to comment.