-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket-query.c
174 lines (174 loc) · 6.06 KB
/
socket-query.c
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <sys/socket.h>
#include <netinet/ip6.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#define Q_LOCAL_ADDR 1
#define Q_REMOTE_ADDR 2
#define Q_LOCAL_PORT 3
#define Q_REMOTE_PORT 4
#define Q_LOCAL_ALL 5
#define Q_REMOTE_ALL 6
int main(int argc, char **argv) {
int use_raw = 0;
int type = 0;
int opt = 0;
int socket_num = 0;
while ((opt = getopt(argc, argv, "aplrRf:")) >= 0) {
switch(opt) {
case 'a':
if (type == Q_LOCAL_ALL) type = Q_LOCAL_ADDR;
else if (type == Q_REMOTE_ALL) type = Q_REMOTE_ADDR;
else goto invalid_ap;
break;
case 'p':
if (type == Q_LOCAL_ALL) type = Q_LOCAL_PORT;
else if (type == Q_REMOTE_ALL) type = Q_REMOTE_PORT;
else goto invalid_ap;
break;
case 'l':
type = Q_LOCAL_ALL;
break;
case 'r':
type = Q_REMOTE_ALL;
break;
case 'R':
use_raw = 1;
break;
case 'f':
socket_num = atoi(optarg);
break;
default:
invalid_ap:
fprintf(stderr, "Usage: %s [-l[a|p]] [-r[a|p]] -R [-f SOCKET_FD]\n", argv[0]);
return 1;
break;
}
}
socklen_t l = sizeof(struct sockaddr_in6);
struct sockaddr_in6 result = {0};
socklen_t l2 = sizeof(struct sockaddr_in6);
struct sockaddr_in6 result2 = {0};
switch(type) {
case Q_REMOTE_ADDR:
case Q_REMOTE_PORT:
case Q_REMOTE_ALL:
if (getpeername(socket_num, (struct sockaddr *) &result, &l) || result.sin6_family != AF_INET6) {
perror("getpeername");
return 1;
}
break;
case Q_LOCAL_ADDR:
case Q_LOCAL_PORT:
case Q_LOCAL_ALL:
if (getsockname(socket_num, (struct sockaddr *) &result, &l) || result.sin6_family != AF_INET6) {
perror("getsockname");
return 1;
}
break;
default:
if (getpeername(socket_num, (struct sockaddr *) &result, &l) || result.sin6_family != AF_INET6) {
perror("getpeername");
return 1;
}
if (getsockname(socket_num, (struct sockaddr *) &result2, &l2) || result2.sin6_family != AF_INET6) {
perror("getsockname");
return 1;
}
break;
}
switch(type) {
case Q_REMOTE_ADDR:
case Q_LOCAL_ADDR:
if (use_raw) {
printf("%08lX%08lX%08lX%08lX\n",
(unsigned long) ntohl(result.sin6_addr.s6_addr32[0]),
(unsigned long) ntohl(result.sin6_addr.s6_addr32[1]),
(unsigned long) ntohl(result.sin6_addr.s6_addr32[2]),
(unsigned long) ntohl(result.sin6_addr.s6_addr32[3]));
} else {
printf("%04lx:%04lx:%04lx:%04lx:%04lx:%04lx:%04lx:%04lx\n",
(unsigned long) ntohs(result.sin6_addr.s6_addr16[0]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[1]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[2]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[3]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[4]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[5]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[6]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[7]));
}
break;
case Q_LOCAL_PORT:
case Q_REMOTE_PORT:
if (use_raw) {
printf("%04lX\n", (unsigned long) ntohs(result.sin6_port));
} else {
printf("%lu\n", (unsigned long) ntohs(result.sin6_port));
}
break;
case Q_LOCAL_ALL:
case Q_REMOTE_ALL:
if (use_raw) {
printf("%08lX%08lX%08lX%08lX:%04lX\n",
(unsigned long) ntohl(result.sin6_addr.s6_addr32[0]),
(unsigned long) ntohl(result.sin6_addr.s6_addr32[1]),
(unsigned long) ntohl(result.sin6_addr.s6_addr32[2]),
(unsigned long) ntohl(result.sin6_addr.s6_addr32[3]),
(unsigned long) ntohs(result.sin6_port));
} else {
printf("[%04lx:%04lx:%04lx:%04lx:%04lx:%04lx:%04lx:%04lx]:%lu\n",
(unsigned long) ntohs(result.sin6_addr.s6_addr16[0]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[1]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[2]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[3]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[4]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[5]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[6]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[7]),
(unsigned long) ntohs(result.sin6_port));
}
break;
default:
if (use_raw) {
printf("%08lX%08lX%08lX%08lX:%04lX %08lX%08lX%08lX%08lX:%04lX\n",
(unsigned long) ntohl(result2.sin6_addr.s6_addr32[0]),
(unsigned long) ntohl(result2.sin6_addr.s6_addr32[1]),
(unsigned long) ntohl(result2.sin6_addr.s6_addr32[2]),
(unsigned long) ntohl(result2.sin6_addr.s6_addr32[3]),
(unsigned long) ntohs(result2.sin6_port),
(unsigned long) ntohl(result.sin6_addr.s6_addr32[0]),
(unsigned long) ntohl(result.sin6_addr.s6_addr32[1]),
(unsigned long) ntohl(result.sin6_addr.s6_addr32[2]),
(unsigned long) ntohl(result.sin6_addr.s6_addr32[3]),
(unsigned long) ntohs(result.sin6_port));
} else {
printf("[%04lx:%04lx:%04lx:%04lx:%04lx:%04lx:%04lx:%04lx]:%lu [%04lx:%04lx:%04lx:%04lx:%04lx:%04lx:%04lx:%04lx]:%lu\n",
(unsigned long) ntohs(result2.sin6_addr.s6_addr16[0]),
(unsigned long) ntohs(result2.sin6_addr.s6_addr16[1]),
(unsigned long) ntohs(result2.sin6_addr.s6_addr16[2]),
(unsigned long) ntohs(result2.sin6_addr.s6_addr16[3]),
(unsigned long) ntohs(result2.sin6_addr.s6_addr16[4]),
(unsigned long) ntohs(result2.sin6_addr.s6_addr16[5]),
(unsigned long) ntohs(result2.sin6_addr.s6_addr16[6]),
(unsigned long) ntohs(result2.sin6_addr.s6_addr16[7]),
(unsigned long) ntohs(result2.sin6_port),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[0]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[1]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[2]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[3]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[4]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[5]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[6]),
(unsigned long) ntohs(result.sin6_addr.s6_addr16[7]),
(unsigned long) ntohs(result.sin6_port));
}
break;
}
if (fflush(stdout) || ferror(stdout)) {
perror("stdout: write error");
return 1;
}
return 0;
}