Lazarus: How to list all the available network connection on a system?

前端 未结 2 1732
孤城傲影
孤城傲影 2021-01-15 00:43

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

2条回答
  •  一生所求
    2021-01-15 01:15

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

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

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    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 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

    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.

提交回复
热议问题