-
Notifications
You must be signed in to change notification settings - Fork 87
/
myipaddr.c
77 lines (71 loc) · 1.75 KB
/
myipaddr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
*/
#include "myipaddr.h"
#include <stdio.h>
#include <stdlib.h>
#include <ifaddrs.h>
#include <string.h>
#include <stdbool.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#if ! defined(IFT_ETHER)
#define IFT_ETHER 0x6/* Ethernet CSMACD */
#endif
char * myipaddr(void)
{
bool success;
struct ifaddrs *addrs;
const struct ifaddrs *cursor;
const struct sockaddr_dl *dlAddr;
const uint8_t *base;
const struct ifaddrs * en0cursor=NULL;
success = getifaddrs(&addrs) == 0;
if (success) {
cursor = addrs;
while (cursor != NULL) {
if ((cursor->ifa_flags & IFF_LOOPBACK) == 0 ) {
#ifdef DEBUG
printf("%s ", (char *)cursor->ifa_name);
printf("%s\n",inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr));
#endif
if(!strcmp((char*)cursor->ifa_name,"en0"))
en0cursor=cursor;
}
if ( (cursor->ifa_addr->sa_family == AF_LINK)
&& (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type ==IFT_ETHER)
) {
dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
// fprintf(stderr, " sdl_nlen = %d\n", dlAddr->sdl_nlen);
// fprintf(stderr, " sdl_alen = %d\n", dlAddr->sdl_alen);
base = (const uint8_t *) &dlAddr->sdl_data[dlAddr->sdl_nlen];
#ifdef DEBUG
{
int i;
printf(" MAC address ");
for (i = 0; i < dlAddr->sdl_alen; i++) {
if (i != 0) {
printf(":");
}
printf("%02x", base[i]);
}
printf("\n");
}
#endif
}
cursor = cursor->ifa_next;
}
}
if(addrs)
freeifaddrs(addrs);
if(en0cursor!=NULL) {
char *s = inet_ntoa(((struct sockaddr_in *)en0cursor->ifa_addr)->sin_addr);
if (strcmp(s,"6.3.6.0"))
return s;
}
return "no WiFi";
}