ip: code shrink
[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 /* Please conditionalize exotic protocols on CONFIG_something */
18
19 static const uint16_t llproto_ids[] = {
20 #define __PF(f,n) ETH_P_##f,
21 __PF(LOOP,loop)
22 __PF(PUP,pup)
23 #ifdef ETH_P_PUPAT
24 __PF(PUPAT,pupat)
25 #endif
26 __PF(IP,ip)
27 __PF(X25,x25)
28 __PF(ARP,arp)
29 __PF(BPQ,bpq)
30 #ifdef ETH_P_IEEEPUP
31 __PF(IEEEPUP,ieeepup)
32 #endif
33 #ifdef ETH_P_IEEEPUPAT
34 __PF(IEEEPUPAT,ieeepupat)
35 #endif
36 __PF(DEC,dec)
37 __PF(DNA_DL,dna_dl)
38 __PF(DNA_RC,dna_rc)
39 __PF(DNA_RT,dna_rt)
40 __PF(LAT,lat)
41 __PF(DIAG,diag)
42 __PF(CUST,cust)
43 __PF(SCA,sca)
44 __PF(RARP,rarp)
45 __PF(ATALK,atalk)
46 __PF(AARP,aarp)
47 __PF(IPX,ipx)
48 __PF(IPV6,ipv6)
49 #ifdef ETH_P_PPP_DISC
50 __PF(PPP_DISC,ppp_disc)
51 #endif
52 #ifdef ETH_P_PPP_SES
53 __PF(PPP_SES,ppp_ses)
54 #endif
55 #ifdef ETH_P_ATMMPOA
56 __PF(ATMMPOA,atmmpoa)
57 #endif
58 #ifdef ETH_P_ATMFATE
59 __PF(ATMFATE,atmfate)
60 #endif
61
62 __PF(802_3,802_3)
63 __PF(AX25,ax25)
64 __PF(ALL,all)
65 __PF(802_2,802_2)
66 __PF(SNAP,snap)
67 __PF(DDCMP,ddcmp)
68 __PF(WAN_PPP,wan_ppp)
69 __PF(PPP_MP,ppp_mp)
70 __PF(LOCALTALK,localtalk)
71 __PF(PPPTALK,ppptalk)
72 __PF(TR_802_2,tr_802_2)
73 __PF(MOBITEX,mobitex)
74 __PF(CONTROL,control)
75 __PF(IRDA,irda)
76 #ifdef ETH_P_ECONET
77 __PF(ECONET,econet)
78 #endif
79
80 0x8100,
81 ETH_P_IP
82 };
83 #undef __PF
84
85 /* Keep declarations above and below in sync! */
86
87 static const char llproto_names[] =
88 #define __PF(f,n) #n "\0"
89 __PF(LOOP,loop)
90 __PF(PUP,pup)
91 #ifdef ETH_P_PUPAT
92 __PF(PUPAT,pupat)
93 #endif
94 __PF(IP,ip)
95 __PF(X25,x25)
96 __PF(ARP,arp)
97 __PF(BPQ,bpq)
98 #ifdef ETH_P_IEEEPUP
99 __PF(IEEEPUP,ieeepup)
100 #endif
101 #ifdef ETH_P_IEEEPUPAT
102 __PF(IEEEPUPAT,ieeepupat)
103 #endif
104 __PF(DEC,dec)
105 __PF(DNA_DL,dna_dl)
106 __PF(DNA_RC,dna_rc)
107 __PF(DNA_RT,dna_rt)
108 __PF(LAT,lat)
109 __PF(DIAG,diag)
110 __PF(CUST,cust)
111 __PF(SCA,sca)
112 __PF(RARP,rarp)
113 __PF(ATALK,atalk)
114 __PF(AARP,aarp)
115 __PF(IPX,ipx)
116 __PF(IPV6,ipv6)
117 #ifdef ETH_P_PPP_DISC
118 __PF(PPP_DISC,ppp_disc)
119 #endif
120 #ifdef ETH_P_PPP_SES
121 __PF(PPP_SES,ppp_ses)
122 #endif
123 #ifdef ETH_P_ATMMPOA
124 __PF(ATMMPOA,atmmpoa)
125 #endif
126 #ifdef ETH_P_ATMFATE
127 __PF(ATMFATE,atmfate)
128 #endif
129
130 __PF(802_3,802_3)
131 __PF(AX25,ax25)
132 __PF(ALL,all)
133 __PF(802_2,802_2)
134 __PF(SNAP,snap)
135 __PF(DDCMP,ddcmp)
136 __PF(WAN_PPP,wan_ppp)
137 __PF(PPP_MP,ppp_mp)
138 __PF(LOCALTALK,localtalk)
139 __PF(PPPTALK,ppptalk)
140 __PF(TR_802_2,tr_802_2)
141 __PF(MOBITEX,mobitex)
142 __PF(CONTROL,control)
143 __PF(IRDA,irda)
144 #ifdef ETH_P_ECONET
145 __PF(ECONET,econet)
146 #endif
147
148 "802.1Q" "\0"
149 "ipv4" "\0"
150 ;
151 #undef __PF
152
153
154 const char* FAST_FUNC ll_proto_n2a(unsigned short id, char *buf, int len)
155 {
156         unsigned i;
157         id = ntohs(id);
158         for (i = 0; i < ARRAY_SIZE(llproto_ids); i++) {
159                 if (llproto_ids[i] == id)
160                         return nth_string(llproto_names, i);
161         }
162         snprintf(buf, len, "[%u]", id);
163         return buf;
164 }
165
166 int FAST_FUNC ll_proto_a2n(unsigned short *id, char *buf)
167 {
168         unsigned i;
169         const char *name = llproto_names;
170         for (i = 0; i < ARRAY_SIZE(llproto_ids); i++) {
171                 if (strcasecmp(name, buf) == 0) {
172                         i = llproto_ids[i];
173                         goto good;
174                 }
175                 name += strlen(name) + 1;
176         }
177         errno = 0;
178         i = bb_strtou(buf, NULL, 0);
179         if (errno || i > 0xffff)
180                 return -1;
181  good:
182         *id = htons(i);
183         return 0;
184 }