socket_info.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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. }
  227. }
  228. static char* get_proto_name(unsigned short proto)
  229. {
  230. switch(proto){
  231. case PROTO_NONE:
  232. return "*";
  233. case PROTO_UDP:
  234. return "udp";
  235. #ifdef USE_TCP
  236. case PROTO_TCP:
  237. return "tcp";
  238. #endif
  239. #ifdef USE_TLS
  240. case PROTO_TLS:
  241. return "tls";
  242. #endif
  243. #ifdef USE_SCTP
  244. case PROTO_SCTP:
  245. return "sctp";
  246. #endif
  247. default:
  248. return "unknown";
  249. }
  250. }
  251. /* returns 0 if support for the protocol is not compiled or if proto is
  252. invalid */
  253. static struct socket_info** get_sock_info_list(unsigned short proto)
  254. {
  255. switch(proto){
  256. case PROTO_UDP:
  257. return &udp_listen;
  258. break;
  259. case PROTO_TCP:
  260. #ifdef USE_TCP
  261. return &tcp_listen;
  262. #endif
  263. break;
  264. case PROTO_TLS:
  265. #ifdef USE_TLS
  266. return &tls_listen;
  267. #endif
  268. break;
  269. case PROTO_SCTP:
  270. #ifdef USE_SCTP
  271. return &sctp_listen;
  272. #endif
  273. break;
  274. default:
  275. LOG(L_CRIT, "BUG: get_sock_info_list: invalid proto %d\n", proto);
  276. }
  277. return 0;
  278. }
  279. /* helper function for grep_sock_info
  280. * params:
  281. * host - hostname to compare with
  282. * name - official name
  283. * addr_str - name's resolved ip address converted to string
  284. * ip_addr - name's ip address
  285. * flags - set to SI_IS_IP if name contains an IP
  286. *
  287. * returns 0 if host matches, -1 if not */
  288. inline static int si_hname_cmp(str* host, str* name, str* addr_str,
  289. struct ip_addr* ip_addr, int flags)
  290. {
  291. #ifdef USE_IPV6
  292. struct ip_addr* ip6;
  293. #endif
  294. if ( (host->len==name->len) &&
  295. (strncasecmp(host->s, name->s, name->len)==0) /*slower*/)
  296. /* comp. must be case insensitive, host names
  297. * can be written in mixed case, it will also match
  298. * ipv6 addresses if we are lucky*/
  299. goto found;
  300. /* check if host == ip address */
  301. #ifdef USE_IPV6
  302. /* ipv6 case is uglier, host can be [3ffe::1] */
  303. ip6=str2ip6(host);
  304. if (ip6){
  305. if (ip_addr_cmp(ip6, ip_addr))
  306. goto found; /* match */
  307. else
  308. return -1; /* no match, but this is an ipv6 address
  309. so no point in trying ipv4 */
  310. }
  311. #endif
  312. /* ipv4 */
  313. if ( (!(flags&SI_IS_IP)) && (host->len==addr_str->len) &&
  314. (memcmp(host->s, addr_str->s, addr_str->len)==0) )
  315. goto found;
  316. return -1;
  317. found:
  318. return 0;
  319. }
  320. /* checks if the proto: host:port is one of the address we listen on
  321. * and returns the corresponding socket_info structure.
  322. * if port==0, the port number is ignored
  323. * if proto==0 (PROTO_NONE) the protocol is ignored
  324. * returns 0 if not found
  325. * WARNING: uses str2ip6 so it will overwrite any previous
  326. * unsaved result of this function (static buffer)
  327. */
  328. struct socket_info* grep_sock_info(str* host, unsigned short port,
  329. unsigned short proto)
  330. {
  331. str hname;
  332. struct socket_info* si;
  333. struct socket_info** list;
  334. struct addr_info* ai;
  335. unsigned short c_proto;
  336. hname=*host;
  337. #ifdef USE_IPV6
  338. if ((hname.len>2)&&((*hname.s)=='[')&&(hname.s[hname.len-1]==']')){
  339. /* ipv6 reference, skip [] */
  340. hname.s++;
  341. hname.len-=2;
  342. }
  343. #endif
  344. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  345. do{
  346. /* get the proper sock_list */
  347. list=get_sock_info_list(c_proto);
  348. if (list==0) /* disabled or unknown protocol */
  349. continue;
  350. for (si=*list; si; si=si->next){
  351. DBG("grep_sock_info - checking if host==us: %d==%d && "
  352. " [%.*s] == [%.*s]\n",
  353. hname.len,
  354. si->name.len,
  355. hname.len, hname.s,
  356. si->name.len, si->name.s
  357. );
  358. if (port) {
  359. DBG("grep_sock_info - checking if port %d matches port %d\n",
  360. si->port_no, port);
  361. if (si->port_no!=port) {
  362. continue;
  363. }
  364. }
  365. if (si_hname_cmp(&hname, &si->name, &si->address_str,
  366. &si->address, si->flags)==0)
  367. goto found;
  368. /* try among the extra addresses */
  369. for (ai=si->addr_info_lst; ai; ai=ai->next)
  370. if (si_hname_cmp(&hname, &ai->name, &ai->address_str,
  371. &ai->address, ai->flags)==0)
  372. goto found;
  373. }
  374. }while( (proto==0) && (c_proto=next_proto(c_proto)) );
  375. /* not_found: */
  376. return 0;
  377. found:
  378. return si;
  379. }
  380. /* checks if the proto:port is one of the ports we listen on
  381. * and returns the corresponding socket_info structure.
  382. * if proto==0 (PROTO_NONE) the protocol is ignored
  383. * returns 0 if not found
  384. */
  385. struct socket_info* grep_sock_info_by_port(unsigned short port,
  386. unsigned short proto)
  387. {
  388. struct socket_info* si;
  389. struct socket_info** list;
  390. unsigned short c_proto;
  391. if (!port) {
  392. goto not_found;
  393. }
  394. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  395. do{
  396. /* get the proper sock_list */
  397. list=get_sock_info_list(c_proto);
  398. if (list==0) /* disabled or unknown protocol */
  399. continue;
  400. for (si=*list; si; si=si->next){
  401. DBG("grep_sock_info_by_port - checking if port %d matches"
  402. " port %d\n", si->port_no, port);
  403. if (si->port_no==port) {
  404. goto found;
  405. }
  406. }
  407. }while( (proto==0) && (c_proto=next_proto(c_proto)) );
  408. not_found:
  409. return 0;
  410. found:
  411. return si;
  412. }
  413. /* checks if the proto: ip:port is one of the address we listen on
  414. * and returns the corresponding socket_info structure.
  415. * (same as grep_socket_info, but use ip addr instead)
  416. * if port==0, the port number is ignored
  417. * if proto==0 (PROTO_NONE) the protocol is ignored
  418. * returns 0 if not found
  419. * WARNING: uses str2ip6 so it will overwrite any previous
  420. * unsaved result of this function (static buffer)
  421. */
  422. struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
  423. unsigned short proto)
  424. {
  425. struct socket_info* si;
  426. struct socket_info** list;
  427. struct addr_info* ai;
  428. unsigned short c_proto;
  429. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  430. do{
  431. /* get the proper sock_list */
  432. list=get_sock_info_list(c_proto);
  433. if (list==0) /* disabled or unknown protocol */
  434. continue;
  435. for (si=*list; si; si=si->next){
  436. if (port) {
  437. if (si->port_no!=port) {
  438. continue;
  439. }
  440. }
  441. if (ip_addr_cmp(ip, &si->address))
  442. goto found;
  443. for (ai=si->addr_info_lst; ai; ai=ai->next)
  444. if (ip_addr_cmp(ip, &ai->address))
  445. goto found;
  446. }
  447. }while( (proto==0) && (c_proto=next_proto(c_proto)) );
  448. /* not_found: */
  449. return 0;
  450. found:
  451. return si;
  452. }
  453. /* append a new sock_info structure to the corresponding list
  454. * return new sock info on success, 0 on error */
  455. static struct socket_info* new_sock2list(char* name, struct name_lst* addr_l,
  456. unsigned short port,
  457. unsigned short proto, enum si_flags flags,
  458. struct socket_info** list)
  459. {
  460. struct socket_info* si;
  461. si=new_sock_info(name, addr_l, port, proto, flags);
  462. if (si==0){
  463. LOG(L_ERR, "ERROR: new_sock2list: new_sock_info failed\n");
  464. goto error;
  465. }
  466. sock_listadd(list, si);
  467. return si;
  468. error:
  469. return 0;
  470. }
  471. /* adds a new sock_info structure immediately after "after"
  472. * return new sock info on success, 0 on error */
  473. static struct socket_info* new_sock2list_after(char* name,
  474. struct name_lst* addr_l,
  475. unsigned short port,
  476. unsigned short proto, enum si_flags flags,
  477. struct socket_info* after)
  478. {
  479. struct socket_info* si;
  480. si=new_sock_info(name, addr_l, port, proto, flags);
  481. if (si==0){
  482. LOG(L_ERR, "ERROR: new_sock2list_after: new_sock_info failed\n");
  483. goto error;
  484. }
  485. sock_listins(si, after);
  486. return si;
  487. error:
  488. return 0;
  489. }
  490. /* adds a sock_info structure to the corresponding proto list
  491. * return 0 on success, -1 on error */
  492. int add_listen_iface(char* name, struct name_lst* addr_l,
  493. unsigned short port, unsigned short proto,
  494. enum si_flags flags)
  495. {
  496. struct socket_info** list;
  497. unsigned short c_proto;
  498. struct name_lst* a_l;
  499. unsigned short c_port;
  500. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  501. do{
  502. list=get_sock_info_list(c_proto);
  503. if (list==0) /* disabled or unknown protocol */
  504. continue;
  505. if (port==0){ /* use default port */
  506. c_port=
  507. #ifdef USE_TLS
  508. ((c_proto)==PROTO_TLS)?tls_port_no:
  509. #endif
  510. port_no;
  511. }
  512. #ifdef USE_TLS
  513. else if ((c_proto==PROTO_TLS) && (proto==0)){
  514. /* -l ip:port => on udp:ip:port; tcp:ip:port and tls:ip:port+1?*/
  515. c_port=port+1;
  516. }
  517. #endif
  518. else{
  519. c_port=port;
  520. }
  521. if (c_proto!=PROTO_SCTP){
  522. if (new_sock2list(name, 0, c_port, c_proto,
  523. flags & ~SI_IS_MHOMED, list)==0){
  524. LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
  525. goto error;
  526. }
  527. /* add the other addresses in the list as separate sockets
  528. * since only SCTP can bind to multiple addresses */
  529. for (a_l=addr_l; a_l; a_l=a_l->next){
  530. if (new_sock2list(a_l->name, 0, c_port,
  531. c_proto, flags & ~SI_IS_MHOMED, list)==0){
  532. LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list"
  533. " failed\n");
  534. goto error;
  535. }
  536. }
  537. }else{
  538. if (new_sock2list(name, addr_l, c_port, c_proto, flags, list)==0){
  539. LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
  540. goto error;
  541. }
  542. }
  543. }while( (proto==0) && (c_proto=next_proto(c_proto)));
  544. return 0;
  545. error:
  546. return -1;
  547. }
  548. /* add all family type addresses of interface if_name to the socket_info array
  549. * if if_name==0, adds all addresses on all interfaces
  550. * WARNING: it only works with ipv6 addresses on FreeBSD
  551. * return: -1 on error, 0 on success
  552. */
  553. int add_interfaces(char* if_name, int family, unsigned short port,
  554. unsigned short proto,
  555. struct addr_info** ai_l)
  556. {
  557. struct ifconf ifc;
  558. struct ifreq ifr;
  559. struct ifreq ifrcopy;
  560. char* last;
  561. char* p;
  562. int size;
  563. int lastlen;
  564. int s;
  565. char* tmp;
  566. struct ip_addr addr;
  567. int ret;
  568. enum si_flags flags;
  569. #ifdef HAVE_SOCKADDR_SA_LEN
  570. #ifndef MAX
  571. #define MAX(a,b) ( ((a)>(b))?(a):(b))
  572. #endif
  573. #endif
  574. /* ipv4 or ipv6 only*/
  575. flags=SI_NONE;
  576. s=socket(family, SOCK_DGRAM, 0);
  577. ret=-1;
  578. lastlen=0;
  579. ifc.ifc_req=0;
  580. for (size=100; ; size*=2){
  581. ifc.ifc_len=size*sizeof(struct ifreq);
  582. ifc.ifc_req=(struct ifreq*) pkg_malloc(size*sizeof(struct ifreq));
  583. if (ifc.ifc_req==0){
  584. LOG(L_ERR, "ERROR: add_interfaces: memory allocation failure\n");
  585. goto error;
  586. }
  587. if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
  588. if(errno==EBADF) return 0; /* invalid descriptor => no such ifs*/
  589. LOG(L_ERR, "ERROR: add_interfaces: ioctl failed: %s\n",
  590. strerror(errno));
  591. goto error;
  592. }
  593. if ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
  594. len not changed*/
  595. lastlen=ifc.ifc_len;
  596. /* try a bigger array*/
  597. pkg_free(ifc.ifc_req);
  598. }
  599. last=(char*)ifc.ifc_req+ifc.ifc_len;
  600. for(p=(char*)ifc.ifc_req; p<last;
  601. p+=
  602. #ifdef __OS_linux
  603. sizeof(ifr) /* works on x86_64 too */
  604. #else
  605. (sizeof(ifr.ifr_name)+
  606. #ifdef HAVE_SOCKADDR_SA_LEN
  607. MAX(ifr.ifr_addr.sa_len, sizeof(struct sockaddr))
  608. #else
  609. ( (ifr.ifr_addr.sa_family==AF_INET)?
  610. sizeof(struct sockaddr_in):
  611. #ifdef USE_IPV6
  612. ((ifr.ifr_addr.sa_family==AF_INET6)?
  613. sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
  614. #else /* USE_IPV6 */
  615. sizeof(struct sockaddr) )
  616. #endif /* USE_IPV6 */
  617. #endif
  618. )
  619. #endif
  620. )
  621. {
  622. /* copy contents into ifr structure
  623. * warning: it might be longer (e.g. ipv6 address) */
  624. memcpy(&ifr, p, sizeof(ifr));
  625. if (ifr.ifr_addr.sa_family!=family){
  626. /*printf("strange family %d skipping...\n",
  627. ifr->ifr_addr.sa_family);*/
  628. continue;
  629. }
  630. /*get flags*/
  631. ifrcopy=ifr;
  632. if (ioctl(s, SIOCGIFFLAGS, &ifrcopy)!=-1){ /* ignore errors */
  633. /* ignore down ifs only if listening on all of them*/
  634. if (if_name==0){
  635. /* if if not up, skip it*/
  636. if (!(ifrcopy.ifr_flags & IFF_UP)) continue;
  637. }
  638. }
  639. if ((if_name==0)||
  640. (strncmp(if_name, ifr.ifr_name, sizeof(ifr.ifr_name))==0)){
  641. /*add address*/
  642. sockaddr2ip_addr(&addr,
  643. (struct sockaddr*)(p+(long)&((struct ifreq*)0)->ifr_addr));
  644. if ((tmp=ip_addr2a(&addr))==0) goto error;
  645. /* check if loopback */
  646. if (ifrcopy.ifr_flags & IFF_LOOPBACK)
  647. flags|=SI_IS_LO;
  648. /* save the info */
  649. if (new_addr_info2list(tmp, flags, ai_l)!=0){
  650. LOG(L_ERR, "ERROR: add_interfaces: "
  651. "new_addr_info2list failed\n");
  652. goto error;
  653. }
  654. ret=0;
  655. }
  656. /*
  657. printf("%s:\n", ifr->ifr_name);
  658. printf(" ");
  659. print_sockaddr(&(ifr->ifr_addr));
  660. printf(" ");
  661. ls_ifflags(ifr->ifr_name, family, options);
  662. printf("\n");*/
  663. }
  664. pkg_free(ifc.ifc_req); /*clean up*/
  665. close(s);
  666. return ret;
  667. error:
  668. if (ifc.ifc_req) pkg_free(ifc.ifc_req);
  669. close(s);
  670. return -1;
  671. }
  672. /* internal helper function: resolve host names and add aliases
  673. * name is a value result parameter: it should contain the hostname that
  674. * will be used to fill all the other members, including name itself
  675. * in some situation (name->s should be a 0 terminated pkg_malloc'ed string)
  676. * return 0 on success and -1 on error */
  677. static int fix_hostname(str* name, struct ip_addr* address, str* address_str,
  678. enum si_flags* flags, int* type_flags,
  679. struct socket_info* s)
  680. {
  681. struct hostent* he;
  682. char* tmp;
  683. char** h;
  684. /* get "official hostnames", all the aliases etc. */
  685. he=resolvehost(name->s);
  686. if (he==0){
  687. LOG(L_ERR, "ERROR: fix_hostname: could not resolve %s\n", name->s);
  688. goto error;
  689. }
  690. /* check if we got the official name */
  691. if (strcasecmp(he->h_name, name->s)!=0){
  692. if (add_alias(name->s, name->len, s->port_no, s->proto)<0){
  693. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  694. }
  695. /* change the official name */
  696. pkg_free(name->s);
  697. name->s=(char*)pkg_malloc(strlen(he->h_name)+1);
  698. if (name->s==0){
  699. LOG(L_ERR, "ERROR: fix_hostname: out of memory.\n");
  700. goto error;
  701. }
  702. name->len=strlen(he->h_name);
  703. strncpy(name->s, he->h_name, name->len+1);
  704. }
  705. /* add the aliases*/
  706. for(h=he->h_aliases; h && *h; h++)
  707. if (add_alias(*h, strlen(*h), s->port_no, s->proto)<0){
  708. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  709. }
  710. hostent2ip_addr(address, he, 0); /*convert to ip_addr format*/
  711. if (type_flags){
  712. *type_flags|=(address->af==AF_INET)?SOCKET_T_IPV4:SOCKET_T_IPV6;
  713. }
  714. if ((tmp=ip_addr2a(address))==0) goto error;
  715. address_str->s=pkg_malloc(strlen(tmp)+1);
  716. if (address_str->s==0){
  717. LOG(L_ERR, "ERROR: fix_hostname: out of memory.\n");
  718. goto error;
  719. }
  720. strncpy(address_str->s, tmp, strlen(tmp)+1);
  721. /* set is_ip (1 if name is an ip address, 0 otherwise) */
  722. address_str->len=strlen(tmp);
  723. if ((address_str->len==name->len) &&
  724. (strncasecmp(address_str->s, name->s, address_str->len)==0)){
  725. *flags|=SI_IS_IP;
  726. /* do rev. DNS on it (for aliases)*/
  727. he=rev_resolvehost(address);
  728. if (he==0){
  729. LOG(L_WARN, "WARNING: fix_hostname: could not rev. resolve %s\n",
  730. name->s);
  731. }else{
  732. /* add the aliases*/
  733. if (add_alias(he->h_name, strlen(he->h_name), s->port_no,
  734. s->proto)<0){
  735. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  736. }
  737. for(h=he->h_aliases; h && *h; h++)
  738. if (add_alias(*h, strlen(*h), s->port_no, s->proto) < 0){
  739. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  740. }
  741. }
  742. }
  743. #ifdef USE_MCAST
  744. /* Check if it is an multicast address and
  745. * set the flag if so
  746. */
  747. if (is_mcast(address)){
  748. *flags |= SI_IS_MCAST;
  749. }
  750. #endif /* USE_MCAST */
  751. /* check if INADDR_ANY */
  752. if (ip_addr_any(address))
  753. *flags|=SI_IS_ANY;
  754. else if (ip_addr_loopback(address)) /* check for loopback */
  755. *flags|=SI_IS_LO;
  756. return 0;
  757. error:
  758. return -1;
  759. }
  760. /* append new elements to a socket_info list after "list"
  761. * each element is created from addr_info_lst + port, protocol and flags
  762. * return 0 on succes, -1 on error
  763. */
  764. static int addr_info_to_si_lst(struct addr_info* ai_lst, unsigned short port,
  765. char proto, enum si_flags flags,
  766. struct socket_info** list)
  767. {
  768. struct addr_info* ail;
  769. for (ail=ai_lst; ail; ail=ail->next){
  770. if(new_sock2list(ail->name.s, 0, port_no, proto, ail->flags | flags,
  771. list)==0)
  772. return -1;
  773. }
  774. return 0;
  775. }
  776. /* insert new elements to a socket_info list after "el",
  777. * each element is created from addr_info_lst + port, * protocol and flags
  778. * return 0 on succes, -1 on error
  779. */
  780. static int addr_info_to_si_lst_after(struct addr_info* ai_lst,
  781. unsigned short port,
  782. char proto, enum si_flags flags,
  783. struct socket_info* el)
  784. {
  785. struct addr_info* ail;
  786. struct socket_info* new_si;
  787. for (ail=ai_lst; ail; ail=ail->next){
  788. if((new_si=new_sock2list_after(ail->name.s, 0, port_no, proto,
  789. ail->flags | flags, el))==0)
  790. return -1;
  791. el=new_si;
  792. }
  793. return 0;
  794. }
  795. /* fixes a socket list => resolve addresses,
  796. * interface names, fills missing members, remove duplicates
  797. * fills type_flags if not null with SOCKET_T_IPV4 and/or SOCKET_T_IPV6*/
  798. static int fix_socket_list(struct socket_info **list, int* type_flags)
  799. {
  800. struct socket_info* si;
  801. struct socket_info* new_si;
  802. struct socket_info* l;
  803. struct socket_info* next;
  804. struct socket_info* next_si;
  805. struct socket_info* del_si;
  806. struct socket_info* keep_si;
  807. char* tmp;
  808. int len;
  809. struct addr_info* ai_lst;
  810. struct addr_info* ail;
  811. struct addr_info* tmp_ail;
  812. struct addr_info* tmp_ail_next;
  813. struct addr_info* ail_next;
  814. if (type_flags)
  815. *type_flags=0;
  816. /* try to change all the interface names into addresses
  817. * --ugly hack */
  818. for (si=*list;si;){
  819. next=si->next;
  820. ai_lst=0;
  821. if (add_interfaces(si->name.s, AF_INET, si->port_no,
  822. si->proto, &ai_lst)!=-1){
  823. if (si->flags & SI_IS_MHOMED){
  824. if((new_si=new_sock2list_after(ai_lst->name.s, 0, si->port_no,
  825. si->proto,
  826. ai_lst->flags|si->flags, si))==0)
  827. break;
  828. ail=ai_lst;
  829. ai_lst=ai_lst->next;
  830. free_addr_info(ail); /* free the first elem. */
  831. if (ai_lst){
  832. ai_lst->prev=0;
  833. /* find the end */
  834. for (ail=ai_lst; ail->next; ail=ail->next);
  835. /* add the mh list after the last position in ai_lst */
  836. addr_info_list_ins_lst(si->addr_info_lst, ail);
  837. new_si->addr_info_lst=ai_lst;
  838. si->addr_info_lst=0; /* detached and moved to new_si */
  839. ail=ail->next; /* ail== old si->addr_info_lst */
  840. }else{
  841. ail=si->addr_info_lst;
  842. new_si->addr_info_lst=ail;
  843. si->addr_info_lst=0; /* detached and moved to new_si */
  844. }
  845. }else{
  846. /* add all addr. as separate interfaces */
  847. if (addr_info_to_si_lst_after(ai_lst, si->port_no, si->proto,
  848. si->flags, si)!=0)
  849. goto error;
  850. /* ai_lst not needed anymore */
  851. free_addr_info_lst(&ai_lst);
  852. ail=0;
  853. new_si=0;
  854. }
  855. /* success => remove current entry (shift the entire array)*/
  856. sock_listrm(list, si);
  857. free_sock_info(si);
  858. }else{
  859. new_si=si;
  860. ail=si->addr_info_lst;
  861. }
  862. if (ail){
  863. if (new_si && (new_si->flags & SI_IS_MHOMED)){
  864. ai_lst=0;
  865. for (; ail;){
  866. ail_next=ail->next;
  867. if (add_interfaces(ail->name.s, AF_INET, new_si->port_no,
  868. new_si->proto, &ai_lst)!=-1){
  869. /* add the resolved list after the current position */
  870. addr_info_list_ins_lst(ai_lst, ail);
  871. /* success, remove the current entity */
  872. addr_info_listrm(&new_si->addr_info_lst, ail);
  873. free_addr_info(ail);
  874. ai_lst=0;
  875. }
  876. ail=ail_next;
  877. }
  878. }
  879. }
  880. si=next;
  881. }
  882. /* get ips & fill the port numbers*/
  883. #ifdef EXTRA_DEBUG
  884. DBG("Listening on \n");
  885. #endif
  886. for (si=*list;si;si=si->next){
  887. /* fix port number, port_no should be !=0 here */
  888. if (si->port_no==0){
  889. #ifdef USE_TLS
  890. si->port_no= (si->proto==PROTO_TLS)?tls_port_no:port_no;
  891. #else
  892. si->port_no= port_no;
  893. #endif
  894. }
  895. tmp=int2str(si->port_no, &len);
  896. if (len>=MAX_PORT_LEN){
  897. LOG(L_ERR, "ERROR: fix_socket_list: bad port number: %d\n",
  898. si->port_no);
  899. goto error;
  900. }
  901. si->port_no_str.s=(char*)pkg_malloc(len+1);
  902. if (si->port_no_str.s==0){
  903. LOG(L_ERR, "ERROR: fix_socket_list: out of memory.\n");
  904. goto error;
  905. }
  906. strncpy(si->port_no_str.s, tmp, len+1);
  907. si->port_no_str.len=len;
  908. if (fix_hostname(&si->name, &si->address, &si->address_str,
  909. &si->flags, type_flags, si) !=0 )
  910. goto error;
  911. /* fix hostnames in mh addresses */
  912. for (ail=si->addr_info_lst; ail; ail=ail->next){
  913. if (fix_hostname(&ail->name, &ail->address, &ail->address_str,
  914. &ail->flags, type_flags, si) !=0 )
  915. goto error;
  916. }
  917. #ifdef EXTRA_DEBUG
  918. printf(" %.*s [%s]:%s%s\n", si->name.len,
  919. si->name.s, si->address_str.s, si->port_no_str.s,
  920. si->flags & SI_IS_MCAST ? " mcast" : "");
  921. #endif
  922. }
  923. /* removing duplicate addresses*/
  924. for (si=*list;si; ){
  925. next_si=si->next;
  926. for (l=si->next;l;){
  927. next=l->next;
  928. if ((si->port_no==l->port_no) &&
  929. (si->address.af==l->address.af) &&
  930. (memcmp(si->address.u.addr, l->address.u.addr,
  931. si->address.len) == 0)
  932. ){
  933. /* remove the socket with no extra addresses.,
  934. * if both of them have extra addresses, remove one of them
  935. * and merge the extra addresses into the other */
  936. if (l->addr_info_lst==0){
  937. del_si=l;
  938. keep_si=si;
  939. }else if (si->addr_info_lst==0){
  940. del_si=si;
  941. keep_si=l;
  942. }else{
  943. /* move l->addr_info_lst to si->addr_info_lst */
  944. /* find last elem */
  945. for (ail=si->addr_info_lst; ail->next; ail=ail->next);
  946. /* add the l list after the last position in si lst */
  947. addr_info_list_ins_lst(l->addr_info_lst, ail);
  948. l->addr_info_lst=0; /* detached */
  949. del_si=l; /* l will be removed */
  950. keep_si=l;
  951. }
  952. #ifdef EXTRA_DEBUG
  953. printf("removing duplicate %s [%s] == %s [%s]\n",
  954. keep_si->name.s, keep_si->address_str.s,
  955. del_si->name.s, del_si->address_str.s);
  956. #endif
  957. /* add the name to the alias list*/
  958. if ((!(del_si->flags& SI_IS_IP)) && (
  959. (del_si->name.len!=keep_si->name.len)||
  960. (strncmp(del_si->name.s, keep_si->name.s,
  961. del_si->name.len)!=0))
  962. )
  963. add_alias(del_si->name.s, del_si->name.len,
  964. l->port_no, l->proto);
  965. /* remove del_si*/
  966. sock_listrm(list, del_si);
  967. free_sock_info(del_si);
  968. }
  969. l=next;
  970. }
  971. si=next_si;
  972. }
  973. /* check for duplicates in extra_addresses */
  974. for (si=*list;si; si=si->next){
  975. /* check for & remove internal duplicates: */
  976. for (ail=si->addr_info_lst; ail;){
  977. ail_next=ail->next;
  978. /* 1. check if the extra addresses contain a duplicate for the
  979. * main one */
  980. if ((ail->address.af==si->address.af) &&
  981. (memcmp(ail->address.u.addr, si->address.u.addr,
  982. ail->address.len) == 0)){
  983. /* add the name to the alias list*/
  984. if ((!(ail->flags& SI_IS_IP)) && (
  985. (ail->name.len!=si->name.len)||
  986. (strncmp(ail->name.s, si->name.s, ail->name.len)!=0)))
  987. add_alias(ail->name.s, ail->name.len, si->port_no,
  988. si->proto);
  989. /* remove ail*/
  990. addr_info_listrm(&si->addr_info_lst, ail);
  991. free_addr_info(ail);
  992. ail=ail_next;
  993. continue;
  994. }
  995. /* 2. check if the extra addresses contain a duplicates for
  996. * other addresses in the same list */
  997. for (tmp_ail=ail->next; tmp_ail;){
  998. tmp_ail_next=tmp_ail->next;
  999. if ((ail->address.af==tmp_ail->address.af) &&
  1000. (memcmp(ail->address.u.addr, tmp_ail->address.u.addr,
  1001. ail->address.len) == 0)){
  1002. /* add the name to the alias list*/
  1003. if ((!(tmp_ail->flags& SI_IS_IP)) && (
  1004. (ail->name.len!=tmp_ail->name.len)||
  1005. (strncmp(ail->name.s, tmp_ail->name.s,
  1006. tmp_ail->name.len)!=0))
  1007. )
  1008. add_alias(tmp_ail->name.s, tmp_ail->name.len,
  1009. si->port_no, si->proto);
  1010. /* remove tmp_ail*/
  1011. addr_info_listrm(&si->addr_info_lst, tmp_ail);
  1012. free_addr_info(tmp_ail);
  1013. }
  1014. tmp_ail=tmp_ail_next;
  1015. }
  1016. ail=ail_next;
  1017. }
  1018. /* check for duplicates between extra addresses (e.g. sctp MH)
  1019. * and other main addresses, on conflict remove the corresponding
  1020. * extra addresses (another possible solution would be to join
  1021. * the 2 si entries into one). */
  1022. for (ail=si->addr_info_lst; ail;){
  1023. ail_next=ail->next;
  1024. for (l=*list;l; l=l->next){
  1025. if (l==si) continue;
  1026. if (si->port_no==l->port_no){
  1027. if ((ail->address.af==l->address.af) &&
  1028. (memcmp(ail->address.u.addr, l->address.u.addr,
  1029. ail->address.len) == 0)){
  1030. /* add the name to the alias list*/
  1031. if ((!(ail->flags& SI_IS_IP)) && (
  1032. (ail->name.len!=l->name.len)||
  1033. (strncmp(ail->name.s, l->name.s, l->name.len)!=0))
  1034. )
  1035. add_alias(ail->name.s, ail->name.len,
  1036. l->port_no, l->proto);
  1037. /* remove ail*/
  1038. addr_info_listrm(&si->addr_info_lst, ail);
  1039. free_addr_info(ail);
  1040. break;
  1041. }
  1042. /* check for duplicates with other extra addresses
  1043. * lists */
  1044. for (tmp_ail=l->addr_info_lst; tmp_ail; ){
  1045. tmp_ail_next=tmp_ail->next;
  1046. if ((ail->address.af==tmp_ail->address.af) &&
  1047. (memcmp(ail->address.u.addr,
  1048. tmp_ail->address.u.addr,
  1049. ail->address.len) == 0)){
  1050. /* add the name to the alias list*/
  1051. if ((!(tmp_ail->flags& SI_IS_IP)) && (
  1052. (ail->name.len!=tmp_ail->name.len)||
  1053. (strncmp(ail->name.s, tmp_ail->name.s,
  1054. tmp_ail->name.len)!=0))
  1055. )
  1056. add_alias(tmp_ail->name.s, tmp_ail->name.len,
  1057. l->port_no, l->proto);
  1058. /* remove tmp_ail*/
  1059. addr_info_listrm(&l->addr_info_lst, tmp_ail);
  1060. free_addr_info(tmp_ail);
  1061. }
  1062. tmp_ail=tmp_ail_next;
  1063. }
  1064. }
  1065. }
  1066. ail=ail_next;
  1067. }
  1068. }
  1069. #ifdef USE_MCAST
  1070. /* Remove invalid multicast entries */
  1071. si=*list;
  1072. while(si){
  1073. if ((si->proto == PROTO_TCP)
  1074. #ifdef USE_TLS
  1075. || (si->proto == PROTO_TLS)
  1076. #endif /* USE_TLS */
  1077. #ifdef USE_SCTP
  1078. || (si->proto == PROTO_SCTP)
  1079. #endif
  1080. ){
  1081. if (si->flags & SI_IS_MCAST){
  1082. LOG(L_WARN, "WARNING: removing entry %s:%s [%s]:%s\n",
  1083. get_proto_name(si->proto), si->name.s,
  1084. si->address_str.s, si->port_no_str.s);
  1085. l = si;
  1086. si=si->next;
  1087. sock_listrm(list, l);
  1088. free_sock_info(l);
  1089. }else{
  1090. ail=si->addr_info_lst;
  1091. while(ail){
  1092. if (ail->flags & SI_IS_MCAST){
  1093. LOG(L_WARN, "WARNING: removing mh entry %s:%s"
  1094. " [%s]:%s\n",
  1095. get_proto_name(si->proto), ail->name.s,
  1096. ail->address_str.s, si->port_no_str.s);
  1097. tmp_ail=ail;
  1098. ail=ail->next;
  1099. addr_info_listrm(&si->addr_info_lst, tmp_ail);
  1100. free_addr_info(tmp_ail);
  1101. }else{
  1102. ail=ail->next;
  1103. }
  1104. }
  1105. si=si->next;
  1106. }
  1107. } else {
  1108. si=si->next;
  1109. }
  1110. }
  1111. #endif /* USE_MCAST */
  1112. return 0;
  1113. error:
  1114. return -1;
  1115. }
  1116. int socket_types = 0;
  1117. /* fix all 3 socket lists, fills socket_types if non-null
  1118. * return 0 on success, -1 on error */
  1119. int fix_all_socket_lists()
  1120. {
  1121. struct utsname myname;
  1122. int flags;
  1123. struct addr_info* ai_lst;
  1124. ai_lst=0;
  1125. if ((udp_listen==0)
  1126. #ifdef USE_TCP
  1127. && (tcp_listen==0)
  1128. #ifdef USE_TLS
  1129. && (tls_listen==0)
  1130. #endif
  1131. #endif
  1132. #ifdef USE_SCTP
  1133. && (sctp_listen==0)
  1134. #endif
  1135. ){
  1136. /* get all listening ipv4 interfaces */
  1137. if ((add_interfaces(0, AF_INET, 0, PROTO_UDP, &ai_lst)==0) &&
  1138. (addr_info_to_si_lst(ai_lst, 0, PROTO_UDP, 0, &udp_listen)==0)){
  1139. free_addr_info_lst(&ai_lst);
  1140. ai_lst=0;
  1141. /* if ok, try to add the others too */
  1142. #ifdef USE_TCP
  1143. if (!tcp_disable){
  1144. if ((add_interfaces(0, AF_INET, 0, PROTO_TCP, &ai_lst)!=0) ||
  1145. (addr_info_to_si_lst(ai_lst, 0, PROTO_TCP, 0,
  1146. &tcp_listen)!=0))
  1147. goto error;
  1148. free_addr_info_lst(&ai_lst);
  1149. ai_lst=0;
  1150. #ifdef USE_TLS
  1151. if (!tls_disable){
  1152. if ((add_interfaces(0, AF_INET, 0, PROTO_TLS,
  1153. &ai_lst)!=0) ||
  1154. (addr_info_to_si_lst(ai_lst, 0, PROTO_TLS, 0,
  1155. &tls_listen)!=0))
  1156. goto error;
  1157. }
  1158. free_addr_info_lst(&ai_lst);
  1159. ai_lst=0;
  1160. #endif
  1161. }
  1162. #endif
  1163. #ifdef USE_SCTP
  1164. if (!sctp_disable){
  1165. if ((add_interfaces(0, AF_INET, 0, PROTO_SCTP, &ai_lst)!=0)||
  1166. (addr_info_to_si_lst(ai_lst, 0, PROTO_SCTP, 0,
  1167. &sctp_listen)!=0))
  1168. goto error;
  1169. free_addr_info_lst(&ai_lst);
  1170. ai_lst=0;
  1171. }
  1172. #endif /* USE_SCTP */
  1173. }else{
  1174. /* if error fall back to get hostname */
  1175. /* get our address, only the first one */
  1176. if (uname (&myname) <0){
  1177. LOG(L_ERR, "ERROR: fix_all_socket_lists: cannot determine"
  1178. " hostname, try -l address\n");
  1179. goto error;
  1180. }
  1181. if (add_listen_iface(myname.nodename, 0, 0, 0, 0)!=0){
  1182. LOG(L_ERR, "ERROR: fix_all_socket_lists: add_listen_iface "
  1183. "failed \n");
  1184. goto error;
  1185. }
  1186. }
  1187. }
  1188. flags=0;
  1189. if (fix_socket_list(&udp_listen, &flags)!=0){
  1190. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1191. " udp failed\n");
  1192. goto error;
  1193. }
  1194. if (flags){
  1195. socket_types|=flags|SOCKET_T_UDP;
  1196. }
  1197. #ifdef USE_TCP
  1198. flags=0;
  1199. if (!tcp_disable && (fix_socket_list(&tcp_listen, &flags)!=0)){
  1200. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1201. " tcp failed\n");
  1202. goto error;
  1203. }
  1204. if (flags){
  1205. socket_types|=flags|SOCKET_T_TCP;
  1206. }
  1207. #ifdef USE_TLS
  1208. flags=0;
  1209. if (!tls_disable && (fix_socket_list(&tls_listen, &flags)!=0)){
  1210. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1211. " tls failed\n");
  1212. goto error;
  1213. }
  1214. if (flags){
  1215. socket_types|=flags|SOCKET_T_TLS;
  1216. }
  1217. #endif
  1218. #endif
  1219. #ifdef USE_SCTP
  1220. flags=0;
  1221. if (!sctp_disable && (fix_socket_list(&sctp_listen, &flags)!=0)){
  1222. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1223. " sctp failed\n");
  1224. goto error;
  1225. }
  1226. if (flags){
  1227. socket_types|=flags|SOCKET_T_SCTP;
  1228. }
  1229. #endif /* USE_SCTP */
  1230. if ((udp_listen==0)
  1231. #ifdef USE_TCP
  1232. && (tcp_listen==0)
  1233. #ifdef USE_TLS
  1234. && (tls_listen==0)
  1235. #endif
  1236. #endif
  1237. #ifdef USE_SCTP
  1238. && (sctp_listen==0)
  1239. #endif
  1240. ){
  1241. LOG(L_ERR, "ERROR: fix_all_socket_lists: no listening sockets\n");
  1242. goto error;
  1243. }
  1244. return 0;
  1245. error:
  1246. if (ai_lst) free_addr_info_lst(&ai_lst);
  1247. return -1;
  1248. }
  1249. void print_all_socket_lists()
  1250. {
  1251. struct socket_info *si;
  1252. struct socket_info** list;
  1253. struct addr_info* ai;
  1254. unsigned short proto;
  1255. proto=PROTO_UDP;
  1256. do{
  1257. list=get_sock_info_list(proto);
  1258. for(si=list?*list:0; si; si=si->next){
  1259. if (si->addr_info_lst){
  1260. printf(" %s: (%s",
  1261. get_proto_name(proto),
  1262. si->address_str.s);
  1263. for (ai=si->addr_info_lst; ai; ai=ai->next)
  1264. printf(", %s", ai->address_str.s);
  1265. printf("):%s%s%s\n",
  1266. si->port_no_str.s,
  1267. si->flags & SI_IS_MCAST ? " mcast" : "",
  1268. si->flags & SI_IS_MHOMED? " mhomed" : "");
  1269. }else{
  1270. printf(" %s: %s",
  1271. get_proto_name(proto),
  1272. si->name.s);
  1273. if (!si->flags & SI_IS_IP)
  1274. printf(" [%s]", si->address_str.s);
  1275. printf( ":%s%s%s\n",
  1276. si->port_no_str.s,
  1277. si->flags & SI_IS_MCAST ? " mcast" : "",
  1278. si->flags & SI_IS_MHOMED? " mhomed" : "");
  1279. }
  1280. }
  1281. }while((proto=next_proto(proto)));
  1282. }
  1283. void print_aliases()
  1284. {
  1285. struct host_alias* a;
  1286. for(a=aliases; a; a=a->next)
  1287. if (a->port)
  1288. printf(" %s: %.*s:%d\n", get_proto_name(a->proto),
  1289. a->alias.len, a->alias.s, a->port);
  1290. else
  1291. printf(" %s: %.*s:*\n", get_proto_name(a->proto),
  1292. a->alias.len, a->alias.s);
  1293. }
  1294. void init_proto_order()
  1295. {
  1296. int r;
  1297. /* fix proto list (remove disabled protocols)*/
  1298. #ifdef USE_TCP
  1299. if (tcp_disable)
  1300. #endif
  1301. for(r=PROTO_NONE; r<=PROTO_LAST; r++){
  1302. if (nxt_proto[r]==PROTO_TCP)
  1303. nxt_proto[r]=nxt_proto[PROTO_TCP];
  1304. }
  1305. #ifdef USE_TCP
  1306. #ifdef USE_TLS
  1307. if (tls_disable || tcp_disable)
  1308. #endif
  1309. #endif
  1310. for(r=PROTO_NONE; r<=PROTO_LAST; r++){
  1311. if (nxt_proto[r]==PROTO_TLS)
  1312. nxt_proto[r]=nxt_proto[PROTO_TLS];
  1313. }
  1314. #ifdef USE_SCTP
  1315. if (sctp_disable)
  1316. #endif
  1317. for(r=PROTO_NONE; r<=PROTO_LAST; r++){
  1318. if (nxt_proto[r]==PROTO_SCTP)
  1319. nxt_proto[r]=nxt_proto[PROTO_SCTP];
  1320. }
  1321. }