1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <errno.h>
4 #include <netinet/in.h>
5 #include <linux/if_arp.h>
6 #include <linux/if_infiniband.h>
7 #include <string.h>
8 
9 #include "arphrd-util.h"
10 #include "macro.h"
11 
12 static const struct arphrd_name* lookup_arphrd(register const char *str, register GPERF_LEN_TYPE len);
13 
14 #include "arphrd-from-name.h"
15 #include "arphrd-to-name.h"
16 
arphrd_from_name(const char * name)17 int arphrd_from_name(const char *name) {
18         const struct arphrd_name *sc;
19 
20         assert(name);
21 
22         sc = lookup_arphrd(name, strlen(name));
23         if (!sc)
24                 return -EINVAL;
25 
26         return sc->id;
27 }
28 
arphrd_to_hw_addr_len(uint16_t arphrd)29 size_t arphrd_to_hw_addr_len(uint16_t arphrd) {
30         switch (arphrd) {
31         case ARPHRD_ETHER:
32                 return ETH_ALEN;
33         case ARPHRD_INFINIBAND:
34                 return INFINIBAND_ALEN;
35         case ARPHRD_TUNNEL:
36         case ARPHRD_SIT:
37         case ARPHRD_IPGRE:
38                 return sizeof(struct in_addr);
39         case ARPHRD_TUNNEL6:
40         case ARPHRD_IP6GRE:
41                 return sizeof(struct in6_addr);
42         default:
43                 return 0;
44         }
45 }
46