-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetif_ip.c
More file actions
43 lines (31 loc) · 1.03 KB
/
getif_ip.c
File metadata and controls
43 lines (31 loc) · 1.03 KB
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
/*
* name: getif_ip.c
* this file is part of the "Net-Injector" project. It defines the getif_ip_by_name() function
* Copyright (C) 2002 by Jean Philippe GUILLEMIN <jp.guillemin@free.fr>
* license: This software is under GPL license
* date: 04 17 2003
* rev: 0.8
*/
#include "neti.h"
int getif_ip_by_name(char *ip, u_int32_t *inet_ip, char *ifname){
struct ifreq ifr;
struct sockaddr_in *addr;
int sock;
addr = (struct sockaddr_in *) &ifr.ifr_addr;
memset(&ifr, 0, IFREQ_SIZE);
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) >= 0) {
strncpy(ifr.ifr_name, ifname, 32);
if (ioctl(sock, SIOCGIFADDR, &ifr) == 0){
memcpy (inet_ip, &addr->sin_addr.s_addr, sizeof(addr->sin_addr.s_addr));
strncpy(ip, inet_ntoa(addr->sin_addr), 32);
/* fprintf(stdout, "%s\n", inet_ntoa(addr->sin_addr)); debug */
}else{
fprintf(stderr, "Error : (getif_ip) in call to ioctl() !\n");
close(sock);
}
}else {
fprintf(stderr, "Error : (getif_ip) cannot get src address !\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}