socket_info.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  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. /*!
  44. * \file
  45. * \brief SIP-router core ::
  46. * \ingroup core
  47. * Module: \ref core
  48. */
  49. #include <string.h>
  50. #include <errno.h>
  51. #include <unistd.h>
  52. #include <sys/types.h>
  53. #include <sys/socket.h>
  54. #include <sys/utsname.h>
  55. #include <stdio.h>
  56. #include <sys/ioctl.h>
  57. #include <net/if.h>
  58. #ifdef HAVE_SYS_SOCKIO_H
  59. #include <sys/sockio.h>
  60. #endif
  61. #include "globals.h"
  62. #include "socket_info.h"
  63. #include "dprint.h"
  64. #include "mem/mem.h"
  65. #include "ut.h"
  66. #include "resolve.h"
  67. #include "name_alias.h"
  68. /* list manip. functions (internal use only) */
  69. /* append */
  70. #define sock_listadd(head, el) \
  71. do{\
  72. if (*(head)==0) *(head)=(el); \
  73. else{ \
  74. for((el)->next=*(head); (el)->next->next;\
  75. (el)->next=(el)->next->next); \
  76. (el)->next->next=(el); \
  77. (el)->prev=(el)->next; \
  78. (el)->next=0; \
  79. }\
  80. }while(0)
  81. /* insert after "after" */
  82. #define sock_listins(el, after) \
  83. do{ \
  84. if ((after)){\
  85. (el)->next=(after)->next; \
  86. if ((after)->next) (after)->next->prev=(el); \
  87. (after)->next=(el); \
  88. (el)->prev=(after); \
  89. }else{ /* after==0 = list head */ \
  90. (after)=(el); \
  91. (el)->next=(el)->prev=0; \
  92. }\
  93. }while(0)
  94. #define sock_listrm(head, el) \
  95. do {\
  96. if (*(head)==(el)) *(head)=(el)->next; \
  97. if ((el)->next) (el)->next->prev=(el)->prev; \
  98. if ((el)->prev) (el)->prev->next=(el)->next; \
  99. }while(0)
  100. #define addr_info_listadd sock_listadd
  101. #define addr_info_listins sock_listins
  102. #define addr_info_listrm sock_listrm
  103. inline static void addr_info_list_ins_lst(struct addr_info* lst,
  104. struct addr_info* after)
  105. {
  106. struct addr_info* l;
  107. struct addr_info* n;
  108. if (lst){
  109. n=after->next;
  110. after->next=lst;
  111. lst->prev=after;
  112. if (n){
  113. for(l=lst; l->next; l=l->next);
  114. l->next=n;
  115. n->prev=l;
  116. }
  117. }
  118. }
  119. /* protocol order, filled by init_proto_order() */
  120. enum sip_protos nxt_proto[PROTO_LAST+1]=
  121. { PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_SCTP, 0 };
  122. /* Deliberately left PROTO_WS and PROTO_WSS out of this as they are just
  123. upgraded TCP and TLS connections */
  124. /* another helper function, it just fills a struct addr_info
  125. * returns: 0 on success, -1 on error*/
  126. static int init_addr_info(struct addr_info* a,
  127. char* name, enum si_flags flags)
  128. {
  129. memset(a, 0, sizeof(*a));
  130. a->name.len=strlen(name);
  131. a->name.s=pkg_malloc(a->name.len+1); /* include \0 */
  132. if (a->name.s==0) goto error;
  133. memcpy(a->name.s, name, a->name.len+1);
  134. a->flags=flags;
  135. return 0;
  136. error:
  137. LOG(L_ERR, "ERROR: init_addr_info: memory allocation error\n");
  138. return -1;
  139. }
  140. /* returns 0 on error, new addr_info_lst element on success */
  141. static inline struct addr_info* new_addr_info(char* name,
  142. enum si_flags gf)
  143. {
  144. struct addr_info* al;
  145. al=pkg_malloc(sizeof(*al));
  146. if (al==0) goto error;
  147. al->next=0;
  148. al->prev=0;
  149. if (init_addr_info(al, name, gf)!=0) goto error;
  150. return al;
  151. error:
  152. LOG(L_ERR, "ERROR: new_addr_info: memory allocation error\n");
  153. if (al){
  154. if (al->name.s) pkg_free(al->name.s);
  155. pkg_free(al);
  156. }
  157. return 0;
  158. }
  159. static inline void free_addr_info(struct addr_info* a)
  160. {
  161. if (a){
  162. if (a->name.s){
  163. pkg_free(a->name.s);
  164. a->name.s=0;
  165. }
  166. pkg_free(a);
  167. }
  168. }
  169. static inline void free_addr_info_lst(struct addr_info** lst)
  170. {
  171. struct addr_info* a;
  172. struct addr_info* tmp;
  173. a=*lst;
  174. while(a){
  175. tmp=a;
  176. a=a->next;
  177. free_addr_info(tmp);
  178. }
  179. }
  180. /* adds a new add_info_lst element to the corresponding list
  181. * returns 0 on success, -1 on error */
  182. static int new_addr_info2list(char* name, enum si_flags f,
  183. struct addr_info** l)
  184. {
  185. struct addr_info * al;
  186. al=new_addr_info(name, f);
  187. if (al==0) goto error;
  188. addr_info_listadd(l, al);
  189. return 0;
  190. error:
  191. return -1;
  192. }
  193. /* another helper function, it just creates a socket_info struct */
  194. static inline struct socket_info* new_sock_info( char* name,
  195. struct name_lst* addr_l,
  196. unsigned short port, unsigned short proto,
  197. char *usename, unsigned short useport,
  198. enum si_flags flags)
  199. {
  200. struct socket_info* si;
  201. struct name_lst* n;
  202. struct hostent* he;
  203. char *p;
  204. si=(struct socket_info*) pkg_malloc(sizeof(struct socket_info));
  205. if (si==0) goto error;
  206. memset(si, 0, sizeof(struct socket_info));
  207. si->socket=-1;
  208. si->name.len=strlen(name);
  209. si->name.s=(char*)pkg_malloc(si->name.len+1); /* include \0 */
  210. if (si->name.s==0) goto error;
  211. memcpy(si->name.s, name, si->name.len+1);
  212. /* set port & proto */
  213. si->port_no=port;
  214. si->proto=proto;
  215. si->flags=flags;
  216. si->addr_info_lst=0;
  217. for (n=addr_l; n; n=n->next){
  218. if (new_addr_info2list(n->name, n->flags, &si->addr_info_lst)!=0){
  219. LOG(L_ERR, "ERROR: new_sockk_info:new_addr_info2list failed\n");
  220. goto error;
  221. }
  222. }
  223. if(usename!=NULL)
  224. {
  225. si->useinfo.name.len=strlen(usename);
  226. si->useinfo.name.s=(char*)pkg_malloc(si->useinfo.name.len+1);
  227. if (si->useinfo.name.s==0)
  228. goto error;
  229. strcpy(si->useinfo.name.s, usename);
  230. if(usename[0]=='[' && usename[si->useinfo.name.len-1]==']')
  231. {
  232. si->useinfo.address_str.len = si->useinfo.name.len - 2;
  233. p = si->useinfo.name.s + 1;
  234. } else {
  235. si->useinfo.address_str.len = si->useinfo.name.len;
  236. p = si->useinfo.name.s;
  237. }
  238. si->useinfo.address_str.s=(char*)pkg_malloc(si->useinfo.address_str.len+1);
  239. if(si->useinfo.address_str.s==NULL)
  240. goto error;
  241. strncpy(si->useinfo.address_str.s, p, si->useinfo.address_str.len);
  242. si->useinfo.address_str.s[si->useinfo.address_str.len] = '\0';
  243. p = int2str(useport, &si->useinfo.port_no_str.len);
  244. if(p==NULL)
  245. goto error;
  246. si->useinfo.port_no_str.s=(char*)pkg_malloc(si->useinfo.port_no_str.len+1);
  247. if(si->useinfo.port_no_str.s==NULL)
  248. goto error;
  249. strcpy(si->useinfo.port_no_str.s, p);
  250. si->useinfo.port_no = useport;
  251. he=resolvehost(si->useinfo.name.s);
  252. if (he==0){
  253. LM_ERR(" unable to resolve advertised name %s\n", si->useinfo.name.s);
  254. goto error;
  255. }
  256. hostent2ip_addr(&si->useinfo.address, he, 0);
  257. }
  258. return si;
  259. error:
  260. LOG(L_ERR, "ERROR: new_sock_info: memory allocation error\n");
  261. if (si) {
  262. if(si->name.s)
  263. pkg_free(si->name.s);
  264. pkg_free(si);
  265. }
  266. return 0;
  267. }
  268. /* delete a socket_info struct */
  269. static void free_sock_info(struct socket_info* si)
  270. {
  271. if(si){
  272. if(si->name.s) pkg_free(si->name.s);
  273. if(si->address_str.s) pkg_free(si->address_str.s);
  274. if(si->port_no_str.s) pkg_free(si->port_no_str.s);
  275. if(si->addr_info_lst) free_addr_info_lst(&si->addr_info_lst);
  276. if(si->sock_str.s) pkg_free(si->sock_str.s);
  277. if(si->useinfo.name.s) pkg_free(si->useinfo.name.s);
  278. if(si->useinfo.port_no_str.s) pkg_free(si->useinfo.port_no_str.s);
  279. if(si->useinfo.sock_str.s) pkg_free(si->useinfo.sock_str.s);
  280. }
  281. }
  282. char* get_valid_proto_name(unsigned short proto)
  283. {
  284. switch(proto){
  285. case PROTO_NONE:
  286. return "*";
  287. case PROTO_UDP:
  288. return "udp";
  289. #ifdef USE_TCP
  290. case PROTO_TCP:
  291. return "tcp";
  292. #endif
  293. #ifdef USE_TLS
  294. case PROTO_TLS:
  295. return "tls";
  296. #endif
  297. #ifdef USE_SCTP
  298. case PROTO_SCTP:
  299. return "sctp";
  300. #endif
  301. default:
  302. return "unknown";
  303. }
  304. }
  305. /** Convert socket to its textual representation.
  306. *
  307. * This function converts the transport protocol, the IP address and the port
  308. * number in a comma delimited string of form proto:ip:port. The resulting
  309. * string is NOT zero terminated
  310. *
  311. * @param s is a pointer to the destination memory buffer
  312. * @param len is a pointer to an integer variable. Initially the variable
  313. * should contain the size of the buffer in s. The value of the variable
  314. * will be changed to the length of the resulting string on success and
  315. * to the desired size of the destination buffer if it is too small
  316. * @param si is a pointer to the socket_info structure to be printed
  317. * @return -1 on error and 0 on success
  318. */
  319. int socket2str(char* s, int* len, struct socket_info* si)
  320. {
  321. return socketinfo2str(s, len, si, 0);
  322. }
  323. int socketinfo2str(char* s, int* len, struct socket_info* si, int mode)
  324. {
  325. str proto;
  326. int l;
  327. proto.s = get_valid_proto_name(si->proto);
  328. proto.len = strlen(proto.s);
  329. if(mode==1)
  330. l = proto.len + si->useinfo.name.len + si->useinfo.port_no_str.len + 2;
  331. else
  332. l = proto.len + si->address_str.len + si->port_no_str.len + 2;
  333. if(si->address.af==AF_INET6)
  334. l += 2;
  335. if (*len < l) {
  336. LM_ERR("Destionation buffer too short\n");
  337. *len = l;
  338. return -1;
  339. }
  340. memcpy(s, proto.s, proto.len);
  341. s += proto.len;
  342. *s = ':'; s++;
  343. if(mode==1){
  344. memcpy(s, si->useinfo.name.s, si->useinfo.name.len);
  345. s += si->useinfo.name.len;
  346. *s = ':'; s++;
  347. memcpy(s, si->useinfo.port_no_str.s, si->useinfo.port_no_str.len);
  348. s += si->useinfo.port_no_str.len;
  349. } else {
  350. if(si->address.af==AF_INET6) {
  351. *s = '['; s++;
  352. }
  353. memcpy(s, si->address_str.s, si->address_str.len);
  354. s += si->address_str.len;
  355. if(si->address.af==AF_INET6) {
  356. *s = ']'; s++;
  357. }
  358. *s = ':'; s++;
  359. memcpy(s, si->port_no_str.s, si->port_no_str.len);
  360. s += si->port_no_str.len;
  361. }
  362. *len = l;
  363. return 0;
  364. }
  365. /* Fill si->sock_str with string representing the socket_info structure,
  366. * format of the string is 'proto:address:port'. Returns 0 on success and
  367. * negative number on failure.
  368. */
  369. static int fix_sock_str(struct socket_info* si)
  370. {
  371. int len = MAX_SOCKET_STR;
  372. if (si->sock_str.s) pkg_free(si->sock_str.s);
  373. si->sock_str.s = pkg_malloc(len + 1);
  374. if (si->sock_str.s == NULL) {
  375. ERR("fix_sock_str: No memory left\n");
  376. return -1;
  377. }
  378. if (socketinfo2str(si->sock_str.s, &len, si, 0) < 0) {
  379. BUG("fix_sock_str: Error in socket to str\n");
  380. return -1;
  381. }
  382. si->sock_str.s[len] = '\0';
  383. si->sock_str.len = len;
  384. if(si->useinfo.name.s!=NULL)
  385. {
  386. len = MAX_SOCKET_STR;
  387. if (si->useinfo.sock_str.s) pkg_free(si->useinfo.sock_str.s);
  388. si->useinfo.sock_str.s = pkg_malloc(len + 1);
  389. if (si->useinfo.sock_str.s == NULL) {
  390. ERR("fix_sock_str: No memory left\n");
  391. return -1;
  392. }
  393. if (socketinfo2str(si->useinfo.sock_str.s, &len, si, 1) < 0) {
  394. BUG("fix_sock_str: Error in socket to str\n");
  395. return -1;
  396. }
  397. si->useinfo.sock_str.s[len] = '\0';
  398. si->useinfo.sock_str.len = len;
  399. }
  400. return 0;
  401. }
  402. /* returns 0 if support for the protocol is not compiled or if proto is
  403. invalid */
  404. struct socket_info** get_sock_info_list(unsigned short proto)
  405. {
  406. switch(proto){
  407. case PROTO_UDP:
  408. return &udp_listen;
  409. break;
  410. case PROTO_TCP:
  411. case PROTO_WS:
  412. #ifdef USE_TCP
  413. return &tcp_listen;
  414. #endif
  415. break;
  416. case PROTO_TLS:
  417. case PROTO_WSS:
  418. #ifdef USE_TLS
  419. return &tls_listen;
  420. #endif
  421. break;
  422. case PROTO_SCTP:
  423. #ifdef USE_SCTP
  424. return &sctp_listen;
  425. #endif
  426. break;
  427. default:
  428. LOG(L_CRIT, "BUG: get_sock_info_list: invalid proto %d\n", proto);
  429. }
  430. return 0;
  431. }
  432. /* helper function for grep_sock_info
  433. * params:
  434. * host - hostname to compare with
  435. * name - official name
  436. * addr_str - name's resolved ip address converted to string
  437. * ip_addr - name's ip address
  438. * flags - set to SI_IS_IP if name contains an IP
  439. *
  440. * returns 0 if host matches, -1 if not */
  441. inline static int si_hname_cmp(str* host, str* name, str* addr_str,
  442. struct ip_addr* ip_addr, int flags)
  443. {
  444. #ifdef USE_IPV6
  445. struct ip_addr* ip6;
  446. #endif
  447. if ( (host->len==name->len) &&
  448. (strncasecmp(host->s, name->s, name->len)==0) /*slower*/)
  449. /* comp. must be case insensitive, host names
  450. * can be written in mixed case, it will also match
  451. * ipv6 addresses if we are lucky*/
  452. goto found;
  453. /* check if host == ip address */
  454. #ifdef USE_IPV6
  455. /* ipv6 case is uglier, host can be [3ffe::1] */
  456. ip6=str2ip6(host);
  457. if (ip6){
  458. if (ip_addr_cmp(ip6, ip_addr))
  459. goto found; /* match */
  460. else
  461. return -1; /* no match, but this is an ipv6 address
  462. so no point in trying ipv4 */
  463. }
  464. #endif
  465. /* ipv4 */
  466. if ( (!(flags&SI_IS_IP)) && (host->len==addr_str->len) &&
  467. (memcmp(host->s, addr_str->s, addr_str->len)==0) )
  468. goto found;
  469. return -1;
  470. found:
  471. return 0;
  472. }
  473. /* checks if the proto: host:port is one of the address we listen on
  474. * and returns the corresponding socket_info structure.
  475. * if port==0, the port number is ignored
  476. * if proto==0 (PROTO_NONE) the protocol is ignored
  477. * returns 0 if not found
  478. * WARNING: uses str2ip6 so it will overwrite any previous
  479. * unsaved result of this function (static buffer)
  480. */
  481. struct socket_info* grep_sock_info(str* host, unsigned short port,
  482. unsigned short proto)
  483. {
  484. str hname;
  485. struct socket_info* si;
  486. struct socket_info** list;
  487. struct addr_info* ai;
  488. unsigned short c_proto;
  489. hname=*host;
  490. #ifdef USE_IPV6
  491. if ((hname.len>2)&&((*hname.s)=='[')&&(hname.s[hname.len-1]==']')){
  492. /* ipv6 reference, skip [] */
  493. hname.s++;
  494. hname.len-=2;
  495. }
  496. #endif
  497. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  498. retry:
  499. do{
  500. /* get the proper sock_list */
  501. list=get_sock_info_list(c_proto);
  502. if (list==0) /* disabled or unknown protocol */
  503. continue;
  504. for (si=*list; si; si=si->next){
  505. DBG("grep_sock_info - checking if host==us: %d==%d &&"
  506. " [%.*s] == [%.*s]\n",
  507. hname.len,
  508. si->name.len,
  509. hname.len, hname.s,
  510. si->name.len, si->name.s
  511. );
  512. if (port) {
  513. DBG("grep_sock_info - checking if port %d (advertise %d)"
  514. " matches port %d\n",
  515. si->port_no, si->useinfo.port_no, port);
  516. if (si->port_no!=port && si->useinfo.port_no!=port) {
  517. continue;
  518. }
  519. }
  520. if (si_hname_cmp(&hname, &si->name, &si->address_str,
  521. &si->address, si->flags)==0)
  522. goto found;
  523. if(si->useinfo.name.s!=NULL)
  524. {
  525. DBG("grep_sock_info - checking advertise if host==us:"
  526. " %d==%d && [%.*s] == [%.*s]\n",
  527. hname.len,
  528. si->useinfo.name.len,
  529. hname.len, hname.s,
  530. si->useinfo.name.len, si->useinfo.name.s
  531. );
  532. if (si_hname_cmp(&hname, &si->useinfo.name,
  533. &si->useinfo.address_str, &si->useinfo.address,
  534. si->flags)==0)
  535. goto found;
  536. }
  537. /* try among the extra addresses */
  538. for (ai=si->addr_info_lst; ai; ai=ai->next)
  539. if (si_hname_cmp(&hname, &ai->name, &ai->address_str,
  540. &ai->address, ai->flags)==0)
  541. goto found;
  542. }
  543. }while( (proto==0) && (c_proto=next_proto(c_proto)) );
  544. #ifdef USE_TLS
  545. if (unlikely(c_proto == PROTO_WS)) {
  546. c_proto = PROTO_WSS;
  547. goto retry;
  548. }
  549. #endif
  550. /* not_found: */
  551. return 0;
  552. found:
  553. return si;
  554. }
  555. /* checks if the proto:port is one of the ports we listen on
  556. * and returns the corresponding socket_info structure.
  557. * if proto==0 (PROTO_NONE) the protocol is ignored
  558. * returns 0 if not found
  559. */
  560. struct socket_info* grep_sock_info_by_port(unsigned short port,
  561. unsigned short proto)
  562. {
  563. struct socket_info* si;
  564. struct socket_info** list;
  565. unsigned short c_proto;
  566. if (!port) {
  567. goto not_found;
  568. }
  569. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  570. do{
  571. /* get the proper sock_list */
  572. list=get_sock_info_list(c_proto);
  573. if (list==0) /* disabled or unknown protocol */
  574. continue;
  575. for (si=*list; si; si=si->next){
  576. DBG("grep_sock_info_by_port - checking if port %d matches"
  577. " port %d\n", si->port_no, port);
  578. if (si->port_no==port) {
  579. goto found;
  580. }
  581. }
  582. }while( (proto==0) && (c_proto=next_proto(c_proto)) );
  583. not_found:
  584. return 0;
  585. found:
  586. return si;
  587. }
  588. /* checks if the proto: ip:port is one of the address we listen on
  589. * and returns the corresponding socket_info structure.
  590. * (same as grep_socket_info, but use ip addr instead)
  591. * if port==0, the port number is ignored
  592. * if proto==0 (PROTO_NONE) the protocol is ignored
  593. * returns 0 if not found
  594. * WARNING: uses str2ip6 so it will overwrite any previous
  595. * unsaved result of this function (static buffer)
  596. */
  597. struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
  598. unsigned short proto)
  599. {
  600. struct socket_info* si;
  601. struct socket_info** list;
  602. struct addr_info* ai;
  603. unsigned short c_proto;
  604. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  605. do{
  606. /* get the proper sock_list */
  607. list=get_sock_info_list(c_proto);
  608. if (list==0) /* disabled or unknown protocol */
  609. continue;
  610. for (si=*list; si; si=si->next){
  611. if (port) {
  612. if (si->port_no!=port) {
  613. continue;
  614. }
  615. }
  616. if (ip_addr_cmp(ip, &si->address)
  617. || ip_addr_cmp(ip, &si->useinfo.address))
  618. goto found;
  619. for (ai=si->addr_info_lst; ai; ai=ai->next)
  620. if (ip_addr_cmp(ip, &ai->address))
  621. goto found;
  622. }
  623. }while( (proto==0) && (c_proto=next_proto(c_proto)) );
  624. /* not_found: */
  625. return 0;
  626. found:
  627. return si;
  628. }
  629. /* append a new sock_info structure to the corresponding list
  630. * return new sock info on success, 0 on error */
  631. static struct socket_info* new_sock2list(char* name, struct name_lst* addr_l,
  632. unsigned short port,
  633. unsigned short proto,
  634. char *usename, unsigned short useport,
  635. enum si_flags flags,
  636. struct socket_info** list)
  637. {
  638. struct socket_info* si;
  639. si=new_sock_info(name, addr_l, port, proto, usename, useport, flags);
  640. if (si==0){
  641. LOG(L_ERR, "ERROR: new_sock2list: new_sock_info failed\n");
  642. goto error;
  643. }
  644. if(socket_workers>0) {
  645. si->workers = socket_workers;
  646. socket_workers = 0;
  647. }
  648. sock_listadd(list, si);
  649. return si;
  650. error:
  651. return 0;
  652. }
  653. /* adds a new sock_info structure immediately after "after"
  654. * return new sock info on success, 0 on error */
  655. static struct socket_info* new_sock2list_after(char* name,
  656. struct name_lst* addr_l,
  657. unsigned short port,
  658. unsigned short proto,
  659. char *usename,
  660. unsigned short useport,
  661. enum si_flags flags,
  662. struct socket_info* after)
  663. {
  664. struct socket_info* si;
  665. si=new_sock_info(name, addr_l, port, proto, usename, useport, flags);
  666. if (si==0){
  667. LOG(L_ERR, "ERROR: new_sock2list_after: new_sock_info failed\n");
  668. goto error;
  669. }
  670. sock_listins(si, after);
  671. return si;
  672. error:
  673. return 0;
  674. }
  675. /* adds a sock_info structure to the corresponding proto list
  676. * return 0 on success, -1 on error */
  677. int add_listen_advertise_iface(char* name, struct name_lst* addr_l,
  678. unsigned short port, unsigned short proto,
  679. char *usename, unsigned short useport,
  680. enum si_flags flags)
  681. {
  682. struct socket_info** list;
  683. unsigned short c_proto;
  684. struct name_lst* a_l;
  685. unsigned short c_port;
  686. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  687. do{
  688. list=get_sock_info_list(c_proto);
  689. if (list==0) /* disabled or unknown protocol */
  690. continue;
  691. if (port==0){ /* use default port */
  692. c_port=
  693. #ifdef USE_TLS
  694. ((c_proto)==PROTO_TLS)?tls_port_no:
  695. #endif
  696. port_no;
  697. }
  698. #ifdef USE_TLS
  699. else if ((c_proto==PROTO_TLS) && (proto==0)){
  700. /* -l ip:port => on udp:ip:port; tcp:ip:port and tls:ip:port+1?*/
  701. c_port=port+1;
  702. }
  703. #endif
  704. else{
  705. c_port=port;
  706. }
  707. if (c_proto!=PROTO_SCTP){
  708. if (new_sock2list(name, 0, c_port, c_proto, usename, useport,
  709. flags & ~SI_IS_MHOMED, list)==0){
  710. LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
  711. goto error;
  712. }
  713. /* add the other addresses in the list as separate sockets
  714. * since only SCTP can bind to multiple addresses */
  715. for (a_l=addr_l; a_l; a_l=a_l->next){
  716. if (new_sock2list(a_l->name, 0, c_port,
  717. c_proto, usename, useport,
  718. flags & ~SI_IS_MHOMED, list)==0){
  719. LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list"
  720. " failed\n");
  721. goto error;
  722. }
  723. }
  724. }else{
  725. if (new_sock2list(name, addr_l, c_port, c_proto, usename, useport,
  726. flags, list)==0){
  727. LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
  728. goto error;
  729. }
  730. }
  731. }while( (proto==0) && (c_proto=next_proto(c_proto)));
  732. return 0;
  733. error:
  734. return -1;
  735. }
  736. /* adds a sock_info structure to the corresponding proto list
  737. * return 0 on success, -1 on error */
  738. int add_listen_iface(char* name, struct name_lst* addr_l,
  739. unsigned short port, unsigned short proto,
  740. enum si_flags flags)
  741. {
  742. return add_listen_advertise_iface(name, addr_l, port, proto, 0, 0, flags);
  743. }
  744. #ifdef __OS_linux
  745. #include "linux/types.h"
  746. #include "linux/netlink.h"
  747. #include "linux/rtnetlink.h"
  748. #include "arpa/inet.h"
  749. #define MAX_IF_LEN 64
  750. struct idx
  751. {
  752. struct idx * next;
  753. int family;
  754. unsigned ifa_flags;
  755. char addr[MAX_IF_LEN];
  756. };
  757. struct idxlist{
  758. struct idx* addresses;
  759. int index;
  760. char name[MAX_IF_LEN];
  761. unsigned flags;
  762. };
  763. #define MAX_IFACE_NO 32
  764. static struct idxlist *ifaces = NULL;
  765. static int seq = 0;
  766. #define SADDR(s) ((struct sockaddr_in*)s)->sin_addr.s_addr
  767. #define NLMSG_TAIL(nmsg) \
  768. ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
  769. int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
  770. int alen)
  771. {
  772. int len = RTA_LENGTH(alen);
  773. struct rtattr *rta;
  774. if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
  775. fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
  776. return -1;
  777. }
  778. rta = NLMSG_TAIL(n);
  779. rta->rta_type = type;
  780. rta->rta_len = len;
  781. memcpy(RTA_DATA(rta), data, alen);
  782. n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
  783. return 0;
  784. }
  785. static int nl_bound_sock(void)
  786. {
  787. int sock;
  788. struct sockaddr_nl la;
  789. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  790. if(sock <= 0){
  791. LM_ERR("could not create NETLINK sock to get interface list");
  792. goto error;
  793. }
  794. /* bind NETLINK socket to pid */
  795. bzero(&la, sizeof(la));
  796. la.nl_family = AF_NETLINK;
  797. la.nl_pad = 0;
  798. la.nl_pid = getpid();
  799. la.nl_groups = 0;
  800. if ( bind(sock, (struct sockaddr*) &la, sizeof(la)) < 0){
  801. LM_ERR("could not bind NETLINK sock to sockaddr_nl\n");
  802. goto error;
  803. }
  804. return sock;
  805. error:
  806. if(sock > 0) close(sock);
  807. return -1;
  808. }
  809. #define fill_nl_req(req, type, family) do {\
  810. memset(&req, 0, sizeof(req));\
  811. req.nlh.nlmsg_len = sizeof(req);\
  812. req.nlh.nlmsg_type = type;\
  813. req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST|NLM_F_DUMP;\
  814. req.nlh.nlmsg_pid = getpid();\
  815. req.nlh.nlmsg_seq = seq++;\
  816. req.g.rtgen_family = family;\
  817. } while(0);
  818. static int get_flags(int family){
  819. struct {
  820. struct nlmsghdr nlh;
  821. struct rtgenmsg g;
  822. } req;
  823. int rtn = 0;
  824. struct nlmsghdr* nlp;
  825. struct ifinfomsg *ifi;
  826. char buf[8192];
  827. char *p = buf;
  828. int nll = 0;
  829. int nl_sock = 0;
  830. fill_nl_req(req, RTM_GETLINK, AF_INET);
  831. if((nl_sock = nl_bound_sock()) < 0) return -1;
  832. if(send(nl_sock, (void*)&req, sizeof(req), 0) < 0)
  833. {
  834. LM_ERR("error sending NETLINK request\n");
  835. goto error;
  836. }
  837. while(1) {
  838. rtn = recv(nl_sock, p, sizeof(buf) - nll, 0);
  839. nlp = (struct nlmsghdr *) p;
  840. if(nlp->nlmsg_type == NLMSG_DONE){
  841. LM_DBG("done\n");
  842. break;
  843. }
  844. if(nlp->nlmsg_type == NLMSG_ERROR){
  845. LM_DBG("Error on message to netlink");
  846. break;
  847. }
  848. p += rtn;
  849. nll += rtn;
  850. }
  851. nlp = (struct nlmsghdr *) buf;
  852. for(;NLMSG_OK(nlp, nll);nlp=NLMSG_NEXT(nlp, nll)){
  853. ifi = NLMSG_DATA(nlp);
  854. if (nlp->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
  855. goto error;
  856. LM_ERR("Interface with index %d has flags %d\n", ifi->ifi_index, ifi->ifi_flags);
  857. if(ifaces == NULL){
  858. LM_ERR("get_flags must not be called on empty interface list");
  859. goto error;
  860. }
  861. if(ifi->ifi_index >= MAX_IFACE_NO){
  862. LM_ERR("invalid network interface index returned %d", ifi->ifi_index);
  863. goto error;
  864. }
  865. ifaces[ifi->ifi_index].flags = ifi->ifi_flags;
  866. }
  867. if(nl_sock>0) close(nl_sock);
  868. return 0;
  869. error:
  870. if(nl_sock>0) close(nl_sock);
  871. return -1;
  872. }
  873. static int build_iface_list(void)
  874. {
  875. struct {
  876. struct nlmsghdr nlh;
  877. struct rtgenmsg g;
  878. } req;
  879. int seq = 0;
  880. int rtn = 0;
  881. struct nlmsghdr* nlp;
  882. struct ifaddrmsg *ifi;
  883. int rtl;
  884. char buf[8192];
  885. char *p = buf;
  886. int nll = 0;
  887. struct rtattr * rtap;
  888. int index, i;
  889. struct idx* entry;
  890. struct idx* tmp;
  891. int nl_sock = 0;
  892. int families[] = {AF_INET, AF_INET6};
  893. char name[MAX_IF_LEN];
  894. int is_link_local = 0;
  895. if(ifaces == NULL){
  896. if((ifaces = (struct idxlist*)pkg_malloc(MAX_IFACE_NO*sizeof(struct idxlist))) == NULL){
  897. LM_ERR("No more pkg memory\n");
  898. return -1;
  899. }
  900. memset(ifaces, 0, sizeof(struct idxlist)*MAX_IFACE_NO);
  901. }
  902. /* bind netlink socket */
  903. if((nl_sock = nl_bound_sock()) < 0) return -1;
  904. for (i = 0 ; i < sizeof(families)/sizeof(int); i++) {
  905. fill_nl_req(req, RTM_GETADDR, families[i]);
  906. if(send(nl_sock, (void*)&req, sizeof(req), 0) < 0){
  907. LM_ERR("error sending NETLINK request\n");
  908. goto error;
  909. };
  910. memset(buf, 0, sizeof(buf));
  911. nll = 0;
  912. p = buf;
  913. while(1) {
  914. rtn = recv(nl_sock, p, sizeof(buf) - nll, 0);
  915. LM_DBG("received %d byles \n", rtn);
  916. nlp = (struct nlmsghdr *) p;
  917. if(nlp->nlmsg_type == NLMSG_DONE){
  918. LM_DBG("done receiving netlink info \n");
  919. break;
  920. }
  921. if(nlp->nlmsg_type == NLMSG_ERROR){
  922. LM_ERR("Error on message to netlink");
  923. break;
  924. }
  925. p += rtn;
  926. nll += rtn;
  927. }
  928. nlp = (struct nlmsghdr *) buf;
  929. for(;NLMSG_OK(nlp, nll);nlp=NLMSG_NEXT(nlp, nll)){
  930. ifi = NLMSG_DATA(nlp);
  931. if (nlp->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
  932. continue;
  933. // init all the strings
  934. // inner loop: loop thru all the attributes of
  935. // one route entry
  936. rtap = (struct rtattr *) IFA_RTA(ifi);
  937. rtl = IFA_PAYLOAD(nlp);
  938. index = ifi->ifa_index;
  939. if(index >= MAX_IFACE_NO){
  940. LM_ERR("Invalid interface index returned: %d\n", index);
  941. goto error;
  942. }
  943. entry = (struct idx*)pkg_malloc(sizeof(struct idx));
  944. if(entry == 0)
  945. {
  946. LM_ERR("could not allocate memory\n");
  947. goto error;
  948. }
  949. entry->next = 0;
  950. entry->family = families[i];
  951. entry->ifa_flags = ifi->ifa_flags;
  952. is_link_local = 0;
  953. for(;RTA_OK(rtap, rtl);rtap=RTA_NEXT(rtap,rtl)){
  954. switch(rtap->rta_type){
  955. case IFA_ADDRESS:
  956. if((*(int*)RTA_DATA(rtap))== htons(0xfe80)){
  957. LM_DBG("Link Local Address, ignoring ...\n");
  958. is_link_local = 1;
  959. break;
  960. }
  961. inet_ntop(families[i], RTA_DATA(rtap), entry->addr, MAX_IF_LEN);
  962. LM_DBG("iface <IFA_ADDRESS> addr is %s\n", entry->addr);
  963. break;
  964. case IFA_LOCAL:
  965. if((*(int*)RTA_DATA(rtap))== htons(0xfe80)){
  966. LM_DBG("Link Local Address, ignoring ...\n");
  967. is_link_local = 1;
  968. }
  969. inet_ntop(families[i], RTA_DATA(rtap), entry->addr, MAX_IF_LEN);
  970. LM_DBG("iface <IFA_LOCAL> addr is %s\n", entry->addr);
  971. break;
  972. case IFA_LABEL:
  973. LM_DBG("iface name is %s\n", (char*)RTA_DATA(rtap));
  974. strncpy(name, (char*)RTA_DATA(rtap), MAX_IF_LEN);
  975. break;
  976. case IFA_BROADCAST:
  977. case IFA_ANYCAST:
  978. case IFA_UNSPEC:
  979. case IFA_CACHEINFO:
  980. default:
  981. break;
  982. }
  983. }
  984. if(is_link_local) continue; /* link local addresses are not bindable */
  985. if(strlen(ifaces[index].name)==0)
  986. strncpy(ifaces[index].name, name, MAX_IF_LEN);
  987. ifaces[index].index = index;
  988. if(ifaces[index].addresses == 0 )
  989. ifaces[index].addresses = entry;
  990. else {
  991. for(tmp = ifaces[index].addresses; tmp->next ; tmp = tmp->next)/*empty*/;
  992. tmp->next = entry;
  993. }
  994. }
  995. }
  996. if(nl_sock>0) close(nl_sock);
  997. /* the socket should be closed so we can bind again */
  998. for(i = 0; i < sizeof(families)/sizeof(int); i++){
  999. /* get device flags */
  1000. get_flags(families[i]); /* AF_INET or AF_INET6 */
  1001. }
  1002. return 0;
  1003. error:
  1004. if(nl_sock>0) close(nl_sock);
  1005. return -1;
  1006. }
  1007. /* add all family type addresses of interface if_to the socket_info array
  1008. * if if_name==0, adds all addresses on all interfaces
  1009. * uses RTNETLINK sockets to get addresses on the present interface on LINUX
  1010. * return: -1 on error, 0 on success
  1011. */
  1012. int add_interfaces_via_netlink(char* if_name, int family, unsigned short port,
  1013. unsigned short proto,
  1014. struct addr_info** ai_l)
  1015. {
  1016. int i;
  1017. struct idx* tmp;
  1018. enum si_flags flags;
  1019. if(ifaces == NULL && (build_iface_list()!=0)){
  1020. LM_ERR("Could not get network interface list\n");
  1021. return -1;
  1022. }
  1023. flags=SI_NONE;
  1024. for(i=0; i< MAX_IFACE_NO; ++i){
  1025. if(ifaces[i].addresses == NULL) continue; /* not present/configured */
  1026. if ((if_name==0)||
  1027. (strncmp(if_name, ifaces[i].name, strlen(ifaces[i].name))==0)){
  1028. /* check if iface is up */
  1029. //if(! (ifaces[i].flags & IFF_UP) ) continue;
  1030. for(tmp = ifaces[i].addresses; tmp; tmp = tmp->next){
  1031. LM_DBG("\t in add_iface_via_netlink Name %s Address %s\n", ifaces[i].name, tmp->addr);
  1032. /* match family */
  1033. if (family == tmp->family){
  1034. /* check if loopback */
  1035. if (ifaces[i].flags & IFF_LOOPBACK){
  1036. LM_DBG("INTERFACE %s is loopback", ifaces[i].name);
  1037. flags|=SI_IS_LO;
  1038. }
  1039. /* save the info */
  1040. if (new_addr_info2list(tmp->addr, flags, ai_l)!=0){
  1041. LOG(L_ERR, "ERROR: add_interfaces: "
  1042. "new_addr_info2list failed\n");
  1043. goto error;
  1044. }
  1045. }
  1046. }
  1047. }
  1048. }
  1049. return 0;
  1050. error:
  1051. return -1;
  1052. }
  1053. #endif /* __OS_linux */
  1054. /* add all family type addresses of interface if_name to the socket_info array
  1055. * if if_name==0, adds all addresses on all interfaces
  1056. * WARNING: it only works with ipv6 addresses on FreeBSD
  1057. * return: -1 on error, 0 on success
  1058. */
  1059. int add_interfaces(char* if_name, int family, unsigned short port,
  1060. unsigned short proto,
  1061. struct addr_info** ai_l)
  1062. {
  1063. struct ifconf ifc;
  1064. struct ifreq ifr;
  1065. struct ifreq ifrcopy;
  1066. char* last;
  1067. char* p;
  1068. int size;
  1069. int lastlen;
  1070. int s;
  1071. char* tmp;
  1072. struct ip_addr addr;
  1073. int ret;
  1074. enum si_flags flags;
  1075. #ifdef HAVE_SOCKADDR_SA_LEN
  1076. #ifndef MAX
  1077. #define MAX(a,b) ( ((a)>(b))?(a):(b))
  1078. #endif
  1079. #endif
  1080. /* ipv4 or ipv6 only*/
  1081. flags=SI_NONE;
  1082. s=socket(family, SOCK_DGRAM, 0);
  1083. ret=-1;
  1084. lastlen=0;
  1085. ifc.ifc_req=0;
  1086. for (size=100; ; size*=2){
  1087. ifc.ifc_len=size*sizeof(struct ifreq);
  1088. ifc.ifc_req=(struct ifreq*) pkg_malloc(size*sizeof(struct ifreq));
  1089. if (ifc.ifc_req==0){
  1090. LOG(L_ERR, "ERROR: add_interfaces: memory allocation failure\n");
  1091. goto error;
  1092. }
  1093. if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
  1094. if(errno==EBADF) return 0; /* invalid descriptor => no such ifs*/
  1095. LOG(L_ERR, "ERROR: add_interfaces: ioctl failed: %s\n",
  1096. strerror(errno));
  1097. goto error;
  1098. }
  1099. if ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
  1100. len not changed*/
  1101. lastlen=ifc.ifc_len;
  1102. /* try a bigger array*/
  1103. pkg_free(ifc.ifc_req);
  1104. }
  1105. last=(char*)ifc.ifc_req+ifc.ifc_len;
  1106. for(p=(char*)ifc.ifc_req; p<last;
  1107. p+=
  1108. #ifdef __OS_linux
  1109. sizeof(ifr) /* works on x86_64 too */
  1110. #else
  1111. (sizeof(ifr.ifr_name)+
  1112. #ifdef HAVE_SOCKADDR_SA_LEN
  1113. MAX(ifr.ifr_addr.sa_len, sizeof(struct sockaddr))
  1114. #else
  1115. ( (ifr.ifr_addr.sa_family==AF_INET)?
  1116. sizeof(struct sockaddr_in):
  1117. #ifdef USE_IPV6
  1118. ((ifr.ifr_addr.sa_family==AF_INET6)?
  1119. sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
  1120. #else /* USE_IPV6 */
  1121. sizeof(struct sockaddr) )
  1122. #endif /* USE_IPV6 */
  1123. #endif
  1124. )
  1125. #endif
  1126. )
  1127. {
  1128. /* copy contents into ifr structure
  1129. * warning: it might be longer (e.g. ipv6 address) */
  1130. memcpy(&ifr, p, sizeof(ifr));
  1131. if (ifr.ifr_addr.sa_family!=family){
  1132. /*printf("strange family %d skipping...\n",
  1133. ifr->ifr_addr.sa_family);*/
  1134. continue;
  1135. }
  1136. /*get flags*/
  1137. ifrcopy=ifr;
  1138. if (ioctl(s, SIOCGIFFLAGS, &ifrcopy)!=-1){ /* ignore errors */
  1139. /* ignore down ifs only if listening on all of them*/
  1140. if (if_name==0){
  1141. /* if if not up, skip it*/
  1142. if (!(ifrcopy.ifr_flags & IFF_UP)) continue;
  1143. }
  1144. }
  1145. if ((if_name==0)||
  1146. (strncmp(if_name, ifr.ifr_name, sizeof(ifr.ifr_name))==0)){
  1147. /*add address*/
  1148. sockaddr2ip_addr(&addr,
  1149. (struct sockaddr*)(p+(long)&((struct ifreq*)0)->ifr_addr));
  1150. if ((tmp=ip_addr2a(&addr))==0) goto error;
  1151. /* check if loopback */
  1152. if (ifrcopy.ifr_flags & IFF_LOOPBACK)
  1153. flags|=SI_IS_LO;
  1154. /* save the info */
  1155. if (new_addr_info2list(tmp, flags, ai_l)!=0){
  1156. LOG(L_ERR, "ERROR: add_interfaces: "
  1157. "new_addr_info2list failed\n");
  1158. goto error;
  1159. }
  1160. ret=0;
  1161. }
  1162. /*
  1163. printf("%s:\n", ifr->ifr_name);
  1164. printf(" ");
  1165. print_sockaddr(&(ifr->ifr_addr));
  1166. printf(" ");
  1167. ls_ifflags(ifr->ifr_name, family, options);
  1168. printf("\n");*/
  1169. }
  1170. pkg_free(ifc.ifc_req); /*clean up*/
  1171. close(s);
  1172. return ret;
  1173. error:
  1174. if (ifc.ifc_req) pkg_free(ifc.ifc_req);
  1175. close(s);
  1176. return -1;
  1177. }
  1178. /* internal helper function: resolve host names and add aliases
  1179. * name is a value result parameter: it should contain the hostname that
  1180. * will be used to fill all the other members, including name itself
  1181. * in some situation (name->s should be a 0 terminated pkg_malloc'ed string)
  1182. * return 0 on success and -1 on error */
  1183. static int fix_hostname(str* name, struct ip_addr* address, str* address_str,
  1184. enum si_flags* flags, int* type_flags,
  1185. struct socket_info* s)
  1186. {
  1187. struct hostent* he;
  1188. char* tmp;
  1189. char** h;
  1190. /* get "official hostnames", all the aliases etc. */
  1191. he=resolvehost(name->s);
  1192. if (he==0){
  1193. LOG(L_ERR, "ERROR: fix_hostname: could not resolve %s\n", name->s);
  1194. goto error;
  1195. }
  1196. /* check if we got the official name */
  1197. if (strcasecmp(he->h_name, name->s)!=0){
  1198. if (sr_auto_aliases &&
  1199. add_alias(name->s, name->len, s->port_no, s->proto)<0){
  1200. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  1201. }
  1202. /* change the official name */
  1203. pkg_free(name->s);
  1204. name->s=(char*)pkg_malloc(strlen(he->h_name)+1);
  1205. if (name->s==0){
  1206. LOG(L_ERR, "ERROR: fix_hostname: out of memory.\n");
  1207. goto error;
  1208. }
  1209. name->len=strlen(he->h_name);
  1210. strncpy(name->s, he->h_name, name->len+1);
  1211. }
  1212. /* add the aliases*/
  1213. for(h=he->h_aliases; sr_auto_aliases && h && *h; h++)
  1214. if (add_alias(*h, strlen(*h), s->port_no, s->proto)<0){
  1215. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  1216. }
  1217. hostent2ip_addr(address, he, 0); /*convert to ip_addr format*/
  1218. if (type_flags){
  1219. *type_flags|=(address->af==AF_INET)?SOCKET_T_IPV4:SOCKET_T_IPV6;
  1220. }
  1221. if ((tmp=ip_addr2a(address))==0) goto error;
  1222. address_str->s=pkg_malloc(strlen(tmp)+1);
  1223. if (address_str->s==0){
  1224. LOG(L_ERR, "ERROR: fix_hostname: out of memory.\n");
  1225. goto error;
  1226. }
  1227. strncpy(address_str->s, tmp, strlen(tmp)+1);
  1228. /* set is_ip (1 if name is an ip address, 0 otherwise) */
  1229. address_str->len=strlen(tmp);
  1230. if (sr_auto_aliases && (address_str->len==name->len) &&
  1231. (strncasecmp(address_str->s, name->s, address_str->len)==0)){
  1232. *flags|=SI_IS_IP;
  1233. /* do rev. DNS on it (for aliases)*/
  1234. he=rev_resolvehost(address);
  1235. if (he==0){
  1236. LOG(L_WARN, "WARNING: fix_hostname: could not rev. resolve %s\n",
  1237. name->s);
  1238. }else{
  1239. /* add the aliases*/
  1240. if (add_alias(he->h_name, strlen(he->h_name), s->port_no,
  1241. s->proto)<0){
  1242. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  1243. }
  1244. for(h=he->h_aliases; h && *h; h++)
  1245. if (add_alias(*h, strlen(*h), s->port_no, s->proto) < 0){
  1246. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  1247. }
  1248. }
  1249. }
  1250. #ifdef USE_MCAST
  1251. /* Check if it is an multicast address and
  1252. * set the flag if so
  1253. */
  1254. if (is_mcast(address)){
  1255. *flags |= SI_IS_MCAST;
  1256. }
  1257. #endif /* USE_MCAST */
  1258. /* check if INADDR_ANY */
  1259. if (ip_addr_any(address))
  1260. *flags|=SI_IS_ANY;
  1261. else if (ip_addr_loopback(address)) /* check for loopback */
  1262. *flags|=SI_IS_LO;
  1263. return 0;
  1264. error:
  1265. return -1;
  1266. }
  1267. /* append new elements to a socket_info list after "list"
  1268. * each element is created from addr_info_lst + port, protocol and flags
  1269. * return 0 on succes, -1 on error
  1270. */
  1271. static int addr_info_to_si_lst(struct addr_info* ai_lst, unsigned short port,
  1272. char proto, enum si_flags flags,
  1273. struct socket_info** list)
  1274. {
  1275. struct addr_info* ail;
  1276. for (ail=ai_lst; ail; ail=ail->next){
  1277. if(new_sock2list(ail->name.s, 0, port, proto, 0, 0,
  1278. ail->flags | flags, list)==0)
  1279. return -1;
  1280. }
  1281. return 0;
  1282. }
  1283. /* insert new elements to a socket_info list after "el",
  1284. * each element is created from addr_info_lst + port, * protocol and flags
  1285. * return 0 on succes, -1 on error
  1286. */
  1287. static int addr_info_to_si_lst_after(struct addr_info* ai_lst,
  1288. unsigned short port,
  1289. char proto, enum si_flags flags,
  1290. struct socket_info* el)
  1291. {
  1292. struct addr_info* ail;
  1293. struct socket_info* new_si;
  1294. for (ail=ai_lst; ail; ail=ail->next){
  1295. if((new_si=new_sock2list_after(ail->name.s, 0, port, proto,
  1296. 0, 0, ail->flags | flags, el))==0)
  1297. return -1;
  1298. el=new_si;
  1299. }
  1300. return 0;
  1301. }
  1302. /* fixes a socket list => resolve addresses,
  1303. * interface names, fills missing members, remove duplicates
  1304. * fills type_flags if not null with SOCKET_T_IPV4 and/or SOCKET_T_IPV6*/
  1305. static int fix_socket_list(struct socket_info **list, int* type_flags)
  1306. {
  1307. struct socket_info* si;
  1308. struct socket_info* new_si;
  1309. struct socket_info* l;
  1310. struct socket_info* next;
  1311. struct socket_info* next_si;
  1312. struct socket_info* del_si;
  1313. struct socket_info* keep_si;
  1314. char* tmp;
  1315. int len;
  1316. struct addr_info* ai_lst;
  1317. struct addr_info* ail;
  1318. struct addr_info* tmp_ail;
  1319. struct addr_info* tmp_ail_next;
  1320. struct addr_info* ail_next;
  1321. if (type_flags)
  1322. *type_flags=0;
  1323. /* try to change all the interface names into addresses
  1324. * --ugly hack */
  1325. for (si=*list;si;){
  1326. next=si->next;
  1327. ai_lst=0;
  1328. if (add_interfaces(si->name.s, AF_INET, si->port_no,
  1329. si->proto, &ai_lst)!=-1){
  1330. if (si->flags & SI_IS_MHOMED){
  1331. if((new_si=new_sock2list_after(ai_lst->name.s, 0, si->port_no,
  1332. si->proto, si->useinfo.name.s,
  1333. si->useinfo.port_no,
  1334. ai_lst->flags|si->flags, si))==0)
  1335. break;
  1336. ail=ai_lst;
  1337. ai_lst=ai_lst->next;
  1338. free_addr_info(ail); /* free the first elem. */
  1339. if (ai_lst){
  1340. ai_lst->prev=0;
  1341. /* find the end */
  1342. for (ail=ai_lst; ail->next; ail=ail->next);
  1343. /* add the mh list after the last position in ai_lst */
  1344. addr_info_list_ins_lst(si->addr_info_lst, ail);
  1345. new_si->addr_info_lst=ai_lst;
  1346. si->addr_info_lst=0; /* detached and moved to new_si */
  1347. ail=ail->next; /* ail== old si->addr_info_lst */
  1348. }else{
  1349. ail=si->addr_info_lst;
  1350. new_si->addr_info_lst=ail;
  1351. si->addr_info_lst=0; /* detached and moved to new_si */
  1352. }
  1353. }else{
  1354. /* add all addr. as separate interfaces */
  1355. if (addr_info_to_si_lst_after(ai_lst, si->port_no, si->proto,
  1356. si->flags, si)!=0)
  1357. goto error;
  1358. /* ai_lst not needed anymore */
  1359. free_addr_info_lst(&ai_lst);
  1360. ail=0;
  1361. new_si=0;
  1362. }
  1363. /* success => remove current entry (shift the entire array)*/
  1364. sock_listrm(list, si);
  1365. free_sock_info(si);
  1366. }else{
  1367. new_si=si;
  1368. ail=si->addr_info_lst;
  1369. }
  1370. if (ail){
  1371. if (new_si && (new_si->flags & SI_IS_MHOMED)){
  1372. ai_lst=0;
  1373. for (; ail;){
  1374. ail_next=ail->next;
  1375. if (add_interfaces(ail->name.s, AF_INET, new_si->port_no,
  1376. new_si->proto, &ai_lst)!=-1){
  1377. /* add the resolved list after the current position */
  1378. addr_info_list_ins_lst(ai_lst, ail);
  1379. /* success, remove the current entity */
  1380. addr_info_listrm(&new_si->addr_info_lst, ail);
  1381. free_addr_info(ail);
  1382. ai_lst=0;
  1383. }
  1384. ail=ail_next;
  1385. }
  1386. }
  1387. }
  1388. si=next;
  1389. }
  1390. /* get ips & fill the port numbers*/
  1391. #ifdef EXTRA_DEBUG
  1392. DBG("Listening on \n");
  1393. #endif
  1394. for (si=*list;si;si=si->next){
  1395. /* fix port number, port_no should be !=0 here */
  1396. if (si->port_no==0){
  1397. #ifdef USE_TLS
  1398. si->port_no= (si->proto==PROTO_TLS)?tls_port_no:port_no;
  1399. #else
  1400. si->port_no= port_no;
  1401. #endif
  1402. }
  1403. tmp=int2str(si->port_no, &len);
  1404. if (len>=MAX_PORT_LEN){
  1405. LOG(L_ERR, "ERROR: fix_socket_list: bad port number: %d\n",
  1406. si->port_no);
  1407. goto error;
  1408. }
  1409. si->port_no_str.s=(char*)pkg_malloc(len+1);
  1410. if (si->port_no_str.s==0){
  1411. LOG(L_ERR, "ERROR: fix_socket_list: out of memory.\n");
  1412. goto error;
  1413. }
  1414. strncpy(si->port_no_str.s, tmp, len+1);
  1415. si->port_no_str.len=len;
  1416. if (fix_hostname(&si->name, &si->address, &si->address_str,
  1417. &si->flags, type_flags, si) !=0 )
  1418. goto error;
  1419. /* fix hostnames in mh addresses */
  1420. for (ail=si->addr_info_lst; ail; ail=ail->next){
  1421. if (fix_hostname(&ail->name, &ail->address, &ail->address_str,
  1422. &ail->flags, type_flags, si) !=0 )
  1423. goto error;
  1424. }
  1425. if (fix_sock_str(si) < 0) goto error;
  1426. #ifdef EXTRA_DEBUG
  1427. printf(" %.*s [%s]:%s%s\n", si->name.len,
  1428. si->name.s, si->address_str.s, si->port_no_str.s,
  1429. si->flags & SI_IS_MCAST ? " mcast" : "");
  1430. #endif
  1431. }
  1432. /* removing duplicate addresses*/
  1433. for (si=*list;si; ){
  1434. next_si=si->next;
  1435. for (l=si->next;l;){
  1436. next=l->next;
  1437. if ((si->port_no==l->port_no) &&
  1438. (si->address.af==l->address.af) &&
  1439. (memcmp(si->address.u.addr, l->address.u.addr,
  1440. si->address.len) == 0)
  1441. ){
  1442. /* remove the socket with no extra addresses.,
  1443. * if both of them have extra addresses, remove one of them
  1444. * and merge the extra addresses into the other */
  1445. if (l->addr_info_lst==0){
  1446. del_si=l;
  1447. keep_si=si;
  1448. }else if (si->addr_info_lst==0){
  1449. del_si=si;
  1450. keep_si=l;
  1451. }else{
  1452. /* move l->addr_info_lst to si->addr_info_lst */
  1453. /* find last elem */
  1454. for (ail=si->addr_info_lst; ail->next; ail=ail->next);
  1455. /* add the l list after the last position in si lst */
  1456. addr_info_list_ins_lst(l->addr_info_lst, ail);
  1457. l->addr_info_lst=0; /* detached */
  1458. del_si=l; /* l will be removed */
  1459. keep_si=l;
  1460. }
  1461. #ifdef EXTRA_DEBUG
  1462. printf("removing duplicate %s [%s] == %s [%s]\n",
  1463. keep_si->name.s, keep_si->address_str.s,
  1464. del_si->name.s, del_si->address_str.s);
  1465. #endif
  1466. /* add the name to the alias list*/
  1467. if ((!(del_si->flags& SI_IS_IP)) && (
  1468. (del_si->name.len!=keep_si->name.len)||
  1469. (strncmp(del_si->name.s, keep_si->name.s,
  1470. del_si->name.len)!=0))
  1471. )
  1472. add_alias(del_si->name.s, del_si->name.len,
  1473. l->port_no, l->proto);
  1474. /* make sure next_si doesn't point to del_si */
  1475. if (del_si==next_si)
  1476. next_si=next_si->next;
  1477. /* remove del_si*/
  1478. sock_listrm(list, del_si);
  1479. free_sock_info(del_si);
  1480. }
  1481. l=next;
  1482. }
  1483. si=next_si;
  1484. }
  1485. /* check for duplicates in extra_addresses */
  1486. for (si=*list;si; si=si->next){
  1487. /* check for & remove internal duplicates: */
  1488. for (ail=si->addr_info_lst; ail;){
  1489. ail_next=ail->next;
  1490. /* 1. check if the extra addresses contain a duplicate for the
  1491. * main one */
  1492. if ((ail->address.af==si->address.af) &&
  1493. (memcmp(ail->address.u.addr, si->address.u.addr,
  1494. ail->address.len) == 0)){
  1495. /* add the name to the alias list*/
  1496. if ((!(ail->flags& SI_IS_IP)) && (
  1497. (ail->name.len!=si->name.len)||
  1498. (strncmp(ail->name.s, si->name.s, ail->name.len)!=0)))
  1499. add_alias(ail->name.s, ail->name.len, si->port_no,
  1500. si->proto);
  1501. /* remove ail*/
  1502. addr_info_listrm(&si->addr_info_lst, ail);
  1503. free_addr_info(ail);
  1504. ail=ail_next;
  1505. continue;
  1506. }
  1507. /* 2. check if the extra addresses contain a duplicates for
  1508. * other addresses in the same list */
  1509. for (tmp_ail=ail->next; tmp_ail;){
  1510. tmp_ail_next=tmp_ail->next;
  1511. if ((ail->address.af==tmp_ail->address.af) &&
  1512. (memcmp(ail->address.u.addr, tmp_ail->address.u.addr,
  1513. ail->address.len) == 0)){
  1514. /* add the name to the alias list*/
  1515. if ((!(tmp_ail->flags& SI_IS_IP)) && (
  1516. (ail->name.len!=tmp_ail->name.len)||
  1517. (strncmp(ail->name.s, tmp_ail->name.s,
  1518. tmp_ail->name.len)!=0))
  1519. )
  1520. add_alias(tmp_ail->name.s, tmp_ail->name.len,
  1521. si->port_no, si->proto);
  1522. /* remove tmp_ail*/
  1523. addr_info_listrm(&si->addr_info_lst, tmp_ail);
  1524. free_addr_info(tmp_ail);
  1525. }
  1526. tmp_ail=tmp_ail_next;
  1527. }
  1528. ail=ail_next;
  1529. }
  1530. /* check for duplicates between extra addresses (e.g. sctp MH)
  1531. * and other main addresses, on conflict remove the corresponding
  1532. * extra addresses (another possible solution would be to join
  1533. * the 2 si entries into one). */
  1534. for (ail=si->addr_info_lst; ail;){
  1535. ail_next=ail->next;
  1536. for (l=*list;l; l=l->next){
  1537. if (l==si) continue;
  1538. if (si->port_no==l->port_no){
  1539. if ((ail->address.af==l->address.af) &&
  1540. (memcmp(ail->address.u.addr, l->address.u.addr,
  1541. ail->address.len) == 0)){
  1542. /* add the name to the alias list*/
  1543. if ((!(ail->flags& SI_IS_IP)) && (
  1544. (ail->name.len!=l->name.len)||
  1545. (strncmp(ail->name.s, l->name.s, l->name.len)!=0))
  1546. )
  1547. add_alias(ail->name.s, ail->name.len,
  1548. l->port_no, l->proto);
  1549. /* remove ail*/
  1550. addr_info_listrm(&si->addr_info_lst, ail);
  1551. free_addr_info(ail);
  1552. break;
  1553. }
  1554. /* check for duplicates with other extra addresses
  1555. * lists */
  1556. for (tmp_ail=l->addr_info_lst; tmp_ail; ){
  1557. tmp_ail_next=tmp_ail->next;
  1558. if ((ail->address.af==tmp_ail->address.af) &&
  1559. (memcmp(ail->address.u.addr,
  1560. tmp_ail->address.u.addr,
  1561. ail->address.len) == 0)){
  1562. /* add the name to the alias list*/
  1563. if ((!(tmp_ail->flags& SI_IS_IP)) && (
  1564. (ail->name.len!=tmp_ail->name.len)||
  1565. (strncmp(ail->name.s, tmp_ail->name.s,
  1566. tmp_ail->name.len)!=0))
  1567. )
  1568. add_alias(tmp_ail->name.s, tmp_ail->name.len,
  1569. l->port_no, l->proto);
  1570. /* remove tmp_ail*/
  1571. addr_info_listrm(&l->addr_info_lst, tmp_ail);
  1572. free_addr_info(tmp_ail);
  1573. }
  1574. tmp_ail=tmp_ail_next;
  1575. }
  1576. }
  1577. }
  1578. ail=ail_next;
  1579. }
  1580. }
  1581. #ifdef USE_MCAST
  1582. /* Remove invalid multicast entries */
  1583. si=*list;
  1584. while(si){
  1585. if ((si->proto == PROTO_TCP)
  1586. #ifdef USE_TLS
  1587. || (si->proto == PROTO_TLS)
  1588. #endif /* USE_TLS */
  1589. #ifdef USE_SCTP
  1590. || (si->proto == PROTO_SCTP)
  1591. #endif
  1592. ){
  1593. if (si->flags & SI_IS_MCAST){
  1594. LOG(L_WARN, "WARNING: removing entry %s:%s [%s]:%s\n",
  1595. get_valid_proto_name(si->proto), si->name.s,
  1596. si->address_str.s, si->port_no_str.s);
  1597. l = si;
  1598. si=si->next;
  1599. sock_listrm(list, l);
  1600. free_sock_info(l);
  1601. }else{
  1602. ail=si->addr_info_lst;
  1603. while(ail){
  1604. if (ail->flags & SI_IS_MCAST){
  1605. LOG(L_WARN, "WARNING: removing mh entry %s:%s"
  1606. " [%s]:%s\n",
  1607. get_valid_proto_name(si->proto), ail->name.s,
  1608. ail->address_str.s, si->port_no_str.s);
  1609. tmp_ail=ail;
  1610. ail=ail->next;
  1611. addr_info_listrm(&si->addr_info_lst, tmp_ail);
  1612. free_addr_info(tmp_ail);
  1613. }else{
  1614. ail=ail->next;
  1615. }
  1616. }
  1617. si=si->next;
  1618. }
  1619. } else {
  1620. si=si->next;
  1621. }
  1622. }
  1623. #endif /* USE_MCAST */
  1624. return 0;
  1625. error:
  1626. return -1;
  1627. }
  1628. int socket_types = 0;
  1629. /* fix all 3 socket lists, fills socket_types if non-null
  1630. * return 0 on success, -1 on error */
  1631. int fix_all_socket_lists()
  1632. {
  1633. struct utsname myname;
  1634. int flags;
  1635. struct addr_info* ai_lst;
  1636. ai_lst=0;
  1637. if ((udp_listen==0)
  1638. #ifdef USE_TCP
  1639. && (tcp_listen==0)
  1640. #ifdef USE_TLS
  1641. && (tls_listen==0)
  1642. #endif
  1643. #endif
  1644. #ifdef USE_SCTP
  1645. && (sctp_listen==0)
  1646. #endif
  1647. ){
  1648. /* get all listening ipv4/ipv6 interfaces */
  1649. if ( ( (add_interfaces(0, AF_INET, 0, PROTO_UDP, &ai_lst)==0)
  1650. #ifdef USE_IPV6
  1651. #ifdef __OS_linux
  1652. && (!auto_bind_ipv6 || add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_UDP, &ai_lst) == 0)
  1653. #else
  1654. && ( !auto_bind_ipv6 || add_interfaces(0, AF_INET6, 0, PROTO_UDP, &ai_lst) !=0 ) /* add_interface does not work for IPv6 on Linux */
  1655. #endif /* __OS_linux */
  1656. #endif /* USE_IPV6 */
  1657. ) && (addr_info_to_si_lst(ai_lst, 0, PROTO_UDP, 0, &udp_listen)==0)){
  1658. free_addr_info_lst(&ai_lst);
  1659. ai_lst=0;
  1660. /* if ok, try to add the others too */
  1661. #ifdef USE_TCP
  1662. if (!tcp_disable){
  1663. if ( ((add_interfaces(0, AF_INET, 0, PROTO_TCP, &ai_lst)!=0)
  1664. #ifdef USE_IPV6
  1665. #ifdef __OS_linux
  1666. || (auto_bind_ipv6 && add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_TCP, &ai_lst) != 0)
  1667. #else
  1668. || (auto_bind_ipv6 && add_interfaces(0, AF_INET6, 0, PROTO_TCP, &ai_lst) !=0 )
  1669. #endif /* __OS_linux */
  1670. #endif /* USE_IPV6 */
  1671. ) || (addr_info_to_si_lst(ai_lst, 0, PROTO_TCP, 0,
  1672. &tcp_listen)!=0))
  1673. goto error;
  1674. free_addr_info_lst(&ai_lst);
  1675. ai_lst=0;
  1676. #ifdef USE_TLS
  1677. if (!tls_disable){
  1678. if (((add_interfaces(0, AF_INET, 0, PROTO_TLS,
  1679. &ai_lst)!=0)
  1680. #ifdef USE_IPV6
  1681. #ifdef __OS_linux
  1682. || (auto_bind_ipv6 && add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_TLS, &ai_lst) != 0)
  1683. #else
  1684. || (auto_bind_ipv6 && add_interfaces(0, AF_INET6, 0, PROTO_TLS, &ai_lst)!=0)
  1685. #endif /* __OS_linux */
  1686. #endif /* USE_IPV6 */
  1687. ) || (addr_info_to_si_lst(ai_lst, 0, PROTO_TLS, 0,
  1688. &tls_listen)!=0))
  1689. goto error;
  1690. }
  1691. free_addr_info_lst(&ai_lst);
  1692. ai_lst=0;
  1693. #endif
  1694. }
  1695. #endif
  1696. #ifdef USE_SCTP
  1697. if (!sctp_disable){
  1698. if (((add_interfaces(0, AF_INET, 0, PROTO_SCTP, &ai_lst)!=0)
  1699. #ifdef USE_IPV6
  1700. #ifdef __OS_linux
  1701. || (auto_bind_ipv6 && add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_SCTP, &ai_lst) != 0)
  1702. #else
  1703. || (auto_bind_ipv6 && add_interfaces(0, AF_INET6, 0, PROTO_SCTP, &ai_lst) != 0)
  1704. #endif /* __OS_linux */
  1705. #endif /* USE_IPV6 */
  1706. ) || (addr_info_to_si_lst(ai_lst, 0, PROTO_SCTP, 0,
  1707. &sctp_listen)!=0))
  1708. goto error;
  1709. free_addr_info_lst(&ai_lst);
  1710. ai_lst=0;
  1711. }
  1712. #endif /* USE_SCTP */
  1713. }else{
  1714. /* if error fall back to get hostname */
  1715. /* get our address, only the first one */
  1716. if (uname (&myname) <0){
  1717. LOG(L_ERR, "ERROR: fix_all_socket_lists: cannot determine"
  1718. " hostname, try -l address\n");
  1719. goto error;
  1720. }
  1721. if (add_listen_iface(myname.nodename, 0, 0, 0, 0)!=0){
  1722. LOG(L_ERR, "ERROR: fix_all_socket_lists: add_listen_iface "
  1723. "failed \n");
  1724. goto error;
  1725. }
  1726. }
  1727. }
  1728. flags=0;
  1729. if (fix_socket_list(&udp_listen, &flags)!=0){
  1730. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1731. " udp failed\n");
  1732. goto error;
  1733. }
  1734. if (flags){
  1735. socket_types|=flags|SOCKET_T_UDP;
  1736. }
  1737. #ifdef USE_TCP
  1738. flags=0;
  1739. if (!tcp_disable && (fix_socket_list(&tcp_listen, &flags)!=0)){
  1740. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1741. " tcp failed\n");
  1742. goto error;
  1743. }
  1744. if (flags){
  1745. socket_types|=flags|SOCKET_T_TCP;
  1746. }
  1747. #ifdef USE_TLS
  1748. flags=0;
  1749. if (!tls_disable && (fix_socket_list(&tls_listen, &flags)!=0)){
  1750. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1751. " tls failed\n");
  1752. goto error;
  1753. }
  1754. if (flags){
  1755. socket_types|=flags|SOCKET_T_TLS;
  1756. }
  1757. #endif
  1758. #endif
  1759. #ifdef USE_SCTP
  1760. flags=0;
  1761. if (!sctp_disable && (fix_socket_list(&sctp_listen, &flags)!=0)){
  1762. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1763. " sctp failed\n");
  1764. goto error;
  1765. }
  1766. if (flags){
  1767. socket_types|=flags|SOCKET_T_SCTP;
  1768. }
  1769. #endif /* USE_SCTP */
  1770. if ((udp_listen==0)
  1771. #ifdef USE_TCP
  1772. && (tcp_listen==0)
  1773. #ifdef USE_TLS
  1774. && (tls_listen==0)
  1775. #endif
  1776. #endif
  1777. #ifdef USE_SCTP
  1778. && (sctp_listen==0)
  1779. #endif
  1780. ){
  1781. LOG(L_ERR, "ERROR: fix_all_socket_lists: no listening sockets\n");
  1782. goto error;
  1783. }
  1784. return 0;
  1785. error:
  1786. if (ai_lst) free_addr_info_lst(&ai_lst);
  1787. return -1;
  1788. }
  1789. void print_all_socket_lists()
  1790. {
  1791. struct socket_info *si;
  1792. struct socket_info** list;
  1793. struct addr_info* ai;
  1794. unsigned short proto;
  1795. proto=PROTO_UDP;
  1796. do{
  1797. list=get_sock_info_list(proto);
  1798. for(si=list?*list:0; si; si=si->next){
  1799. if (si->addr_info_lst){
  1800. printf(" %s: (%s",
  1801. get_valid_proto_name(proto),
  1802. si->address_str.s);
  1803. for (ai=si->addr_info_lst; ai; ai=ai->next)
  1804. printf(", %s", ai->address_str.s);
  1805. printf("):%s%s%s\n",
  1806. si->port_no_str.s,
  1807. si->flags & SI_IS_MCAST ? " mcast" : "",
  1808. si->flags & SI_IS_MHOMED? " mhomed" : "");
  1809. }else{
  1810. printf(" %s: %s",
  1811. get_valid_proto_name(proto),
  1812. si->name.s);
  1813. if (!si->flags & SI_IS_IP)
  1814. printf(" [%s]", si->address_str.s);
  1815. printf( ":%s%s%s",
  1816. si->port_no_str.s,
  1817. si->flags & SI_IS_MCAST ? " mcast" : "",
  1818. si->flags & SI_IS_MHOMED? " mhomed" : "");
  1819. if (si->useinfo.name.s)
  1820. printf(" advertise %s:%d", si->useinfo.name.s, si->useinfo.port_no);
  1821. printf("\n");
  1822. }
  1823. }
  1824. }while((proto=next_proto(proto)));
  1825. }
  1826. void print_aliases()
  1827. {
  1828. struct host_alias* a;
  1829. for(a=aliases; a; a=a->next)
  1830. if (a->port)
  1831. printf(" %s: %.*s:%d\n", get_valid_proto_name(a->proto),
  1832. a->alias.len, a->alias.s, a->port);
  1833. else
  1834. printf(" %s: %.*s:*\n", get_valid_proto_name(a->proto),
  1835. a->alias.len, a->alias.s);
  1836. }
  1837. void init_proto_order()
  1838. {
  1839. int r;
  1840. /* fix proto list (remove disabled protocols)*/
  1841. #ifdef USE_TCP
  1842. if (tcp_disable)
  1843. #endif
  1844. for(r=PROTO_NONE; r<=PROTO_LAST; r++){
  1845. if (nxt_proto[r]==PROTO_TCP)
  1846. nxt_proto[r]=nxt_proto[PROTO_TCP];
  1847. }
  1848. #ifdef USE_TCP
  1849. #ifdef USE_TLS
  1850. if (tls_disable || tcp_disable)
  1851. #endif
  1852. #endif
  1853. for(r=PROTO_NONE; r<=PROTO_LAST; r++){
  1854. if (nxt_proto[r]==PROTO_TLS)
  1855. nxt_proto[r]=nxt_proto[PROTO_TLS];
  1856. }
  1857. #ifdef USE_SCTP
  1858. if (sctp_disable)
  1859. #endif
  1860. for(r=PROTO_NONE; r<=PROTO_LAST; r++){
  1861. if (nxt_proto[r]==PROTO_SCTP)
  1862. nxt_proto[r]=nxt_proto[PROTO_SCTP];
  1863. }
  1864. /* Deliberately skipping PROTO_WS and PROTO_WSS here as these
  1865. are just upgraded TCP and TLS connections */
  1866. }