common.awk 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright (C) 2006 OpenWrt.org
  2. function portstr(type, str) {
  3. gsub(/-/, ":", str)
  4. if (insmod_mport != 1) {
  5. print "insmod ipt_multiport >&- 2>&-"
  6. insmod_mport = 1
  7. }
  8. if (type == "src") return " -m multiport --sports " str
  9. else return " -m multiport --dports " str
  10. }
  11. function str2ipt(str) {
  12. str2data(str)
  13. _cmd = ""
  14. if (_l["src"] != "") _cmd = _cmd " -s " _l["src"]
  15. if (_l["dest"] != "") _cmd = _cmd " -d " _l["dest"]
  16. if (_l["proto"] != "") {
  17. _cmd = _cmd " -p " _l["proto"]
  18. }
  19. # scripts need to check for proto="" and emit two rules in that case
  20. if ((_l["proto"] == "") || (_l["proto"] == "tcp") || (_l["proto"] == "udp")) {
  21. if (_l["sport"] != "") _cmd = _cmd portstr("src", _l["sport"])
  22. if (_l["dport"] != "") _cmd = _cmd portstr("dest", _l["dport"])
  23. }
  24. if (_l["layer7"] != "") {
  25. if (insmod_l7 != 1) {
  26. print "insmod ipt_layer7 >&- 2>&-"
  27. insmod_l7 = 1
  28. }
  29. _cmd = _cmd " -m layer7 --l7proto " _l["layer7"]
  30. }
  31. return _cmd
  32. }
  33. function str2data(str) {
  34. delete _l
  35. _n = split(str, _o, "[\t ]")
  36. for (_i = 1; _i <= _n; _i++) {
  37. _n2 = split(_o[_i], _c, "=")
  38. if (_n2 == 2) _l[_c[1]] = _c[2]
  39. }
  40. }
  41. function bitcount(c) {
  42. c=and(rshift(c, 1),0x55555555)+and(c,0x55555555)
  43. c=and(rshift(c, 2),0x33333333)+and(c,0x33333333)
  44. c=and(rshift(c, 4),0x0f0f0f0f)+and(c,0x0f0f0f0f)
  45. c=and(rshift(c, 8),0x00ff00ff)+and(c,0x00ff00ff)
  46. c=and(rshift(c,16),0x0000ffff)+and(c,0x0000ffff)
  47. return c
  48. }
  49. function validate_netmask(nm) {
  50. return and(-nm,compl(nm))
  51. }
  52. function ip2int(ip) {
  53. for (ret=0,n=split(ip,a,"\."),x=1;x<=n;x++) ret=or(lshift(ret,8),a[x])
  54. return ret
  55. }
  56. function int2ip(ip,ret,x) {
  57. ret=and(ip,255)
  58. ip=rshift(ip,8)
  59. for(;x<3;ret=and(ip,255)"."ret,ip=rshift(ip,8),x++);
  60. return ret
  61. }