ip_addr.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. /*
  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. LOG(L_CRIT, "ERROR: mk_net: 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. LOG(L_CRIT, "ERROR: mk_net: 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. LOG(L_WARN, "WARNING: mk_net: 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. LOG(L_CRIT, "ERROR: mk_net_bitlen: 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. #ifdef USE_IPV6
  163. if (unlikely(t == 0))
  164. t = str2ip6(s);
  165. #endif /* USE_IPV6 */
  166. if (likely(t))
  167. return mk_net_bitlen(dst, t, t->len*8);
  168. /* not a simple ip, maybe an ip/netmask pair */
  169. p = q_memchr(s->s, '/', s->len);
  170. if (likely(p)) {
  171. addr.s = s->s;
  172. addr.len = (int)(long)(p - s->s);
  173. mask.s = p + 1;
  174. mask.len = s->len - (addr.len + 1);
  175. /* allow '/' enclosed by whitespace */
  176. trim_trailing(&addr);
  177. trim_leading(&mask);
  178. t = str2ip(&addr);
  179. if (likely(t)) {
  180. /* it can be a number */
  181. if (str2int(&mask, &bitlen) == 0)
  182. return mk_net_bitlen(dst, t, bitlen);
  183. ip = *t;
  184. t = str2ip(&mask);
  185. if (likely(t))
  186. return mk_net(dst, &ip, t);
  187. /* error */
  188. return -1;
  189. }
  190. #ifdef USE_IPV6
  191. else {
  192. t = str2ip6(&addr);
  193. if (likely(t)) {
  194. /* it can be a number */
  195. if (str2int(&mask, &bitlen) == 0)
  196. return mk_net_bitlen(dst, t, bitlen);
  197. ip = *t;
  198. t = str2ip6(&mask);
  199. if (likely(t))
  200. return mk_net(dst, &ip, t);
  201. /* error */
  202. return -1;
  203. }
  204. }
  205. #endif /* USE_IPV6 */
  206. }
  207. return -1;
  208. }
  209. void print_ip(char* p, struct ip_addr* ip, char *s)
  210. {
  211. switch(ip->af){
  212. case AF_INET:
  213. DBG("%s%d.%d.%d.%d%s", (p)?p:"",
  214. ip->u.addr[0],
  215. ip->u.addr[1],
  216. ip->u.addr[2],
  217. ip->u.addr[3],
  218. (s)?s:""
  219. );
  220. break;
  221. #ifdef USE_IPV6
  222. case AF_INET6:
  223. DBG("%s%x:%x:%x:%x:%x:%x:%x:%x%s", (p)?p:"",
  224. htons(ip->u.addr16[0]),
  225. htons(ip->u.addr16[1]),
  226. htons(ip->u.addr16[2]),
  227. htons(ip->u.addr16[3]),
  228. htons(ip->u.addr16[4]),
  229. htons(ip->u.addr16[5]),
  230. htons(ip->u.addr16[6]),
  231. htons(ip->u.addr16[7]),
  232. (s)?s:""
  233. );
  234. break;
  235. #endif /* USE_IPV6 */
  236. default:
  237. DBG("print_ip: warning unknown address family %d\n", ip->af);
  238. }
  239. }
  240. void stdout_print_ip(struct ip_addr* ip)
  241. {
  242. switch(ip->af){
  243. case AF_INET:
  244. printf("%d.%d.%d.%d", ip->u.addr[0],
  245. ip->u.addr[1],
  246. ip->u.addr[2],
  247. ip->u.addr[3]);
  248. break;
  249. #ifdef USE_IPV6
  250. case AF_INET6:
  251. printf("%x:%x:%x:%x:%x:%x:%x:%x", htons(ip->u.addr16[0]),
  252. htons(ip->u.addr16[1]),
  253. htons(ip->u.addr16[2]),
  254. htons(ip->u.addr16[3]),
  255. htons(ip->u.addr16[4]),
  256. htons(ip->u.addr16[5]),
  257. htons(ip->u.addr16[6]),
  258. htons(ip->u.addr16[7])
  259. );
  260. break;
  261. #endif /* USE_IPV6 */
  262. default:
  263. DBG("print_ip: warning unknown address family %d\n", ip->af);
  264. }
  265. }
  266. void print_net(struct net* net)
  267. {
  268. if (net==0){
  269. LOG(L_WARN, "ERROR: print net: null pointer\n");
  270. return;
  271. }
  272. print_ip("", &net->ip, "/"); print_ip("", &net->mask, "");
  273. }
  274. #ifdef USE_MCAST
  275. /* Returns 1 if the given address is a multicast address */
  276. int is_mcast(struct ip_addr* ip)
  277. {
  278. if (!ip){
  279. LOG(L_ERR, "ERROR: is_mcast: Invalid parameter value\n");
  280. return -1;
  281. }
  282. if (ip->af==AF_INET){
  283. return IN_MULTICAST(htonl(ip->u.addr32[0]));
  284. #ifdef USE_IPV6
  285. } else if (ip->af==AF_INET6){
  286. return IN6_IS_ADDR_MULTICAST((struct in6_addr*)ip->u.addr32);
  287. #endif /* USE_IPV6 */
  288. } else {
  289. LOG(L_ERR, "ERROR: is_mcast: Unsupported protocol family\n");
  290. return -1;
  291. }
  292. }
  293. #endif /* USE_MCAST */
  294. /** get protocol name (asciiz).
  295. * @param proto - protocol number
  296. * @return string with the protocol name or "unknown".
  297. */
  298. char* get_proto_name(unsigned int proto)
  299. {
  300. switch(proto){
  301. case PROTO_NONE:
  302. return "*";
  303. case PROTO_UDP:
  304. return "udp";
  305. case PROTO_TCP:
  306. return "tcp";
  307. case PROTO_TLS:
  308. return "tls";
  309. case PROTO_SCTP:
  310. return "sctp";
  311. case PROTO_WS:
  312. return "ws";
  313. case PROTO_WSS:
  314. return "wss";
  315. default:
  316. return "unknown";
  317. }
  318. }
  319. /**
  320. * match ip address with net address and bitmask
  321. * - return 0 on match, -1 otherwise
  322. */
  323. int ip_addr_match_net(ip_addr_t *iaddr, ip_addr_t *naddr,
  324. int mask)
  325. {
  326. unsigned char c;
  327. int i;
  328. int mbytes;
  329. int mbits;
  330. if(mask==0)
  331. return 0;
  332. if(iaddr==NULL || naddr==NULL || mask<0)
  333. return -1;
  334. if(iaddr->af != naddr->af)
  335. return -1;
  336. if(iaddr->af == AF_INET)
  337. {
  338. if(mask>32)
  339. return -1;
  340. if(mask==32)
  341. {
  342. if(ip_addr_cmp(iaddr, naddr))
  343. return 0;
  344. return -1;
  345. }
  346. } else if(iaddr->af == AF_INET6) {
  347. if(mask>128)
  348. return -1;
  349. if(mask==128)
  350. {
  351. if(ip_addr_cmp(iaddr, naddr))
  352. return 0;
  353. return -1;
  354. }
  355. }
  356. mbytes = mask / 8;
  357. for(i=0; i<mbytes; i++)
  358. {
  359. if(iaddr->u.addr[i] != naddr->u.addr[i])
  360. return -1;
  361. }
  362. mbits = mask % 8;
  363. if(mbits==0)
  364. return 0;
  365. c = naddr->u.addr[i] & (~((1 << (8 - mbits)) - 1));
  366. if((iaddr->u.addr[i] & c) == c)
  367. return 0;
  368. return -1;
  369. }
  370. int si_get_signaling_data(struct socket_info *si, str **addr, str **port)
  371. {
  372. if(si==NULL)
  373. return -1;
  374. if(addr) {
  375. if(si->useinfo.name.len>0) {
  376. *addr = &si->useinfo.name;
  377. } else {
  378. *addr = &si->address_str;
  379. }
  380. }
  381. if(port) {
  382. if(si->useinfo.port_no>0) {
  383. *port = &si->useinfo.port_no_str;
  384. } else {
  385. *port = &si->port_no_str;
  386. }
  387. }
  388. return 0;
  389. }