netsock2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * Viagénie <asteriskv6@viagenie.ca>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Network socket handling
  21. *
  22. * \author Viagénie <asteriskv6@viagenie.ca>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/config.h"
  30. #include "asterisk/netsock2.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/threadstorage.h"
  33. int ast_sockaddr_ipv4_mapped(const struct ast_sockaddr *addr, struct ast_sockaddr *ast_mapped)
  34. {
  35. const struct sockaddr_in6 *sin6;
  36. struct sockaddr_in sin4;
  37. if (!ast_sockaddr_is_ipv6(addr)) {
  38. return 0;
  39. }
  40. if (!ast_sockaddr_is_ipv4_mapped(addr)) {
  41. return 0;
  42. }
  43. sin6 = (const struct sockaddr_in6*)&addr->ss;
  44. memset(&sin4, 0, sizeof(sin4));
  45. sin4.sin_family = AF_INET;
  46. sin4.sin_port = sin6->sin6_port;
  47. sin4.sin_addr.s_addr = ((uint32_t *)&sin6->sin6_addr)[3];
  48. ast_sockaddr_from_sin(ast_mapped, &sin4);
  49. return 1;
  50. }
  51. AST_THREADSTORAGE(ast_sockaddr_stringify_buf);
  52. char *ast_sockaddr_stringify_fmt(const struct ast_sockaddr *sa, int format)
  53. {
  54. struct ast_sockaddr sa_ipv4;
  55. const struct ast_sockaddr *sa_tmp;
  56. char host[NI_MAXHOST];
  57. char port[NI_MAXSERV];
  58. struct ast_str *str;
  59. int e;
  60. static const size_t size = sizeof(host) - 1 + sizeof(port) - 1 + 4;
  61. if (ast_sockaddr_isnull(sa)) {
  62. return "(null)";
  63. }
  64. if (!(str = ast_str_thread_get(&ast_sockaddr_stringify_buf, size))) {
  65. return "";
  66. }
  67. if (ast_sockaddr_ipv4_mapped(sa, &sa_ipv4)) {
  68. sa_tmp = &sa_ipv4;
  69. } else {
  70. sa_tmp = sa;
  71. }
  72. if ((e = getnameinfo((struct sockaddr *)&sa_tmp->ss, sa_tmp->len,
  73. format & AST_SOCKADDR_STR_ADDR ? host : NULL,
  74. format & AST_SOCKADDR_STR_ADDR ? sizeof(host) : 0,
  75. format & AST_SOCKADDR_STR_PORT ? port : 0,
  76. format & AST_SOCKADDR_STR_PORT ? sizeof(port): 0,
  77. NI_NUMERICHOST | NI_NUMERICSERV))) {
  78. ast_log(LOG_ERROR, "getnameinfo(): %s\n", gai_strerror(e));
  79. return "";
  80. }
  81. if ((format & AST_SOCKADDR_STR_REMOTE) == AST_SOCKADDR_STR_REMOTE) {
  82. char *p;
  83. if (ast_sockaddr_is_ipv6_link_local(sa) && (p = strchr(host, '%'))) {
  84. *p = '\0';
  85. }
  86. }
  87. switch ((format & AST_SOCKADDR_STR_FORMAT_MASK)) {
  88. case AST_SOCKADDR_STR_DEFAULT:
  89. ast_str_set(&str, 0, sa_tmp->ss.ss_family == AF_INET6 ?
  90. "[%s]:%s" : "%s:%s", host, port);
  91. break;
  92. case AST_SOCKADDR_STR_ADDR:
  93. ast_str_set(&str, 0, "%s", host);
  94. break;
  95. case AST_SOCKADDR_STR_HOST:
  96. ast_str_set(&str, 0,
  97. sa_tmp->ss.ss_family == AF_INET6 ? "[%s]" : "%s", host);
  98. break;
  99. case AST_SOCKADDR_STR_PORT:
  100. ast_str_set(&str, 0, "%s", port);
  101. break;
  102. default:
  103. ast_log(LOG_ERROR, "Invalid format\n");
  104. return "";
  105. }
  106. return ast_str_buffer(str);
  107. }
  108. int ast_sockaddr_cidr_bits(const struct ast_sockaddr *sa)
  109. {
  110. struct ast_sockaddr sa_ipv4;
  111. const struct ast_sockaddr *sa_tmp;
  112. int bits = 0;
  113. int bytes;
  114. int i;
  115. int j;
  116. char *addr;
  117. if (ast_sockaddr_isnull(sa)) {
  118. return 0;
  119. }
  120. if (ast_sockaddr_ipv4_mapped(sa, &sa_ipv4)) {
  121. sa_tmp = &sa_ipv4;
  122. } else {
  123. sa_tmp = sa;
  124. }
  125. bytes = sa_tmp->len;
  126. addr = ((struct sockaddr *)&sa_tmp->ss)->sa_data;
  127. for (i = 0; i < bytes ; ++i) {
  128. for (j = 0; j < 8; ++j) {
  129. if ((addr[i] >> j) & 1) {
  130. bits++;
  131. }
  132. }
  133. }
  134. return bits;
  135. }
  136. int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags)
  137. {
  138. char *s = str;
  139. char *orig_str = str;/* Original string in case the port presence is incorrect. */
  140. char *host_end = NULL;/* Delay terminating the host in case the port presence is incorrect. */
  141. ast_debug(5, "Splitting '%s' into...\n", str);
  142. *host = NULL;
  143. *port = NULL;
  144. if (*s == '[') {
  145. *host = ++s;
  146. for (; *s && *s != ']'; ++s) {
  147. }
  148. if (*s == ']') {
  149. host_end = s;
  150. ++s;
  151. }
  152. if (*s == ':') {
  153. *port = s + 1;
  154. }
  155. } else {
  156. *host = s;
  157. for (; *s; ++s) {
  158. if (*s == ':') {
  159. if (*port) {
  160. *port = NULL;
  161. break;
  162. } else {
  163. *port = s;
  164. }
  165. }
  166. }
  167. if (*port) {
  168. host_end = *port;
  169. ++*port;
  170. }
  171. }
  172. switch (flags & PARSE_PORT_MASK) {
  173. case PARSE_PORT_IGNORE:
  174. *port = NULL;
  175. break;
  176. case PARSE_PORT_REQUIRE:
  177. if (*port == NULL) {
  178. ast_log(LOG_WARNING, "Port missing in %s\n", orig_str);
  179. return 0;
  180. }
  181. break;
  182. case PARSE_PORT_FORBID:
  183. if (*port != NULL) {
  184. ast_log(LOG_WARNING, "Port disallowed in %s\n", orig_str);
  185. return 0;
  186. }
  187. break;
  188. }
  189. /* Can terminate the host string now if needed. */
  190. if (host_end) {
  191. *host_end = '\0';
  192. }
  193. ast_debug(5, "...host '%s' and port '%s'.\n", *host, *port ? *port : "");
  194. return 1;
  195. }
  196. int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
  197. {
  198. struct addrinfo hints;
  199. struct addrinfo *res;
  200. char *s;
  201. char *host;
  202. char *port;
  203. int e;
  204. s = ast_strdupa(str);
  205. if (!ast_sockaddr_split_hostport(s, &host, &port, flags)) {
  206. return 0;
  207. }
  208. memset(&hints, 0, sizeof(hints));
  209. /* Hint to get only one entry from getaddrinfo */
  210. hints.ai_socktype = SOCK_DGRAM;
  211. #ifdef AI_NUMERICSERV
  212. hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
  213. #else
  214. hints.ai_flags = AI_NUMERICHOST;
  215. #endif
  216. if ((e = getaddrinfo(host, port, &hints, &res))) {
  217. if (e != EAI_NONAME) { /* if this was just a host name rather than a ip address, don't print error */
  218. ast_log(LOG_ERROR, "getaddrinfo(\"%s\", \"%s\", ...): %s\n",
  219. host, S_OR(port, "(null)"), gai_strerror(e));
  220. }
  221. return 0;
  222. }
  223. /*
  224. * I don't see how this could be possible since we're not resolving host
  225. * names. But let's be careful...
  226. */
  227. if (res->ai_next != NULL) {
  228. ast_log(LOG_WARNING, "getaddrinfo() returned multiple "
  229. "addresses. Ignoring all but the first.\n");
  230. }
  231. if (addr) {
  232. addr->len = res->ai_addrlen;
  233. memcpy(&addr->ss, res->ai_addr, addr->len);
  234. }
  235. freeaddrinfo(res);
  236. return 1;
  237. }
  238. int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
  239. int flags, int family)
  240. {
  241. struct addrinfo hints, *res, *ai;
  242. char *s, *host, *port;
  243. int e, i, res_cnt;
  244. if (!str) {
  245. *addrs = NULL;
  246. return 0;
  247. }
  248. s = ast_strdupa(str);
  249. if (!ast_sockaddr_split_hostport(s, &host, &port, flags)) {
  250. *addrs = NULL;
  251. return 0;
  252. }
  253. memset(&hints, 0, sizeof(hints));
  254. hints.ai_family = family;
  255. hints.ai_socktype = SOCK_DGRAM;
  256. if ((e = getaddrinfo(host, port, &hints, &res))) {
  257. ast_log(LOG_ERROR, "getaddrinfo(\"%s\", \"%s\", ...): %s\n",
  258. host, S_OR(port, "(null)"), gai_strerror(e));
  259. *addrs = NULL;
  260. return 0;
  261. }
  262. res_cnt = 0;
  263. for (ai = res; ai; ai = ai->ai_next) {
  264. res_cnt++;
  265. }
  266. if (res_cnt == 0) {
  267. *addrs = NULL;
  268. goto cleanup;
  269. }
  270. if ((*addrs = ast_malloc(res_cnt * sizeof(struct ast_sockaddr))) == NULL) {
  271. res_cnt = 0;
  272. goto cleanup;
  273. }
  274. i = 0;
  275. for (ai = res; ai; ai = ai->ai_next) {
  276. (*addrs)[i].len = ai->ai_addrlen;
  277. memcpy(&(*addrs)[i].ss, ai->ai_addr, ai->ai_addrlen);
  278. ++i;
  279. }
  280. cleanup:
  281. freeaddrinfo(res);
  282. return res_cnt;
  283. }
  284. int ast_sockaddr_apply_netmask(const struct ast_sockaddr *addr, const struct ast_sockaddr *netmask,
  285. struct ast_sockaddr *result)
  286. {
  287. int res = 0;
  288. if (ast_sockaddr_is_ipv4(addr)) {
  289. struct sockaddr_in result4 = { 0, };
  290. struct sockaddr_in *addr4 = (struct sockaddr_in *) &addr->ss;
  291. struct sockaddr_in *mask4 = (struct sockaddr_in *) &netmask->ss;
  292. result4.sin_family = AF_INET;
  293. result4.sin_addr.s_addr = addr4->sin_addr.s_addr & mask4->sin_addr.s_addr;
  294. ast_sockaddr_from_sin(result, &result4);
  295. } else if (ast_sockaddr_is_ipv6(addr)) {
  296. struct sockaddr_in6 result6 = { 0, };
  297. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &addr->ss;
  298. struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *) &netmask->ss;
  299. int i;
  300. result6.sin6_family = AF_INET6;
  301. for (i = 0; i < 4; ++i) {
  302. V6_WORD(&result6, i) = V6_WORD(addr6, i) & V6_WORD(mask6, i);
  303. }
  304. memcpy(&result->ss, &result6, sizeof(result6));
  305. result->len = sizeof(result6);
  306. } else {
  307. /* Unsupported address scheme */
  308. res = -1;
  309. }
  310. return res;
  311. }
  312. int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
  313. {
  314. const struct ast_sockaddr *a_tmp, *b_tmp;
  315. struct ast_sockaddr ipv4_mapped;
  316. a_tmp = a;
  317. b_tmp = b;
  318. if (a_tmp->len != b_tmp->len) {
  319. if (ast_sockaddr_ipv4_mapped(a, &ipv4_mapped)) {
  320. a_tmp = &ipv4_mapped;
  321. } else if (ast_sockaddr_ipv4_mapped(b, &ipv4_mapped)) {
  322. b_tmp = &ipv4_mapped;
  323. }
  324. }
  325. if (a_tmp->len < b_tmp->len) {
  326. return -1;
  327. } else if (a_tmp->len > b_tmp->len) {
  328. return 1;
  329. }
  330. return memcmp(&a_tmp->ss, &b_tmp->ss, a_tmp->len);
  331. }
  332. int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
  333. {
  334. const struct ast_sockaddr *a_tmp, *b_tmp;
  335. struct ast_sockaddr ipv4_mapped;
  336. const struct in_addr *ip4a, *ip4b;
  337. const struct in6_addr *ip6a, *ip6b;
  338. int ret = -1;
  339. a_tmp = a;
  340. b_tmp = b;
  341. if (a_tmp->len != b_tmp->len) {
  342. if (ast_sockaddr_ipv4_mapped(a, &ipv4_mapped)) {
  343. a_tmp = &ipv4_mapped;
  344. } else if (ast_sockaddr_ipv4_mapped(b, &ipv4_mapped)) {
  345. b_tmp = &ipv4_mapped;
  346. }
  347. }
  348. if (a->len < b->len) {
  349. ret = -1;
  350. } else if (a->len > b->len) {
  351. ret = 1;
  352. }
  353. switch (a_tmp->ss.ss_family) {
  354. case AF_INET:
  355. ip4a = &((const struct sockaddr_in*)&a_tmp->ss)->sin_addr;
  356. ip4b = &((const struct sockaddr_in*)&b_tmp->ss)->sin_addr;
  357. ret = memcmp(ip4a, ip4b, sizeof(*ip4a));
  358. break;
  359. case AF_INET6:
  360. ip6a = &((const struct sockaddr_in6*)&a_tmp->ss)->sin6_addr;
  361. ip6b = &((const struct sockaddr_in6*)&b_tmp->ss)->sin6_addr;
  362. ret = memcmp(ip6a, ip6b, sizeof(*ip6a));
  363. break;
  364. }
  365. return ret;
  366. }
  367. uint16_t _ast_sockaddr_port(const struct ast_sockaddr *addr, const char *file, int line, const char *func)
  368. {
  369. /*
  370. * Test addr->len first to be tolerant of an ast_sockaddr_setnull()
  371. * addr. In that case addr->len might be the only value initialized.
  372. */
  373. if (addr->len == sizeof(struct sockaddr_in)
  374. && addr->ss.ss_family == AF_INET) {
  375. return ntohs(((struct sockaddr_in *)&addr->ss)->sin_port);
  376. }
  377. if (addr->len == sizeof(struct sockaddr_in6)
  378. && addr->ss.ss_family == AF_INET6) {
  379. return ntohs(((struct sockaddr_in6 *)&addr->ss)->sin6_port);
  380. }
  381. if (DEBUG_ATLEAST(1)) {
  382. ast_log(__LOG_DEBUG, file, line, func, "Not an IPv4 nor IPv6 address, cannot get port.\n");
  383. }
  384. return 0;
  385. }
  386. void _ast_sockaddr_set_port(struct ast_sockaddr *addr, uint16_t port, const char *file, int line, const char *func)
  387. {
  388. /*
  389. * Test addr->len first to be tolerant of an ast_sockaddr_setnull()
  390. * addr. In that case addr->len might be the only value initialized.
  391. */
  392. if (addr->len == sizeof(struct sockaddr_in)
  393. && addr->ss.ss_family == AF_INET) {
  394. ((struct sockaddr_in *)&addr->ss)->sin_port = htons(port);
  395. } else if (addr->len == sizeof(struct sockaddr_in6)
  396. && addr->ss.ss_family == AF_INET6) {
  397. ((struct sockaddr_in6 *)&addr->ss)->sin6_port = htons(port);
  398. } else if (DEBUG_ATLEAST(1)) {
  399. ast_log(__LOG_DEBUG, file, line, func,
  400. "Not an IPv4 nor IPv6 address, cannot set port.\n");
  401. }
  402. }
  403. uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr)
  404. {
  405. const struct sockaddr_in *sin = (struct sockaddr_in *)&addr->ss;
  406. return ntohl(sin->sin_addr.s_addr);
  407. }
  408. int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr)
  409. {
  410. /*
  411. * Test addr->len first to be tolerant of an ast_sockaddr_setnull()
  412. * addr. In that case addr->len might be the only value initialized.
  413. */
  414. return addr->len == sizeof(struct sockaddr_in)
  415. && addr->ss.ss_family == AF_INET;
  416. }
  417. int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr)
  418. {
  419. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr->ss;
  420. return addr->len && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr);
  421. }
  422. int ast_sockaddr_is_ipv4_multicast(const struct ast_sockaddr *addr)
  423. {
  424. return ((ast_sockaddr_ipv4(addr) & 0xf0000000) == 0xe0000000);
  425. }
  426. int ast_sockaddr_is_ipv6_link_local(const struct ast_sockaddr *addr)
  427. {
  428. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr->ss;
  429. return ast_sockaddr_is_ipv6(addr) && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr);
  430. }
  431. int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr)
  432. {
  433. /*
  434. * Test addr->len first to be tolerant of an ast_sockaddr_setnull()
  435. * addr. In that case addr->len might be the only value initialized.
  436. */
  437. return addr->len == sizeof(struct sockaddr_in6)
  438. && addr->ss.ss_family == AF_INET6;
  439. }
  440. int ast_sockaddr_is_any(const struct ast_sockaddr *addr)
  441. {
  442. union {
  443. struct sockaddr_storage ss;
  444. struct sockaddr_in sin;
  445. struct sockaddr_in6 sin6;
  446. } tmp_addr = {
  447. .ss = addr->ss,
  448. };
  449. return (ast_sockaddr_is_ipv4(addr) && (tmp_addr.sin.sin_addr.s_addr == INADDR_ANY)) ||
  450. (ast_sockaddr_is_ipv6(addr) && IN6_IS_ADDR_UNSPECIFIED(&tmp_addr.sin6.sin6_addr));
  451. }
  452. int ast_sockaddr_hash(const struct ast_sockaddr *addr)
  453. {
  454. /*
  455. * For IPv4, return the IP address as-is. For IPv6, return the last 32
  456. * bits.
  457. */
  458. switch (addr->ss.ss_family) {
  459. case AF_INET:
  460. return ((const struct sockaddr_in *)&addr->ss)->sin_addr.s_addr;
  461. case AF_INET6:
  462. return ((uint32_t *)&((const struct sockaddr_in6 *)&addr->ss)->sin6_addr)[3];
  463. default:
  464. ast_log(LOG_ERROR, "Unknown address family '%d'.\n",
  465. addr->ss.ss_family);
  466. return 0;
  467. }
  468. }
  469. const char *ast_transport2str(enum ast_transport transport)
  470. {
  471. switch (transport) {
  472. case AST_TRANSPORT_TLS:
  473. return "TLS";
  474. case AST_TRANSPORT_UDP:
  475. return "UDP";
  476. case AST_TRANSPORT_TCP:
  477. return "TCP";
  478. case AST_TRANSPORT_WS:
  479. return "WS";
  480. case AST_TRANSPORT_WSS:
  481. return "WSS";
  482. }
  483. return "Undefined";
  484. }
  485. int ast_accept(int sockfd, struct ast_sockaddr *addr)
  486. {
  487. addr->len = sizeof(addr->ss);
  488. return accept(sockfd, (struct sockaddr *)&addr->ss, &addr->len);
  489. }
  490. int ast_bind(int sockfd, const struct ast_sockaddr *addr)
  491. {
  492. return bind(sockfd, (const struct sockaddr *)&addr->ss, addr->len);
  493. }
  494. int ast_connect(int sockfd, const struct ast_sockaddr *addr)
  495. {
  496. return connect(sockfd, (const struct sockaddr *)&addr->ss, addr->len);
  497. }
  498. int ast_getsockname(int sockfd, struct ast_sockaddr *addr)
  499. {
  500. addr->len = sizeof(addr->ss);
  501. return getsockname(sockfd, (struct sockaddr *)&addr->ss, &addr->len);
  502. }
  503. ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags,
  504. struct ast_sockaddr *src_addr)
  505. {
  506. src_addr->len = sizeof(src_addr->ss);
  507. return recvfrom(sockfd, buf, len, flags,
  508. (struct sockaddr *)&src_addr->ss, &src_addr->len);
  509. }
  510. ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags,
  511. const struct ast_sockaddr *dest_addr)
  512. {
  513. return sendto(sockfd, buf, len, flags,
  514. (const struct sockaddr *)&dest_addr->ss, dest_addr->len);
  515. }
  516. int ast_set_qos(int sockfd, int tos, int cos, const char *desc)
  517. {
  518. int res = 0;
  519. int set_tos;
  520. int set_tclass;
  521. struct ast_sockaddr addr;
  522. /* If the sock address is IPv6, the TCLASS field must be set. */
  523. set_tclass = !ast_getsockname(sockfd, &addr) && ast_sockaddr_is_ipv6(&addr) ? 1 : 0;
  524. /* If the sock address is IPv4 or (IPv6 set to any address [::]) set TOS bits */
  525. set_tos = (!set_tclass || (set_tclass && ast_sockaddr_is_any(&addr))) ? 1 : 0;
  526. if (set_tos) {
  527. if ((res = setsockopt(sockfd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)))) {
  528. ast_log(LOG_WARNING, "Unable to set %s DSCP TOS value to %d (may be you have no "
  529. "root privileges): %s\n", desc, tos, strerror(errno));
  530. } else if (tos) {
  531. ast_verb(2, "Using %s TOS bits %d\n", desc, tos);
  532. }
  533. }
  534. #if defined(IPV6_TCLASS) && defined(IPPROTO_IPV6)
  535. if (set_tclass) {
  536. if (!ast_getsockname(sockfd, &addr) && ast_sockaddr_is_ipv6(&addr)) {
  537. if ((res = setsockopt(sockfd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)))) {
  538. ast_log(LOG_WARNING, "Unable to set %s DSCP TCLASS field to %d (may be you have no "
  539. "root privileges): %s\n", desc, tos, strerror(errno));
  540. } else if (tos) {
  541. ast_verb(2, "Using %s TOS bits %d in TCLASS field.\n", desc, tos);
  542. }
  543. }
  544. }
  545. #endif
  546. #ifdef linux
  547. if (setsockopt(sockfd, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos))) {
  548. ast_log(LOG_WARNING, "Unable to set %s CoS to %d: %s\n", desc, cos,
  549. strerror(errno));
  550. } else if (cos) {
  551. ast_verb(2, "Using %s CoS mark %d\n", desc, cos);
  552. }
  553. #endif
  554. return res;
  555. }
  556. int _ast_sockaddr_to_sin(const struct ast_sockaddr *addr,
  557. struct sockaddr_in *sin, const char *file, int line, const char *func)
  558. {
  559. if (ast_sockaddr_isnull(addr)) {
  560. memset(sin, 0, sizeof(*sin));
  561. return 1;
  562. }
  563. if (addr->len != sizeof(*sin)) {
  564. ast_log(__LOG_ERROR, file, line, func, "Bad address cast to IPv4\n");
  565. return 0;
  566. }
  567. if (addr->ss.ss_family != AF_INET && DEBUG_ATLEAST(1)) {
  568. ast_log(__LOG_DEBUG, file, line, func, "Address family is not AF_INET\n");
  569. }
  570. *sin = *(struct sockaddr_in *)&addr->ss;
  571. return 1;
  572. }
  573. void _ast_sockaddr_from_sin(struct ast_sockaddr *addr, const struct sockaddr_in *sin,
  574. const char *file, int line, const char *func)
  575. {
  576. memcpy(&addr->ss, sin, sizeof(*sin));
  577. if (addr->ss.ss_family != AF_INET && DEBUG_ATLEAST(1)) {
  578. ast_log(__LOG_DEBUG, file, line, func, "Address family is not AF_INET\n");
  579. }
  580. addr->len = sizeof(*sin);
  581. }