7aac8364d362a574bf33066b3ee1b2229967852e
[oweals/busybox.git] / networking / libiproute / ll_proto.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License
5  * as published by the Free Software Foundation; either version
6  * 2 of the License, or (at your option) any later version.
7  *
8  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  */
10
11 #include "libbb.h"
12 #include "rt_names.h"
13 #include "utils.h"
14
15 #include <netinet/if_ether.h>
16
17 #if !ENABLE_WERROR
18 #warning de-bloat
19 #endif
20 /* Before re-enabling this, please (1) conditionalize exotic protocols
21  * on CONFIG_something, and (2) decouple strings and numbers
22  * (use llproto_ids[] = n,n,n..; and llproto_names[] = "loop\0" "pup\0" ...;)
23  */
24
25 #define __PF(f,n) { ETH_P_##f, #n },
26 static struct {
27         int id;
28         const char *name;
29 } llproto_names[] = {
30 __PF(LOOP,loop)
31 __PF(PUP,pup)
32 #ifdef ETH_P_PUPAT
33 __PF(PUPAT,pupat)
34 #endif
35 __PF(IP,ip)
36 __PF(X25,x25)
37 __PF(ARP,arp)
38 __PF(BPQ,bpq)
39 #ifdef ETH_P_IEEEPUP
40 __PF(IEEEPUP,ieeepup)
41 #endif
42 #ifdef ETH_P_IEEEPUPAT
43 __PF(IEEEPUPAT,ieeepupat)
44 #endif
45 __PF(DEC,dec)
46 __PF(DNA_DL,dna_dl)
47 __PF(DNA_RC,dna_rc)
48 __PF(DNA_RT,dna_rt)
49 __PF(LAT,lat)
50 __PF(DIAG,diag)
51 __PF(CUST,cust)
52 __PF(SCA,sca)
53 __PF(RARP,rarp)
54 __PF(ATALK,atalk)
55 __PF(AARP,aarp)
56 __PF(IPX,ipx)
57 __PF(IPV6,ipv6)
58 #ifdef ETH_P_PPP_DISC
59 __PF(PPP_DISC,ppp_disc)
60 #endif
61 #ifdef ETH_P_PPP_SES
62 __PF(PPP_SES,ppp_ses)
63 #endif
64 #ifdef ETH_P_ATMMPOA
65 __PF(ATMMPOA,atmmpoa)
66 #endif
67 #ifdef ETH_P_ATMFATE
68 __PF(ATMFATE,atmfate)
69 #endif
70
71 __PF(802_3,802_3)
72 __PF(AX25,ax25)
73 __PF(ALL,all)
74 __PF(802_2,802_2)
75 __PF(SNAP,snap)
76 __PF(DDCMP,ddcmp)
77 __PF(WAN_PPP,wan_ppp)
78 __PF(PPP_MP,ppp_mp)
79 __PF(LOCALTALK,localtalk)
80 __PF(PPPTALK,ppptalk)
81 __PF(TR_802_2,tr_802_2)
82 __PF(MOBITEX,mobitex)
83 __PF(CONTROL,control)
84 __PF(IRDA,irda)
85 #ifdef ETH_P_ECONET
86 __PF(ECONET,econet)
87 #endif
88
89 { 0x8100, "802.1Q" },
90 { ETH_P_IP, "ipv4" },
91 };
92 #undef __PF
93
94
95 const char* FAST_FUNC ll_proto_n2a(unsigned short id, char *buf, int len)
96 {
97         unsigned i;
98         id = ntohs(id);
99         for (i = 0; i < ARRAY_SIZE(llproto_names); i++) {
100                  if (llproto_names[i].id == id)
101                         return llproto_names[i].name;
102         }
103         snprintf(buf, len, "[%d]", id);
104         return buf;
105 }
106
107 int FAST_FUNC ll_proto_a2n(unsigned short *id, char *buf)
108 {
109         unsigned i;
110         for (i = 0; i < ARRAY_SIZE(llproto_names); i++) {
111                  if (strcasecmp(llproto_names[i].name, buf) == 0) {
112                          i = llproto_names[i].id;
113                          goto good;
114                  }
115         }
116         i = bb_strtou(buf, NULL, 0);
117         if (errno || i > 0xffff)
118                 return -1;
119  good:
120         *id = htons(i);
121         return 0;
122 }