1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include <gtest/gtest.h>
- #include "toolkit.h"
- #ifdef __linux__
- #include <net/if.h>
- #include <sys/ioctl.h>
- TEST(TestNet, TestNetMac)
- {
- int fd, interface;
- struct ifreq buf[16];
- struct ifconf ifc;
- char mac[32] = { 0 };
- if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
- int i = 0;
- ifc.ifc_len = sizeof(buf);
- ifc.ifc_buf = (caddr_t)buf;
- if (!ioctl(fd, SIOCGIFCONF, (char*)&ifc)) {
- interface = ifc.ifc_len / sizeof(struct ifreq);
- printf("interface num is %d\n", interface);
- while (i < interface) {
- printf("net device %s\n", buf[i].ifr_name);
- if (!(ioctl(fd, SIOCGIFHWADDR, (char*)&buf[i]))) {
- sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X",
- (unsigned char)buf[i].ifr_hwaddr.sa_data[0],
- (unsigned char)buf[i].ifr_hwaddr.sa_data[1],
- (unsigned char)buf[i].ifr_hwaddr.sa_data[2],
- (unsigned char)buf[i].ifr_hwaddr.sa_data[3],
- (unsigned char)buf[i].ifr_hwaddr.sa_data[4],
- (unsigned char)buf[i].ifr_hwaddr.sa_data[5]);
- printf("HWaddr %s\n", mac);
- }
- printf("\n");
- i++;
- }
- }
- close(fd);
- }
- }
- #endif
- TEST(TestNet, TestNetInterfac)
- {
- char buf[512];
- toolkit_interface_address_t* info;
- int count, i;
- toolkit_interface_addresses(&info, &count);
- i = count;
- printf("Number of interfaces: %d\n", count);
- while (i--) {
- toolkit_interface_address_t interface = info[i];
- printf("Name: %s\n", interface.name);
- printf("Internal? %s\n", interface.is_internal ? "Yes" : "No");
- if (interface.address.address4.sin_family == AF_INET) {
- toolkit_ip4_name(&interface.address.address4, buf, sizeof(buf));
- printf("IPv4 address: %s\n", buf);
- } else if (interface.address.address4.sin_family == AF_INET6) {
- toolkit_ip6_name(&interface.address.address6, buf, sizeof(buf));
- printf("IPv6 address: %s\n", buf);
- }
- printf("\n");
- }
- toolkit_free_interface_addresses(info, count);
- }
|