blob: 59e3fcf1b8a932aa738c9e821b9f3c6432b5e0e8 (
plain)
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
|
/* $OpenBSD: ifaddr.c,v 1.7 2001/01/30 04:26:01 kjell Exp $ */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <string.h>
#include <err.h>
#include "ifaddr.h"
/*
* if_addr():
* given a string containing an interface name (e.g. "ppp0")
* return the IP address it represents
*
* The OpenBSD community considers this feature to be quite useful and
* suggests inclusion into other platforms. The closest alternative is
* to define /etc/networks with suitable values.
*/
int if_addr(name, ap)
char *name;
struct in_addr *ap;
{
struct sockaddr_in *sin;
struct ifreq ifr;
int s;
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
warn("socket");
return (0);
}
strncpy(ifr.ifr_name, name, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = '\0';
if (ioctl(s, SIOCGIFADDR, &ifr) < 0)
return (0);
sin = (struct sockaddr_in *)&ifr.ifr_addr;
*ap = sin->sin_addr;
return (1);
}
|