code: cleanup lint and extra compile warnings
This commit is contained in:
116
src/fast_ping.c
116
src/fast_ping.c
@@ -124,7 +124,7 @@ struct ping_host_struct {
|
||||
};
|
||||
|
||||
struct fast_ping_struct {
|
||||
int run;
|
||||
atomic_t run;
|
||||
pthread_t tid;
|
||||
pthread_mutex_t lock;
|
||||
unsigned short ident;
|
||||
@@ -151,7 +151,7 @@ static int bool_print_log = 1;
|
||||
static uint16_t _fast_ping_checksum(uint16_t *header, size_t len)
|
||||
{
|
||||
uint32_t sum = 0;
|
||||
int i;
|
||||
unsigned int i = 0;
|
||||
|
||||
for (i = 0; i < len / sizeof(uint16_t); i++) {
|
||||
sum += ntohs(header[i]);
|
||||
@@ -262,13 +262,13 @@ static uint32_t _fast_ping_hash_key(unsigned int sid, struct sockaddr *addr)
|
||||
|
||||
switch (addr->sa_family) {
|
||||
case AF_INET: {
|
||||
struct sockaddr_in *addr_in;
|
||||
struct sockaddr_in *addr_in = NULL;
|
||||
addr_in = (struct sockaddr_in *)addr;
|
||||
sin_addr = &addr_in->sin_addr.s_addr;
|
||||
sin_addr_len = IPV4_ADDR_LEN;
|
||||
} break;
|
||||
case AF_INET6: {
|
||||
struct sockaddr_in6 *addr_in6;
|
||||
struct sockaddr_in6 *addr_in6 = NULL;
|
||||
addr_in6 = (struct sockaddr_in6 *)addr;
|
||||
if (IN6_IS_ADDR_V4MAPPED(&addr_in6->sin6_addr)) {
|
||||
sin_addr = addr_in6->sin6_addr.s6_addr + 12;
|
||||
@@ -358,7 +358,7 @@ static void _fast_ping_close_host_sock(struct ping_host_struct *ping_host)
|
||||
if (ping_host->fd < 0) {
|
||||
return;
|
||||
}
|
||||
struct epoll_event *event;
|
||||
struct epoll_event *event = NULL;
|
||||
event = (struct epoll_event *)1;
|
||||
epoll_ctl(ping.epoll_fd, EPOLL_CTL_DEL, ping_host->fd, event);
|
||||
close(ping_host->fd);
|
||||
@@ -442,8 +442,8 @@ static int _fast_ping_sendping_v6(struct ping_host_struct *ping_host)
|
||||
packet->msg.seq = ping_host->seq;
|
||||
icmp6->icmp6_cksum = _fast_ping_checksum((void *)packet, sizeof(struct fast_ping_packet));
|
||||
|
||||
len = sendto(ping.fd_icmp6, &ping_host->packet, sizeof(struct fast_ping_packet), 0,
|
||||
(struct sockaddr *)&ping_host->addr, ping_host->addr_len);
|
||||
len = sendto(ping.fd_icmp6, &ping_host->packet, sizeof(struct fast_ping_packet), 0, &ping_host->addr,
|
||||
ping_host->addr_len);
|
||||
if (len < 0 || len != sizeof(struct fast_ping_packet)) {
|
||||
int err = errno;
|
||||
if (errno == ENETUNREACH || errno == EINVAL || errno == EADDRNOTAVAIL) {
|
||||
@@ -474,7 +474,7 @@ static int _fast_ping_sendping_v4(struct ping_host_struct *ping_host)
|
||||
{
|
||||
struct fast_ping_packet *packet = &ping_host->packet;
|
||||
struct icmp *icmp = &packet->icmp;
|
||||
int len;
|
||||
int len = 0;
|
||||
|
||||
ping_host->seq++;
|
||||
memset(icmp, 0, sizeof(*icmp));
|
||||
@@ -491,8 +491,7 @@ static int _fast_ping_sendping_v4(struct ping_host_struct *ping_host)
|
||||
packet->msg.cookie = ping_host->cookie;
|
||||
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, (struct sockaddr *)&ping_host->addr,
|
||||
ping_host->addr_len);
|
||||
len = sendto(ping.fd_icmp, packet, sizeof(struct fast_ping_packet), 0, &ping_host->addr, ping_host->addr_len);
|
||||
if (len < 0 || len != sizeof(struct fast_ping_packet)) {
|
||||
int err = errno;
|
||||
if (errno == ENETUNREACH || errno == EINVAL || errno == EADDRNOTAVAIL) {
|
||||
@@ -514,7 +513,7 @@ errout:
|
||||
static int _fast_ping_sendping_udp(struct ping_host_struct *ping_host)
|
||||
{
|
||||
struct ping_dns_head dns_head;
|
||||
int len;
|
||||
int len = 0;
|
||||
int flag = 0;
|
||||
int fd = 0;
|
||||
|
||||
@@ -539,7 +538,7 @@ static int _fast_ping_sendping_udp(struct ping_host_struct *ping_host)
|
||||
dns_head.id = htons(ping_host->sid);
|
||||
dns_head.flag = flag;
|
||||
gettimeofday(&ping_host->last, NULL);
|
||||
len = sendto(fd, &dns_head, sizeof(dns_head), 0, (struct sockaddr *)&ping_host->addr, ping_host->addr_len);
|
||||
len = sendto(fd, &dns_head, sizeof(dns_head), 0, &ping_host->addr, ping_host->addr_len);
|
||||
if (len < 0 || len != sizeof(dns_head)) {
|
||||
int err = errno;
|
||||
if (errno == ENETUNREACH || errno == EINVAL || errno == EADDRNOTAVAIL) {
|
||||
@@ -561,7 +560,7 @@ errout:
|
||||
static int _fast_ping_sendping_tcp(struct ping_host_struct *ping_host)
|
||||
{
|
||||
struct epoll_event event;
|
||||
int flags;
|
||||
int flags = 0;
|
||||
int fd = -1;
|
||||
int yes = 1;
|
||||
const int priority = SOCKET_PRIORITY;
|
||||
@@ -586,7 +585,7 @@ static int _fast_ping_sendping_tcp(struct ping_host_struct *ping_host)
|
||||
set_sock_lingertime(fd, 0);
|
||||
|
||||
ping_host->seq++;
|
||||
if (connect(fd, (struct sockaddr *)&ping_host->addr, ping_host->addr_len) != 0) {
|
||||
if (connect(fd, &ping_host->addr, ping_host->addr_len) != 0) {
|
||||
if (errno != EINPROGRESS) {
|
||||
char ping_host_name[PING_MAX_HOSTLEN];
|
||||
if (errno == ENETUNREACH || errno == EINVAL || errno == EADDRNOTAVAIL) {
|
||||
@@ -711,6 +710,9 @@ static int _fast_ping_create_icmp_sock(FAST_PING_TYPE type)
|
||||
setsockopt(fd, SOL_IP, IP_TTL, &val, sizeof(val));
|
||||
setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos));
|
||||
|
||||
icmp_host->fd = fd;
|
||||
icmp_host->type = type;
|
||||
|
||||
memset(&event, 0, sizeof(event));
|
||||
event.events = EPOLLIN;
|
||||
event.data.ptr = icmp_host;
|
||||
@@ -718,12 +720,14 @@ static int _fast_ping_create_icmp_sock(FAST_PING_TYPE type)
|
||||
goto errout;
|
||||
}
|
||||
|
||||
icmp_host->fd = fd;
|
||||
icmp_host->type = type;
|
||||
return fd;
|
||||
|
||||
errout:
|
||||
close(fd);
|
||||
if (icmp_host) {
|
||||
icmp_host->fd = -1;
|
||||
icmp_host->type = 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -883,7 +887,7 @@ static int _fast_ping_get_addr_by_icmp(const char *ip_str, int port, struct addr
|
||||
struct addrinfo *gai = NULL;
|
||||
int socktype = 0;
|
||||
int domain = -1;
|
||||
FAST_PING_TYPE ping_type;
|
||||
FAST_PING_TYPE ping_type = 0;
|
||||
int sockproto = 0;
|
||||
char *service = NULL;
|
||||
|
||||
@@ -932,7 +936,7 @@ static int _fast_ping_get_addr_by_tcp(const char *ip_str, int port, struct addri
|
||||
{
|
||||
struct addrinfo *gai = NULL;
|
||||
int socktype = 0;
|
||||
FAST_PING_TYPE ping_type;
|
||||
FAST_PING_TYPE ping_type = 0;
|
||||
int sockproto = 0;
|
||||
char *service = NULL;
|
||||
char port_str[MAX_IP_LEN];
|
||||
@@ -968,7 +972,7 @@ static int _fast_ping_get_addr_by_dns(const char *ip_str, int port, struct addri
|
||||
{
|
||||
struct addrinfo *gai = NULL;
|
||||
int socktype = 0;
|
||||
FAST_PING_TYPE ping_type;
|
||||
FAST_PING_TYPE ping_type = 0;
|
||||
int sockproto = 0;
|
||||
char port_str[MAX_IP_LEN];
|
||||
int domain = -1;
|
||||
@@ -1045,11 +1049,11 @@ struct ping_host_struct *fast_ping_start(PING_TYPE type, const char *host, int c
|
||||
{
|
||||
struct ping_host_struct *ping_host = NULL;
|
||||
struct addrinfo *gai = NULL;
|
||||
uint32_t addrkey;
|
||||
uint32_t addrkey = 0;
|
||||
char ip_str[PING_MAX_HOSTLEN];
|
||||
int port = -1;
|
||||
FAST_PING_TYPE ping_type = FAST_PING_END;
|
||||
unsigned int seed;
|
||||
unsigned int seed = 0;
|
||||
int ret = 0;
|
||||
|
||||
if (parse_ip(host, ip_str, &port) != 0) {
|
||||
@@ -1114,7 +1118,7 @@ struct ping_host_struct *fast_ping_start(PING_TYPE type, const char *host, int c
|
||||
return ping_host;
|
||||
errout_remove:
|
||||
ping_host->ping_callback(ping_host, ping_host->host, PING_RESULT_ERROR, &ping_host->addr, ping_host->addr_len,
|
||||
ping_host->seq, ping_host->ttl, 0, ping_host->error, ping_host->userptr);
|
||||
ping_host->seq, ping_host->ttl, NULL, ping_host->error, ping_host->userptr);
|
||||
fast_ping_stop(ping_host);
|
||||
_fast_ping_host_put(ping_host);
|
||||
ping_host = NULL;
|
||||
@@ -1154,22 +1158,24 @@ static void tv_sub(struct timeval *out, struct timeval *in)
|
||||
static struct fast_ping_packet *_fast_ping_icmp6_packet(struct ping_host_struct *ping_host, struct msghdr *msg,
|
||||
u_char *packet_data, int data_len)
|
||||
{
|
||||
int icmp_len;
|
||||
int icmp_len = 0;
|
||||
struct fast_ping_packet *packet = (struct fast_ping_packet *)packet_data;
|
||||
struct icmp6_hdr *icmp6 = &packet->icmp6;
|
||||
struct cmsghdr *c;
|
||||
struct cmsghdr *c = NULL;
|
||||
int hops = 0;
|
||||
|
||||
for (c = CMSG_FIRSTHDR(msg); c; c = CMSG_NXTHDR(msg, c)) {
|
||||
if (c->cmsg_level != IPPROTO_IPV6)
|
||||
if (c->cmsg_level != IPPROTO_IPV6) {
|
||||
continue;
|
||||
}
|
||||
switch (c->cmsg_type) {
|
||||
case IPV6_HOPLIMIT:
|
||||
#ifdef IPV6_2292HOPLIMIT
|
||||
case IPV6_2292HOPLIMIT:
|
||||
#endif
|
||||
if (c->cmsg_len < CMSG_LEN(sizeof(int)))
|
||||
if (c->cmsg_len < CMSG_LEN(sizeof(int))) {
|
||||
continue;
|
||||
}
|
||||
memcpy(&hops, CMSG_DATA(c), sizeof(hops));
|
||||
}
|
||||
}
|
||||
@@ -1200,10 +1206,10 @@ static struct fast_ping_packet *_fast_ping_icmp_packet(struct ping_host_struct *
|
||||
u_char *packet_data, int data_len)
|
||||
{
|
||||
struct ip *ip = (struct ip *)packet_data;
|
||||
struct fast_ping_packet *packet;
|
||||
struct icmp *icmp;
|
||||
int hlen;
|
||||
int icmp_len;
|
||||
struct fast_ping_packet *packet = NULL;
|
||||
struct icmp *icmp = NULL;
|
||||
int hlen = 0;
|
||||
int icmp_len = 0;
|
||||
|
||||
hlen = ip->ip_hl << 2;
|
||||
packet = (struct fast_ping_packet *)(packet_data + hlen);
|
||||
@@ -1263,18 +1269,18 @@ errout:
|
||||
|
||||
static int _fast_ping_process_icmp(struct ping_host_struct *ping_host, struct timeval *now)
|
||||
{
|
||||
int len;
|
||||
int len = 0;
|
||||
u_char inpacket[ICMP_INPACKET_SIZE];
|
||||
struct sockaddr_storage from;
|
||||
struct ping_host_struct *recv_ping_host;
|
||||
struct ping_host_struct *recv_ping_host = NULL;
|
||||
struct fast_ping_packet *packet = NULL;
|
||||
socklen_t from_len = sizeof(from);
|
||||
uint32_t addrkey;
|
||||
uint32_t addrkey = 0;
|
||||
struct timeval tvresult = *now;
|
||||
struct timeval *tvsend = NULL;
|
||||
unsigned int sid;
|
||||
unsigned int seq;
|
||||
unsigned int cookie;
|
||||
unsigned int sid = 0;
|
||||
unsigned int seq = 0;
|
||||
unsigned int cookie = 0;
|
||||
struct msghdr msg;
|
||||
struct iovec iov;
|
||||
char ans_data[4096];
|
||||
@@ -1391,20 +1397,20 @@ errout:
|
||||
|
||||
static int _fast_ping_process_udp(struct ping_host_struct *ping_host, struct timeval *now)
|
||||
{
|
||||
int len;
|
||||
ssize_t len = 0;
|
||||
u_char inpacket[ICMP_INPACKET_SIZE];
|
||||
struct sockaddr_storage from;
|
||||
struct ping_host_struct *recv_ping_host;
|
||||
struct ping_host_struct *recv_ping_host = NULL;
|
||||
struct ping_dns_head *dns_head = NULL;
|
||||
socklen_t from_len = sizeof(from);
|
||||
uint32_t addrkey;
|
||||
uint32_t addrkey = 0;
|
||||
struct timeval tvresult = *now;
|
||||
struct timeval *tvsend = NULL;
|
||||
unsigned int sid;
|
||||
unsigned int sid = 0;
|
||||
struct msghdr msg;
|
||||
struct iovec iov;
|
||||
char ans_data[4096];
|
||||
struct cmsghdr *cmsg;
|
||||
struct cmsghdr *cmsg = NULL;
|
||||
int ttl = 0;
|
||||
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
@@ -1439,7 +1445,7 @@ static int _fast_ping_process_udp(struct ping_host_struct *ping_host, struct tim
|
||||
|
||||
from_len = msg.msg_namelen;
|
||||
dns_head = (struct ping_dns_head *)inpacket;
|
||||
if (len < sizeof(*dns_head)) {
|
||||
if (len < (ssize_t)sizeof(*dns_head)) {
|
||||
goto errout;
|
||||
}
|
||||
|
||||
@@ -1514,7 +1520,7 @@ static void _fast_ping_remove_all(void)
|
||||
struct ping_host_struct *ping_host = NULL;
|
||||
struct ping_host_struct *ping_host_tmp = NULL;
|
||||
struct hlist_node *tmp = NULL;
|
||||
int i;
|
||||
unsigned long i = 0;
|
||||
|
||||
LIST_HEAD(remove_list);
|
||||
|
||||
@@ -1536,11 +1542,11 @@ static void _fast_ping_period_run(void)
|
||||
struct ping_host_struct *ping_host = NULL;
|
||||
struct ping_host_struct *ping_host_tmp = NULL;
|
||||
struct hlist_node *tmp = NULL;
|
||||
int i = 0;
|
||||
unsigned long i = 0;
|
||||
struct timeval now;
|
||||
struct timezone tz;
|
||||
struct timeval interval;
|
||||
int64_t millisecond;
|
||||
int64_t millisecond = 0;
|
||||
gettimeofday(&now, &tz);
|
||||
LIST_HEAD(action);
|
||||
|
||||
@@ -1606,8 +1612,8 @@ static void _fast_ping_period_run(void)
|
||||
static void *_fast_ping_work(void *arg)
|
||||
{
|
||||
struct epoll_event events[PING_MAX_EVENTS + 1];
|
||||
int num;
|
||||
int i;
|
||||
int num = 0;
|
||||
int i = 0;
|
||||
unsigned long now = {0};
|
||||
struct timeval tvnow = {0};
|
||||
int sleep = 100;
|
||||
@@ -1617,7 +1623,7 @@ static void *_fast_ping_work(void *arg)
|
||||
sleep_time = sleep;
|
||||
now = get_tick_count() - sleep;
|
||||
expect_time = now + sleep;
|
||||
while (ping.run) {
|
||||
while (atomic_read(&ping.run)) {
|
||||
now = get_tick_count();
|
||||
if (now >= expect_time) {
|
||||
_fast_ping_period_run();
|
||||
@@ -1657,7 +1663,7 @@ int fast_ping_init(void)
|
||||
{
|
||||
pthread_attr_t attr;
|
||||
int epollfd = -1;
|
||||
int ret;
|
||||
int ret = 0;
|
||||
bool_print_log = 1;
|
||||
|
||||
if (ping.epoll_fd > 0) {
|
||||
@@ -1679,7 +1685,7 @@ int fast_ping_init(void)
|
||||
ping.epoll_fd = epollfd;
|
||||
ping.no_unprivileged_ping = !has_unprivileged_ping();
|
||||
ping.ident = (getpid() & 0XFFFF);
|
||||
ping.run = 1;
|
||||
atomic_set(&ping.run, 1);
|
||||
ret = pthread_create(&ping.tid, &attr, _fast_ping_work, NULL);
|
||||
if (ret != 0) {
|
||||
tlog(TLOG_ERROR, "create ping work thread failed, %s\n", strerror(errno));
|
||||
@@ -1688,10 +1694,11 @@ int fast_ping_init(void)
|
||||
|
||||
return 0;
|
||||
errout:
|
||||
if (ping.tid > 0) {
|
||||
if (ping.tid) {
|
||||
void *retval = NULL;
|
||||
ping.run = 0;
|
||||
atomic_set(&ping.run, 0);
|
||||
pthread_join(ping.tid, &retval);
|
||||
ping.tid = 0;
|
||||
}
|
||||
|
||||
if (epollfd) {
|
||||
@@ -1729,10 +1736,11 @@ static void _fast_ping_close_fds(void)
|
||||
|
||||
void fast_ping_exit(void)
|
||||
{
|
||||
if (ping.tid > 0) {
|
||||
if (ping.tid) {
|
||||
void *ret = NULL;
|
||||
ping.run = 0;
|
||||
atomic_set(&ping.run, 0);
|
||||
pthread_join(ping.tid, &ret);
|
||||
ping.tid = 0;
|
||||
}
|
||||
|
||||
_fast_ping_close_fds();
|
||||
|
||||
Reference in New Issue
Block a user