socket_info.c 52 KB

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