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