ip_addr.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. /*
  31. * History:
  32. * --------
  33. * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free
  34. * 2004-10-01 mk_net fixes bad network addresses now (andrei)
  35. */
  36. /** inernal ip addresses representation functions.
  37. * @file ip_addr.c
  38. * @ingroup core
  39. * Module: @ref core
  40. */
  41. #include <stdlib.h>
  42. #include <stdio.h>
  43. #include "ip_addr.h"
  44. #include "dprint.h"
  45. #include "mem/mem.h"
  46. #include "resolve.h"
  47. #include "trim.h"
  48. struct net* mk_new_net(struct ip_addr* ip, struct ip_addr* mask)
  49. {
  50. struct net* n;
  51. int warning;
  52. int r;
  53. warning=0;
  54. if ((ip->af != mask->af) || (ip->len != mask->len)){
  55. LM_CRIT("trying to use a different mask family"
  56. " (eg. ipv4/ipv6mask or ipv6/ipv4mask)\n");
  57. goto error;
  58. }
  59. n=(struct net*)pkg_malloc(sizeof(struct net));
  60. if (n==0){
  61. LM_CRIT("memory allocation failure\n");
  62. goto error;
  63. }
  64. n->ip=*ip;
  65. n->mask=*mask;
  66. for (r=0; r<n->ip.len/4; r++) { /*ipv4 & ipv6 addresses are multiple of 4*/
  67. n->ip.u.addr32[r] &= n->mask.u.addr32[r];
  68. if (n->ip.u.addr32[r]!=ip->u.addr32[r]) warning=1;
  69. };
  70. if (warning){
  71. LM_WARN("invalid network address/netmask "
  72. "combination fixed...\n");
  73. print_ip("original network address:", ip, "/");
  74. print_ip("", mask, "\n");
  75. print_ip("fixed network address:", &(n->ip), "/");
  76. print_ip("", &(n->mask), "\n");
  77. };
  78. return n;
  79. error:
  80. return 0;
  81. }
  82. struct net* mk_new_net_bitlen(struct ip_addr* ip, unsigned int bitlen)
  83. {
  84. struct ip_addr mask;
  85. int r;
  86. if (bitlen>ip->len*8){
  87. LM_CRIT("bad bitlen number %d\n", bitlen);
  88. goto error;
  89. }
  90. memset(&mask,0, sizeof(mask));
  91. for (r=0;r<bitlen/8;r++) mask.u.addr[r]=0xff;
  92. if (bitlen%8) mask.u.addr[r]= ~((1<<(8-(bitlen%8)))-1);
  93. mask.af=ip->af;
  94. mask.len=ip->len;
  95. return mk_new_net(ip, &mask);
  96. error:
  97. return 0;
  98. }
  99. /** fills a net structure from an ip and a mask.
  100. *
  101. * This function will not print any error messages or allocate
  102. * memory (as opposed to mk_new_net() above).
  103. *
  104. * @param n - destination net structure
  105. * @param ip
  106. * @param mask
  107. * @return -1 on error (af mismatch), 0 on success
  108. */
  109. int mk_net(struct net* n, struct ip_addr* ip, struct ip_addr* mask)
  110. {
  111. int r;
  112. if (unlikely((ip->af != mask->af) || (ip->len != mask->len))) {
  113. return -1;
  114. }
  115. n->ip=*ip;
  116. n->mask=*mask;
  117. /* fix the network part of the mask */
  118. for (r=0; r<n->ip.len/4; r++) { /*ipv4 & ipv6 addresses are multiple of 4*/
  119. n->ip.u.addr32[r] &= n->mask.u.addr32[r];
  120. };
  121. return 0;
  122. }
  123. /** fills a net structure from an ip and a bitlen.
  124. *
  125. * This function will not print any error messages or allocate
  126. * memory (as opposed to mk_new_net_bitlen() above).
  127. *
  128. * @param n - destination net structure
  129. * @param ip
  130. * @param bitlen
  131. * @return -1 on error (af mismatch), 0 on success
  132. */
  133. int mk_net_bitlen(struct net* n, struct ip_addr* ip, unsigned int bitlen)
  134. {
  135. struct ip_addr mask;
  136. int r;
  137. if (unlikely(bitlen>ip->len*8))
  138. /* bitlen too big */
  139. return -1;
  140. memset(&mask,0, sizeof(mask));
  141. for (r=0;r<bitlen/8;r++) mask.u.addr[r]=0xff;
  142. if (bitlen%8) mask.u.addr[r]= ~((1<<(8-(bitlen%8)))-1);
  143. mask.af=ip->af;
  144. mask.len=ip->len;
  145. return mk_net(n, ip, &mask);
  146. }
  147. /** initializes a net structure from a string.
  148. * @param dst - net structure that will be filled
  149. * @param s - string of the form "ip", "ip/mask_len" or "ip/ip_mak".
  150. * @return -1 on error, 0 on succes
  151. */
  152. int mk_net_str(struct net* dst, str* s)
  153. {
  154. struct ip_addr* t;
  155. char* p;
  156. struct ip_addr ip;
  157. str addr;
  158. str mask;
  159. unsigned int bitlen;
  160. /* test for ip only */
  161. t = str2ip(s);
  162. if (unlikely(t == 0))
  163. t = str2ip6(s);
  164. if (likely(t))
  165. return mk_net_bitlen(dst, t, t->len*8);
  166. /* not a simple ip, maybe an ip/netmask pair */
  167. p = q_memchr(s->s, '/', s->len);
  168. if (likely(p)) {
  169. addr.s = s->s;
  170. addr.len = (int)(long)(p - s->s);
  171. mask.s = p + 1;
  172. mask.len = s->len - (addr.len + 1);
  173. /* allow '/' enclosed by whitespace */
  174. trim_trailing(&addr);
  175. trim_leading(&mask);
  176. t = str2ip(&addr);
  177. if (likely(t)) {
  178. /* it can be a number */
  179. if (str2int(&mask, &bitlen) == 0)
  180. return mk_net_bitlen(dst, t, bitlen);
  181. ip = *t;
  182. t = str2ip(&mask);
  183. if (likely(t))
  184. return mk_net(dst, &ip, t);
  185. /* error */
  186. return -1;
  187. }
  188. else {
  189. t = str2ip6(&addr);
  190. if (likely(t)) {
  191. /* it can be a number */
  192. if (str2int(&mask, &bitlen) == 0)
  193. return mk_net_bitlen(dst, t, bitlen);
  194. ip = *t;
  195. t = str2ip6(&mask);
  196. if (likely(t))
  197. return mk_net(dst, &ip, t);
  198. /* error */
  199. return -1;
  200. }
  201. }
  202. }
  203. return -1;
  204. }
  205. void print_ip(char* p, struct ip_addr* ip, char *s)
  206. {
  207. switch(ip->af){
  208. case AF_INET:
  209. DBG("%s%d.%d.%d.%d%s", (p)?p:"",
  210. ip->u.addr[0],
  211. ip->u.addr[1],
  212. ip->u.addr[2],
  213. ip->u.addr[3],
  214. (s)?s:""
  215. );
  216. break;
  217. case AF_INET6:
  218. DBG("%s%x:%x:%x:%x:%x:%x:%x:%x%s", (p)?p:"",
  219. htons(ip->u.addr16[0]),
  220. htons(ip->u.addr16[1]),
  221. htons(ip->u.addr16[2]),
  222. htons(ip->u.addr16[3]),
  223. htons(ip->u.addr16[4]),
  224. htons(ip->u.addr16[5]),
  225. htons(ip->u.addr16[6]),
  226. htons(ip->u.addr16[7]),
  227. (s)?s:""
  228. );
  229. break;
  230. default:
  231. DBG("print_ip: warning unknown address family %d\n", ip->af);
  232. }
  233. }
  234. void stdout_print_ip(struct ip_addr* ip)
  235. {
  236. switch(ip->af){
  237. case AF_INET:
  238. printf("%d.%d.%d.%d", ip->u.addr[0],
  239. ip->u.addr[1],
  240. ip->u.addr[2],
  241. ip->u.addr[3]);
  242. break;
  243. case AF_INET6:
  244. printf("%x:%x:%x:%x:%x:%x:%x:%x", htons(ip->u.addr16[0]),
  245. htons(ip->u.addr16[1]),
  246. htons(ip->u.addr16[2]),
  247. htons(ip->u.addr16[3]),
  248. htons(ip->u.addr16[4]),
  249. htons(ip->u.addr16[5]),
  250. htons(ip->u.addr16[6]),
  251. htons(ip->u.addr16[7])
  252. );
  253. break;
  254. default:
  255. DBG("print_ip: warning unknown address family %d\n", ip->af);
  256. }
  257. }
  258. void print_net(struct net* net)
  259. {
  260. if (net==0){
  261. LM_WARN("null pointer\n");
  262. return;
  263. }
  264. print_ip("", &net->ip, "/"); print_ip("", &net->mask, "");
  265. }
  266. #ifdef USE_MCAST
  267. /* Returns 1 if the given address is a multicast address */
  268. int is_mcast(struct ip_addr* ip)
  269. {
  270. if (!ip){
  271. LM_ERR("Invalid parameter value\n");
  272. return -1;
  273. }
  274. if (ip->af==AF_INET){
  275. return IN_MULTICAST(htonl(ip->u.addr32[0]));
  276. } else if (ip->af==AF_INET6){
  277. return IN6_IS_ADDR_MULTICAST((struct in6_addr*)ip->u.addr32);
  278. } else {
  279. LM_ERR("Unsupported protocol family\n");
  280. return -1;
  281. }
  282. }
  283. #endif /* USE_MCAST */
  284. /** get protocol name (asciiz).
  285. * @param proto - protocol number
  286. * @return string with the protocol name or "unknown".
  287. */
  288. char* get_proto_name(unsigned int proto)
  289. {
  290. switch(proto){
  291. case PROTO_NONE:
  292. return "*";
  293. case PROTO_UDP:
  294. return "udp";
  295. case PROTO_TCP:
  296. return "tcp";
  297. case PROTO_TLS:
  298. return "tls";
  299. case PROTO_SCTP:
  300. return "sctp";
  301. case PROTO_WS:
  302. case PROTO_WSS:
  303. return "ws";
  304. default:
  305. return "unknown";
  306. }
  307. }
  308. /**
  309. * match ip address with net address and bitmask
  310. * - return 0 on match, -1 otherwise
  311. */
  312. int ip_addr_match_net(ip_addr_t *iaddr, ip_addr_t *naddr,
  313. int mask)
  314. {
  315. unsigned char c;
  316. int i;
  317. int mbytes;
  318. int mbits;
  319. if(mask==0)
  320. return 0;
  321. if(iaddr==NULL || naddr==NULL || mask<0)
  322. return -1;
  323. if(iaddr->af != naddr->af)
  324. return -1;
  325. if(iaddr->af == AF_INET)
  326. {
  327. if(mask>32)
  328. return -1;
  329. if(mask==32)
  330. {
  331. if(ip_addr_cmp(iaddr, naddr))
  332. return 0;
  333. return -1;
  334. }
  335. } else if(iaddr->af == AF_INET6) {
  336. if(mask>128)
  337. return -1;
  338. if(mask==128)
  339. {
  340. if(ip_addr_cmp(iaddr, naddr))
  341. return 0;
  342. return -1;
  343. }
  344. }
  345. mbytes = mask / 8;
  346. for(i=0; i<mbytes; i++)
  347. {
  348. if(iaddr->u.addr[i] != naddr->u.addr[i])
  349. return -1;
  350. }
  351. mbits = mask % 8;
  352. if(mbits==0)
  353. return 0;
  354. c = naddr->u.addr[i] & (~((1 << (8 - mbits)) - 1));
  355. if((iaddr->u.addr[i] & c) == c)
  356. return 0;
  357. return -1;
  358. }
  359. int si_get_signaling_data(struct socket_info *si, str **addr, str **port)
  360. {
  361. if(si==NULL)
  362. return -1;
  363. if(addr) {
  364. if(si->useinfo.name.len>0) {
  365. *addr = &si->useinfo.name;
  366. } else {
  367. *addr = &si->address_str;
  368. }
  369. }
  370. if(port) {
  371. if(si->useinfo.port_no>0) {
  372. *port = &si->useinfo.port_no_str;
  373. } else {
  374. *port = &si->port_no_str;
  375. }
  376. }
  377. return 0;
  378. }