ip_addr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * ip address & address family related functions
  6. *
  7. * Copyright (C) 2001-2003 Fhg Fokus
  8. *
  9. * This file is part of ser, a free SIP server.
  10. *
  11. * ser is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * For a license to use the ser software under conditions
  17. * other than those described here, or to purchase support for this
  18. * software, please contact iptel.org by e-mail at the following addresses:
  19. * [email protected]
  20. *
  21. * ser is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include "ip_addr.h"
  33. #include "dprint.h"
  34. struct net* mk_net(struct ip_addr* ip, struct ip_addr* mask)
  35. {
  36. struct net* n;
  37. if ((ip->af != mask->af) || (ip->len != mask->len)){
  38. LOG(L_CRIT, "ERROR: mk_net: trying to use a different mask family"
  39. " (eg. ipv4/ipv6mask or ipv6/ipv4mask)\n");
  40. goto error;
  41. }
  42. n=(struct net*)malloc(sizeof(struct net));
  43. if (n==0){
  44. LOG(L_CRIT, "ERROR: mk_net: memory allocation failure\n");
  45. goto error;
  46. }
  47. n->ip=*ip;
  48. n->mask=*mask;
  49. return n;
  50. error:
  51. return 0;
  52. }
  53. struct net* mk_net_bitlen(struct ip_addr* ip, unsigned int bitlen)
  54. {
  55. struct net* n;
  56. int r;
  57. if (bitlen>ip->len*8){
  58. LOG(L_CRIT, "ERROR: mk_net_bitlen: bad bitlen number %d\n", bitlen);
  59. goto error;
  60. }
  61. n=(struct net*)malloc(sizeof(struct net));
  62. if (n==0){
  63. LOG(L_CRIT, "ERROR: mk_net_bitlen: memory allocation failure\n");
  64. goto error;
  65. }
  66. memset(n,0, sizeof(struct net));
  67. n->ip=*ip;
  68. for (r=0;r<bitlen/8;r++) n->mask.u.addr[r]=0xff;
  69. if (bitlen%8) n->mask.u.addr[r]= ~((1<<(8-(bitlen%8)))-1);
  70. n->mask.af=ip->af;
  71. n->mask.len=ip->len;
  72. return n;
  73. error:
  74. return 0;
  75. }
  76. void print_ip(struct ip_addr* ip)
  77. {
  78. switch(ip->af){
  79. case AF_INET:
  80. DBG("%d.%d.%d.%d", ip->u.addr[0],
  81. ip->u.addr[1],
  82. ip->u.addr[2],
  83. ip->u.addr[3]);
  84. break;
  85. case AF_INET6:
  86. DBG("%x:%x:%x:%x:%x:%x:%x:%x", htons(ip->u.addr16[0]),
  87. htons(ip->u.addr16[1]),
  88. htons(ip->u.addr16[2]),
  89. htons(ip->u.addr16[3]),
  90. htons(ip->u.addr16[4]),
  91. htons(ip->u.addr16[5]),
  92. htons(ip->u.addr16[6]),
  93. htons(ip->u.addr16[7])
  94. );
  95. break;
  96. default:
  97. DBG("print_ip: warning unknown adress family %d\n", ip->af);
  98. }
  99. }
  100. void stdout_print_ip(struct ip_addr* ip)
  101. {
  102. switch(ip->af){
  103. case AF_INET:
  104. printf("%d.%d.%d.%d", ip->u.addr[0],
  105. ip->u.addr[1],
  106. ip->u.addr[2],
  107. ip->u.addr[3]);
  108. break;
  109. case AF_INET6:
  110. printf("%x:%x:%x:%x:%x:%x:%x:%x", htons(ip->u.addr16[0]),
  111. htons(ip->u.addr16[1]),
  112. htons(ip->u.addr16[2]),
  113. htons(ip->u.addr16[3]),
  114. htons(ip->u.addr16[4]),
  115. htons(ip->u.addr16[5]),
  116. htons(ip->u.addr16[6]),
  117. htons(ip->u.addr16[7])
  118. );
  119. break;
  120. default:
  121. DBG("print_ip: warning unknown adress family %d\n", ip->af);
  122. }
  123. }
  124. void print_net(struct net* net)
  125. {
  126. if (net==0){
  127. LOG(L_WARN, "ERROR: print net: null pointer\n");
  128. return;
  129. }
  130. print_ip(&net->ip); DBG("/"); print_ip(&net->mask);
  131. }