Home | History | Annotate | Download | only in lib
      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 (at) ms2.inr.ac.ru>
     10  */
     11 
     12 #include <stdio.h>
     13 #include <stdlib.h>
     14 #include <unistd.h>
     15 #include <syslog.h>
     16 #include <fcntl.h>
     17 #include <sys/ioctl.h>
     18 #include <sys/socket.h>
     19 #include <netinet/in.h>
     20 #include <arpa/inet.h>
     21 #include <string.h>
     22 
     23 #include <linux/netdevice.h>
     24 #include <linux/if_arp.h>
     25 #include <linux/sockios.h>
     26 
     27 #include "utils.h"
     28 #include "rt_names.h"
     29 
     30 
     31 #define __PF(f,n) { ETH_P_##f, #n },
     32 static const struct {
     33 	int id;
     34 	const char *name;
     35 } llproto_names[] = {
     36 __PF(LOOP,loop)
     37 __PF(PUP,pup)
     38 __PF(PUPAT,pupat)
     39 __PF(IP,ip)
     40 __PF(X25,x25)
     41 __PF(ARP,arp)
     42 __PF(BPQ,bpq)
     43 __PF(IEEEPUP,ieeepup)
     44 __PF(IEEEPUPAT,ieeepupat)
     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 __PF(PPP_DISC,ppp_disc)
     59 __PF(PPP_SES,ppp_ses)
     60 __PF(ATMMPOA,atmmpoa)
     61 __PF(ATMFATE,atmfate)
     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(CAN,can)
     72 __PF(PPPTALK,ppptalk)
     73 __PF(TR_802_2,tr_802_2)
     74 __PF(MOBITEX,mobitex)
     75 __PF(CONTROL,control)
     76 __PF(IRDA,irda)
     77 __PF(ECONET,econet)
     78 __PF(TIPC,tipc)
     79 __PF(AOE,aoe)
     80 __PF(8021Q,802.1Q)
     81 __PF(8021AD,802.1ad)
     82 
     83 { 0x8100, "802.1Q" },
     84 { 0x88cc, "LLDP" },
     85 { ETH_P_IP, "ipv4" },
     86 };
     87 #undef __PF
     88 
     89 
     90 const char * ll_proto_n2a(unsigned short id, char *buf, int len)
     91 {
     92         int i;
     93 
     94 	id = ntohs(id);
     95 
     96         for (i=0; i<sizeof(llproto_names)/sizeof(llproto_names[0]); i++) {
     97                  if (llproto_names[i].id == id)
     98 			return llproto_names[i].name;
     99 	}
    100         snprintf(buf, len, "[%d]", id);
    101         return buf;
    102 }
    103 
    104 int ll_proto_a2n(unsigned short *id, const char *buf)
    105 {
    106         int i;
    107         for (i=0; i < sizeof(llproto_names)/sizeof(llproto_names[0]); i++) {
    108                  if (strcasecmp(llproto_names[i].name, buf) == 0) {
    109 			 *id = htons(llproto_names[i].id);
    110 			 return 0;
    111 		 }
    112 	}
    113 	if (get_be16(id, buf, 0))
    114 		return -1;
    115 	return 0;
    116 }
    117