| 1 | |
| 2 | |
| 3 | |
| 4 | #include <arpa/inet.h> |
| 5 | #include <errno.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <ifaddrs.h> |
| 8 | #include <net/if.h> |
| 9 | #include <netinet/in.h> |
| 10 | #include <poll.h> |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/socket.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | static int unicastSource(const struct in6_addr *address) { |
| 17 | return !IN6_IS_ADDR_UNSPECIFIED(address) && !IN6_IS_ADDR_LOOPBACK(address) |
| 18 | && !IN6_IS_ADDR_LINKLOCAL(address) && !IN6_IS_ADDR_MULTICAST(address); |
| 19 | } |
| 20 | |
| 21 | |
| 22 | |
| 23 | static int sendProbe(const char *name, const char *run, const char *check) { |
| 24 | if (strlen(run)!=32 || strlen(check)!=32 || strspn(run,"0123456789abcdef")!=32 || strspn(check,"0123456789abcdef")!=32) return 2; |
| 25 | unsigned index=if_nametoindex(name); if(!index) return 1; |
| 26 | int fd=socket(AF_INET6,SOCK_DGRAM,0), hops=1; |
| 27 | if(fd<0 || setsockopt(fd,IPPROTO_IPV6,IPV6_BOUND_IF,&index,sizeof(index))!=0 || |
| 28 | setsockopt(fd,IPPROTO_IPV6,IPV6_MULTICAST_IF,&index,sizeof(index))!=0 || |
| 29 | setsockopt(fd,IPPROTO_IPV6,IPV6_MULTICAST_HOPS,&hops,sizeof(hops))!=0) return 1; |
| 30 | struct sockaddr_in6 dst={.sin6_family=AF_INET6,.sin6_len=sizeof(dst),.sin6_scope_id=index,.sin6_port=htons(49417)}; |
| 31 | if(inet_pton(AF_INET6,"ff02::2",&dst.sin6_addr)!=1) return 1; |
| 32 | char payload[120];int len=snprintf(payload,sizeof(payload),"Cage IPv6 link probe v1 %s %s",run,check); |
| 33 | ssize_t sent=sendto(fd,payload,(size_t)len,0,(struct sockaddr *)&dst,sizeof(dst)); |
| 34 | close(fd); if(sent!=len) return 1; puts("sent");return 0; |
| 35 | } |
| 36 | |
| 37 | int main(int argc, char **argv) { |
| 38 | if (argc==5 && strcmp(argv[1],"link")==0) return sendProbe(argv[2],argv[3],argv[4]); |
| 39 | if (argc != 2) return 2; |
| 40 | int bound = strcmp(argv[1], "default") != 0; |
| 41 | unsigned index = bound ? if_nametoindex(argv[1]) : 0; |
| 42 | if (bound && !index) return 2; |
| 43 | struct ifaddrs *interfaces; |
| 44 | if (getifaddrs(&interfaces) != 0) return 1; |
| 45 | int sources = 0, found = !bound; |
| 46 | for (struct ifaddrs *item = interfaces; item; item = item->ifa_next) { |
| 47 | if (bound && strcmp(item->ifa_name, argv[1])) continue; |
| 48 | found = 1; |
| 49 | if (item->ifa_addr && item->ifa_addr->sa_family == AF_INET6 && (item->ifa_flags & IFF_UP)) { |
| 50 | struct sockaddr_in6 *a = (struct sockaddr_in6 *)item->ifa_addr; |
| 51 | if (unicastSource(&a->sin6_addr)) sources++; |
| 52 | } |
| 53 | } |
| 54 | freeifaddrs(interfaces); |
| 55 | if (!found) return 1; |
| 56 | int fd = socket(AF_INET6, SOCK_STREAM, 0); |
| 57 | if (fd < 0 || fcntl(fd, F_SETFL, O_NONBLOCK) < 0) return 1; |
| 58 | if (bound && setsockopt(fd, IPPROTO_IPV6, IPV6_BOUND_IF, &index, sizeof(index)) != 0) return 1; |
| 59 | struct sockaddr_in6 dst = {.sin6_len = sizeof(dst), .sin6_family = AF_INET6, .sin6_port = htons(443)}; |
| 60 | if (inet_pton(AF_INET6, "2606:4700:4700::1111", &dst.sin6_addr) != 1) return 1; |
| 61 | int error = connect(fd, (struct sockaddr *)&dst, sizeof(dst)) == 0 ? 0 : errno; |
| 62 | if (error == EINPROGRESS) { |
| 63 | struct pollfd pfd = {fd, POLLOUT, 0}; |
| 64 | int ready = poll(&pfd, 1, 1500); |
| 65 | if (ready == 0) error = ETIMEDOUT; |
| 66 | else if (ready < 0) return 1; |
| 67 | else { |
| 68 | socklen_t len = sizeof(error); |
| 69 | if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0) return 1; |
| 70 | } |
| 71 | } |
| 72 | close(fd); |
| 73 | const char *status = error == 0 ? "connected" |
| 74 | : (error == ENETUNREACH || error == EHOSTUNREACH || error == EADDRNOTAVAIL) ? "no-route" |
| 75 | : error == ETIMEDOUT ? "timeout" : "unknown"; |
| 76 | printf("{\"globalSources\":%d,\"boundIndex\":%u,\"result\":\"%s\",\"errno\":%d}\n", sources, index, status, error); |
| 77 | return 0; |
| 78 | } |