Răsfoiți Sursa

add ifconfig

ithewei 6 ani în urmă
părinte
comite
11a7033964
4 a modificat fișierele cu 208 adăugiri și 9 ștergeri
  1. 1 1
      Makefile
  2. 172 0
      hifconf.cpp
  3. 27 0
      hifconf.h
  4. 8 8
      main.cpp.tmpl

+ 1 - 1
Makefile

@@ -62,7 +62,7 @@ LDFLAGS += $(addprefix -L, $(LIBDIRS))
 ifeq ($(OS), Windows)
 	LDFLAGS += -static-libgcc -static-libstdc++
 	LDFLAGS += -Wl,-Bstatic -lstdc++ -lpthread -lm
-	LDFLAGS += -Wl,-Bdynamic -lwinmm
+	LDFLAGS += -Wl,-Bdynamic -lwinmm -liphlpapi -lws2_32
 else
 	LDFLAGS += -Wl,-Bdynamic -lstdc++ -lpthread -lm
 endif

+ 172 - 0
hifconf.cpp

@@ -0,0 +1,172 @@
+#include "hifconf.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef __unix__
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <netinet/in.h>
+#include <linux/if.h>
+#include <arpa/inet.h>
+
+int ifconfig(std::vector<ifconfig_t>& ifcs) {
+    int sock = socket(AF_INET, SOCK_DGRAM, 0);
+    if (sock < 0) {
+        return -10;
+    }
+
+    struct ifconf ifc;
+    char buf[1024];
+    ifc.ifc_len = sizeof(buf);
+    ifc.ifc_buf = buf;
+
+    int iRet = ioctl(sock, SIOCGIFCONF, &ifc);
+    if (iRet != 0) {
+        close(sock);
+        return iRet;
+    }
+
+    int cnt = ifc.ifc_len / sizeof(struct ifreq);
+    //printf("ifc.size=%d\n", cnt);
+    if (cnt == 0) {
+        close(sock);
+        return -20;
+    }
+
+    struct ifreq ifr;
+    ifcs.clear();
+    ifconfig_t tmp;
+    char macaddr[32] = {'\0'};
+    for (int i = 0; i < cnt; ++i) {
+        // name
+        strcpy(ifr.ifr_name, ifc.ifc_req[i].ifr_name);
+        //printf("name: %s\n", ifr.ifr_name);
+        strncpy(tmp.name, ifr.ifr_name, sizeof(tmp.name));
+        // flags
+        //iRet = ioctl(sock, SIOCGIFFLAGS, &ifr);
+        //short flags = ifr.ifr_flags;
+        // addr
+        iRet = ioctl(sock, SIOCGIFADDR, &ifr);
+        struct sockaddr_in* addr = (struct sockaddr_in*)&ifr.ifr_addr;
+        char* ip = inet_ntoa(addr->sin_addr);
+        //printf("ip: %s\n", ip);
+        strncpy(tmp.ip, ip, sizeof(tmp.ip));
+        // netmask
+        iRet = ioctl(sock, SIOCGIFNETMASK, &ifr);
+        addr = (struct sockaddr_in*)&ifr.ifr_netmask;
+        char* netmask = inet_ntoa(addr->sin_addr);
+        //printf("netmask: %s\n", netmask);
+        strncpy(tmp.mask, netmask, sizeof(tmp.mask));
+        // broadaddr
+        //iRet = ioctl(sock, SIOCGIFBRDADDR, &ifr);
+        //addr = (struct sockaddr_in*)&ifr.ifr_broadaddr;
+        //char* broadaddr = inet_ntoa(addr->sin_addr);
+        //printf("broadaddr: %s\n", broadaddr);
+        // hwaddr
+        iRet = ioctl(sock, SIOCGIFHWADDR, &ifr);
+        snprintf(macaddr, sizeof(macaddr), "%02x:%02x:%02x:%02x:%02x:%02x",
+            (unsigned char)ifr.ifr_hwaddr.sa_data[0],
+            (unsigned char)ifr.ifr_hwaddr.sa_data[1],
+            (unsigned char)ifr.ifr_hwaddr.sa_data[2],
+            (unsigned char)ifr.ifr_hwaddr.sa_data[3],
+            (unsigned char)ifr.ifr_hwaddr.sa_data[4],
+            (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
+        //printf("mac: %s\n", macaddr);
+        strncpy(tmp.mac, macaddr, sizeof(tmp.mac));
+        //printf("\n");
+
+        if (strcmp(tmp.ip, "0.0.0.0") == 0 ||
+            strcmp(tmp.ip, "127.0.0.1") == 0 ||
+            strcmp(tmp.mac, "00:00:00:00:00:00") == 0) {
+            continue;
+        }
+
+        ifcs.push_back(tmp);
+    }
+
+    close(sock);
+    return 0;
+}
+#elif defined(_WIN32)
+#include <winsock2.h>
+#include <windows.h>
+#include <IphlpApi.h>
+#ifdef _MSC_VER
+#pragma comment(lib, "Iphlpapi.lib")
+#pragma comment(lib, "ws2_32.lib")
+#endif
+int ifconfig(std::vector<ifconfig_t>& ifcs) {
+    PIP_ADAPTER_ADDRESSES pAddrs = NULL;
+    ULONG buflen = 0;
+    GetAdaptersAddresses(AF_INET, 0, NULL, pAddrs, &buflen);
+    pAddrs = (PIP_ADAPTER_ADDRESSES)malloc(buflen);
+    GetAdaptersAddresses(AF_INET, 0, NULL, pAddrs, &buflen);
+
+    PIP_ADAPTER_INFO pInfos = NULL;
+    buflen = 0;
+    GetAdaptersInfo(pInfos, &buflen);
+    pInfos = (PIP_ADAPTER_INFO)malloc(buflen);
+    GetAdaptersInfo(pInfos, &buflen);
+
+    ifconfig_t ifc;
+    std::vector<ifconfig_t> tmp_ifcs;
+    PIP_ADAPTER_ADDRESSES pAddr = pAddrs;
+    char mac[32] = {'\0'};
+    while (pAddr) {
+        snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
+            pAddr->PhysicalAddress[0],
+            pAddr->PhysicalAddress[1],
+            pAddr->PhysicalAddress[2],
+            pAddr->PhysicalAddress[3],
+            pAddr->PhysicalAddress[4],
+            pAddr->PhysicalAddress[5]);
+
+        memset(&ifc, 0, sizeof(ifc));
+        strncpy(ifc.name, pAddr->AdapterName, sizeof(ifc.name));
+        strncpy(ifc.ip, "0.0.0.0", sizeof(ifc.ip));
+        strncpy(ifc.mask, "0.0.0.0", sizeof(ifc.ip));
+        strncpy(ifc.mac, mac, sizeof(ifc.mac));
+        tmp_ifcs.push_back(ifc);
+
+        pAddr = pAddr->Next;
+    }
+
+    PIP_ADAPTER_INFO pInfo = pInfos;
+    while (pInfo) {
+        for (auto& item : tmp_ifcs) {
+            if (strcmp(item.name, pInfo->AdapterName) == 0) {
+                // description more friendly
+                strncpy(item.name, pInfo->Description, sizeof(item.name));
+                strncpy(item.ip, pInfo->IpAddressList.IpAddress.String, sizeof(item.ip));
+                strncpy(item.mask, pInfo->IpAddressList.IpMask.String, sizeof(item.mask));
+            }
+        }
+
+        pInfo = pInfo->Next;
+    }
+
+    free(pAddrs);
+    free(pInfos);
+
+    // filter
+    ifcs.clear();
+    for (auto& item : tmp_ifcs) {
+        if (strcmp(item.ip, "0.0.0.0") == 0 ||
+            strcmp(item.ip, "127.0.0.1") == 0 ||
+            strcmp(item.mac, "00:00:00:00:00:00") == 0) {
+            continue;
+        }
+        ifcs.push_back(item);
+    }
+
+    return 0;
+}
+#else
+int ifconfig(std::vector<ifconfig_t>& ifcs) {
+    return -10; // unimplemented
+}
+#endif

+ 27 - 0
hifconf.h

@@ -0,0 +1,27 @@
+#ifndef H_IFCONFIG_H_
+#define H_IFCONFIG_H_
+
+#include <vector>
+
+typedef struct ifconfig_s {
+    char name[128];
+    char ip[32];
+    char mask[32];
+    char mac[32];
+} ifconfig_t;
+
+/*
+ *  @test
+    std::vector<ifconfig_t> ifcs;
+    ifconfig(ifcs);
+    for (auto& item : ifcs) {
+        printf("%s\nip: %s\nmask: %s\nmac: %s\n\n",
+                item.name,
+                item.ip,
+                item.mask,
+                item.mac);
+    }
+ */
+int ifconfig(std::vector<ifconfig_t>& ifcs);
+
+#endif // H_IFCONFIG_H_

+ 8 - 8
main.cpp.tmpl

@@ -288,14 +288,6 @@ void handle_signal() {
     }
 }
 
-void master_proc(void* userdata) {
-    while(1) msleep(1000);
-}
-
-void worker_proc(void* userdata) {
-    while(1) msleep(1000);
-}
-
 int main(int argc, char** argv) {
     // g_main_ctx
     main_ctx_init(argc, argv);
@@ -372,3 +364,11 @@ int main(int argc, char** argv) {
     return 0;
 }
 
+void master_proc(void* userdata) {
+    while(1) msleep(1000);
+}
+
+void worker_proc(void* userdata) {
+    while(1) msleep(1000);
+}
+