-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathip.c
44 lines (36 loc) · 751 Bytes
/
ip.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
/* File: ip.c
* ----------
*
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void GetIpFromDevice(uint8_t ip[4], const char DeviceName[])
{
int fd;
struct ifreq ifr;
assert(strlen(DeviceName) <= IFNAMSIZ);
fd = socket(AF_INET, SOCK_DGRAM, 0);
assert(fd>0);
strncpy(ifr.ifr_name, DeviceName, IFNAMSIZ);
ifr.ifr_addr.sa_family = AF_INET;
if (ioctl(fd, SIOCGIFADDR, &ifr) == 0)
{
struct sockaddr_in *p = (void*) &(ifr.ifr_addr);
memcpy(ip, &(p->sin_addr), 4);
}
else
{
// 查询不到IP时默认填零处理
memset(ip, 0x00, 4);
}
close(fd);
return;
}