optioncodes.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict'
  2. exports.toString = function (type) {
  3. switch (type) {
  4. // list at
  5. // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-11
  6. case 1: return 'LLQ'
  7. case 2: return 'UL'
  8. case 3: return 'NSID'
  9. case 5: return 'DAU'
  10. case 6: return 'DHU'
  11. case 7: return 'N3U'
  12. case 8: return 'CLIENT_SUBNET'
  13. case 9: return 'EXPIRE'
  14. case 10: return 'COOKIE'
  15. case 11: return 'TCP_KEEPALIVE'
  16. case 12: return 'PADDING'
  17. case 13: return 'CHAIN'
  18. case 14: return 'KEY_TAG'
  19. case 26946: return 'DEVICEID'
  20. }
  21. if (type < 0) {
  22. return null
  23. }
  24. return `OPTION_${type}`
  25. }
  26. exports.toCode = function (name) {
  27. if (typeof name === 'number') {
  28. return name
  29. }
  30. if (!name) {
  31. return -1
  32. }
  33. switch (name.toUpperCase()) {
  34. case 'OPTION_0': return 0
  35. case 'LLQ': return 1
  36. case 'UL': return 2
  37. case 'NSID': return 3
  38. case 'OPTION_4': return 4
  39. case 'DAU': return 5
  40. case 'DHU': return 6
  41. case 'N3U': return 7
  42. case 'CLIENT_SUBNET': return 8
  43. case 'EXPIRE': return 9
  44. case 'COOKIE': return 10
  45. case 'TCP_KEEPALIVE': return 11
  46. case 'PADDING': return 12
  47. case 'CHAIN': return 13
  48. case 'KEY_TAG': return 14
  49. case 'DEVICEID': return 26946
  50. case 'OPTION_65535': return 65535
  51. }
  52. const m = name.match(/_(\d+)$/)
  53. if (m) {
  54. return parseInt(m[1], 10)
  55. }
  56. return -1
  57. }