Obtaining SubnetMask in C
Asked Answered
C

3

6

I wanted to get the IP address and the subnet mask. Now the IP part is done, however I couldn't find any socket function that would return a structure with the subnet mask in it. Does a socket function exist, that returns it in a structure?

Thanks!

Collywobbles answered 7/8, 2013 at 10:18 Comment(0)
D
3

In windows using IPHelper.

#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "IPHLPAPI.lib")

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

/* Note: could also use malloc() and free() */

int __cdecl main()
{

    PIP_ADAPTER_INFO pAdapterInfo;
    ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
    pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO));
    GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
    printf("\tIP Mask: \t%s\n", pAdapterInfo->IpAddressList.IpMask.String);
    }
    if (pAdapterInfo)
        FREE(pAdapterInfo);

    return 0;
}
Dissimilation answered 7/8, 2013 at 10:43 Comment(0)
H
11

In Unix using getifaddrs

struct ifaddrs haves a member named ifa_netmask (Netmask of interface)

#include <arpa/inet.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <stdio.h>

int main ()
{
    struct ifaddrs *ifap, *ifa;
    struct sockaddr_in *sa;
    char *addr;

    getifaddrs (&ifap);
    for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
        if (ifa->ifa_addr->sa_family==AF_INET) {
            sa = (struct sockaddr_in *) ifa->ifa_netmask;
            addr = inet_ntoa(sa->sin_addr);
            printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, addr);
        }
    }

    freeifaddrs(ifap);
    return 0;
}

Output

Interface: lo   Address: 255.0.0.0
Interface: eth0 Address: 255.255.255.0
Hello answered 7/8, 2013 at 10:35 Comment(0)
D
3

In windows using IPHelper.

#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "IPHLPAPI.lib")

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

/* Note: could also use malloc() and free() */

int __cdecl main()
{

    PIP_ADAPTER_INFO pAdapterInfo;
    ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
    pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO));
    GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
    printf("\tIP Mask: \t%s\n", pAdapterInfo->IpAddressList.IpMask.String);
    }
    if (pAdapterInfo)
        FREE(pAdapterInfo);

    return 0;
}
Dissimilation answered 7/8, 2013 at 10:43 Comment(0)
R
2

Borrowed code from Linux Man page and referred to the code from Keine Lust:

#define _GNU_SOURCE     /* To get defns of NI_MAXSERV and NI_MAXHOST */

#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/if_link.h>
#include <string.h>     /* strcasecmp() */

int get_addr_and_netmask_using_ifaddrs(const char* ifa_name, 
                                       char *addr, char *netmask)
{
    struct ifaddrs *ifap, *ifa;
    struct sockaddr_in *sa;
    char *s;
    int found = 0;

    if (getifaddrs(&ifap) == -1) {
        perror("getifaddrs");
        exit(EXIT_FAILURE);
    }

    for (ifa = ifap; ifa && !found; ifa = ifa->ifa_next) {
        if (ifa->ifa_addr == NULL)
            continue;

        if (strcasecmp(ifa_name, ifa->ifa_name))
            continue;

        /* IPv4 */
        if (ifa->ifa_addr->sa_family != AF_INET)
            continue;

        sa = (struct sockaddr_in *) ifa->ifa_addr;
        s = inet_ntoa(sa->sin_addr);
        strcpy(addr, s);

        sa = (struct sockaddr_in *) ifa->ifa_netmask;
        s = inet_ntoa(sa->sin_addr);
        strcpy(netmask, s);

        found = 1;
    }

    freeifaddrs(ifap);

    if (found)
        return EXIT_SUCCESS;
    return EXIT_FAILURE;
}

int main(void)
{
    char *addr = malloc(NI_MAXHOST);
    char *netmask = malloc(NI_MAXHOST);

    if (!get_addr_and_netmask_using_ifaddrs ("enp6s0", addr, netmask))
        printf("[%s]%s %s\n", __func__, addr, netmask);
    else
        printf("interface error.\n");

    free(addr);
    free(netmask);

    return 0;
}
Reichert answered 11/2, 2017 at 3:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.