Skip to content

Commit

Permalink
Merge pull request #12 from michael-yuji/master
Browse files Browse the repository at this point in the history
add flags to reuse addr and port
  • Loading branch information
samyk authored Dec 22, 2017
2 parents 10d6e10 + da5d04f commit 5de412c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pwnat.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ struct sockaddr_in remote;
int debug_level = 1; //NO_DEBUG;
int ipver = SOCK_IPV4;

int reuseaddr = 0;
int reuseport = 0;

int udpclient(int argc, char *argv[]);
int udpserver(int argc, char *argv[]);
void usage(char *progname);
Expand All @@ -54,7 +57,7 @@ int main(int argc, char *argv[])
ERROR_GOTO(ret != 0, "WSAStartup() failed", error);
#endif

while((ret = getopt(argc, argv, "hscv6")) != EOF)
while((ret = getopt(argc, argv, "hscv6ap")) != EOF)
{
switch(ret)
{
Expand All @@ -70,6 +73,14 @@ int main(int argc, char *argv[])
isserv = 0;
break;

case 'a':
reuseaddr = 1;
break;

case 'p':
reuseport = 1;
break;

case 'v':
if(debug_level < 3)
debug_level++;
Expand Down Expand Up @@ -117,5 +128,7 @@ void usage(char *progname)
" <args>: [local ip] [proxy port (def:2222)] [[allowed host]:[allowed port] ...]\n"
" -6 use IPv6\n"
" -v show debug output (up to 2)\n"
" -a reuse address\n"
" -p reuse port\n"
" -h show this help and exit\n");
}
5 changes: 5 additions & 0 deletions socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "common.h"

extern int debug_level;
extern int reuseaddr;
extern int reuseport;

void print_hexdump(char *data, int len);

Expand Down Expand Up @@ -149,6 +151,9 @@ int sock_connect(socket_t *sock, int is_serv, char *port)
sa.sin_port = htons(atoi(port));
sa.sin_addr.s_addr = htonl(INADDR_ANY);

setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int));
setsockopt(sock->fd, SOL_SOCKET, SO_REUSEPORT, &reuseport, sizeof(int));

if(sock->type == SOCK_DGRAM)
if( bind(sock->fd, (const struct sockaddr *)&sa, sizeof(struct sockaddr_in))!= 0)
printf("Bind failed\n");
Expand Down

0 comments on commit 5de412c

Please sign in to comment.