-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudpreflect.cpp
More file actions
119 lines (104 loc) · 2.91 KB
/
udpreflect.cpp
File metadata and controls
119 lines (104 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*===============================================================
* Copyright (C) 2013 All rights reserved.
*
* file : test.cpp
* author : joshua.wu
* date : 2013-10-20
* descripe :
*
* modify :
*
================================================================*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define NROUTES 9 /* number of record route slots */
#define SET_ERROR(e) {\
int err = e;\
printf ("%s, errno: %d, %s %d\n",\
strerror (err), err, __FILE__, __LINE__); }
char *m_ip = NULL;
int m_ttl = 3;
uint16_t m_port = 53; ///< most firewall won't block DNS request
void exit_usage ()
{
printf ("*** no use, sorce address was dequerate by NAT device ***"
"focking TP-Link");
printf ("usage\n"
"\t./udpreflect [ -t [ttl] -p [port] ] ip\n"
"\tdefault: ttl = 3, port = 53\n");
exit (0);
}
int main (int argc, char *argv[])
{
int ch;
while ((ch = getopt (argc, argv, "h?" "t:p:")) != EOF) {
switch (ch) {
case 't':
m_ttl = atoi (optarg);
break;
case 'p':
m_port = atoi (optarg);
break;
case 'h':
case '?':
default:
exit_usage ();
}
}
argc -= optind;
argv += optind;
if (argc == 1)
m_ip = *argv;
else
exit_usage ();
int udp_sock = socket (PF_INET, SOCK_DGRAM, 0);
if (setsockopt (udp_sock, IPPROTO_IP, IP_TTL,
&m_ttl, sizeof (m_ttl)) == -1) {
SET_ERROR (errno);
}
/* record route option */
char rspace[3 + 4 * NROUTES + 1]; /* record route space */
memset (rspace, 0, sizeof (rspace));
rspace[0] = IPOPT_NOP;
rspace[1+IPOPT_OPTVAL] = IPOPT_RR;
rspace[1+IPOPT_OLEN] = sizeof (rspace)-1;
rspace[1+IPOPT_OFFSET] = IPOPT_MINOFF;
int optlen = 40;
/*
if (setsockopt (udp_sock, IPPROTO_IP, IP_OPTIONS,
rspace, sizeof (rspace)) < 0) {
SET_ERROR (errno);
}
*/
struct sockaddr_in whereto = {0};
whereto.sin_port = htons (m_port);
whereto.sin_family = AF_INET;
if (inet_pton (AF_INET, m_ip, &whereto.sin_addr) == 1) {
} else {
struct hostent *hp;
hp = gethostbyname (m_ip);
if (!hp) {
SET_ERROR (errno);
return -1;
}
memcpy (&whereto.sin_addr, hp->h_addr, 4);
}
uint16_t pid = htons (getpid () & 0xFFFF);
char buf[128] = {0};
memcpy (buf, &pid, sizeof (pid));
int r = sendto (udp_sock, buf, sizeof (buf), 0,
(struct sockaddr *)&whereto, sizeof (whereto));
if (r == -1)
SET_ERROR (errno);
return 0;
}