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