heiher / hev-socks5-tunnel

A high-performance tun2socks for Linux/Android/FreeBSD/macOS/iOS/WSL2 (IPv4/IPv6/TCP/UDP)
MIT License
636 stars 130 forks source link

macos ip rule #105

Closed cuicanju closed 3 months ago

cuicanju commented 3 months ago

Could you please advise on how to set up routing rules on macOS?

cuicanju commented 3 months ago
static int
hev_socks5_session_tcp_bind (HevSocks5 *self, int fd,
                             const struct sockaddr *dest)
{
    HevConfigServer *srv;
    unsigned int mark;

    LOG_D ("%p socks5 session tcp bind", self);

    srv = hev_config_get_socks5_server ();
    mark = srv->mark;

    if (mark) {
        int res = 0;

#if defined(__linux__)
        res = setsockopt (fd, SOL_SOCKET, SO_MARK, &mark, sizeof (mark));
#elif defined(__FreeBSD__)
        res = setsockopt (fd, SOL_SOCKET, SO_USER_COOKIE, &mark, sizeof (mark));
#endif
        if (res < 0)
            return -1;
    }

    return 0;
}

And why is there no need to set a mark in the macOS environment here?

cattyhouse commented 3 months ago
static int
hev_socks5_session_tcp_bind (HevSocks5 *self, int fd,
                             const struct sockaddr *dest)
{
    HevConfigServer *srv;
    unsigned int mark;

    LOG_D ("%p socks5 session tcp bind", self);

    srv = hev_config_get_socks5_server ();
    mark = srv->mark;

    if (mark) {
        int res = 0;

#if defined(__linux__)
        res = setsockopt (fd, SOL_SOCKET, SO_MARK, &mark, sizeof (mark));
#elif defined(__FreeBSD__)
        res = setsockopt (fd, SOL_SOCKET, SO_USER_COOKIE, &mark, sizeof (mark));
#endif
        if (res < 0)
            return -1;
    }

    return 0;
}

And why is there no need to set a mark in the macOS environment here?

cuicanju commented 3 months ago
static int
hev_socks5_session_tcp_bind (HevSocks5 *self, int fd,
                             const struct sockaddr *dest)
{
    HevConfigServer *srv;
    unsigned int mark;

    LOG_D ("%p socks5 session tcp bind", self);

    srv = hev_config_get_socks5_server ();
    mark = srv->mark;

    if (mark) {
        int res = 0;

#if defined(__linux__)
        res = setsockopt (fd, SOL_SOCKET, SO_MARK, &mark, sizeof (mark));
#elif defined(__FreeBSD__)
        res = setsockopt (fd, SOL_SOCKET, SO_USER_COOKIE, &mark, sizeof (mark));
#endif
        if (res < 0)
            return -1;
    }

    return 0;
}

And why is there no need to set a mark in the macOS environment here?

  • From freebsd manpage, that's for ipfw/dummynet. macOS uses pf which does not uses any kind of mark.
SO_USER_COOKIE can be used to set the uint32_t so_user_cookie field  in
       the  socket.   The  value is an uint32_t, and can be used in the   kernel
       code that manipulates traffic related to   the socket.  The default value
       for the field is   0.  As an example, the value can be used as the skipto
       target or pipe number in   ipfw/dummynet

Thank you very much for your response and the discussions that helped solve the problem that has troubled me for a long time