Statistics
| Branch: | Revision:

streamers / net_helpers.c @ 9d8a82ea

History | View | Annotate | Download (3.61 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *  Copyright (c) 2010 Csaba Kiraly
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 */
7
#include <sys/types.h>
8
#include <ifaddrs.h>
9
#include <sys/ioctl.h>
10
#include <sys/socket.h>
11
#include <netinet/in.h>
12
#include <arpa/inet.h>
13
#include <net/if.h>     /* For struct ifreq */
14
#include <unistd.h>
15
#include <stdlib.h>
16
#include <stdio.h>
17
#include <string.h>
18
#include <netdb.h>
19

    
20
#include "net_helpers.h"
21

    
22
char *iface_addr(const char *iface)
23
{
24
    int s, res;
25
    struct ifreq iface_request;
26
    struct sockaddr_in *sin;
27
    char buff[512];
28

    
29
    s = socket(AF_INET, SOCK_DGRAM, 0);
30
    if (s < 0) {
31
        return NULL;
32
    }
33

    
34
    memset(&iface_request, 0, sizeof(struct ifreq));
35
    sin = (struct sockaddr_in *)&iface_request.ifr_addr;
36
    strcpy(iface_request.ifr_name, iface);
37
    /* sin->sin_family = AF_INET); */
38
    res = ioctl(s, SIOCGIFADDR, &iface_request);
39
    if (res < 0) {
40
        perror("ioctl(SIOCGIFADDR)");
41
        close(s);
42

    
43
        return NULL;
44
    }
45
    close(s);
46

    
47
    inet_ntop(AF_INET, &sin->sin_addr, buff, sizeof(buff));
48

    
49
    return strdup(buff);
50
}
51

    
52

    
53

    
54
/*
55
char *default_ip_addr()
56
{
57
  char hostname[256];
58
  struct hostent *host_entry;
59
  char *ip;
60

61
  fprintf(stderr, "Trying to guess IP ...");
62
  if (gethostname(hostname, sizeof hostname) < 0) {
63
    fprintf(stderr, "can't get hostname\n");
64
    return NULL;
65
  }
66
  fprintf(stderr, "hostname is: %s ...", hostname);
67

68
  host_entry = gethostbyname(hostname);
69
  if (! host_entry) {
70
    fprintf(stderr, "can't resolve IP\n");
71
    return NULL;
72
  }
73
  inet_ntop(AF_INET, &sin->sin_addr, buff, sizeof(buff));
74
  ip = inet_ntoa(host_entry->h_addr);
75
  fprintf(stderr, "IP is: %s ...", ip);
76

77
  return ip;
78
}
79
*/
80

    
81
const char *autodetect_ip_address() {
82
#ifndef __linux__
83
        return NULL;
84
#endif
85

    
86
        static char addr[128] = "";
87
        char iface[IFNAMSIZ] = "";
88
        char line[128] = "x";
89
        struct ifaddrs *ifAddrStruct = NULL;
90

    
91
        FILE *r = fopen("/proc/net/route", "r");
92
        if (!r) return NULL;
93

    
94
        while (1) {
95
                char dst[32];
96
                char ifc[IFNAMSIZ];
97

    
98
                fgets(line, 127, r);
99
                if (feof(r)) break;
100
                if ((sscanf(line, "%s\t%s", iface, dst) == 2) && !strcpy(dst, "00000000")) {
101
                        strcpy(iface, ifc);
102
                         break;
103
                }
104
        }
105
        if (iface[0] == 0) return NULL;
106

    
107
        if (getifaddrs(&ifAddrStruct) < 0) return NULL;
108

    
109
        while (ifAddrStruct) {
110
                if (ifAddrStruct->ifa_addr && ifAddrStruct->ifa_addr->sa_family == AF_INET && 
111
                        ifAddrStruct->ifa_name && !strcmp(ifAddrStruct->ifa_name, iface))  {
112
                        void *tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
113
                        return inet_ntop(AF_INET, tmpAddrPtr, addr, 127);
114
                }
115
        ifAddrStruct=ifAddrStruct->ifa_next;
116
        }
117

    
118
        return NULL;
119
}
120

    
121

    
122
char *default_ip_addr()
123
{
124
  char hostname[256];
125
  const char *ip;
126
  struct addrinfo * result;
127
  struct addrinfo * res;
128
  int error;
129

    
130

    
131
  fprintf(stderr, "Trying to guess IP ...");
132
  if (gethostname(hostname, sizeof hostname) < 0) {
133
    fprintf(stderr, "can't get hostname\n");
134
    return NULL;
135
  }
136
  fprintf(stderr, "hostname is: %s ...", hostname);
137

    
138

    
139
  /* resolve the domain name into a list of addresses */
140
  error = getaddrinfo(hostname, NULL, NULL, &result);
141
  if (error != 0) {
142
    fprintf(stderr, "can't resolve IP: %s\n", gai_strerror(error));
143
    return NULL;
144
  }
145

    
146
  /* loop over all returned results and do inverse lookup */
147
  for (res = result; res != NULL; res = res->ai_next) {
148
    ip = inet_ntoa(((struct sockaddr_in*)res->ai_addr)->sin_addr);
149
    fprintf(stderr, "IP is: %s ...", ip);
150
    if ( strncmp("127.", ip, 4) == 0) {
151
      fprintf(stderr, ":( ...");
152
      ip = NULL;
153
    } else {
154
      break;
155
    }
156
  }
157
  freeaddrinfo(result);
158

    
159
  if (!ip) {
160
    ip = autodetect_ip_address();
161
  }
162
  fprintf(stderr, "IP is: %s ...\n", ip);
163

    
164
  return strdup(ip);
165
}