Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket family is problematic when setting TTL #90

Closed
dartvader316 opened this issue Aug 15, 2024 · 1 comment
Closed

Socket family is problematic when setting TTL #90

dartvader316 opened this issue Aug 15, 2024 · 1 comment

Comments

@dartvader316
Copy link
Contributor

When setting TTL for socket in setttl function, passed socket family is not the same as in used socket. Somehow Linux don't give any errors for this and even sets TTL correctly, but while i tested on NetBSD in #75 it gives error:

setsockopt IP_TTL: Protocol option not available (A bad option or level was specified in a getsockopt(2) or setsockopt(2) call.)

Simple patch for main branch to test:

diff --git a/desync.c b/desync.c
index f4a3aba..e9975d6 100644
--- a/desync.c
+++ b/desync.c
@@ -46,10 +46,21 @@ int get_family(struct sockaddr *dst)
     return dst->sa_family;
 }
 
+int socket_family(int fd){
+    struct sockaddr_in fd_addr;
+    socklen_t fd_addr_len = sizeof(fd_addr);
+    getsockname(fd, (struct sockaddr *) &fd_addr, &fd_addr_len);
+    
+    return fd_addr.sin_family;
+}
+
 
 int setttl(int fd, int ttl, int family) {
     int _ttl = ttl;
     
+    printf("PASSED:ipv%c \n", family == AF_INET6 ? '6' : '4');
+    printf("DETECTED:ipv%c \n", socket_family(fd) == AF_INET6 ? '6' : '4');
+    fflush(stdout);
     if (family == AF_INET) {
         if (setsockopt(fd, IPPROTO_IP,
                  IP_TTL, (char *)&_ttl, sizeof(_ttl)) < 0) {

And when running with ./ciadpi -p 8080 -g 1 even on Linux, it prints after connecting to any domain:

PASSED:ipv4
DETECTED:ipv6

Using my socket_family to detect socket family makes setting TTL correct without any error in NetBSD, however somehow it breaks setting TTL on Linux. So i think this problem is something bigger and not good to fix with workaround for NetBSD i did in 87e368f .

@hufrea
Copy link
Owner

hufrea commented Aug 15, 2024

The socket can be with the AF_INET6 domain, but use an IPv4-mapped address when connecting. When setting TTL in Linux, you need to look at the type of address, not the socket. In NetBSD it seems to be the other way around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants