/
netinfo.c
128 lines (102 loc) · 2.83 KB
/
netinfo.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
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
120
121
122
123
124
125
126
127
128
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* MAC Changer
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2002,2013 Alvaro Lopez Ortega
*
* This program 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 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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include "netinfo.h"
net_info_t *
mc_net_info_new (const char *device)
{
net_info_t *new = (net_info_t *) malloc (sizeof(net_info_t));
new->sock = socket (AF_INET, SOCK_DGRAM, 0);
if (new->sock<0) {
perror ("[ERROR] Socket");
free(new);
return NULL;
}
strncpy (new->dev.ifr_name, device, sizeof(new->dev.ifr_name));
new->dev.ifr_name[sizeof(new->dev.ifr_name)-1] = '\0';
if (ioctl(new->sock, SIOCGIFHWADDR, &new->dev) < 0) {
perror ("[ERROR] Set device name");
free(new);
return NULL;
}
return new;
}
void
mc_net_info_free (net_info_t *net)
{
close(net->sock);
free(net);
}
mac_t *
mc_net_info_get_mac (const net_info_t *net)
{
int i;
mac_t *new = (mac_t *) malloc (sizeof(mac_t));
for (i=0; i<6; i++) {
new->byte[i] = net->dev.ifr_hwaddr.sa_data[i] & 0xFF;
}
return new;
}
int
mc_net_info_set_mac (net_info_t *net, const mac_t *mac)
{
int i;
for (i=0; i<6; i++) {
net->dev.ifr_hwaddr.sa_data[i] = mac->byte[i];
}
if (ioctl(net->sock, SIOCSIFHWADDR, &net->dev) < 0) {
perror ("[ERROR] Could not change MAC: interface up or insufficient permissions");
return -1;
}
return 0;
}
mac_t *
mc_net_info_get_permanent_mac (const net_info_t *net)
{
int i;
struct ifreq req;
struct ethtool_perm_addr *epa;
mac_t *newmac;
newmac = (mac_t *) calloc (1, sizeof(mac_t));
epa = (struct ethtool_perm_addr*) malloc(sizeof(struct ethtool_perm_addr) + IFHWADDRLEN);
epa->cmd = ETHTOOL_GPERMADDR;
epa->size = IFHWADDRLEN;
memcpy(&req, &(net->dev), sizeof(struct ifreq));
req.ifr_data = (caddr_t)epa;
if (ioctl(net->sock, SIOCETHTOOL, &req) < 0) {
perror ("[ERROR] Could not read permanent MAC");
} else {
for (i=0; i<6; i++) {
newmac->byte[i] = epa->data[i];
}
}
free(epa);
return newmac;
}