blob: 921b3a4c0829c6b48dd8d44a4f23d5251e99cf41 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <ctype.h>
6#include <linux/fs.h>
7#include <sys/ioctl.h>
8
9int
10getnext (
11 char * src,
12 int separator,
13 char * dest
14 )
15{
16 char * c;
17 int len;
18
19 if ( (src == NULL) || (dest == NULL) ) {
20 return -1;
21 }
22
23 c = strchr(src, separator);
24 if (c == NULL) {
25 return -1;
26 }
27 len = c - src;
28 strncpy(dest, src, len);
29 dest[len] = '\0';
30 return len + 1;
31}
32
33int
34str_to_mac (
35 unsigned char * mac,
36 char * str
37 )
38{
39 int len;
40 char * ptr = str;
41 char buf[128];
42 int i;
43
44 for (i = 0; i < 5; i++) {
45 if ((len = getnext(ptr, ':', buf)) == -1) {
46 return 1; /* parse error */
47 }
48 mac[i] = strtol(buf, NULL, 16);
49 ptr += len;
50 }
51 mac[5] = strtol(ptr, NULL, 16);
52
53 return 0;
54}
55
56int
57str_to_ip (
58 unsigned int * ip,
59 char * str
60 )
61{
62 int len;
63 char * ptr = str;
64 char buf[128];
65 unsigned char c[4];
66 int i;
67
68 for (i = 0; i < 3; ++i) {
69 if ((len = getnext(ptr, '.', buf)) == -1) {
70 return 1; /* parse error */
71 }
72 c[i] = atoi(buf);
73 ptr += len;
74 }
75 c[3] = atoi(ptr);
76 *ip = (c[0]<<24) + (c[1]<<16) + (c[2]<<8) + c[3];
77 return 0;
78}
79
80int
81str_to_ipv6 (
82 unsigned int * ipv6,
83 char * str,
84 unsigned int byte)
85{
86 int len;
87 char * ptr = str;
88 char buf[128];
89 unsigned short c[8];
90 int i;
91 for (i = 0; i < 7; i++) {
92
93 if ((len = getnext(ptr, ':', buf)) == -1) {
94 return 1; /* parse error */
95 }
96 c[i] = strtoul(buf, NULL, 16);
97 ptr += len;
98 //printf("len=%d, c[%d]=%x\n",len, i, c[i]);
99
100 }
101 c[7] = atoi(ptr);
102 *ipv6 = (c[2*byte] <<16) + (c[2*byte + 1]);
103 return 0;
104}
105