Skip to content

Latest commit

 

History

History
100 lines (84 loc) · 2.28 KB

usb_moded-mac.c

File metadata and controls

100 lines (84 loc) · 2.28 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
@file usb_moded-mac.c
Copyright (C) 2013 Jolla. All rights reserved.
@author: Philippe De Swert <philippe.deswert@jollamobile.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public License
version 2 as published by the Free Software Foundation.
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 Lesser GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include <stdio.h>
#include "usb_moded-mac.h"
Jan 18, 2013
Jan 18, 2013
25
#include "usb_moded-log.h"
26
27
28
29
static void random_ether_addr(unsigned char *addr)
{
FILE *random;
Jul 5, 2013
Jul 5, 2013
30
size_t count = 0;
31
32
random = fopen("/dev/urandom", "r");
Jul 5, 2013
Jul 5, 2013
33
count = fread(addr, 1, 6, random);
Jul 5, 2013
Jul 5, 2013
36
37
38
39
40
41
42
if(count > 0 )
{
addr [0] &= 0xfe; /* clear multicast bit */
addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
}
else
log_warning("MAC generation failed!\n");
43
44
45
46
47
48
49
50
}
void generate_random_mac (void)
{
unsigned char addr[6];
int i;
FILE *g_ether;
Jan 18, 2013
Jan 18, 2013
51
log_debug("Getting random usb ethernet mac\n");
52
53
54
random_ether_addr(addr);
g_ether = fopen("/etc/modprobe.d/g_ether.conf", "w");
Mar 31, 2013
Mar 31, 2013
55
56
57
58
59
if(!g_ether)
{
log_warning("Failed to write mac address to /etc/modprobe.d/g_ether.conf\n");
return;
}
60
61
62
63
64
65
fprintf(g_ether, "options g_ether host_addr=");
for(i=0; i<5; i++)
{
fprintf(g_ether, "%02x:",addr[i]);
}
Jan 18, 2013
Jan 18, 2013
66
fprintf(g_ether, "%02x\n",addr[i]);
Aug 14, 2013
Aug 14, 2013
69
70
71
72
73
74
char * read_mac(void)
{
FILE *g_ether;
char *mac = NULL, *ret = NULL;
size_t read = 0;
Oct 29, 2013
Oct 29, 2013
75
int test = 0;
Aug 14, 2013
Aug 14, 2013
76
77
78
79
80
81
82
g_ether = fopen("/etc/modprobe.d/g_ether.conf", "r");
if(!g_ether)
{
log_warning("Failed to read mac address from /etc/modprobe.d/g_ether.conf\n");
return(NULL);
}
Oct 29, 2013
Oct 29, 2013
83
84
test = fseek(g_ether, 26, SEEK_SET);
if(test == -1)
Oct 17, 2013
Oct 17, 2013
85
86
{
fclose(g_ether);
Sep 19, 2013
Sep 19, 2013
87
return 0;
Oct 17, 2013
Oct 17, 2013
88
}
Aug 14, 2013
Aug 14, 2013
89
90
91
92
93
94
95
mac = malloc(sizeof(char) *17);
if(mac)
read = fread(mac, 1, 17, g_ether);
if(read == 17)
ret = strndup(mac,17);
else
ret = 0;
Oct 29, 2013
Oct 29, 2013
96
Aug 14, 2013
Aug 14, 2013
97
free(mac);
Sep 19, 2013
Sep 19, 2013
98
fclose(g_ether);
Aug 14, 2013
Aug 14, 2013
99
100
return(ret);
}