Skip to content

Commit

Permalink
try fix #122 #123 #124 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zfl9 committed Apr 17, 2023
1 parent 61ff03c commit 94e5b8a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 7 deletions.
6 changes: 3 additions & 3 deletions dnl.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ static void do_debug(u32 gfw_addr0, u32 gfw_n, u32 chn_addr0, u32 chn_n) {
}
}

/* check map2 hash collisions */
/* check map2 hash collisions */
if (!map_is_null(&s_map2)) {
int maxlen = 0;
for (u32 idx = 0, n = map_cap(&s_map2); idx < n; ++idx) {
Expand Down Expand Up @@ -579,8 +579,8 @@ u8 get_name_tag(const char *noalias name, int namelen) {
assert(n > 0);

u8 name_tag;
for (int i = 0; i < n; ++i) {
if (exists_in_dnl(sub_names[i], sub_namelens[i], &name_tag))
while (--n >= 0) {
if (exists_in_dnl(sub_names[n], sub_namelens[n], &name_tag))
return name_tag;
}

Expand Down
4 changes: 2 additions & 2 deletions ipset.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ void ipset_init(void) {
static inline int send_req(int n_msg) {
assert(n_msg > 0);
assert(n_msg <= MSG_N);
int n_sent = sendall(sendmmsg, s_sock, s_msgv, n_msg, 0);
int n_sent = sendall(x_sendmmsg, s_sock, s_msgv, n_msg, 0);
assert(n_sent != 0);
unlikely_if (n_sent != n_msg)
log_error("failed to send nlmsg: %d != %d, (%d) %s", n_sent, n_msg, errno, strerror(errno));
Expand All @@ -477,7 +477,7 @@ static inline int send_req(int n_msg) {
static inline int recv_res(int n_msg, bool err_if_nomsg) {
assert(n_msg > 0);
assert(n_msg <= MSG_N);
int n_recv = recvmmsg(s_sock, s_msgv, n_msg, MSG_DONTWAIT, NULL);
int n_recv = x_recvmmsg(s_sock, s_msgv, n_msg, MSG_DONTWAIT, NULL);
assert(n_recv != 0);
if (n_recv < 0) { /* no-msg or error */
if (errno == EAGAIN || errno == EWOULDBLOCK)
Expand Down
6 changes: 4 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static void handle_local_packet(void) {

log_verbose("forward [%s] to %s (%s)", s_name_buf, g_upstream_addrs[i], is_chinadns_idx(i) ? "chinadns" : "trustdns");

int n_sent = sendmmsg(s_upstream_sockfds[i], msgv, msg_n, 0);
int n_sent = x_sendmmsg(s_upstream_sockfds[i], msgv, msg_n, 0);
unlikely_if (n_sent != msg_n) {
if (n_sent < 0)
log_error("failed to send query to %s: (%d) %s", g_upstream_addrs[i], errno, strerror(errno));
Expand Down Expand Up @@ -325,7 +325,7 @@ static void handle_remote_packet(int index) {
}

static void handle_timeout_event(struct queryctx *context) {
log_warning("upstream reply timeout, unique msgid: %u", (uint)context->unique_msgid);
log_verbose("upstream reply timeout, unique msgid: %u", (uint)context->unique_msgid);
free_context(context);
}

Expand All @@ -334,6 +334,8 @@ int main(int argc, char *argv[]) {
setvbuf(stdout, NULL, _IOLBF, 256);
opt_parse(argc, argv);

net_init();

log_info("local listen addr: %s#%u", g_bind_ip, (uint)g_bind_port);

if (g_upstream_addrs[CHINADNS1_IDX]) log_info("chinadns server#1: %s", g_upstream_addrs[CHINADNS1_IDX]);
Expand Down
73 changes: 73 additions & 0 deletions net.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define _GNU_SOURCE
#include "net.h"
#include "log.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
Expand All @@ -11,6 +12,78 @@
#define SO_REUSEPORT 15
#endif

int (*x_recvmmsg)(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags, struct timespec *timeout);

int (*x_sendmmsg)(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags);

static int my_recvmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags, struct timespec *timeout) {
unlikely_if (vlen <= 0 || timeout) {
errno = EINVAL;
return -1;
}

bool wait_for_one = flags & MSG_WAITFORONE;
flags &= ~MSG_WAITFORONE;

int nrecv = 0;

for (uint i = 0; i < vlen; ++i) {
ssize_t res = recvmsg(sockfd, &msgvec[i].msg_hdr, flags);
if (res < 0) break;

msgvec[i].msg_len = res;
++nrecv;

if (wait_for_one)
flags |= MSG_DONTWAIT;
}

return nrecv ?: -1;
}

static int my_sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags) {
unlikely_if (vlen <= 0) {
errno = EINVAL;
return -1;
}

int nsent = 0;

for (uint i = 0; i < vlen; ++i) {
ssize_t res = sendmsg(sockfd, &msgvec[i].msg_hdr, flags);
if (res < 0) break;

msgvec[i].msg_len = res;
++nsent;
}

return nsent ?: -1;
}

void net_init(void) {
int res = recvmmsg(-1, NULL, 0, 0, NULL);
assert(res == -1);
(void)res;

if (errno != ENOSYS) {
x_recvmmsg = recvmmsg;
} else {
log_info("recvmmsg not implemented, use recvmsg to simulate");
x_recvmmsg = my_recvmmsg;
}

res = sendmmsg(-1, NULL, 0, 0);
assert(res == -1);
(void)res;

if (errno != ENOSYS) {
x_sendmmsg = sendmmsg;
} else {
log_info("sendmmsg not implemented, use sendmsg to simulate");
x_sendmmsg = my_sendmmsg;
}
}

/* setsockopt(IPV6_V6ONLY) */
static inline void set_ipv6_only(int sockfd) {
unlikely_if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &(int){1}, sizeof(int))) {
Expand Down
8 changes: 8 additions & 0 deletions net.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ union skaddr {
#define skaddr_is_sin6(p) (skaddr_family(p) == AF_INET6)
#define skaddr_size(p) (skaddr_is_sin(p) ? sizeof((p)->sin) : sizeof((p)->sin6))

/* compatible with old kernel (runtime) */
extern int (*x_recvmmsg)(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags, struct timespec *timeout);

/* compatible with old kernel (runtime) */
extern int (*x_sendmmsg)(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, int flags);

void net_init(void);

void set_reuse_port(int sockfd);

int new_udp_socket(int family);
Expand Down

0 comments on commit 94e5b8a

Please sign in to comment.