-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux.cc
executable file
·159 lines (129 loc) · 3.61 KB
/
linux.cc
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# include <glib.h>
# include <unistd.h>
# include <libwebsockets.h>
# include "linux.h"
ssize_t http_sendfile ( http_request *h, int in_fd ) {
int out_fd = g_io_channel_unix_get_fd( h->channel );
struct stat s;
ssize_t r;
off_t len = 0;
g_assert( in_fd > 0 );
g_assert( out_fd > 0 );
r = fstat( in_fd, &s );
if (r == -1) {
g_message("http_sendfile stat: %m");
return -1;
}
r = sendfile( out_fd, in_fd, &len, s.st_size );
if (r == -1) {
g_message("http_sendfile send: %m");
return -1;
}
return r;
}
gchar* peer_arp( peer *p ) {
gchar ip[50], hw[18];
FILE *arp;
g_assert( p != NULL );
arp = fopen( "/proc/net/arp", "r" );
if ( arp == NULL ){ g_message( "Can't open /proc/net/arp: %m" );}
fscanf(arp, "%*s %*s %*s %*s %*s %*s %*s %*s %*s"); // Skip first line
while (fscanf( arp, "%15s %*s %*s %17s %*s %*s\n", ip, hw ) != EOF) {
if ( strncmp( p->ip, ip, sizeof(p->ip) ) == 0 ) {
g_strncpy( p->hw, hw, sizeof(p->hw) );
break;
}
}
fclose( arp );
return ( p->hw[0] != '\0' ? p->hw : NULL );
}
gchar* get_mac_address (const gchar *dev) {
int r, s;
struct ifreq ifr;
gchar *dest = g_new0( gchar, 13 );
gchar *hwaddr;
strcpy( ifr.ifr_name, dev );
s = socket( PF_INET, SOCK_DGRAM, 0 );
if (s == -1) {
g_message("get_mac_address socket: %m");
g_free(dest);
return NULL;
}
r = ioctl( s, SIOCGIFHWADDR, &ifr );
if (r == -1) {
g_message("get_mac_address: SIOCGIFHWADDR: %m");
g_free(dest);
close(s);
return NULL;
}
hwaddr = ifr.ifr_hwaddr.sa_data;
g_snprintf( dest, 13, "%02X%02X%02X%02X%02X%02X",
hwaddr[0] & 0xFF,
hwaddr[1] & 0xFF,
hwaddr[2] & 0xFF,
hwaddr[3] & 0xFF,
hwaddr[4] & 0xFF,
hwaddr[5] & 0xFF);
close(s);
return dest;
}
gchar* get_network_address (const gchar *dev) {
int r, s;
struct ifreq ifaddr, ifnetmask;
gchar *dest = g_new0( gchar, 33 );
gchar *addr, *mask;
s = socket( PF_INET, SOCK_DGRAM, 0 );
if (s == -1) {
g_message("get_network_address socket: %m");
g_free(dest);
return NULL;
}
strncpy( ifaddr.ifr_name, dev, IFNAMSIZ );
r = ioctl( s, SIOCGIFADDR, &ifaddr );
if (r == -1) {
g_message("get_network_address: SIOCGIFADDR: %m");
g_free(dest);
close(s);
return NULL;
}
strncpy( ifnetmask.ifr_name, dev, IFNAMSIZ );
r = ioctl( s, SIOCGIFNETMASK, &ifnetmask );
if (r == -1) {
g_message("get_network_address: SIOCGIFNETMASK: %m");
g_free(dest);
close(s);
return NULL;
}
addr = (char *) &(((struct sockaddr_in *)&ifaddr.ifr_addr)->sin_addr);
mask = (char *) &(((struct sockaddr_in *)&ifnetmask.ifr_netmask)->sin_addr);
g_snprintf( dest, 32, "%d.%d.%d.%d/%d.%d.%d.%d",
addr[0] & mask[0] & 0xFF,
addr[1] & mask[1] & 0xFF,
addr[2] & mask[2] & 0xFF,
addr[3] & mask[3] & 0xFF,
mask[0] & 0xFF,
mask[1] & 0xFF,
mask[2] & 0xFF,
mask[3] & 0xFF );
close(s);
return dest;
}
gchar* detect_network_device(const gchar *exclude) {
gchar dev[7], dest[9];
gchar *out = NULL;
FILE *route;
route = fopen( "/proc/net/route", "r" );
if ( route == NULL ){g_error( "detect_network_device: Can't open /proc/net/route: %m" );}
// Skip first line
fscanf(route, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s");
while (fscanf( route, "%6s %8s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n", dev, dest ) != EOF) {
if ( exclude ?
(strncmp( dev, exclude, 6 ) != 0) : // not the whatever it is
(strcmp( dest, "00000000" ) == 0) ) { // default route
out = g_strdup(dev);
break;
}
}
fclose( route );
return out;
}