aboutsummaryrefslogtreecommitdiff
path: root/src/iface.c
blob: c6986c70663329a131e9b32a60f8f0ecaf38e15e (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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* 
 * The Network Discovery Tool
 * Copyright (C) 2005 Tadas Vilkeliskis aka vptr (vpointer@gmail.com)
 *
 * The Ultimate Network Tool is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA.
 */

#include <stdio.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <string.h>
#include <netinet/ether.h>
#include "iface.h"
#include "defs.h"

struct ifreq iface;

struct interface
{
	int index,
	    mtu,
	    metric;
	short flags;
	struct in_addr ip, netmask, broadcast;
	struct ether_addr mac;
	
} interface;
	
void iface_init(int socket, char *name)
{
	PDEBUG("iface_init() called\n");
	memset(&iface, 0, sizeof(struct ifreq));
	strncpy(iface.ifr_name, name, strlen(name));
	
	if (ioctl(socket, SIOCGIFINDEX, &iface) == -1)
		interface.index = -1;
	else {
		interface.index = iface.ifr_ifindex;

		if (ioctl(socket, SIOCGIFADDR, &iface) != -1)
			//memcpy(&iface_ip, &(((struct sockaddr_in * )&iface.ifr_addr)->sin_addr), 4);
			interface.ip = ((struct sockaddr_in * )&iface.ifr_addr)->sin_addr;

		if (ioctl(socket, SIOCGIFNETMASK, &iface) != -1)
			//memcpy(&iface_nmask, &(((struct sockaddr_in * )&iface.ifr_netmask)->sin_addr), 4);
			interface.netmask = ((struct sockaddr_in * )&iface.ifr_netmask)->sin_addr;

		if (ioctl(socket, SIOCGIFBRDADDR, &iface) != -1)
			//memcpy(&iface_bcast, &(((struct sockaddr_in * )&iface.ifr_broadaddr)->sin_addr), 4);
			interface.broadcast = ((struct sockaddr_in * )&iface.ifr_broadaddr)->sin_addr;

		if (ioctl(socket, SIOCGIFHWADDR, &iface) != -1)
			memcpy((void*)interface.mac.ether_addr_octet, (void*)&iface.ifr_hwaddr.sa_data, 6);

		if (ioctl(socket, SIOCGIFMTU, &iface) != -1)
			interface.mtu = iface.ifr_mtu;
		
		if (ioctl(socket, SIOCGIFFLAGS, &iface) != -1)
			interface.flags = iface.ifr_flags;

		if (ioctl(socket, SIOCGIFMETRIC, &iface) != -1)
			interface.metric = iface.ifr_metric;
	}
	PDEBUG("iface_init() ended\n");
}

int iface_get_index()
{
	return (interface.index);
}

int iface_get_mtu()
{
	return (interface.mtu);
}

int iface_get_metric()
{
	return (interface.metric);
}

short iface_get_flags()
{
	return (interface.flags);
}

void iface_get_ip(struct in_addr *ip)
{
	(*ip) = interface.ip;
}

void iface_get_netmask(struct in_addr *netmask)
{
	(*netmask) = interface.netmask;
}

void iface_get_broadcast(struct in_addr *broadcast)
{
	(*broadcast) = interface.broadcast;
}

void iface_get_mac(struct ether_addr *mac)
{
	(*mac) = interface.mac;
}