1 #include <stdlib.h>
2 #include <netinet/ether.h>
3 #include <stdio.h>
4 #include <unsupported_api.h>
5 #ifdef __LITEOS_A__
6 #include <string.h>
7 #include <errno.h>
8 
9 #define ETHER_FILE_PATH "/etc/ethers"
10 #define BUFFER_SIZE     200
11 #endif
12 
ether_aton_r(const char *x, struct ether_addr *p_a)13 struct ether_addr *ether_aton_r (const char *x, struct ether_addr *p_a)
14 {
15 	struct ether_addr a;
16 	char *y;
17 	for (int ii = 0; ii < 6; ii++) {
18 		unsigned long int n;
19 		if (ii != 0) {
20 			if (x[0] != ':') return 0; /* bad format */
21 			else x++;
22 		}
23 		n = strtoul (x, &y, 16);
24 		x = y;
25 		if (n > 0xFF) return 0; /* bad byte */
26 		a.ether_addr_octet[ii] = n;
27 	}
28 	if (x[0] != 0) return 0; /* bad format */
29 	*p_a = a;
30 	return p_a;
31 }
32 
ether_aton(const char *x)33 struct ether_addr *ether_aton (const char *x)
34 {
35 	static struct ether_addr a;
36 	return ether_aton_r (x, &a);
37 }
38 
ether_ntoa_r(const struct ether_addr *p_a, char *x)39 char *ether_ntoa_r (const struct ether_addr *p_a, char *x) {
40 	char *y;
41 	y = x;
42 	for (int ii = 0; ii < 6; ii++) {
43 		x += sprintf (x, ii == 0 ? "%.2X" : ":%.2X", p_a->ether_addr_octet[ii]);
44 	}
45 	return y;
46 }
47 
ether_ntoa(const struct ether_addr *p_a)48 char *ether_ntoa (const struct ether_addr *p_a) {
49 	static char x[18];
50 	return ether_ntoa_r (p_a, x);
51 }
52 
ether_line(const char *line, struct ether_addr *addr, char *hostname)53 int ether_line(const char *line, struct ether_addr *addr, char *hostname)
54 {
55 #ifdef __LITEOS_A__
56 	char buf[BUFFER_SIZE], *ch, *ptr = NULL;
57 	if (line[0] == '#') {
58 		errno = EINVAL;
59 		return -1;
60 	}
61 	strcpy(buf, line);
62 	ch = strchr(buf, '#');
63 	if (ch)
64 		*ch = '\0';
65 	ch = strtok_r(buf, " \t\n", &ptr);
66 	ch = strtok_r(NULL, " \t\n", &ptr);
67 	if (ch == NULL) {
68 		errno = EINVAL;
69 		return -1;
70 	}
71 	strcpy(hostname, ch);
72 
73 	unsigned int ret;
74 	ch = &buf[0];
75 	for (int i = 0; i < 6; i++) {
76 		ch = strtok_r(ch, ":", &ptr);
77 		if (ch == NULL || sscanf(ch, "%x", &ret) != 1) {
78 			errno = EINVAL;
79 			return -1;
80 		}
81 		addr->ether_addr_octet[i] = (uint8_t)ret;
82 		ch = NULL;
83 	}
84 
85 	return 0;
86 #else
87 	return -1;
88 #endif
89 }
90 
ether_ntohost(char *hostname, const struct ether_addr *addr)91 int ether_ntohost(char *hostname, const struct ether_addr *addr)
92 {
93 #ifdef __LITEOS_A__
94 	FILE *f = NULL;
95 	char buf[BUFFER_SIZE + 1];
96 	char temp_name[BUFFER_SIZE];
97 	struct ether_addr *temp_addr;
98 
99 	if (!(f = fopen(ETHER_FILE_PATH, "r")))
100 		return ENOENT;
101 	temp_addr = (struct ether_addr *)malloc(sizeof(struct ether_addr));
102 	if (temp_addr == NULL) {
103 		fclose(f);
104 		return ENOMEM;
105 	}
106 	while (fgets(buf, BUFFER_SIZE, f)) {
107 		if (ether_line(buf, temp_addr, temp_name) != 0)
108 			continue;
109 		if (memcmp(addr, temp_addr, 6) == 0) {
110 			strcpy(hostname, temp_name);
111 			free(temp_addr);
112 			fclose(f);
113 			return 0;
114 		}
115 	}
116 
117 	free(temp_addr);
118 	fclose(f);
119 	return -1;
120 #else
121 	return -1;
122 #endif
123 }
124 
125 
ether_hostton(const char *hostname, struct ether_addr *addr)126 int ether_hostton(const char *hostname, struct ether_addr *addr)
127 {
128 #ifdef __LITEOS_A__
129 	FILE *f = NULL;
130 	char buf[BUFFER_SIZE + 1];
131 	char temp_name[BUFFER_SIZE];
132 	struct ether_addr *temp_addr;
133 
134 	temp_addr = (struct ether_addr *)malloc(sizeof(struct ether_addr));
135 	if (temp_addr == NULL)
136 		return ENOMEM;
137 	if (!(f = fopen(ETHER_FILE_PATH, "r"))) {
138 		free(temp_addr);
139 		return ENOENT;
140 	}
141 	while (fgets(buf, BUFFER_SIZE, f)) {
142 		if (ether_line(buf, temp_addr, temp_name) != 0)
143 			continue;
144 		if (strcmp(hostname, temp_name) == 0) {
145 			memcpy(addr, temp_addr, 6);
146 			free(temp_addr);
147 			fclose(f);
148 			return 0;
149 		}
150 	}
151 
152 	free(temp_addr);
153 	fclose(f);
154 	return -1;
155 #else
156 	return -1;
157 #endif
158 }
159