This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
tango-tsg-service-chaining-…/common/src/utils.cpp

164 lines
3.4 KiB
C++

#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <net/if.h>
#include <netinet/ip.h>
#include <netinet/ether.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include "utils.h"
#include "log.h"
/******************************************************************************
* mutable_array
******************************************************************************/
void mutable_array_init(struct mutable_array *array)
{
memset(array, 0, sizeof(mutable_array));
array->num = 0;
array->size = sizeof(array->elems) / sizeof(array->elems[0]);
}
void mutable_array_add_elem(struct mutable_array *array, uint64_t elem)
{
if (array->num < array->size)
{
array->elems[array->num] = elem;
array->num++;
}
else
{
LOG_ERROR("%s: fixed num array add elem too much !!!", LOG_TAG_UTILS);
}
}
void mutable_array_del_elem(struct mutable_array *array, uint64_t elem)
{
for (int i = 0; i < array->num; i++)
{
if (array->elems[i] == elem)
{
if (i + 1 != array->size)
{
memmove(&(array->elems[i]), &(array->elems[i + 1]), sizeof(array->elems[0]) * (array->num - i - 1));
}
i--;
array->num--;
}
}
}
int mutable_array_is_full(struct mutable_array *array)
{
if (array->num == array->size)
{
return 1;
}
else
{
return 0;
}
}
int mutable_array_count_elem(struct mutable_array *array)
{
if (array)
{
return array->num;
}
else
{
return 0;
}
}
int mutable_array_exist_elem(struct mutable_array *array, uint64_t elem)
{
for (int i = 0; i < array->num; i++)
{
if (array->elems[i] == elem)
{
return 1;
}
}
return 0;
}
int mutable_array_index_elem(struct mutable_array *array, int index)
{
if (index >= array->num)
{
assert(0);
}
return array->elems[index];
}
/******************************************************************************
* device
******************************************************************************/
int get_ip_by_device_name(const char *dev_name, char *ip_str)
{
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1)
{
return -1;
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(struct ifreq));
ifr.ifr_addr.sa_family = AF_INET;
strcpy(ifr.ifr_name, dev_name);
if (ioctl(fd, SIOCGIFADDR, &ifr) != 0)
{
close(fd);
return -1;
}
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_str)
{
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd == -1)
{
return -1;
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(struct ifreq));
strcpy(ifr.ifr_name, dev_name);
if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0)
{
close(fd);
return -1;
}
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 *mac_str, u_char mac[])
{
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;
}
else
{
return -1;
}
}