3
0

ll_proto.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include "libbb.h"
  11. #include "rt_names.h"
  12. #include "utils.h"
  13. #if defined(__GLIBC__) && __GLIBC__ >=2 && __GLIBC_MINOR__ >= 1
  14. #include <net/ethernet.h>
  15. #else
  16. #include <linux/if_ether.h>
  17. #endif
  18. #if !ENABLE_WERROR
  19. #warning de-bloat
  20. #endif
  21. /* Before re-enabling this, please (1) conditionalize exotic protocols
  22. * on CONFIG_something, and (2) decouple strings and numbers
  23. * (use llproto_ids[] = n,n,n..; and llproto_names[] = "loop\0" "pup\0" ...;)
  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. __PF(802_3,802_3)
  71. __PF(AX25,ax25)
  72. __PF(ALL,all)
  73. __PF(802_2,802_2)
  74. __PF(SNAP,snap)
  75. __PF(DDCMP,ddcmp)
  76. __PF(WAN_PPP,wan_ppp)
  77. __PF(PPP_MP,ppp_mp)
  78. __PF(LOCALTALK,localtalk)
  79. __PF(PPPTALK,ppptalk)
  80. __PF(TR_802_2,tr_802_2)
  81. __PF(MOBITEX,mobitex)
  82. __PF(CONTROL,control)
  83. __PF(IRDA,irda)
  84. #ifdef ETH_P_ECONET
  85. __PF(ECONET,econet)
  86. #endif
  87. { 0x8100, "802.1Q" },
  88. { ETH_P_IP, "ipv4" },
  89. };
  90. #undef __PF
  91. const char* FAST_FUNC ll_proto_n2a(unsigned short id, char *buf, int len)
  92. {
  93. unsigned i;
  94. id = ntohs(id);
  95. for (i = 0; i < ARRAY_SIZE(llproto_names); i++) {
  96. if (llproto_names[i].id == id)
  97. return llproto_names[i].name;
  98. }
  99. snprintf(buf, len, "[%d]", id);
  100. return buf;
  101. }
  102. int FAST_FUNC ll_proto_a2n(unsigned short *id, char *buf)
  103. {
  104. unsigned i;
  105. for (i = 0; i < ARRAY_SIZE(llproto_names); i++) {
  106. if (strcasecmp(llproto_names[i].name, buf) == 0) {
  107. i = llproto_names[i].id;
  108. goto good;
  109. }
  110. }
  111. i = bb_strtou(buf, NULL, 0);
  112. if (errno || i > 0xffff)
  113. return -1;
  114. good:
  115. *id = htons(i);
  116. return 0;
  117. }