Statistics
| Branch: | Revision:

streamers / net_helpers.c @ 568bd1bd

History | View | Annotate | Download (917 Bytes)

1 89e893e2 Luca
#include <sys/ioctl.h>
2
#include <sys/socket.h>
3
#include <netinet/in.h>
4
#include <arpa/inet.h>
5
#include <net/if.h>     /* For struct ifreq */
6
#include <unistd.h>
7
#include <stdlib.h>
8
#include <stdio.h>
9
#include <string.h>
10
11
#include "net_helpers.h"
12
13 568bd1bd Luca Abeni
char *iface_addr(const char *iface)
14 89e893e2 Luca
{
15
    int s, res;
16
    struct ifreq iface_request;
17
    struct sockaddr_in *sin;
18
    char buff[512];
19
20
    s = socket(AF_INET, SOCK_DGRAM, 0);
21
    if (s < 0) {
22
        return NULL;
23
    }
24
25
    memset(&iface_request, 0, sizeof(struct ifreq));
26
    sin = (struct sockaddr_in *)&iface_request.ifr_addr;
27
    strcpy(iface_request.ifr_name, iface);
28
    /* sin->sin_family = AF_INET); */
29
    res = ioctl(s, SIOCGIFADDR, &iface_request);
30
    if (res < 0) {
31
        perror("ioctl(SIOCGIFADDR)");
32
        close(s);
33
34
        return NULL;
35
    }
36
    close(s);
37
38
    inet_ntop(AF_INET, &sin->sin_addr, buff, sizeof(buff));
39
40
    return strdup(buff);
41
}
42
43