⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netinfo.c

📁 获取unix系统进程信息的功能
💻 C
字号:
#ifdef __cplusplus
extern "C" {
#endif

#include "netinfo.h"
#include "common.h"
#include "utils.h"

#ifdef WIN32
#include <WinSock2.h>
#else
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif

#include <stdio.h>

#if defined(WIN32) || defined(LINUX)
int GetSysIP(char *buf, unsigned long buflen, char *errbuf)
{
    char *pserverip = NULL;
    char hostname[MIN_BUF_LEN];
    struct hostent *hostinfo;
    struct sockaddr_in addr;
    int i;

    if (gethostname(hostname, sizeof(hostname)) == 0) 
    { 
        if ((hostinfo = gethostbyname(hostname)) != NULL) 
        {
            memset(buf, 0, buflen);
            for (i = 0; hostinfo->h_addr_list[i] != NULL; i++)
            {
                memset(&addr, 0, sizeof(struct sockaddr_in));
                memcpy(&addr.sin_addr.s_addr, hostinfo->h_addr_list[i], hostinfo->h_length);
                pserverip = inet_ntoa(addr.sin_addr);
                if (strlen(buf)+strlen(pserverip)+2 >= buflen)
                {
                    strcpy(errbuf, "2:ip list is too long");
                    return -1;
                }
                if (i == 0)
                {
                    strcpy(buf, pserverip);
                }
                else
                {
                    strcat(buf, "#");
                    strcat(buf, pserverip);
                }
            }
        }
    }

    return 0;
}
#elif defined(SOLARIS)
#define IFCONFIG_COMMAND "/sbin/ifconfig -a|grep -w \"inet\"|grep -v \"127.0.0.1\"|grep -v \"0.0.0.0\"|awk 'BEGIN{i=0}{if(i==0)printf \"%s\",$2;else printf \"#%s\",$2;i++}'"
int GetSysIP(char *buf, unsigned long buflen, char *errbuf)
{
    int ret;

    ret = my_system(IFCONFIG_COMMAND, DEFAULT_COMMAND_TIMEOUT, buf, buflen);
    if (ret != 0)
    {
        strcpy(errbuf, "2:my_system() error");
        return -1;
    }

    return 0;
}
#elif defined(AIX)
#define IFCONFIG_COMMAND "ifconfig -a|grep -w \"inet\"|grep -v \"127.0.0.1\"|grep -v \"0.0.0.0\"|awk 'BEGIN{i=0}{if(i==0)printf \"%s\",$2;else printf \"#%s\",$2;i++}'"
int GetSysIP(char *buf, unsigned long buflen, char *errbuf)
{
    int ret;

    ret = my_system(IFCONFIG_COMMAND, DEFAULT_COMMAND_TIMEOUT, buf, buflen);
    if (ret != 0)
    {
        strcpy(errbuf, "2:my_system() error");
        return -1;
    }

    return 0;
}
#endif

int GetHostMac(char *buf, unsigned long buflen, char *errbuf)
{
    int ret;
    char szCmd[MIN_BUF_LEN];
    char *p = 0;

#ifdef WIN32
    if (!GetModuleFileName(NULL, szCmd, MIN_BUF_LEN))
    {
        strcpy(errbuf, "2:GetModuleFileName() error");
        return -1;
    }
    p = strstr(szCmd, "\\service");
    if (!p)
    {
        strcpy(errbuf, "2:Can not find path");
        return -1;
    }
    *p = 0;
    strcat(szCmd, "\\plugins\\getmac");
#else
    strcpy(szCmd, "../plugins/getmac");
#endif

    ret = my_system(szCmd, DEFAULT_COMMAND_TIMEOUT, buf, buflen);
    if (ret != 0)
    {
        strcpy(errbuf, "2:my_system() error");
        return -1;
    }

    return 0;
}

#ifdef __cplusplus
}
#endif


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -