Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 75649b6
Author: Nick Peng <[email protected]>
Date:   Sat Jul 20 00:33:24 2024 +0800

    fastping: fix asan warnings.
  • Loading branch information
PikuZheng committed Jul 20, 2024
1 parent 9e1c58a commit 69de1cc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 53 deletions.
24 changes: 12 additions & 12 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

#define member_size(type, member) sizeof(((type *)0)->member)

static int is_aligment(void *ptr, int aligment)
static int is_aligned(void *ptr, int aligment)
{
return ((uintptr_t)(ptr)) % aligment == 0;
}
Expand All @@ -58,7 +58,7 @@ static unsigned short _dns_read_short(unsigned char **buffer)
{
unsigned short value = 0;

if (is_aligment(*buffer, 2)) {
if (is_aligned(*buffer, 2)) {
value = *((unsigned short *)(*buffer));
} else {
memcpy(&value, *buffer, 2);
Expand Down Expand Up @@ -88,7 +88,7 @@ static void _dns_write_short(unsigned char **buffer, unsigned short value)
{
value = htons(value);

if (is_aligment(*buffer, 2)) {
if (is_aligned(*buffer, 2)) {
*((unsigned short *)(*buffer)) = value;
} else {
memcpy(*buffer, &value, 2);
Expand All @@ -110,7 +110,7 @@ static void _dns_write_shortptr(unsigned char **buffer, void *ptrvalue)

value = htons(value);

if (is_aligment(*buffer, 2)) {
if (is_aligned(*buffer, 2)) {
*((unsigned short *)(*buffer)) = value;
} else {
memcpy(*buffer, &value, 2);
Expand All @@ -123,7 +123,7 @@ static void _dns_write_shortptr(unsigned char **buffer, void *ptrvalue)
static void _dns_write_int(unsigned char **buffer, unsigned int value)
{
value = htonl(value);
if (is_aligment(*buffer, 4)) {
if (is_aligned(*buffer, 4)) {
*((unsigned int *)(*buffer)) = value;
} else {
memcpy(*buffer, &value, 4);
Expand All @@ -143,7 +143,7 @@ static void _dns_write_intptr(unsigned char **buffer, void *ptrvalue)
}

value = htonl(value);
if (is_aligment(*buffer, 4)) {
if (is_aligned(*buffer, 4)) {
*((unsigned int *)(*buffer)) = value;
} else {
memcpy(*buffer, &value, 4);
Expand All @@ -156,7 +156,7 @@ static unsigned int _dns_read_int(unsigned char **buffer)
{
unsigned int value = 0;

if (is_aligment(*buffer, 4)) {
if (is_aligned(*buffer, 4)) {
value = *((unsigned int *)(*buffer));
} else {
memcpy(&value, *buffer, 4);
Expand Down Expand Up @@ -663,14 +663,14 @@ int dns_add_rr_nested_memcpy(struct dns_rr_nested *rr_nested, const void *data,
return -1;
}

if (is_aligment(rr_nested->context.ptr, 2) == 0) {
if (is_aligned(rr_nested->context.ptr, 2) == 0) {
return -1;
}

memcpy(rr_nested->context.ptr, data, data_len);
rr_nested->context.ptr += data_len;

if (is_aligment(rr_nested->context.ptr, 2) == 0) {
if (is_aligned(rr_nested->context.ptr, 2) == 0) {
if (_dns_left_len(&rr_nested->context) < 1) {
return -1;
}
Expand Down Expand Up @@ -731,7 +731,7 @@ void *dns_get_rr_nested_next(struct dns_rrs *rrs, void *rr_nested, int rr_nested
void *end = rrs->data + rrs->len;
void *p = rr_nested + rr_nested_len;

if (is_aligment(p, 2) == 0) {
if (is_aligned(p, 2) == 0) {
p++;
}

Expand Down Expand Up @@ -1278,7 +1278,7 @@ int dns_add_HTTPS_start(struct dns_rr_nested *svcparam_buffer, struct dns_packet
safe_strncpy((char *)svcparam_buffer->context.ptr, target, target_len);
svcparam_buffer->context.ptr += target_len;

if (is_aligment(svcparam_buffer->context.ptr, 2) != 1) {
if (is_aligned(svcparam_buffer->context.ptr, 2) != 1) {
if (_dns_left_len(&svcparam_buffer->context) < 1) {
return -1;
}
Expand Down Expand Up @@ -1469,7 +1469,7 @@ int dns_get_HTTPS_svcparm_start(struct dns_rrs *rrs, struct dns_https_param **ht
}

/* PADDING */
if (is_aligment(data, 2) != 1) {
if (is_aligned(data, 2) != 1) {
data++;
rr_len--;
}
Expand Down
1 change: 0 additions & 1 deletion src/dns_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,6 @@ static struct dns_server_info *_dns_client_get_server(char *server_ip, int port,
continue;
}

pthread_mutex_unlock(&client.server_list_lock);
server_info_return = server_info;
break;
}
Expand Down
81 changes: 41 additions & 40 deletions src/fast_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ struct fast_ping_packet_msg {
struct timeval tv;
unsigned int sid;
unsigned int seq;
unsigned int cookie;
};

struct fast_ping_packet {
union {
struct icmphdr icmp;
struct icmp icmp;
struct icmp6_hdr icmp6;
};
unsigned int ttl;
Expand Down Expand Up @@ -121,16 +120,15 @@ struct ping_host_struct {
char host[PING_MAX_HOSTLEN];

int fd;
unsigned int seq;
unsigned short seq;
int ttl;
struct timeval last;
int interval;
int timeout;
int count;
int send;
int run;
unsigned int cookie;
unsigned int sid;
unsigned short sid;
unsigned short port;
unsigned short ss_family;
union {
Expand All @@ -140,6 +138,8 @@ struct ping_host_struct {
};
socklen_t addr_len;
struct fast_ping_packet packet;
/* for memory address alignment */
struct fast_ping_packet recv_packet_buffer;

struct fast_ping_fake_ip *fake;
int fake_time_fd;
Expand Down Expand Up @@ -726,7 +726,6 @@ static int _fast_ping_sendping_v6(struct ping_host_struct *ping_host)
gettimeofday(&packet->msg.tv, NULL);
gettimeofday(&ping_host->last, NULL);
packet->msg.sid = ping_host->sid;
packet->msg.cookie = ping_host->cookie;
packet->msg.seq = ping_host->seq;
icmp6->icmp6_cksum = _fast_ping_checksum((void *)packet, sizeof(struct fast_ping_packet));

Expand Down Expand Up @@ -797,23 +796,22 @@ static int _fast_ping_send_fake(struct ping_host_struct *ping_host, struct fast_
static int _fast_ping_sendping_v4(struct ping_host_struct *ping_host)
{
struct fast_ping_packet *packet = &ping_host->packet;
struct icmphdr *icmp = &packet->icmp;
struct icmp *icmp = &packet->icmp;
int len = 0;

ping_host->seq++;
memset(icmp, 0, sizeof(*icmp));
icmp->type = ICMP_ECHO;
icmp->code = 0;
icmp->checksum = 0;
icmp->un.echo.id = ping.ident;
icmp->un.echo.sequence = htons(ping_host->seq);
icmp->icmp_type = ICMP_ECHO;
icmp->icmp_code = 0;
icmp->icmp_cksum = 0;
icmp->icmp_id = ping.ident;
icmp->icmp_seq = htons(ping_host->seq);

gettimeofday(&packet->msg.tv, NULL);
gettimeofday(&ping_host->last, NULL);
packet->msg.sid = ping_host->sid;
packet->msg.seq = ping_host->seq;
packet->msg.cookie = ping_host->cookie;
icmp->checksum = _fast_ping_checksum((void *)packet, sizeof(struct fast_ping_packet));
icmp->icmp_cksum = _fast_ping_checksum((void *)packet, sizeof(struct fast_ping_packet));

len = sendto(ping.fd_icmp, packet, sizeof(struct fast_ping_packet), 0, &ping_host->addr, ping_host->addr_len);
if (len != sizeof(struct fast_ping_packet)) {
Expand Down Expand Up @@ -1409,7 +1407,6 @@ struct ping_host_struct *fast_ping_start(PING_TYPE type, const char *host, int c
char ip_str[PING_MAX_HOSTLEN];
int port = -1;
FAST_PING_TYPE ping_type = FAST_PING_END;
unsigned int seed = 0;
int ret = 0;
struct fast_ping_fake_ip *fake = NULL;
int fake_time_fd = -1;
Expand Down Expand Up @@ -1438,8 +1435,6 @@ struct ping_host_struct *fast_ping_start(PING_TYPE type, const char *host, int c
atomic_set(&ping_host->ref, 0);
atomic_set(&ping_host->notified, 0);
ping_host->sid = atomic_inc_return(&ping_sid);
seed = ping_host->sid;
ping_host->cookie = rand_r(&seed);
ping_host->run = 0;
if (ping_callback) {
ping_host->ping_callback = ping_callback;
Expand Down Expand Up @@ -1546,6 +1541,11 @@ static struct fast_ping_packet *_fast_ping_icmp6_packet(struct ping_host_struct
struct cmsghdr *c = NULL;
int hops = 0;

if (data_len < (int)sizeof(struct icmp6_hdr)) {
tlog(TLOG_DEBUG, "ping package length is invalid, %d, %d", data_len, (int)sizeof(struct fast_ping_packet));
return NULL;
}

for (c = CMSG_FIRSTHDR(msg); c; c = CMSG_NXTHDR(msg, c)) {
if (c->cmsg_level != IPPROTO_IPV6) {
continue;
Expand Down Expand Up @@ -1589,49 +1589,52 @@ static struct fast_ping_packet *_fast_ping_icmp_packet(struct ping_host_struct *
{
struct ip *ip = (struct ip *)packet_data;
struct fast_ping_packet *packet = NULL;
struct fast_ping_packet packet_data_buffer;
struct icmphdr *icmp = NULL;
struct icmp *icmp = NULL;
int hlen = 0;
int icmp_len = 0;

hlen = ip->ip_hl << 2;
if (data_len - hlen < (int)sizeof(struct fast_ping_packet)) {
tlog(TLOG_ERROR, "ping package length is invalid, %d", data_len - hlen);
if (ping.no_unprivileged_ping) {
hlen = ip->ip_hl << 2;
if (ip->ip_p != IPPROTO_ICMP) {
tlog(TLOG_DEBUG, "ip type failed, %d:%d", ip->ip_p, IPPROTO_ICMP);
return NULL;
}
}

if (data_len - hlen < (int)sizeof(struct icmp)) {
tlog(TLOG_DEBUG, "response ping package length is invalid, len: %d", data_len);
return NULL;
}

if ((uintptr_t)(packet_data + hlen) % __alignof__(void *) == 0 || ping.no_unprivileged_ping == 0) {
packet = (struct fast_ping_packet *)(packet_data + hlen);
} else {
memcpy(&packet_data_buffer, packet_data + hlen, sizeof(packet_data_buffer));
packet = &packet_data_buffer;
int copy_len = sizeof(ping_host->recv_packet_buffer);
if (copy_len > data_len - hlen) {
copy_len = data_len - hlen;
}
memcpy(&ping_host->recv_packet_buffer, packet_data + hlen, copy_len);
packet = &ping_host->recv_packet_buffer;
}

icmp = &packet->icmp;
icmp_len = data_len - hlen;
packet->ttl = ip->ip_ttl;

if (icmp_len < 16) {
tlog(TLOG_ERROR, "length is invalid, %d", icmp_len);
return NULL;
}

if (icmp->type != ICMP_ECHOREPLY) {
if (icmp->icmp_type != ICMP_ECHOREPLY) {
errno = ENETUNREACH;
return NULL;
}

if (ping.no_unprivileged_ping) {
if (ip->ip_p != IPPROTO_ICMP) {
tlog(TLOG_ERROR, "ip type failed, %d:%d", ip->ip_p, IPPROTO_ICMP);
return NULL;
}

if (icmp->un.echo.id != ping.ident) {
tlog(TLOG_ERROR, "ident failed, %d:%d", icmp->un.echo.id, ping.ident);
return NULL;
}
if (icmp->icmp_id != ping.ident && ping.no_unprivileged_ping) {
tlog(TLOG_WARN, "ident failed, %d:%d", icmp->icmp_id, ping.ident);
return NULL;
}

packet->ttl = ip->ip_ttl;
return packet;
}

Expand Down Expand Up @@ -1700,7 +1703,6 @@ static int _fast_ping_process_icmp(struct ping_host_struct *ping_host, struct ti
struct timeval *tvsend = NULL;
unsigned int sid = 0;
unsigned int seq = 0;
unsigned int cookie = 0;
struct msghdr msg;
struct iovec iov;
char ans_data[4096];
Expand Down Expand Up @@ -1737,14 +1739,13 @@ static int _fast_ping_process_icmp(struct ping_host_struct *ping_host, struct ti
tvsend = &packet->msg.tv;
sid = packet->msg.sid;
seq = packet->msg.seq;
cookie = packet->msg.cookie;
addrkey = _fast_ping_hash_key(sid, (struct sockaddr *)&from);
pthread_mutex_lock(&ping.map_lock);
hash_for_each_possible(ping.addrmap, recv_ping_host, addr_node, addrkey)
{
if (_fast_ping_sockaddr_ip_cmp(&recv_ping_host->addr, recv_ping_host->addr_len, (struct sockaddr *)&from,
from_len) == 0 &&
recv_ping_host->sid == sid && recv_ping_host->cookie == cookie) {
recv_ping_host->sid == sid) {
_fast_ping_host_get(recv_ping_host);
break;
}
Expand Down

0 comments on commit 69de1cc

Please sign in to comment.