ll_proto.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include <netinet/if_ether.h>
  14. #if !ENABLE_WERROR
  15. #warning de-bloat
  16. #endif
  17. /* Before re-enabling this, please (1) conditionalize exotic protocols
  18. * on CONFIG_something, and (2) decouple strings and numbers
  19. * (use llproto_ids[] = n,n,n..; and llproto_names[] = "loop\0" "pup\0" ...;)
  20. */
  21. #define __PF(f,n) { ETH_P_##f, #n },
  22. static struct {
  23. int id;
  24. const char *name;
  25. } llproto_names[] = {
  26. __PF(LOOP,loop)
  27. __PF(PUP,pup)
  28. #ifdef ETH_P_PUPAT
  29. __PF(PUPAT,pupat)
  30. #endif
  31. __PF(IP,ip)
  32. __PF(X25,x25)
  33. __PF(ARP,arp)
  34. __PF(BPQ,bpq)
  35. #ifdef ETH_P_IEEEPUP
  36. __PF(IEEEPUP,ieeepup)
  37. #endif
  38. #ifdef ETH_P_IEEEPUPAT
  39. __PF(IEEEPUPAT,ieeepupat)
  40. #endif
  41. __PF(DEC,dec)
  42. __PF(DNA_DL,dna_dl)
  43. __PF(DNA_RC,dna_rc)
  44. __PF(DNA_RT,dna_rt)
  45. __PF(LAT,lat)
  46. __PF(DIAG,diag)
  47. __PF(CUST,cust)
  48. __PF(SCA,sca)
  49. __PF(RARP,rarp)
  50. __PF(ATALK,atalk)
  51. __PF(AARP,aarp)
  52. __PF(IPX,ipx)
  53. __PF(IPV6,ipv6)
  54. #ifdef ETH_P_PPP_DISC
  55. __PF(PPP_DISC,ppp_disc)
  56. #endif
  57. #ifdef ETH_P_PPP_SES
  58. __PF(PPP_SES,ppp_ses)
  59. #endif
  60. #ifdef ETH_P_ATMMPOA
  61. __PF(ATMMPOA,atmmpoa)
  62. #endif
  63. #ifdef ETH_P_ATMFATE
  64. __PF(ATMFATE,atmfate)
  65. #endif
  66. __PF(802_3,802_3)
  67. __PF(AX25,ax25)
  68. __PF(ALL,all)
  69. __PF(802_2,802_2)
  70. __PF(SNAP,snap)
  71. __PF(DDCMP,ddcmp)
  72. __PF(WAN_PPP,wan_ppp)
  73. __PF(PPP_MP,ppp_mp)
  74. __PF(LOCALTALK,localtalk)
  75. __PF(PPPTALK,ppptalk)
  76. __PF(TR_802_2,tr_802_2)
  77. __PF(MOBITEX,mobitex)
  78. __PF(CONTROL,control)
  79. __PF(IRDA,irda)
  80. #ifdef ETH_P_ECONET
  81. __PF(ECONET,econet)
  82. #endif
  83. { 0x8100, "802.1Q" },
  84. { ETH_P_IP, "ipv4" },
  85. };
  86. #undef __PF
  87. const char* FAST_FUNC ll_proto_n2a(unsigned short id, char *buf, int len)
  88. {
  89. unsigned i;
  90. id = ntohs(id);
  91. for (i = 0; i < ARRAY_SIZE(llproto_names); i++) {
  92. if (llproto_names[i].id == id)
  93. return llproto_names[i].name;
  94. }
  95. snprintf(buf, len, "[%d]", id);
  96. return buf;
  97. }
  98. int FAST_FUNC ll_proto_a2n(unsigned short *id, char *buf)
  99. {
  100. unsigned i;
  101. for (i = 0; i < ARRAY_SIZE(llproto_names); i++) {
  102. if (strcasecmp(llproto_names[i].name, buf) == 0) {
  103. i = llproto_names[i].id;
  104. goto good;
  105. }
  106. }
  107. i = bb_strtou(buf, NULL, 0);
  108. if (errno || i > 0xffff)
  109. return -1;
  110. good:
  111. *id = htons(i);
  112. return 0;
  113. }