perf: Reduce str_to_mac() and inet_addr() calls
This commit is contained in:
@@ -196,7 +196,7 @@ void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udp_hdr *udp_hd
|
||||
udp_hdr->uh_sum = CHECKSUM_CARRY(sum);
|
||||
}
|
||||
|
||||
void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, uint16_t ipid, const char *src_addr, const char *dst_addr, uint16_t payload_len)
|
||||
void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, uint16_t ipid, const in_addr_t src_ip, const in_addr_t dst_ip, uint16_t payload_len)
|
||||
{
|
||||
memset(ip_hdr, 0, sizeof(struct ip));
|
||||
|
||||
@@ -206,8 +206,8 @@ void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, uint16_t ipid, co
|
||||
ip_hdr->ip_id = htons(ipid); /* IP ID */
|
||||
ip_hdr->ip_ttl = 80; /* time to live */
|
||||
ip_hdr->ip_p = next_protocol; /* transport protocol */
|
||||
ip_hdr->ip_src.s_addr = inet_addr(src_addr);
|
||||
ip_hdr->ip_dst.s_addr = inet_addr(dst_addr);
|
||||
ip_hdr->ip_src.s_addr = src_ip;
|
||||
ip_hdr->ip_dst.s_addr = dst_ip;
|
||||
ip_hdr->ip_len = htons(sizeof(struct ip) + payload_len); /* total length */
|
||||
ip_hdr->ip_off = htons(0); /* fragmentation flags */
|
||||
ip_hdr->ip_sum = 0; /* do this later */
|
||||
@@ -217,12 +217,12 @@ void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, uint16_t ipid, co
|
||||
}
|
||||
|
||||
// l3_protocol: ETH_P_IPV6/ETH_P_IP
|
||||
void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const char *src_mac, const char *dst_mac)
|
||||
void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const u_char src_mac[], const u_char dst_mac[])
|
||||
{
|
||||
memset(eth_hdr, 0, sizeof(struct ethhdr));
|
||||
|
||||
str_to_mac(src_mac, (char *)eth_hdr->h_source);
|
||||
str_to_mac(dst_mac, (char *)eth_hdr->h_dest);
|
||||
memcpy(eth_hdr->h_source, src_mac, ETH_ALEN);
|
||||
memcpy(eth_hdr->h_dest, dst_mac, ETH_ALEN);
|
||||
eth_hdr->h_proto = htons(next_protocol);
|
||||
}
|
||||
|
||||
@@ -230,7 +230,7 @@ void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const ch
|
||||
* device
|
||||
******************************************************************************/
|
||||
|
||||
int get_ip_by_device_name(const char *dev_name, char *ip_buff)
|
||||
int get_ip_by_device_name(const char *dev_name, char *ip_str)
|
||||
{
|
||||
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (fd == -1)
|
||||
@@ -248,13 +248,13 @@ int get_ip_by_device_name(const char *dev_name, char *ip_buff)
|
||||
return -1;
|
||||
}
|
||||
|
||||
strcpy(ip_buff, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
|
||||
strcpy(ip_str, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_mac_by_device_name(const char *dev_name, char *mac_buff)
|
||||
int get_mac_by_device_name(const char *dev_name, char *mac_str)
|
||||
{
|
||||
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||
if (fd == -1)
|
||||
@@ -271,16 +271,16 @@ int get_mac_by_device_name(const char *dev_name, char *mac_buff)
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char *mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
|
||||
sprintf(mac_buff, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
u_char *mac = (u_char *)ifr.ifr_hwaddr.sa_data;
|
||||
sprintf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int str_to_mac(const char *str, char *mac_buff)
|
||||
int str_to_mac(const char *mac_str, u_char mac[])
|
||||
{
|
||||
if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &(mac_buff[0]), &(mac_buff[1]), &(mac_buff[2]), &(mac_buff[3]), &(mac_buff[4]), &(mac_buff[5])) == 6)
|
||||
if (sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &(mac[0]), &(mac[1]), &(mac[2]), &(mac[3]), &(mac[4]), &(mac[5])) == 6)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user