1/* Convert an IPv6 scope ID from text to the internal representation.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <net-internal.h>
20
21#include <ctype.h>
22#include <errno.h>
23#include <locale.h>
24#include <net/if.h>
25#include <stdbool.h>
26#include <stdint.h>
27#include <stdlib.h>
28
29/* Parse SOURCE as a scope ID for ADDRESS. Return 0 on success and -1
30 on error. */
31internal_function int
32__inet6_scopeid_pton (const struct in6_addr *address, const char *scope,
33 uint32_t *result)
34{
35 if (IN6_IS_ADDR_LINKLOCAL (address)
36 || IN6_IS_ADDR_MC_LINKLOCAL (address))
37 {
38 uint32_t number = __if_nametoindex (scope);
39 if (number != 0)
40 {
41 *result = number;
42 return 0;
43 }
44 }
45
46 if (isdigit_l (scope[0], _nl_C_locobj_ptr))
47 {
48 char *end;
49 unsigned long long number
50 = ____strtoull_l_internal (scope, &end, /*base */ 10, /* group */ 0,
51 _nl_C_locobj_ptr);
52 if (*end == '\0' && number <= UINT32_MAX)
53 {
54 *result = number;
55 return 0;
56 }
57 }
58
59 __set_errno (EINVAL);
60 return -1;
61}
62
63libc_hidden_def (__inet6_scopeid_pton)
64