91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

獲取linux內核所有ip

發布時間:2020-05-27 21:34:08 來源:網絡 閱讀:915 作者:313119992 欄目:網絡安全

獲取linux內核所有ip(C語言)

經常遇到獲取接口ip。記錄一下,方便后續使用。

#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
typedef unsigned int    sf_uint32_t;
typedef unsigned int    in_addr_t;
static inline char* print_ip1(sf_uint32_t ip)
{
    unsigned char* ip_tmp = (unsigned char*) &ip;
    static char buff[20];
    memset(buff ,0 ,sizeof(buff));
    snprintf(buff, sizeof(buff), "%d.%d.%d.%d", ip_tmp[0], ip_tmp[1], ip_tmp[2],
             ip_tmp[3]);
    return buff;
}

in_addr_t
get_myaddr(void)
{
    int             sd, i, lastlen = 0;
    struct ifconf   ifc;
    struct ifreq   *ifrp = NULL;
    in_addr_t       addr;
    char           *buf = NULL;

    if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        return 0;
    }

    /*
     * Cope with lots of interfaces and brokenness of ioctl SIOCGIFCONF on
     * some platforms; see W. R. Stevens, ``Unix Network Programming Volume
     * I'', p.435.  
     */

    for (i = 8;; i += 8) {
        buf = (char *) calloc(i, sizeof(struct ifreq));
        if (buf == NULL) {
            close(sd);
            return 0;
        }
        ifc.ifc_len = i * sizeof(struct ifreq);
        ifc.ifc_buf = (caddr_t) buf;

        if (ioctl(sd, SIOCGIFCONF, (char *) &ifc) < 0) {
            if (errno != EINVAL || lastlen != 0) {
                /*
                 * Something has gone genuinely wrong.  
                 */
                free(buf);
                close(sd);
                return 0;
            }
            /*
             * Otherwise, it could just be that the buffer is too small.  
             */
        } else {
            if (ifc.ifc_len == lastlen) {
                /*
                 * The length is the same as the last time; we're done.  
                 */
                break;
            }
            lastlen = ifc.ifc_len;
        }
        free(buf);
    }

    for (ifrp = ifc.ifc_req;
        (char *)ifrp < (char *)ifc.ifc_req + ifc.ifc_len;
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
        ifrp = (struct ifreq *)(((char *) ifrp) +
                                sizeof(ifrp->ifr_name) +
                                ifrp->ifr_addr.sa_len)
#else
        ifrp++
#endif
        ) {
        if (ifrp->ifr_addr.sa_family != AF_INET) {
            continue;
        }
        addr = ((struct sockaddr_in *) &(ifrp->ifr_addr))->sin_addr.s_addr;

        if (ioctl(sd, SIOCGIFFLAGS, (char *) ifrp) < 0) {
            continue;
        }
        if ((ifrp->ifr_flags & IFF_UP)
#ifdef IFF_RUNNING
            && (ifrp->ifr_flags & IFF_RUNNING)
#endif                          /* IFF_RUNNING */
            /*&& !(ifrp->ifr_flags & IFF_LOOPBACK)
            && addr != LOOPBACK*/) {
            /*
             * I *really* don't understand why this is necessary.  Perhaps for
             * some broken platform?  Leave it for now.  JBPN  
             */
#ifdef SYS_IOCTL_H_HAS_SIOCGIFADDR
            if (ioctl(sd, SIOCGIFADDR, (char *) ifrp) < 0) {
                continue;
            }
            addr =
                ((struct sockaddr_in *) &(ifrp->ifr_addr))->sin_addr.
                s_addr;
#endif
            printf("current ip : %s\n",print_ip1(addr));
        }
    }
    free(buf);
    close(sd);
    return 0;
}

int main()
{
    get_myaddr();
}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

疏附县| 大荔县| 贵港市| 临汾市| 博兴县| 宁国市| 井冈山市| 弋阳县| 洞头县| 龙南县| 安吉县| 客服| 乐山市| 怀来县| 喀什市| 民权县| 桦南县| 阿克陶县| 墨竹工卡县| 六盘水市| 和龙市| 鄂托克前旗| 平利县| 锡林浩特市| 云南省| 玛曲县| 崇礼县| 伊春市| 张北县| 汤原县| 股票| 扎鲁特旗| 抚顺县| 怀来县| 金川县| 麻栗坡县| 广元市| 太康县| 湖州市| 丹凤县| 沁阳市|