跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

macos-ipv6-check.c

78 行 · 3.8 KB历史下载
1// Observe the guest kernel's IPv6 route selection for the fixed public probe.
2// An interface-scoped default on Darwin is selectable by a process that
3// requests IPV6_BOUND_IF, even when an ordinary route lookup returns no route.
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
16static 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// A link-local, one-hop datagram can leave the VM NIC even when the guest
22// deliberately has no ordinary IPv6 default route. It never targets the internet.
23static 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
37int 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}