Lazarus:如何列出系统上所有可用的网络连接? [英] Lazarus: How to list all the available network connection on a system?

查看:94
本文介绍了Lazarus:如何列出系统上所有可用的网络连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Lazarus IDE在Linux系统上编写程序.该程序应该连接到Internet或Intranet.因此,我想向用户显示所有可用的网络连接列表,他们可以使用这些连接来连接到wifi等Internet或Intranet,如果系统上有两个活动的网卡,则此程序应显示其可用的连接.

I am writing a program on a Linux system using Lazarus IDE. The program is supposed to connect to the Internet or Intranet. So, I want to display to the user list of all the available network connections that they can use to connect to the Internet or Intranet like wifi, if there are two active network cards on the system, then this program should display their available connections.

目前,我不知道从何处开始或要使用什么工具.

At the moment, I don't know where to start or what tool(s) to use.

任何提示,线索或建议将不胜感激.

Any hints, clues or advice will be greatly appreciated.

推荐答案

您可以使用ifconfig列出所有可用的网络接口及其状态.

You can use ifconfig to list all available network interfaces and their status.

要以编程方式执行此操作,必须将ioctl与SIOCGIFCONF一起使用.

For doing it programmatically you have to use function ioctl with SIOCGIFCONF.

#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>

int main()
{
    int     sockfd, len, lastlen;
    char    *ptr, *buf;
    struct ifconf ifc;
    struct ifreq *ifr;
    char ifname[IFNAMSIZ + 1];
    char str[INET6_ADDRSTRLEN];

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    lastlen = 0;
    len = 100 * sizeof(struct ifreq);     /* initial buffer size guess */
    for ( ; ; )
    {
        buf = malloc(len);
        ifc.ifc_len = len;
        ifc.ifc_buf = buf;
        if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
        {
            if (errno != EINVAL || lastlen != 0)
                exit(-1);
        }
        else
        {
            if (ifc.ifc_len == lastlen)
                break;          /* success, len has not changed */
            lastlen = ifc.ifc_len;
        }

        len += 10 * sizeof(struct ifreq);     /* increment */
        free(buf);
    }

    printf("LEN: %d\n", ifc.ifc_len);

    for (ptr = buf; ptr < buf + ifc.ifc_len; )
    {
        ifr = (struct ifreq *) ptr;

        ptr += sizeof(struct ifreq); /* for next one in buffer */

        memcpy(ifname, ifr->ifr_name, IFNAMSIZ);

        printf("Interface name: %s\n", ifname);

        const char *res;

        switch (ifr->ifr_addr.sa_family)
        {
            case AF_INET6:
                res = inet_ntop(ifr->ifr_addr.sa_family, &(((struct sockaddr_in6 *)&ifr->ifr_addr)->sin6_addr), str, INET6_ADDRSTRLEN);
                break;
            case AF_INET:
                res = inet_ntop(ifr->ifr_addr.sa_family, &(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr), str, INET_ADDRSTRLEN);
                break;
            default:
                printf("OTHER\n");
                str[0] = 0;
                res = 0;
        }

        if (res != 0)
        {
            printf("IP Address: %s\n", str);
        }
        else
        {
            printf("ERROR\n");
        }
    }

    return 0;
}

如果成功,则ioctl SIOCGIFCONF将返回一个结构ifconf,该结构具有指向结构ifreq数组的指针. 这些结构在net/if.h

ioctl SIOCGIFCONF will return, if success, a struct ifconf which has a pointer to an array of struct ifreq. These structs are defined in net/if.h

使用此代码,您可以从ifc.ifc_req获取所有接口,请查看struct ifreq的声明,以确定每个数组元素的长度和类型.我认为从这里您可以独自继续,如果不能,请告诉我.

Using this code, from ifc.ifc_req you can get all interfaces, please look at the declaration of struct ifreq in order to determine the length and type of each array element. I think from here you can continue alone, if not please let me know.

这篇关于Lazarus:如何列出系统上所有可用的网络连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆