PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/external/iptables/extensions/tos_values.c

http://droidwall.googlecode.com/
C | 96 lines | 71 code | 16 blank | 9 comment | 16 complexity | 23d81aca07ed73c528a71bfe77f0c90c MD5 | raw file
Possible License(s): GPL-2.0
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <linux/ip.h>
  5. #ifndef IPTOS_NORMALSVC
  6. # define IPTOS_NORMALSVC 0
  7. #endif
  8. struct tos_value_mask {
  9. uint8_t value, mask;
  10. };
  11. static const struct tos_symbol_info {
  12. unsigned char value;
  13. const char *name;
  14. } tos_symbol_names[] = {
  15. {IPTOS_LOWDELAY, "Minimize-Delay"},
  16. {IPTOS_THROUGHPUT, "Maximize-Throughput"},
  17. {IPTOS_RELIABILITY, "Maximize-Reliability"},
  18. {IPTOS_MINCOST, "Minimize-Cost"},
  19. {IPTOS_NORMALSVC, "Normal-Service"},
  20. {},
  21. };
  22. /*
  23. * tos_parse_numeric - parse sth. like "15/255"
  24. *
  25. * @s: input string
  26. * @info: accompanying structure
  27. * @bits: number of bits that are allowed
  28. * (8 for IPv4 TOS field, 4 for IPv6 Priority Field)
  29. */
  30. static bool tos_parse_numeric(const char *str, struct tos_value_mask *tvm,
  31. unsigned int bits)
  32. {
  33. const unsigned int max = (1 << bits) - 1;
  34. unsigned int value;
  35. char *end;
  36. xtables_strtoui(str, &end, &value, 0, max);
  37. tvm->value = value;
  38. tvm->mask = max;
  39. if (*end == '/') {
  40. const char *p = end + 1;
  41. if (!xtables_strtoui(p, &end, &value, 0, max))
  42. xtables_error(PARAMETER_PROBLEM, "Illegal value: \"%s\"",
  43. str);
  44. tvm->mask = value;
  45. }
  46. if (*end != '\0')
  47. xtables_error(PARAMETER_PROBLEM, "Illegal value: \"%s\"", str);
  48. return true;
  49. }
  50. static bool tos_parse_symbolic(const char *str, struct tos_value_mask *tvm,
  51. unsigned int def_mask)
  52. {
  53. const unsigned int max = UINT8_MAX;
  54. const struct tos_symbol_info *symbol;
  55. char *tmp;
  56. if (xtables_strtoui(str, &tmp, NULL, 0, max))
  57. return tos_parse_numeric(str, tvm, max);
  58. /* Do not consider ECN bits */
  59. tvm->mask = def_mask;
  60. for (symbol = tos_symbol_names; symbol->name != NULL; ++symbol)
  61. if (strcasecmp(str, symbol->name) == 0) {
  62. tvm->value = symbol->value;
  63. return true;
  64. }
  65. xtables_error(PARAMETER_PROBLEM, "Symbolic name \"%s\" is unknown", str);
  66. return false;
  67. }
  68. static bool tos_try_print_symbolic(const char *prefix,
  69. u_int8_t value, u_int8_t mask)
  70. {
  71. const struct tos_symbol_info *symbol;
  72. if (mask != 0x3F)
  73. return false;
  74. for (symbol = tos_symbol_names; symbol->name != NULL; ++symbol)
  75. if (value == symbol->value) {
  76. printf("%s%s ", prefix, symbol->name);
  77. return true;
  78. }
  79. return false;
  80. }