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
|
/*
* Test if an IP address is assigned to the local system
*
* This uses the fact Linux will not allow binding to an address that
* is not on the system. It is much faster than scanning all the
* interface addresses.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char **argv)
{
int af, s;
if (argc != 2) {
fprintf(stderr, "Usage: %s x.x.x.x\n", argv[0]);
return -1;
}
af = strchr(argv[1], ':') ? AF_INET6 : AF_INET;
s = socket(af, SOCK_STREAM, 0);
if (s < 0) {
perror("socket");
return -1;
}
if (af == AF_INET) {
struct sockaddr_in sin = {
.sin_family = AF_INET,
};
if (inet_pton(af, argv[1], &sin.sin_addr) <= 0) {
fprintf(stderr, "%s: invalid address\n", argv[1]);
return -1;
}
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
if (errno == EADDRNOTAVAIL)
return 1;
perror("bind");
return -1;
}
} else {
struct sockaddr_in6 sin6;
if (inet_pton(af, argv[1], &sin6.sin6_addr) <= 0) {
fprintf(stderr, "%s: invalid address\n", argv[1]);
return -1;
}
if (bind(s, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
if (errno == EADDRNOTAVAIL)
return 1;
perror("bind");
return -1;
}
}
return 0;
}
|