18
18
#include " NetworkInterface.h"
19
19
#include " NetworkStack.h"
20
20
#include < string.h>
21
+ #include " ip6string.h"
21
22
#include " mbed.h"
22
23
23
24
@@ -86,59 +87,9 @@ static void ipv4_from_address(uint8_t *bytes, const char *addr)
86
87
}
87
88
}
88
89
89
- static int ipv6_scan_chunk (uint16_t *shorts, const char *chunk) {
90
- int count = 0 ;
91
- int i = 0 ;
92
-
93
- for (; count < NSAPI_IPv6_BYTES/2 ; count++) {
94
- unsigned short s;
95
- int scanned = sscanf (&chunk[i], " %hx" , &s);
96
- if (scanned < 1 ) {
97
- return count;
98
- }
99
-
100
- shorts[count] = s;
101
-
102
- for (; chunk[i] != ' :' ; i++) {
103
- if (!chunk[i]) {
104
- return count+1 ;
105
- }
106
- }
107
-
108
- i++;
109
- }
110
-
111
- return count;
112
- }
113
-
114
90
static void ipv6_from_address (uint8_t *bytes, const char *addr)
115
91
{
116
- // Start with zeroed address
117
- uint16_t shorts[NSAPI_IPv6_BYTES/2 ];
118
- int suffix = 0 ;
119
-
120
- // Find double colons and scan suffix
121
- for (int i = 0 ; addr[i]; i++) {
122
- if (addr[i] == ' :' && addr[i+1 ] == ' :' ) {
123
- suffix = ipv6_scan_chunk (shorts, &addr[i+2 ]);
124
- break ;
125
- }
126
- }
127
-
128
- // Move suffix to end
129
- memmove (&shorts[NSAPI_IPv6_BYTES/2 -suffix], &shorts[0 ],
130
- suffix*sizeof (uint16_t ));
131
- memset (&shorts[0 ], 0 ,
132
- (NSAPI_IPv6_BYTES/2 -suffix)*sizeof (uint16_t ));
133
-
134
- // Scan prefix
135
- ipv6_scan_chunk (shorts, &addr[0 ]);
136
-
137
- // Flip bytes
138
- for (int i = 0 ; i < NSAPI_IPv6_BYTES/2 ; i++) {
139
- bytes[2 *i+0 ] = (uint8_t )(shorts[i] >> 8 );
140
- bytes[2 *i+1 ] = (uint8_t )(shorts[i] >> 0 );
141
- }
92
+ stoip6 (addr, strlen (addr), bytes);
142
93
}
143
94
144
95
static void ipv4_to_address (char *addr, const uint8_t *bytes)
@@ -148,11 +99,7 @@ static void ipv4_to_address(char *addr, const uint8_t *bytes)
148
99
149
100
static void ipv6_to_address (char *addr, const uint8_t *bytes)
150
101
{
151
- for (int i = 0 ; i < NSAPI_IPv6_BYTES/2 ; i++) {
152
- sprintf (&addr[5 *i], " %02x%02x" , bytes[2 *i], bytes[2 *i+1 ]);
153
- addr[5 *i+4 ] = ' :' ;
154
- }
155
- addr[NSAPI_IPv6_SIZE-1 ] = ' \0 ' ;
102
+ ip6tos (bytes, addr);
156
103
}
157
104
158
105
0 commit comments