Skip to content

Instantly share code, notes, and snippets.

@bearice
Created August 14, 2021 16:03
Show Gist options
  • Select an option

  • Save bearice/21cbcd65a8bf49b72119a2534797cf91 to your computer and use it in GitHub Desktop.

Select an option

Save bearice/21cbcd65a8bf49b72119a2534797cf91 to your computer and use it in GitHub Desktop.

Revisions

  1. bearice created this gist Aug 14, 2021.
    32 changes: 32 additions & 0 deletions ip6t.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #include <assert.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <linux/netfilter_ipv6/ip6_tables.h>

    int main(){
    int sk = socket(AF_INET6,SOCK_STREAM,0);
    printf("sk=%d\n",sk);

    struct sockaddr_in6 addr={0};
    addr.sin6_family = AF_INET6;
    addr.sin6_port = htons(1111);
    assert(0==bind(sk,(struct sockaddr*)&addr,sizeof(addr)));
    assert(0==listen(sk,10));

    struct sockaddr_in6 ca;
    int cal = sizeof(ca);
    int csk = accept(sk,(struct sockaddr*)&ca,&cal);

    char buf[INET6_ADDRSTRLEN];
    inet_ntop(AF_INET6, &(ca.sin6_addr), buf, INET6_ADDRSTRLEN);
    printf("client=%s\n",buf);

    struct sockaddr_in6 sa;
    int sal = sizeof(sa);
    getsockopt(csk,SOL_IPV6,IP6T_SO_ORIGINAL_DST,&sa,&sal);
    inet_ntop(AF_INET6, &(sa.sin6_addr), buf, INET6_ADDRSTRLEN);
    printf("server=%s\n",buf);

    return 0;
    }
    9 changes: 9 additions & 0 deletions test.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    #!/bin/sh
    gcc ip6t.c -o ip6t

    ./ip6t&
    sudo ip6tables -t nat -I OUTPUT -d 2001::1 -p tcp -j REDIRECT --to-ports 1111
    nc 2001::1 11111 -vN

    sleep 1
    sudo ip6tables -t nat -D OUTPUT -d 2001::1 -p tcp -j REDIRECT --to-ports 1111