1 /* Convert an IPv6 scope ID from text to the internal representation.
2    Copyright (C) 2016-2021 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    <https://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.  */
31 int
__inet6_scopeid_pton(const struct in6_addr * address,const char * scope,uint32_t * result)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_NODELOCAL (address)
37       || IN6_IS_ADDR_MC_LINKLOCAL (address))
38     {
39       uint32_t number = __if_nametoindex (scope);
40       if (number != 0)
41         {
42           *result = number;
43           return 0;
44         }
45     }
46 
47   if (isdigit_l (scope[0], _nl_C_locobj_ptr))
48     {
49       char *end;
50       unsigned long long number
51         = ____strtoull_l_internal (scope, &end, /*base */ 10, /* group */ 0,
52                                    _nl_C_locobj_ptr);
53       if (*end == '\0' && number <= UINT32_MAX)
54         {
55           *result = number;
56           return 0;
57         }
58     }
59 
60   __set_errno (EINVAL);
61   return -1;
62 }
63 
64 libc_hidden_def (__inet6_scopeid_pton)
65