-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnobind.c
More file actions
50 lines (46 loc) · 1.23 KB
/
nobind.c
File metadata and controls
50 lines (46 loc) · 1.23 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
/**
* Demonstrate that it's possible to call listen on a TCP inet socket
* without first bindin it, and the result is a socket assigned to an
* ephemeral port number
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/sendfile.h>
#include <fcntl.h>
int main(int argc, char **argv) {
int sock, ephd;
struct sockaddr_storage addr;
char host[100], serv[100];
socklen_t addrlen;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
if (listen(sock, 10) == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
addrlen = sizeof (addr);
if (getsockname(sock, (struct sockaddr*)&addr, &addrlen) == -1) {
perror("getsockname");
exit(EXIT_FAILURE);
}
if (getnameinfo((struct sockaddr*)&addr, addrlen, host, 100, serv, 100,
NI_NUMERICHOST | NI_NUMERICSERV) == -1) {
perror("getnameinfo");
exit(EXIT_FAILURE);
}
puts("Ephemeral port range defined in /proc/sys/net/ipv4/ip_local_port_range");
ephd = open("/proc/sys/net/ipv4/ip_local_port_range", O_RDONLY);
sendfile(STDOUT_FILENO, ephd, NULL, 100);
puts("Host:");
puts(host);
puts("Serv:");
puts(serv);
exit(EXIT_SUCCESS);
}