-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.c
125 lines (100 loc) · 2.97 KB
/
util.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
/* -*- mode: C; c-file-style: "k&r"; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/*
lifted from lorcon2
lorcon 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.
lorcon 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 lorcon; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Copyright (c) 2005 dragorn and Joshua Wright
*/
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include "util.h"
void setebuf(PCHAR ebuf, const char *format, ...)
{
if (NULL != ebuf) {
va_list args;
va_start(args, format);
/* int message_len = strlen(msg); */
/* if (message_len >= AIRPCAP_ERRBUF_SIZE) { */
/* message_len = AIRPCAP_ERRBUF_SIZE - 1; */
/* } */
vsnprintf(ebuf, AIRPCAP_ERRBUF_SIZE, format, args);
/* strncpy(ebuf, msg, message_len); */
/* ebuf[message_len] = '\0'; */
}
}
int ifconfig_set_flags(const char *in_dev, short flags)
{
struct ifreq ifr;
int skfd;
if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
return -1;
}
/* Fetch interface flags */
strncpy(ifr.ifr_name, in_dev, IFNAMSIZ);
ifr.ifr_flags = flags;
if (ioctl(skfd, SIOCSIFFLAGS, &ifr) < 0) {
close(skfd);
return -1;
}
close(skfd);
return 0;
}
int ifconfig_get_flags(const char *in_dev, short *flags)
{
struct ifreq ifr;
int skfd;
if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
return -1;
}
/* Fetch interface flags */
strncpy(ifr.ifr_name, in_dev, IFNAMSIZ);
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
close(skfd);
return -1;
}
(*flags) = ifr.ifr_flags;
close(skfd);
return 0;
}
int ifconfig_ifupdown(const char *in_dev, int devup)
{
int ret;
short rflags;
if ((ret = ifconfig_get_flags(in_dev, &rflags)) < 0)
return ret;
if (devup) {
rflags |= IFF_UP;
} else {
rflags &= ~IFF_UP;
}
return ifconfig_set_flags(in_dev, rflags);
}
int ifconfig_get_hwaddr(const char *dev, uint8_t *hwaddr)
{
struct ifreq ifr;
int skfd;
if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
return -1;
}
/* Fetch interface flags */
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0) {
close(skfd);
return -1;
}
memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, 6);
close(skfd);
return 0;
}