Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RIOT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cm-projects
RIOT
Commits
0cca4867
Commit
0cca4867
authored
10 years ago
by
Martine Lenders
Browse files
Options
Downloads
Patches
Plain Diff
ng_ipv6_addr: add string to address conversion function
parent
9f2e2121
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
sys/include/net/ng_ipv6/addr.h
+17
-0
17 additions, 0 deletions
sys/include/net/ng_ipv6/addr.h
sys/net/network_layer/ng_ipv6/addr/ng_ipv6_addr_from_str.c
+203
-0
203 additions, 0 deletions
sys/net/network_layer/ng_ipv6/addr/ng_ipv6_addr_from_str.c
with
220 additions
and
0 deletions
sys/include/net/ng_ipv6/addr.h
+
17
−
0
View file @
0cca4867
...
@@ -520,6 +520,23 @@ static inline void ng_ipv6_addr_set_solicited_nodes(ng_ipv6_addr_t *out,
...
@@ -520,6 +520,23 @@ static inline void ng_ipv6_addr_set_solicited_nodes(ng_ipv6_addr_t *out,
char
*
ng_ipv6_addr_to_str
(
char
*
result
,
const
ng_ipv6_addr_t
*
addr
,
char
*
ng_ipv6_addr_to_str
(
char
*
result
,
const
ng_ipv6_addr_t
*
addr
,
uint8_t
result_len
);
uint8_t
result_len
);
/**
* @brief Converts an IPv6 address string representation to a byte-represented
* IPv6 address
*
* @see <a href="https://tools.ietf.org/html/rfc5952">
* RFC 5952
* </a>
*
* @param[in] result The resulting byte representation
* @param[in] addr An IPv6 address string representation
*
* @return @p result, on success
* @return NULL, if @p addr was malformed
* @return NULL, if @p result or @p addr was NULL
*/
ng_ipv6_addr_t
*
ng_ipv6_addr_from_str
(
ng_ipv6_addr_t
*
result
,
const
char
*
addr
);
#ifdef __cplusplus
#ifdef __cplusplus
}
}
#endif
#endif
...
...
This diff is collapsed.
Click to expand it.
sys/net/network_layer/ng_ipv6/addr/ng_ipv6_addr_from_str.c
0 → 100644
+
203
−
0
View file @
0cca4867
/*
* Copyright (c) 1996 by Internet Software Consortium.
* Copyright (c) 2015 by Martine Lenders <mlenders@inf.fu-berlin.de>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/**
* @ingroup net_ipv6_addr
* @{
*
* @file
*
* @author Paul Vixie
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#include
<inttypes.h>
#include
<stdlib.h>
#include
<string.h>
#include
"byteorder.h"
#include
"net/ng_ipv6/addr.h"
#define DEC "0123456789"
#define HEX_L "0123456789abcdef"
#define HEX_U "0123456789ABCDEF"
/* XXX: move this to IPv4 when we have it */
/* based on inet_pton4() by Paul Vixie */
static
network_uint32_t
*
ipv4_addr_from_str
(
network_uint32_t
*
result
,
const
char
*
addr
)
{
uint8_t
saw_digit
,
octets
,
ch
;
uint8_t
tmp
[
sizeof
(
network_uint32_t
)],
*
tp
;
saw_digit
=
0
;
octets
=
0
;
*
(
tp
=
tmp
)
=
0
;
while
((
ch
=
*
addr
++
)
!=
'\0'
)
{
const
char
*
pch
;
if
((
pch
=
strchr
(
DEC
,
ch
))
!=
NULL
)
{
uint16_t
new
=
*
tp
*
10
+
(
uint16_t
)(
pch
-
DEC
);
if
(
new
>
255
)
{
return
NULL
;
}
*
tp
=
new
;
if
(
!
saw_digit
)
{
if
(
++
octets
>
4
)
{
return
NULL
;
}
saw_digit
=
1
;
}
}
else
if
(
ch
==
'.'
&&
saw_digit
)
{
if
(
octets
==
4
)
{
return
NULL
;
}
*++
tp
=
0
;
saw_digit
=
0
;
}
else
{
return
NULL
;
}
}
if
(
octets
<
4
)
{
return
NULL
;
}
memcpy
(
result
,
tmp
,
sizeof
(
network_uint32_t
));
return
result
;
}
/* based on inet_pton6() by Paul Vixie */
ng_ipv6_addr_t
*
ng_ipv6_addr_from_str
(
ng_ipv6_addr_t
*
result
,
const
char
*
addr
)
{
uint8_t
*
colonp
=
0
;
const
char
*
curtok
=
addr
;
uint32_t
val
=
0
;
char
ch
;
uint8_t
saw_xdigit
=
0
;
uint8_t
i
=
0
;
if
((
result
==
NULL
)
||
(
addr
==
NULL
))
{
return
NULL
;
}
ng_ipv6_addr_set_unspecified
(
result
);
/* Leading :: requires some special handling. */
if
(
*
addr
==
':'
)
{
if
(
*++
addr
!=
':'
)
{
return
NULL
;
}
}
while
((
ch
=
*
addr
++
)
!=
'\0'
)
{
const
char
*
pch
;
const
char
*
xdigits
;
if
((
pch
=
strchr
((
xdigits
=
HEX_L
),
ch
))
==
NULL
)
{
pch
=
strchr
((
xdigits
=
HEX_U
),
ch
);
}
if
(
pch
!=
NULL
)
{
val
<<=
4
;
val
|=
(
pch
-
xdigits
);
if
(
val
>
0xffff
)
{
return
NULL
;
}
saw_xdigit
=
1
;
continue
;
}
if
(
ch
==
':'
)
{
curtok
=
addr
;
if
(
!
saw_xdigit
)
{
if
(
colonp
!=
NULL
)
{
return
NULL
;
}
colonp
=
&
(
result
->
u8
[
i
]);
continue
;
}
if
(
i
>
sizeof
(
ng_ipv6_addr_t
))
{
return
NULL
;
}
result
->
u8
[
i
++
]
=
(
uint8_t
)(
val
>>
8
)
&
0xff
;
result
->
u8
[
i
++
]
=
(
uint8_t
)
val
&
0xff
;
saw_xdigit
=
0
;
val
=
0
;
continue
;
}
if
(
ch
==
'.'
&&
(
i
<=
sizeof
(
ng_ipv6_addr_t
))
&&
ipv4_addr_from_str
((
network_uint32_t
*
)(
&
(
result
->
u8
[
i
])),
curtok
)
!=
NULL
)
{
i
+=
sizeof
(
network_uint32_t
);
saw_xdigit
=
0
;
break
;
/* '\0' was seen by ipv4_addr_from_str(). */
}
return
NULL
;
}
if
(
saw_xdigit
)
{
if
(
i
+
sizeof
(
uint16_t
)
>
sizeof
(
ng_ipv6_addr_t
))
{
return
NULL
;
}
result
->
u8
[
i
++
]
=
(
uint8_t
)(
val
>>
8
)
&
0xff
;
result
->
u8
[
i
++
]
=
(
uint8_t
)
val
&
0xff
;
}
if
(
colonp
!=
NULL
)
{
/*
* Since some memmove()'s erroneously fail to handle
* overlapping regions, we'll do the shift by hand.
*/
const
int32_t
n
=
&
(
result
->
u8
[
i
++
])
-
colonp
;
for
(
int32_t
j
=
1
;
j
<=
n
;
j
++
)
{
result
->
u8
[
sizeof
(
ng_ipv6_addr_t
)
-
j
]
=
colonp
[
n
-
j
];
colonp
[
n
-
j
]
=
0
;
}
i
=
sizeof
(
ng_ipv6_addr_t
);
}
if
(
i
!=
sizeof
(
ng_ipv6_addr_t
))
{
return
NULL
;
}
return
result
;
}
/**
* @}
*/
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment