socket_info.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. /* $Id$
  2. *
  3. * find & manage listen addresses
  4. *
  5. * Copyright (C) 2001-2003 FhG Fokus
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. */
  28. /*
  29. * This file contains code that initializes and handles ser listen addresses
  30. * lists (struct socket_info). It is used mainly on startup.
  31. *
  32. * History:
  33. * --------
  34. * 2003-10-22 created by andrei
  35. * 2004-10-10 added grep_sock_info (andrei)
  36. * 2004-11-08 added find_si (andrei)
  37. * 2007-08-23 added detection for INADDR_ANY types of sockets (andrei)
  38. * 2008-08-08 sctp support (andrei)
  39. * 2008-08-15 support for handling sctp multihomed sockets (andrei)
  40. * 2008-10-15 fixed protocol list iteration when some protocols are
  41. * compile time disabled (andrei)
  42. */
  43. #include <string.h>
  44. #include <errno.h>
  45. #include <unistd.h>
  46. #include <sys/types.h>
  47. #include <sys/socket.h>
  48. #include <sys/utsname.h>
  49. #include <stdio.h>
  50. #include <sys/ioctl.h>
  51. #include <net/if.h>
  52. #ifdef HAVE_SYS_SOCKIO_H
  53. #include <sys/sockio.h>
  54. #endif
  55. #include "globals.h"
  56. #include "socket_info.h"
  57. #include "dprint.h"
  58. #include "mem/mem.h"
  59. #include "ut.h"
  60. #include "resolve.h"
  61. #include "name_alias.h"
  62. /* list manip. functions (internal use only) */
  63. /* append */
  64. #define sock_listadd(head, el) \
  65. do{\
  66. if (*(head)==0) *(head)=(el); \
  67. else{ \
  68. for((el)->next=*(head); (el)->next->next;\
  69. (el)->next=(el)->next->next); \
  70. (el)->next->next=(el); \
  71. (el)->prev=(el)->next; \
  72. (el)->next=0; \
  73. }\
  74. }while(0)
  75. /* insert after "after" */
  76. #define sock_listins(el, after) \
  77. do{ \
  78. if ((after)){\
  79. (el)->next=(after)->next; \
  80. if ((after)->next) (after)->next->prev=(el); \
  81. (after)->next=(el); \
  82. (el)->prev=(after); \
  83. }else{ /* after==0 = list head */ \
  84. (after)=(el); \
  85. (el)->next=(el)->prev=0; \
  86. }\
  87. }while(0)
  88. #define sock_listrm(head, el) \
  89. do {\
  90. if (*(head)==(el)) *(head)=(el)->next; \
  91. if ((el)->next) (el)->next->prev=(el)->prev; \
  92. if ((el)->prev) (el)->prev->next=(el)->next; \
  93. }while(0)
  94. #define addr_info_listadd sock_listadd
  95. #define addr_info_listins sock_listins
  96. #define addr_info_listrm sock_listrm
  97. inline static void addr_info_list_ins_lst(struct addr_info* lst,
  98. struct addr_info* after)
  99. {
  100. struct addr_info* l;
  101. struct addr_info* n;
  102. if (lst){
  103. n=after->next;
  104. after->next=lst;
  105. lst->prev=after;
  106. if (n){
  107. for(l=lst; l->next; l=l->next);
  108. l->next=n;
  109. n->prev=l;
  110. }
  111. }
  112. }
  113. /* protocol order, filled by init_proto_order() */
  114. enum sip_protos nxt_proto[PROTO_LAST+1]=
  115. { PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_SCTP, 0 };
  116. /* another helper function, it just fills a struct addr_info
  117. * returns: 0 on success, -1 on error*/
  118. static int init_addr_info(struct addr_info* a,
  119. char* name, enum si_flags flags)
  120. {
  121. memset(a, 0, sizeof(*a));
  122. a->name.len=strlen(name);
  123. a->name.s=pkg_malloc(a->name.len+1); /* include \0 */
  124. if (a->name.s==0) goto error;
  125. memcpy(a->name.s, name, a->name.len+1);
  126. a->flags=flags;
  127. return 0;
  128. error:
  129. LOG(L_ERR, "ERROR: init_addr_info: memory allocation error\n");
  130. return -1;
  131. }
  132. /* returns 0 on error, new addr_info_lst element on success */
  133. static inline struct addr_info* new_addr_info(char* name,
  134. enum si_flags gf)
  135. {
  136. struct addr_info* al;
  137. al=pkg_malloc(sizeof(*al));
  138. if (al==0) goto error;
  139. al->next=0;
  140. al->prev=0;
  141. if (init_addr_info(al, name, gf)!=0) goto error;
  142. return al;
  143. error:
  144. LOG(L_ERR, "ERROR: new_addr_info: memory allocation error\n");
  145. if (al){
  146. if (al->name.s) pkg_free(al->name.s);
  147. pkg_free(al);
  148. }
  149. return 0;
  150. }
  151. static inline void free_addr_info(struct addr_info* a)
  152. {
  153. if (a){
  154. if (a->name.s){
  155. pkg_free(a->name.s);
  156. a->name.s=0;
  157. }
  158. pkg_free(a);
  159. }
  160. }
  161. static inline void free_addr_info_lst(struct addr_info** lst)
  162. {
  163. struct addr_info* a;
  164. struct addr_info* tmp;
  165. a=*lst;
  166. while(a){
  167. tmp=a;
  168. a=a->next;
  169. free_addr_info(tmp);
  170. }
  171. }
  172. /* adds a new add_info_lst element to the corresponding list
  173. * returns 0 on success, -1 on error */
  174. static int new_addr_info2list(char* name, enum si_flags f,
  175. struct addr_info** l)
  176. {
  177. struct addr_info * al;
  178. al=new_addr_info(name, f);
  179. if (al==0) goto error;
  180. addr_info_listadd(l, al);
  181. return 0;
  182. error:
  183. return -1;
  184. }
  185. /* another helper function, it just creates a socket_info struct */
  186. static inline struct socket_info* new_sock_info( char* name,
  187. struct name_lst* addr_l,
  188. unsigned short port, unsigned short proto,
  189. enum si_flags flags)
  190. {
  191. struct socket_info* si;
  192. struct name_lst* n;
  193. si=(struct socket_info*) pkg_malloc(sizeof(struct socket_info));
  194. if (si==0) goto error;
  195. memset(si, 0, sizeof(struct socket_info));
  196. si->socket=-1;
  197. si->name.len=strlen(name);
  198. si->name.s=(char*)pkg_malloc(si->name.len+1); /* include \0 */
  199. if (si->name.s==0) goto error;
  200. memcpy(si->name.s, name, si->name.len+1);
  201. /* set port & proto */
  202. si->port_no=port;
  203. si->proto=proto;
  204. si->flags=flags;
  205. si->addr_info_lst=0;
  206. for (n=addr_l; n; n=n->next){
  207. if (new_addr_info2list(n->name, n->flags, &si->addr_info_lst)!=0){
  208. LOG(L_ERR, "ERROR: new_sockk_info:new_addr_info2list failed\n");
  209. goto error;
  210. }
  211. }
  212. return si;
  213. error:
  214. LOG(L_ERR, "ERROR: new_sock_info: memory allocation error\n");
  215. if (si) pkg_free(si);
  216. return 0;
  217. }
  218. /* delete a socket_info struct */
  219. static void free_sock_info(struct socket_info* si)
  220. {
  221. if(si){
  222. if(si->name.s) pkg_free(si->name.s);
  223. if(si->address_str.s) pkg_free(si->address_str.s);
  224. if(si->port_no_str.s) pkg_free(si->port_no_str.s);
  225. if (si->addr_info_lst) free_addr_info_lst(&si->addr_info_lst);
  226. if(si->sock_str.s) pkg_free(si->sock_str.s);
  227. }
  228. }
  229. static char* get_proto_name(unsigned short proto)
  230. {
  231. switch(proto){
  232. case PROTO_NONE:
  233. return "*";
  234. case PROTO_UDP:
  235. return "udp";
  236. #ifdef USE_TCP
  237. case PROTO_TCP:
  238. return "tcp";
  239. #endif
  240. #ifdef USE_TLS
  241. case PROTO_TLS:
  242. return "tls";
  243. #endif
  244. #ifdef USE_SCTP
  245. case PROTO_SCTP:
  246. return "sctp";
  247. #endif
  248. default:
  249. return "unknown";
  250. }
  251. }
  252. /** Convert socket to its textual representation.
  253. *
  254. * This function converts the transport protocol, the IP address and the port
  255. * number in a comma delimited string of form proto:ip:port. The resulting
  256. * string is NOT zero terminated
  257. *
  258. * @param s is a pointer to the destination memory buffer
  259. * @param len is a pointer to an integer variable. Initially the variable
  260. * should contain the size of the buffer in s. The value of the variable
  261. * will be changed to the length of the resulting string on success and
  262. * to the desired size of the destination buffer if it is too small
  263. * @param si is a pointer to the socket_info structure to be printed
  264. * @return -1 on error and 0 on success
  265. */
  266. int socket2str(char* s, int* len, struct socket_info* si)
  267. {
  268. str proto;
  269. int l;
  270. proto.s = get_proto_name(si->proto);
  271. proto.len = strlen(proto.s);
  272. l = proto.len + si->address_str.len + si->port_no_str.len + 2;
  273. if (*len < l) {
  274. ERR("socket2str: Destionation buffer too short\n");
  275. *len = l;
  276. return -1;
  277. }
  278. memcpy(s, proto.s, proto.len);
  279. s += proto.len;
  280. *s = ':'; s++;
  281. memcpy(s, si->address_str.s, si->address_str.len);
  282. s += si->address_str.len;
  283. *s = ':'; s++;
  284. memcpy(s, si->port_no_str.s, si->port_no_str.len);
  285. s += si->port_no_str.len;
  286. *len = l;
  287. return 0;
  288. }
  289. /* Fill si->sock_str with string representing the socket_info structure,
  290. * format of the string is 'proto:address:port'. Returns 0 on success and
  291. * negative number on failure.
  292. */
  293. static int fix_sock_str(struct socket_info* si)
  294. {
  295. int len = MAX_SOCKET_STR;
  296. if (si->sock_str.s) pkg_free(si->sock_str.s);
  297. si->sock_str.s = pkg_malloc(len + 1);
  298. if (si->sock_str.s == NULL) {
  299. ERR("fix_sock_str: No memory left\n");
  300. return -1;
  301. }
  302. if (socket2str(si->sock_str.s, &len, si) < 0) {
  303. BUG("fix_sock_str: Error in socket2str\n");
  304. return -1;
  305. }
  306. si->sock_str.s[len] = '\0';
  307. return 0;
  308. }
  309. /* returns 0 if support for the protocol is not compiled or if proto is
  310. invalid */
  311. struct socket_info** get_sock_info_list(unsigned short proto)
  312. {
  313. switch(proto){
  314. case PROTO_UDP:
  315. return &udp_listen;
  316. break;
  317. case PROTO_TCP:
  318. #ifdef USE_TCP
  319. return &tcp_listen;
  320. #endif
  321. break;
  322. case PROTO_TLS:
  323. #ifdef USE_TLS
  324. return &tls_listen;
  325. #endif
  326. break;
  327. case PROTO_SCTP:
  328. #ifdef USE_SCTP
  329. return &sctp_listen;
  330. #endif
  331. break;
  332. default:
  333. LOG(L_CRIT, "BUG: get_sock_info_list: invalid proto %d\n", proto);
  334. }
  335. return 0;
  336. }
  337. /* helper function for grep_sock_info
  338. * params:
  339. * host - hostname to compare with
  340. * name - official name
  341. * addr_str - name's resolved ip address converted to string
  342. * ip_addr - name's ip address
  343. * flags - set to SI_IS_IP if name contains an IP
  344. *
  345. * returns 0 if host matches, -1 if not */
  346. inline static int si_hname_cmp(str* host, str* name, str* addr_str,
  347. struct ip_addr* ip_addr, int flags)
  348. {
  349. #ifdef USE_IPV6
  350. struct ip_addr* ip6;
  351. #endif
  352. if ( (host->len==name->len) &&
  353. (strncasecmp(host->s, name->s, name->len)==0) /*slower*/)
  354. /* comp. must be case insensitive, host names
  355. * can be written in mixed case, it will also match
  356. * ipv6 addresses if we are lucky*/
  357. goto found;
  358. /* check if host == ip address */
  359. #ifdef USE_IPV6
  360. /* ipv6 case is uglier, host can be [3ffe::1] */
  361. ip6=str2ip6(host);
  362. if (ip6){
  363. if (ip_addr_cmp(ip6, ip_addr))
  364. goto found; /* match */
  365. else
  366. return -1; /* no match, but this is an ipv6 address
  367. so no point in trying ipv4 */
  368. }
  369. #endif
  370. /* ipv4 */
  371. if ( (!(flags&SI_IS_IP)) && (host->len==addr_str->len) &&
  372. (memcmp(host->s, addr_str->s, addr_str->len)==0) )
  373. goto found;
  374. return -1;
  375. found:
  376. return 0;
  377. }
  378. /* checks if the proto: host:port is one of the address we listen on
  379. * and returns the corresponding socket_info structure.
  380. * if port==0, the port number is ignored
  381. * if proto==0 (PROTO_NONE) the protocol is ignored
  382. * returns 0 if not found
  383. * WARNING: uses str2ip6 so it will overwrite any previous
  384. * unsaved result of this function (static buffer)
  385. */
  386. struct socket_info* grep_sock_info(str* host, unsigned short port,
  387. unsigned short proto)
  388. {
  389. str hname;
  390. struct socket_info* si;
  391. struct socket_info** list;
  392. struct addr_info* ai;
  393. unsigned short c_proto;
  394. hname=*host;
  395. #ifdef USE_IPV6
  396. if ((hname.len>2)&&((*hname.s)=='[')&&(hname.s[hname.len-1]==']')){
  397. /* ipv6 reference, skip [] */
  398. hname.s++;
  399. hname.len-=2;
  400. }
  401. #endif
  402. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  403. do{
  404. /* get the proper sock_list */
  405. list=get_sock_info_list(c_proto);
  406. if (list==0) /* disabled or unknown protocol */
  407. continue;
  408. for (si=*list; si; si=si->next){
  409. DBG("grep_sock_info - checking if host==us: %d==%d && "
  410. " [%.*s] == [%.*s]\n",
  411. hname.len,
  412. si->name.len,
  413. hname.len, hname.s,
  414. si->name.len, si->name.s
  415. );
  416. if (port) {
  417. DBG("grep_sock_info - checking if port %d matches port %d\n",
  418. si->port_no, port);
  419. if (si->port_no!=port) {
  420. continue;
  421. }
  422. }
  423. if (si_hname_cmp(&hname, &si->name, &si->address_str,
  424. &si->address, si->flags)==0)
  425. goto found;
  426. /* try among the extra addresses */
  427. for (ai=si->addr_info_lst; ai; ai=ai->next)
  428. if (si_hname_cmp(&hname, &ai->name, &ai->address_str,
  429. &ai->address, ai->flags)==0)
  430. goto found;
  431. }
  432. }while( (proto==0) && (c_proto=next_proto(c_proto)) );
  433. /* not_found: */
  434. return 0;
  435. found:
  436. return si;
  437. }
  438. /* checks if the proto:port is one of the ports we listen on
  439. * and returns the corresponding socket_info structure.
  440. * if proto==0 (PROTO_NONE) the protocol is ignored
  441. * returns 0 if not found
  442. */
  443. struct socket_info* grep_sock_info_by_port(unsigned short port,
  444. unsigned short proto)
  445. {
  446. struct socket_info* si;
  447. struct socket_info** list;
  448. unsigned short c_proto;
  449. if (!port) {
  450. goto not_found;
  451. }
  452. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  453. do{
  454. /* get the proper sock_list */
  455. list=get_sock_info_list(c_proto);
  456. if (list==0) /* disabled or unknown protocol */
  457. continue;
  458. for (si=*list; si; si=si->next){
  459. DBG("grep_sock_info_by_port - checking if port %d matches"
  460. " port %d\n", si->port_no, port);
  461. if (si->port_no==port) {
  462. goto found;
  463. }
  464. }
  465. }while( (proto==0) && (c_proto=next_proto(c_proto)) );
  466. not_found:
  467. return 0;
  468. found:
  469. return si;
  470. }
  471. /* checks if the proto: ip:port is one of the address we listen on
  472. * and returns the corresponding socket_info structure.
  473. * (same as grep_socket_info, but use ip addr instead)
  474. * if port==0, the port number is ignored
  475. * if proto==0 (PROTO_NONE) the protocol is ignored
  476. * returns 0 if not found
  477. * WARNING: uses str2ip6 so it will overwrite any previous
  478. * unsaved result of this function (static buffer)
  479. */
  480. struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
  481. unsigned short proto)
  482. {
  483. struct socket_info* si;
  484. struct socket_info** list;
  485. struct addr_info* ai;
  486. unsigned short c_proto;
  487. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  488. do{
  489. /* get the proper sock_list */
  490. list=get_sock_info_list(c_proto);
  491. if (list==0) /* disabled or unknown protocol */
  492. continue;
  493. for (si=*list; si; si=si->next){
  494. if (port) {
  495. if (si->port_no!=port) {
  496. continue;
  497. }
  498. }
  499. if (ip_addr_cmp(ip, &si->address))
  500. goto found;
  501. for (ai=si->addr_info_lst; ai; ai=ai->next)
  502. if (ip_addr_cmp(ip, &ai->address))
  503. goto found;
  504. }
  505. }while( (proto==0) && (c_proto=next_proto(c_proto)) );
  506. /* not_found: */
  507. return 0;
  508. found:
  509. return si;
  510. }
  511. /* append a new sock_info structure to the corresponding list
  512. * return new sock info on success, 0 on error */
  513. static struct socket_info* new_sock2list(char* name, struct name_lst* addr_l,
  514. unsigned short port,
  515. unsigned short proto, enum si_flags flags,
  516. struct socket_info** list)
  517. {
  518. struct socket_info* si;
  519. si=new_sock_info(name, addr_l, port, proto, flags);
  520. if (si==0){
  521. LOG(L_ERR, "ERROR: new_sock2list: new_sock_info failed\n");
  522. goto error;
  523. }
  524. sock_listadd(list, si);
  525. return si;
  526. error:
  527. return 0;
  528. }
  529. /* adds a new sock_info structure immediately after "after"
  530. * return new sock info on success, 0 on error */
  531. static struct socket_info* new_sock2list_after(char* name,
  532. struct name_lst* addr_l,
  533. unsigned short port,
  534. unsigned short proto, enum si_flags flags,
  535. struct socket_info* after)
  536. {
  537. struct socket_info* si;
  538. si=new_sock_info(name, addr_l, port, proto, flags);
  539. if (si==0){
  540. LOG(L_ERR, "ERROR: new_sock2list_after: new_sock_info failed\n");
  541. goto error;
  542. }
  543. sock_listins(si, after);
  544. return si;
  545. error:
  546. return 0;
  547. }
  548. /* adds a sock_info structure to the corresponding proto list
  549. * return 0 on success, -1 on error */
  550. int add_listen_iface(char* name, struct name_lst* addr_l,
  551. unsigned short port, unsigned short proto,
  552. enum si_flags flags)
  553. {
  554. struct socket_info** list;
  555. unsigned short c_proto;
  556. struct name_lst* a_l;
  557. unsigned short c_port;
  558. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  559. do{
  560. list=get_sock_info_list(c_proto);
  561. if (list==0) /* disabled or unknown protocol */
  562. continue;
  563. if (port==0){ /* use default port */
  564. c_port=
  565. #ifdef USE_TLS
  566. ((c_proto)==PROTO_TLS)?tls_port_no:
  567. #endif
  568. port_no;
  569. }
  570. #ifdef USE_TLS
  571. else if ((c_proto==PROTO_TLS) && (proto==0)){
  572. /* -l ip:port => on udp:ip:port; tcp:ip:port and tls:ip:port+1?*/
  573. c_port=port+1;
  574. }
  575. #endif
  576. else{
  577. c_port=port;
  578. }
  579. if (c_proto!=PROTO_SCTP){
  580. if (new_sock2list(name, 0, c_port, c_proto,
  581. flags & ~SI_IS_MHOMED, list)==0){
  582. LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
  583. goto error;
  584. }
  585. /* add the other addresses in the list as separate sockets
  586. * since only SCTP can bind to multiple addresses */
  587. for (a_l=addr_l; a_l; a_l=a_l->next){
  588. if (new_sock2list(a_l->name, 0, c_port,
  589. c_proto, flags & ~SI_IS_MHOMED, list)==0){
  590. LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list"
  591. " failed\n");
  592. goto error;
  593. }
  594. }
  595. }else{
  596. if (new_sock2list(name, addr_l, c_port, c_proto, flags, list)==0){
  597. LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
  598. goto error;
  599. }
  600. }
  601. }while( (proto==0) && (c_proto=next_proto(c_proto)));
  602. return 0;
  603. error:
  604. return -1;
  605. }
  606. /* add all family type addresses of interface if_name to the socket_info array
  607. * if if_name==0, adds all addresses on all interfaces
  608. * WARNING: it only works with ipv6 addresses on FreeBSD
  609. * return: -1 on error, 0 on success
  610. */
  611. int add_interfaces(char* if_name, int family, unsigned short port,
  612. unsigned short proto,
  613. struct addr_info** ai_l)
  614. {
  615. struct ifconf ifc;
  616. struct ifreq ifr;
  617. struct ifreq ifrcopy;
  618. char* last;
  619. char* p;
  620. int size;
  621. int lastlen;
  622. int s;
  623. char* tmp;
  624. struct ip_addr addr;
  625. int ret;
  626. enum si_flags flags;
  627. #ifdef HAVE_SOCKADDR_SA_LEN
  628. #ifndef MAX
  629. #define MAX(a,b) ( ((a)>(b))?(a):(b))
  630. #endif
  631. #endif
  632. /* ipv4 or ipv6 only*/
  633. flags=SI_NONE;
  634. s=socket(family, SOCK_DGRAM, 0);
  635. ret=-1;
  636. lastlen=0;
  637. ifc.ifc_req=0;
  638. for (size=100; ; size*=2){
  639. ifc.ifc_len=size*sizeof(struct ifreq);
  640. ifc.ifc_req=(struct ifreq*) pkg_malloc(size*sizeof(struct ifreq));
  641. if (ifc.ifc_req==0){
  642. LOG(L_ERR, "ERROR: add_interfaces: memory allocation failure\n");
  643. goto error;
  644. }
  645. if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
  646. if(errno==EBADF) return 0; /* invalid descriptor => no such ifs*/
  647. LOG(L_ERR, "ERROR: add_interfaces: ioctl failed: %s\n",
  648. strerror(errno));
  649. goto error;
  650. }
  651. if ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
  652. len not changed*/
  653. lastlen=ifc.ifc_len;
  654. /* try a bigger array*/
  655. pkg_free(ifc.ifc_req);
  656. }
  657. last=(char*)ifc.ifc_req+ifc.ifc_len;
  658. for(p=(char*)ifc.ifc_req; p<last;
  659. p+=
  660. #ifdef __OS_linux
  661. sizeof(ifr) /* works on x86_64 too */
  662. #else
  663. (sizeof(ifr.ifr_name)+
  664. #ifdef HAVE_SOCKADDR_SA_LEN
  665. MAX(ifr.ifr_addr.sa_len, sizeof(struct sockaddr))
  666. #else
  667. ( (ifr.ifr_addr.sa_family==AF_INET)?
  668. sizeof(struct sockaddr_in):
  669. #ifdef USE_IPV6
  670. ((ifr.ifr_addr.sa_family==AF_INET6)?
  671. sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
  672. #else /* USE_IPV6 */
  673. sizeof(struct sockaddr) )
  674. #endif /* USE_IPV6 */
  675. #endif
  676. )
  677. #endif
  678. )
  679. {
  680. /* copy contents into ifr structure
  681. * warning: it might be longer (e.g. ipv6 address) */
  682. memcpy(&ifr, p, sizeof(ifr));
  683. if (ifr.ifr_addr.sa_family!=family){
  684. /*printf("strange family %d skipping...\n",
  685. ifr->ifr_addr.sa_family);*/
  686. continue;
  687. }
  688. /*get flags*/
  689. ifrcopy=ifr;
  690. if (ioctl(s, SIOCGIFFLAGS, &ifrcopy)!=-1){ /* ignore errors */
  691. /* ignore down ifs only if listening on all of them*/
  692. if (if_name==0){
  693. /* if if not up, skip it*/
  694. if (!(ifrcopy.ifr_flags & IFF_UP)) continue;
  695. }
  696. }
  697. if ((if_name==0)||
  698. (strncmp(if_name, ifr.ifr_name, sizeof(ifr.ifr_name))==0)){
  699. /*add address*/
  700. sockaddr2ip_addr(&addr,
  701. (struct sockaddr*)(p+(long)&((struct ifreq*)0)->ifr_addr));
  702. if ((tmp=ip_addr2a(&addr))==0) goto error;
  703. /* check if loopback */
  704. if (ifrcopy.ifr_flags & IFF_LOOPBACK)
  705. flags|=SI_IS_LO;
  706. /* save the info */
  707. if (new_addr_info2list(tmp, flags, ai_l)!=0){
  708. LOG(L_ERR, "ERROR: add_interfaces: "
  709. "new_addr_info2list failed\n");
  710. goto error;
  711. }
  712. ret=0;
  713. }
  714. /*
  715. printf("%s:\n", ifr->ifr_name);
  716. printf(" ");
  717. print_sockaddr(&(ifr->ifr_addr));
  718. printf(" ");
  719. ls_ifflags(ifr->ifr_name, family, options);
  720. printf("\n");*/
  721. }
  722. pkg_free(ifc.ifc_req); /*clean up*/
  723. close(s);
  724. return ret;
  725. error:
  726. if (ifc.ifc_req) pkg_free(ifc.ifc_req);
  727. close(s);
  728. return -1;
  729. }
  730. /* internal helper function: resolve host names and add aliases
  731. * name is a value result parameter: it should contain the hostname that
  732. * will be used to fill all the other members, including name itself
  733. * in some situation (name->s should be a 0 terminated pkg_malloc'ed string)
  734. * return 0 on success and -1 on error */
  735. static int fix_hostname(str* name, struct ip_addr* address, str* address_str,
  736. enum si_flags* flags, int* type_flags,
  737. struct socket_info* s)
  738. {
  739. struct hostent* he;
  740. char* tmp;
  741. char** h;
  742. /* get "official hostnames", all the aliases etc. */
  743. he=resolvehost(name->s);
  744. if (he==0){
  745. LOG(L_ERR, "ERROR: fix_hostname: could not resolve %s\n", name->s);
  746. goto error;
  747. }
  748. /* check if we got the official name */
  749. if (strcasecmp(he->h_name, name->s)!=0){
  750. if (sr_auto_aliases &&
  751. add_alias(name->s, name->len, s->port_no, s->proto)<0){
  752. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  753. }
  754. /* change the official name */
  755. pkg_free(name->s);
  756. name->s=(char*)pkg_malloc(strlen(he->h_name)+1);
  757. if (name->s==0){
  758. LOG(L_ERR, "ERROR: fix_hostname: out of memory.\n");
  759. goto error;
  760. }
  761. name->len=strlen(he->h_name);
  762. strncpy(name->s, he->h_name, name->len+1);
  763. }
  764. /* add the aliases*/
  765. for(h=he->h_aliases; sr_auto_aliases && h && *h; h++)
  766. if (add_alias(*h, strlen(*h), s->port_no, s->proto)<0){
  767. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  768. }
  769. hostent2ip_addr(address, he, 0); /*convert to ip_addr format*/
  770. if (type_flags){
  771. *type_flags|=(address->af==AF_INET)?SOCKET_T_IPV4:SOCKET_T_IPV6;
  772. }
  773. if ((tmp=ip_addr2a(address))==0) goto error;
  774. address_str->s=pkg_malloc(strlen(tmp)+1);
  775. if (address_str->s==0){
  776. LOG(L_ERR, "ERROR: fix_hostname: out of memory.\n");
  777. goto error;
  778. }
  779. strncpy(address_str->s, tmp, strlen(tmp)+1);
  780. /* set is_ip (1 if name is an ip address, 0 otherwise) */
  781. address_str->len=strlen(tmp);
  782. if (sr_auto_aliases && (address_str->len==name->len) &&
  783. (strncasecmp(address_str->s, name->s, address_str->len)==0)){
  784. *flags|=SI_IS_IP;
  785. /* do rev. DNS on it (for aliases)*/
  786. he=rev_resolvehost(address);
  787. if (he==0){
  788. LOG(L_WARN, "WARNING: fix_hostname: could not rev. resolve %s\n",
  789. name->s);
  790. }else{
  791. /* add the aliases*/
  792. if (add_alias(he->h_name, strlen(he->h_name), s->port_no,
  793. s->proto)<0){
  794. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  795. }
  796. for(h=he->h_aliases; h && *h; h++)
  797. if (add_alias(*h, strlen(*h), s->port_no, s->proto) < 0){
  798. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  799. }
  800. }
  801. }
  802. #ifdef USE_MCAST
  803. /* Check if it is an multicast address and
  804. * set the flag if so
  805. */
  806. if (is_mcast(address)){
  807. *flags |= SI_IS_MCAST;
  808. }
  809. #endif /* USE_MCAST */
  810. /* check if INADDR_ANY */
  811. if (ip_addr_any(address))
  812. *flags|=SI_IS_ANY;
  813. else if (ip_addr_loopback(address)) /* check for loopback */
  814. *flags|=SI_IS_LO;
  815. return 0;
  816. error:
  817. return -1;
  818. }
  819. /* append new elements to a socket_info list after "list"
  820. * each element is created from addr_info_lst + port, protocol and flags
  821. * return 0 on succes, -1 on error
  822. */
  823. static int addr_info_to_si_lst(struct addr_info* ai_lst, unsigned short port,
  824. char proto, enum si_flags flags,
  825. struct socket_info** list)
  826. {
  827. struct addr_info* ail;
  828. for (ail=ai_lst; ail; ail=ail->next){
  829. if(new_sock2list(ail->name.s, 0, port, proto, ail->flags | flags,
  830. list)==0)
  831. return -1;
  832. }
  833. return 0;
  834. }
  835. /* insert new elements to a socket_info list after "el",
  836. * each element is created from addr_info_lst + port, * protocol and flags
  837. * return 0 on succes, -1 on error
  838. */
  839. static int addr_info_to_si_lst_after(struct addr_info* ai_lst,
  840. unsigned short port,
  841. char proto, enum si_flags flags,
  842. struct socket_info* el)
  843. {
  844. struct addr_info* ail;
  845. struct socket_info* new_si;
  846. for (ail=ai_lst; ail; ail=ail->next){
  847. if((new_si=new_sock2list_after(ail->name.s, 0, port, proto,
  848. ail->flags | flags, el))==0)
  849. return -1;
  850. el=new_si;
  851. }
  852. return 0;
  853. }
  854. /* fixes a socket list => resolve addresses,
  855. * interface names, fills missing members, remove duplicates
  856. * fills type_flags if not null with SOCKET_T_IPV4 and/or SOCKET_T_IPV6*/
  857. static int fix_socket_list(struct socket_info **list, int* type_flags)
  858. {
  859. struct socket_info* si;
  860. struct socket_info* new_si;
  861. struct socket_info* l;
  862. struct socket_info* next;
  863. struct socket_info* next_si;
  864. struct socket_info* del_si;
  865. struct socket_info* keep_si;
  866. char* tmp;
  867. int len;
  868. struct addr_info* ai_lst;
  869. struct addr_info* ail;
  870. struct addr_info* tmp_ail;
  871. struct addr_info* tmp_ail_next;
  872. struct addr_info* ail_next;
  873. if (type_flags)
  874. *type_flags=0;
  875. /* try to change all the interface names into addresses
  876. * --ugly hack */
  877. for (si=*list;si;){
  878. next=si->next;
  879. ai_lst=0;
  880. if (add_interfaces(si->name.s, AF_INET, si->port_no,
  881. si->proto, &ai_lst)!=-1){
  882. if (si->flags & SI_IS_MHOMED){
  883. if((new_si=new_sock2list_after(ai_lst->name.s, 0, si->port_no,
  884. si->proto,
  885. ai_lst->flags|si->flags, si))==0)
  886. break;
  887. ail=ai_lst;
  888. ai_lst=ai_lst->next;
  889. free_addr_info(ail); /* free the first elem. */
  890. if (ai_lst){
  891. ai_lst->prev=0;
  892. /* find the end */
  893. for (ail=ai_lst; ail->next; ail=ail->next);
  894. /* add the mh list after the last position in ai_lst */
  895. addr_info_list_ins_lst(si->addr_info_lst, ail);
  896. new_si->addr_info_lst=ai_lst;
  897. si->addr_info_lst=0; /* detached and moved to new_si */
  898. ail=ail->next; /* ail== old si->addr_info_lst */
  899. }else{
  900. ail=si->addr_info_lst;
  901. new_si->addr_info_lst=ail;
  902. si->addr_info_lst=0; /* detached and moved to new_si */
  903. }
  904. }else{
  905. /* add all addr. as separate interfaces */
  906. if (addr_info_to_si_lst_after(ai_lst, si->port_no, si->proto,
  907. si->flags, si)!=0)
  908. goto error;
  909. /* ai_lst not needed anymore */
  910. free_addr_info_lst(&ai_lst);
  911. ail=0;
  912. new_si=0;
  913. }
  914. /* success => remove current entry (shift the entire array)*/
  915. sock_listrm(list, si);
  916. free_sock_info(si);
  917. }else{
  918. new_si=si;
  919. ail=si->addr_info_lst;
  920. }
  921. if (ail){
  922. if (new_si && (new_si->flags & SI_IS_MHOMED)){
  923. ai_lst=0;
  924. for (; ail;){
  925. ail_next=ail->next;
  926. if (add_interfaces(ail->name.s, AF_INET, new_si->port_no,
  927. new_si->proto, &ai_lst)!=-1){
  928. /* add the resolved list after the current position */
  929. addr_info_list_ins_lst(ai_lst, ail);
  930. /* success, remove the current entity */
  931. addr_info_listrm(&new_si->addr_info_lst, ail);
  932. free_addr_info(ail);
  933. ai_lst=0;
  934. }
  935. ail=ail_next;
  936. }
  937. }
  938. }
  939. si=next;
  940. }
  941. /* get ips & fill the port numbers*/
  942. #ifdef EXTRA_DEBUG
  943. DBG("Listening on \n");
  944. #endif
  945. for (si=*list;si;si=si->next){
  946. /* fix port number, port_no should be !=0 here */
  947. if (si->port_no==0){
  948. #ifdef USE_TLS
  949. si->port_no= (si->proto==PROTO_TLS)?tls_port_no:port_no;
  950. #else
  951. si->port_no= port_no;
  952. #endif
  953. }
  954. tmp=int2str(si->port_no, &len);
  955. if (len>=MAX_PORT_LEN){
  956. LOG(L_ERR, "ERROR: fix_socket_list: bad port number: %d\n",
  957. si->port_no);
  958. goto error;
  959. }
  960. si->port_no_str.s=(char*)pkg_malloc(len+1);
  961. if (si->port_no_str.s==0){
  962. LOG(L_ERR, "ERROR: fix_socket_list: out of memory.\n");
  963. goto error;
  964. }
  965. strncpy(si->port_no_str.s, tmp, len+1);
  966. si->port_no_str.len=len;
  967. if (fix_hostname(&si->name, &si->address, &si->address_str,
  968. &si->flags, type_flags, si) !=0 )
  969. goto error;
  970. /* fix hostnames in mh addresses */
  971. for (ail=si->addr_info_lst; ail; ail=ail->next){
  972. if (fix_hostname(&ail->name, &ail->address, &ail->address_str,
  973. &ail->flags, type_flags, si) !=0 )
  974. goto error;
  975. }
  976. if (fix_sock_str(si) < 0) goto error;
  977. #ifdef EXTRA_DEBUG
  978. printf(" %.*s [%s]:%s%s\n", si->name.len,
  979. si->name.s, si->address_str.s, si->port_no_str.s,
  980. si->flags & SI_IS_MCAST ? " mcast" : "");
  981. #endif
  982. }
  983. /* removing duplicate addresses*/
  984. for (si=*list;si; ){
  985. next_si=si->next;
  986. for (l=si->next;l;){
  987. next=l->next;
  988. if ((si->port_no==l->port_no) &&
  989. (si->address.af==l->address.af) &&
  990. (memcmp(si->address.u.addr, l->address.u.addr,
  991. si->address.len) == 0)
  992. ){
  993. /* remove the socket with no extra addresses.,
  994. * if both of them have extra addresses, remove one of them
  995. * and merge the extra addresses into the other */
  996. if (l->addr_info_lst==0){
  997. del_si=l;
  998. keep_si=si;
  999. }else if (si->addr_info_lst==0){
  1000. del_si=si;
  1001. keep_si=l;
  1002. }else{
  1003. /* move l->addr_info_lst to si->addr_info_lst */
  1004. /* find last elem */
  1005. for (ail=si->addr_info_lst; ail->next; ail=ail->next);
  1006. /* add the l list after the last position in si lst */
  1007. addr_info_list_ins_lst(l->addr_info_lst, ail);
  1008. l->addr_info_lst=0; /* detached */
  1009. del_si=l; /* l will be removed */
  1010. keep_si=l;
  1011. }
  1012. #ifdef EXTRA_DEBUG
  1013. printf("removing duplicate %s [%s] == %s [%s]\n",
  1014. keep_si->name.s, keep_si->address_str.s,
  1015. del_si->name.s, del_si->address_str.s);
  1016. #endif
  1017. /* add the name to the alias list*/
  1018. if ((!(del_si->flags& SI_IS_IP)) && (
  1019. (del_si->name.len!=keep_si->name.len)||
  1020. (strncmp(del_si->name.s, keep_si->name.s,
  1021. del_si->name.len)!=0))
  1022. )
  1023. add_alias(del_si->name.s, del_si->name.len,
  1024. l->port_no, l->proto);
  1025. /* make sure next_si doesn't point to del_si */
  1026. if (del_si==next_si)
  1027. next_si=next_si->next;
  1028. /* remove del_si*/
  1029. sock_listrm(list, del_si);
  1030. free_sock_info(del_si);
  1031. }
  1032. l=next;
  1033. }
  1034. si=next_si;
  1035. }
  1036. /* check for duplicates in extra_addresses */
  1037. for (si=*list;si; si=si->next){
  1038. /* check for & remove internal duplicates: */
  1039. for (ail=si->addr_info_lst; ail;){
  1040. ail_next=ail->next;
  1041. /* 1. check if the extra addresses contain a duplicate for the
  1042. * main one */
  1043. if ((ail->address.af==si->address.af) &&
  1044. (memcmp(ail->address.u.addr, si->address.u.addr,
  1045. ail->address.len) == 0)){
  1046. /* add the name to the alias list*/
  1047. if ((!(ail->flags& SI_IS_IP)) && (
  1048. (ail->name.len!=si->name.len)||
  1049. (strncmp(ail->name.s, si->name.s, ail->name.len)!=0)))
  1050. add_alias(ail->name.s, ail->name.len, si->port_no,
  1051. si->proto);
  1052. /* remove ail*/
  1053. addr_info_listrm(&si->addr_info_lst, ail);
  1054. free_addr_info(ail);
  1055. ail=ail_next;
  1056. continue;
  1057. }
  1058. /* 2. check if the extra addresses contain a duplicates for
  1059. * other addresses in the same list */
  1060. for (tmp_ail=ail->next; tmp_ail;){
  1061. tmp_ail_next=tmp_ail->next;
  1062. if ((ail->address.af==tmp_ail->address.af) &&
  1063. (memcmp(ail->address.u.addr, tmp_ail->address.u.addr,
  1064. ail->address.len) == 0)){
  1065. /* add the name to the alias list*/
  1066. if ((!(tmp_ail->flags& SI_IS_IP)) && (
  1067. (ail->name.len!=tmp_ail->name.len)||
  1068. (strncmp(ail->name.s, tmp_ail->name.s,
  1069. tmp_ail->name.len)!=0))
  1070. )
  1071. add_alias(tmp_ail->name.s, tmp_ail->name.len,
  1072. si->port_no, si->proto);
  1073. /* remove tmp_ail*/
  1074. addr_info_listrm(&si->addr_info_lst, tmp_ail);
  1075. free_addr_info(tmp_ail);
  1076. }
  1077. tmp_ail=tmp_ail_next;
  1078. }
  1079. ail=ail_next;
  1080. }
  1081. /* check for duplicates between extra addresses (e.g. sctp MH)
  1082. * and other main addresses, on conflict remove the corresponding
  1083. * extra addresses (another possible solution would be to join
  1084. * the 2 si entries into one). */
  1085. for (ail=si->addr_info_lst; ail;){
  1086. ail_next=ail->next;
  1087. for (l=*list;l; l=l->next){
  1088. if (l==si) continue;
  1089. if (si->port_no==l->port_no){
  1090. if ((ail->address.af==l->address.af) &&
  1091. (memcmp(ail->address.u.addr, l->address.u.addr,
  1092. ail->address.len) == 0)){
  1093. /* add the name to the alias list*/
  1094. if ((!(ail->flags& SI_IS_IP)) && (
  1095. (ail->name.len!=l->name.len)||
  1096. (strncmp(ail->name.s, l->name.s, l->name.len)!=0))
  1097. )
  1098. add_alias(ail->name.s, ail->name.len,
  1099. l->port_no, l->proto);
  1100. /* remove ail*/
  1101. addr_info_listrm(&si->addr_info_lst, ail);
  1102. free_addr_info(ail);
  1103. break;
  1104. }
  1105. /* check for duplicates with other extra addresses
  1106. * lists */
  1107. for (tmp_ail=l->addr_info_lst; tmp_ail; ){
  1108. tmp_ail_next=tmp_ail->next;
  1109. if ((ail->address.af==tmp_ail->address.af) &&
  1110. (memcmp(ail->address.u.addr,
  1111. tmp_ail->address.u.addr,
  1112. ail->address.len) == 0)){
  1113. /* add the name to the alias list*/
  1114. if ((!(tmp_ail->flags& SI_IS_IP)) && (
  1115. (ail->name.len!=tmp_ail->name.len)||
  1116. (strncmp(ail->name.s, tmp_ail->name.s,
  1117. tmp_ail->name.len)!=0))
  1118. )
  1119. add_alias(tmp_ail->name.s, tmp_ail->name.len,
  1120. l->port_no, l->proto);
  1121. /* remove tmp_ail*/
  1122. addr_info_listrm(&l->addr_info_lst, tmp_ail);
  1123. free_addr_info(tmp_ail);
  1124. }
  1125. tmp_ail=tmp_ail_next;
  1126. }
  1127. }
  1128. }
  1129. ail=ail_next;
  1130. }
  1131. }
  1132. #ifdef USE_MCAST
  1133. /* Remove invalid multicast entries */
  1134. si=*list;
  1135. while(si){
  1136. if ((si->proto == PROTO_TCP)
  1137. #ifdef USE_TLS
  1138. || (si->proto == PROTO_TLS)
  1139. #endif /* USE_TLS */
  1140. #ifdef USE_SCTP
  1141. || (si->proto == PROTO_SCTP)
  1142. #endif
  1143. ){
  1144. if (si->flags & SI_IS_MCAST){
  1145. LOG(L_WARN, "WARNING: removing entry %s:%s [%s]:%s\n",
  1146. get_proto_name(si->proto), si->name.s,
  1147. si->address_str.s, si->port_no_str.s);
  1148. l = si;
  1149. si=si->next;
  1150. sock_listrm(list, l);
  1151. free_sock_info(l);
  1152. }else{
  1153. ail=si->addr_info_lst;
  1154. while(ail){
  1155. if (ail->flags & SI_IS_MCAST){
  1156. LOG(L_WARN, "WARNING: removing mh entry %s:%s"
  1157. " [%s]:%s\n",
  1158. get_proto_name(si->proto), ail->name.s,
  1159. ail->address_str.s, si->port_no_str.s);
  1160. tmp_ail=ail;
  1161. ail=ail->next;
  1162. addr_info_listrm(&si->addr_info_lst, tmp_ail);
  1163. free_addr_info(tmp_ail);
  1164. }else{
  1165. ail=ail->next;
  1166. }
  1167. }
  1168. si=si->next;
  1169. }
  1170. } else {
  1171. si=si->next;
  1172. }
  1173. }
  1174. #endif /* USE_MCAST */
  1175. return 0;
  1176. error:
  1177. return -1;
  1178. }
  1179. int socket_types = 0;
  1180. /* fix all 3 socket lists, fills socket_types if non-null
  1181. * return 0 on success, -1 on error */
  1182. int fix_all_socket_lists()
  1183. {
  1184. struct utsname myname;
  1185. int flags;
  1186. struct addr_info* ai_lst;
  1187. ai_lst=0;
  1188. if ((udp_listen==0)
  1189. #ifdef USE_TCP
  1190. && (tcp_listen==0)
  1191. #ifdef USE_TLS
  1192. && (tls_listen==0)
  1193. #endif
  1194. #endif
  1195. #ifdef USE_SCTP
  1196. && (sctp_listen==0)
  1197. #endif
  1198. ){
  1199. /* get all listening ipv4 interfaces */
  1200. if ((add_interfaces(0, AF_INET, 0, PROTO_UDP, &ai_lst)==0) &&
  1201. (addr_info_to_si_lst(ai_lst, 0, PROTO_UDP, 0, &udp_listen)==0)){
  1202. free_addr_info_lst(&ai_lst);
  1203. ai_lst=0;
  1204. /* if ok, try to add the others too */
  1205. #ifdef USE_TCP
  1206. if (!tcp_disable){
  1207. if ((add_interfaces(0, AF_INET, 0, PROTO_TCP, &ai_lst)!=0) ||
  1208. (addr_info_to_si_lst(ai_lst, 0, PROTO_TCP, 0,
  1209. &tcp_listen)!=0))
  1210. goto error;
  1211. free_addr_info_lst(&ai_lst);
  1212. ai_lst=0;
  1213. #ifdef USE_TLS
  1214. if (!tls_disable){
  1215. if ((add_interfaces(0, AF_INET, 0, PROTO_TLS,
  1216. &ai_lst)!=0) ||
  1217. (addr_info_to_si_lst(ai_lst, 0, PROTO_TLS, 0,
  1218. &tls_listen)!=0))
  1219. goto error;
  1220. }
  1221. free_addr_info_lst(&ai_lst);
  1222. ai_lst=0;
  1223. #endif
  1224. }
  1225. #endif
  1226. #ifdef USE_SCTP
  1227. if (!sctp_disable){
  1228. if ((add_interfaces(0, AF_INET, 0, PROTO_SCTP, &ai_lst)!=0)||
  1229. (addr_info_to_si_lst(ai_lst, 0, PROTO_SCTP, 0,
  1230. &sctp_listen)!=0))
  1231. goto error;
  1232. free_addr_info_lst(&ai_lst);
  1233. ai_lst=0;
  1234. }
  1235. #endif /* USE_SCTP */
  1236. }else{
  1237. /* if error fall back to get hostname */
  1238. /* get our address, only the first one */
  1239. if (uname (&myname) <0){
  1240. LOG(L_ERR, "ERROR: fix_all_socket_lists: cannot determine"
  1241. " hostname, try -l address\n");
  1242. goto error;
  1243. }
  1244. if (add_listen_iface(myname.nodename, 0, 0, 0, 0)!=0){
  1245. LOG(L_ERR, "ERROR: fix_all_socket_lists: add_listen_iface "
  1246. "failed \n");
  1247. goto error;
  1248. }
  1249. }
  1250. }
  1251. flags=0;
  1252. if (fix_socket_list(&udp_listen, &flags)!=0){
  1253. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1254. " udp failed\n");
  1255. goto error;
  1256. }
  1257. if (flags){
  1258. socket_types|=flags|SOCKET_T_UDP;
  1259. }
  1260. #ifdef USE_TCP
  1261. flags=0;
  1262. if (!tcp_disable && (fix_socket_list(&tcp_listen, &flags)!=0)){
  1263. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1264. " tcp failed\n");
  1265. goto error;
  1266. }
  1267. if (flags){
  1268. socket_types|=flags|SOCKET_T_TCP;
  1269. }
  1270. #ifdef USE_TLS
  1271. flags=0;
  1272. if (!tls_disable && (fix_socket_list(&tls_listen, &flags)!=0)){
  1273. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1274. " tls failed\n");
  1275. goto error;
  1276. }
  1277. if (flags){
  1278. socket_types|=flags|SOCKET_T_TLS;
  1279. }
  1280. #endif
  1281. #endif
  1282. #ifdef USE_SCTP
  1283. flags=0;
  1284. if (!sctp_disable && (fix_socket_list(&sctp_listen, &flags)!=0)){
  1285. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1286. " sctp failed\n");
  1287. goto error;
  1288. }
  1289. if (flags){
  1290. socket_types|=flags|SOCKET_T_SCTP;
  1291. }
  1292. #endif /* USE_SCTP */
  1293. if ((udp_listen==0)
  1294. #ifdef USE_TCP
  1295. && (tcp_listen==0)
  1296. #ifdef USE_TLS
  1297. && (tls_listen==0)
  1298. #endif
  1299. #endif
  1300. #ifdef USE_SCTP
  1301. && (sctp_listen==0)
  1302. #endif
  1303. ){
  1304. LOG(L_ERR, "ERROR: fix_all_socket_lists: no listening sockets\n");
  1305. goto error;
  1306. }
  1307. return 0;
  1308. error:
  1309. if (ai_lst) free_addr_info_lst(&ai_lst);
  1310. return -1;
  1311. }
  1312. void print_all_socket_lists()
  1313. {
  1314. struct socket_info *si;
  1315. struct socket_info** list;
  1316. struct addr_info* ai;
  1317. unsigned short proto;
  1318. proto=PROTO_UDP;
  1319. do{
  1320. list=get_sock_info_list(proto);
  1321. for(si=list?*list:0; si; si=si->next){
  1322. if (si->addr_info_lst){
  1323. printf(" %s: (%s",
  1324. get_proto_name(proto),
  1325. si->address_str.s);
  1326. for (ai=si->addr_info_lst; ai; ai=ai->next)
  1327. printf(", %s", ai->address_str.s);
  1328. printf("):%s%s%s\n",
  1329. si->port_no_str.s,
  1330. si->flags & SI_IS_MCAST ? " mcast" : "",
  1331. si->flags & SI_IS_MHOMED? " mhomed" : "");
  1332. }else{
  1333. printf(" %s: %s",
  1334. get_proto_name(proto),
  1335. si->name.s);
  1336. if (!si->flags & SI_IS_IP)
  1337. printf(" [%s]", si->address_str.s);
  1338. printf( ":%s%s%s\n",
  1339. si->port_no_str.s,
  1340. si->flags & SI_IS_MCAST ? " mcast" : "",
  1341. si->flags & SI_IS_MHOMED? " mhomed" : "");
  1342. }
  1343. }
  1344. }while((proto=next_proto(proto)));
  1345. }
  1346. void print_aliases()
  1347. {
  1348. struct host_alias* a;
  1349. for(a=aliases; a; a=a->next)
  1350. if (a->port)
  1351. printf(" %s: %.*s:%d\n", get_proto_name(a->proto),
  1352. a->alias.len, a->alias.s, a->port);
  1353. else
  1354. printf(" %s: %.*s:*\n", get_proto_name(a->proto),
  1355. a->alias.len, a->alias.s);
  1356. }
  1357. void init_proto_order()
  1358. {
  1359. int r;
  1360. /* fix proto list (remove disabled protocols)*/
  1361. #ifdef USE_TCP
  1362. if (tcp_disable)
  1363. #endif
  1364. for(r=PROTO_NONE; r<=PROTO_LAST; r++){
  1365. if (nxt_proto[r]==PROTO_TCP)
  1366. nxt_proto[r]=nxt_proto[PROTO_TCP];
  1367. }
  1368. #ifdef USE_TCP
  1369. #ifdef USE_TLS
  1370. if (tls_disable || tcp_disable)
  1371. #endif
  1372. #endif
  1373. for(r=PROTO_NONE; r<=PROTO_LAST; r++){
  1374. if (nxt_proto[r]==PROTO_TLS)
  1375. nxt_proto[r]=nxt_proto[PROTO_TLS];
  1376. }
  1377. #ifdef USE_SCTP
  1378. if (sctp_disable)
  1379. #endif
  1380. for(r=PROTO_NONE; r<=PROTO_LAST; r++){
  1381. if (nxt_proto[r]==PROTO_SCTP)
  1382. nxt_proto[r]=nxt_proto[PROTO_SCTP];
  1383. }
  1384. }