api.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * $Id$
  3. *
  4. * Functions that process IPOPS message
  5. *
  6. * Copyright (C) 2012 Hugh Waite (crocodile-rcs.com)
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. */
  25. #include <stdio.h>
  26. #include "api.h"
  27. #include "ip_parser.h"
  28. extern int _compare_ips(char*, size_t, enum enum_ip_type, char*, size_t, enum enum_ip_type);
  29. extern int _ip_is_in_subnet(char *ip1, size_t len1, enum enum_ip_type ip1_type, char *ip2, size_t len2, enum enum_ip_type ip2_type, int netmask);
  30. /**
  31. *
  32. */
  33. int ipopsapi_compare_ips(const str *const ip1, const str *const ip2)
  34. {
  35. str string1 = *ip1;
  36. str string2 = *ip2;
  37. enum enum_ip_type ip1_type, ip2_type;
  38. switch(ip1_type = ip_parser_execute(string1.s, string1.len)) {
  39. case(ip_type_error):
  40. return -1;
  41. break;
  42. case(ip_type_ipv6_reference):
  43. string1.s += 1;
  44. string1.len -= 2;
  45. ip1_type = ip_type_ipv6;
  46. break;
  47. default:
  48. break;
  49. }
  50. switch(ip2_type = ip_parser_execute(string2.s, string2.len)) {
  51. case(ip_type_error):
  52. return -1;
  53. break;
  54. case(ip_type_ipv6_reference):
  55. string2.s += 1;
  56. string2.len -= 2;
  57. ip2_type = ip_type_ipv6;
  58. break;
  59. default:
  60. break;
  61. }
  62. if (_compare_ips(string1.s, string1.len, ip1_type, string2.s, string2.len, ip2_type))
  63. return 1;
  64. else
  65. return -1;
  66. }
  67. /**
  68. *
  69. */
  70. int ipopsapi_ip_is_in_subnet(const str *const ip1, const str *const ip2)
  71. {
  72. str string1 = *ip1;
  73. str string2 = *ip2;
  74. enum enum_ip_type ip1_type, ip2_type;
  75. char *cidr_pos = NULL;
  76. int netmask = 0;
  77. switch(ip1_type = ip_parser_execute(string1.s, string1.len)) {
  78. case(ip_type_error):
  79. return -1;
  80. break;
  81. case(ip_type_ipv6_reference):
  82. return -1;
  83. break;
  84. default:
  85. break;
  86. }
  87. cidr_pos = string2.s + string2.len - 1;
  88. while (cidr_pos > string2.s)
  89. {
  90. if (*cidr_pos == '/') break;
  91. cidr_pos--;
  92. }
  93. if (cidr_pos == string2.s) return -1;
  94. string2.len = (cidr_pos - string2.s);
  95. netmask = atoi(cidr_pos+1);
  96. switch(ip2_type = ip_parser_execute(string2.s, string2.len)) {
  97. case(ip_type_error):
  98. return -1;
  99. break;
  100. case(ip_type_ipv6_reference):
  101. return -1;
  102. break;
  103. default:
  104. break;
  105. }
  106. if (_ip_is_in_subnet(string1.s, string1.len, ip1_type, string2.s, string2.len, ip2_type, netmask))
  107. return 1;
  108. else
  109. return -1;
  110. }
  111. /**
  112. *
  113. */
  114. int ipopsapi_is_ip(const str * const ip)
  115. {
  116. if (ip_parser_execute(ip->s, ip->len) != ip_type_error)
  117. return 1;
  118. else
  119. return -1;
  120. }
  121. /**
  122. *
  123. */
  124. int bind_ipops(ipops_api_t* api)
  125. {
  126. if (!api) {
  127. ERR("Invalid parameter value\n");
  128. return -1;
  129. }
  130. api->compare_ips = ipopsapi_compare_ips;
  131. api->ip_is_in_subnet = ipopsapi_ip_is_in_subnet;
  132. api->is_ip = ipopsapi_is_ip;
  133. return 0;
  134. }