socket_info.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  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. struct ip_addr* ip6;
  445. if ( (host->len==name->len) &&
  446. (strncasecmp(host->s, name->s, name->len)==0) /*slower*/)
  447. /* comp. must be case insensitive, host names
  448. * can be written in mixed case, it will also match
  449. * ipv6 addresses if we are lucky*/
  450. goto found;
  451. /* check if host == ip address */
  452. /* ipv6 case is uglier, host can be [3ffe::1] */
  453. ip6=str2ip6(host);
  454. if (ip6){
  455. if (ip_addr_cmp(ip6, ip_addr))
  456. goto found; /* match */
  457. else
  458. return -1; /* no match, but this is an ipv6 address
  459. so no point in trying ipv4 */
  460. }
  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. if ((hname.len>2)&&((*hname.s)=='[')&&(hname.s[hname.len-1]==']')){
  487. /* ipv6 reference, skip [] */
  488. hname.s++;
  489. hname.len-=2;
  490. }
  491. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  492. retry:
  493. do{
  494. /* get the proper sock_list */
  495. list=get_sock_info_list(c_proto);
  496. if (list==0) /* disabled or unknown protocol */
  497. continue;
  498. for (si=*list; si; si=si->next){
  499. DBG("grep_sock_info - checking if host==us: %d==%d &&"
  500. " [%.*s] == [%.*s]\n",
  501. hname.len,
  502. si->name.len,
  503. hname.len, hname.s,
  504. si->name.len, si->name.s
  505. );
  506. if (port) {
  507. DBG("grep_sock_info - checking if port %d (advertise %d)"
  508. " matches port %d\n",
  509. si->port_no, si->useinfo.port_no, port);
  510. if (si->port_no!=port && si->useinfo.port_no!=port) {
  511. continue;
  512. }
  513. }
  514. if (si_hname_cmp(&hname, &si->name, &si->address_str,
  515. &si->address, si->flags)==0)
  516. goto found;
  517. if(si->useinfo.name.s!=NULL)
  518. {
  519. DBG("grep_sock_info - checking advertise if host==us:"
  520. " %d==%d && [%.*s] == [%.*s]\n",
  521. hname.len,
  522. si->useinfo.name.len,
  523. hname.len, hname.s,
  524. si->useinfo.name.len, si->useinfo.name.s
  525. );
  526. if (si_hname_cmp(&hname, &si->useinfo.name,
  527. &si->useinfo.address_str, &si->useinfo.address,
  528. si->flags)==0)
  529. goto found;
  530. }
  531. /* try among the extra addresses */
  532. for (ai=si->addr_info_lst; ai; ai=ai->next)
  533. if (si_hname_cmp(&hname, &ai->name, &ai->address_str,
  534. &ai->address, ai->flags)==0)
  535. goto found;
  536. }
  537. }while( (proto==0) && (c_proto=next_proto(c_proto)) );
  538. #ifdef USE_TLS
  539. if (unlikely(c_proto == PROTO_WS)) {
  540. c_proto = PROTO_WSS;
  541. goto retry;
  542. }
  543. #endif
  544. /* not_found: */
  545. return 0;
  546. found:
  547. return si;
  548. }
  549. /* checks if the proto:port is one of the ports we listen on
  550. * and returns the corresponding socket_info structure.
  551. * if proto==0 (PROTO_NONE) the protocol is ignored
  552. * returns 0 if not found
  553. */
  554. struct socket_info* grep_sock_info_by_port(unsigned short port,
  555. unsigned short proto)
  556. {
  557. struct socket_info* si;
  558. struct socket_info** list;
  559. unsigned short c_proto;
  560. if (!port) {
  561. goto not_found;
  562. }
  563. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  564. do{
  565. /* get the proper sock_list */
  566. list=get_sock_info_list(c_proto);
  567. if (list==0) /* disabled or unknown protocol */
  568. continue;
  569. for (si=*list; si; si=si->next){
  570. DBG("grep_sock_info_by_port - checking if port %d matches"
  571. " port %d\n", si->port_no, port);
  572. if (si->port_no==port) {
  573. goto found;
  574. }
  575. }
  576. }while( (proto==0) && (c_proto=next_proto(c_proto)) );
  577. not_found:
  578. return 0;
  579. found:
  580. return si;
  581. }
  582. /* checks if the proto: ip:port is one of the address we listen on
  583. * and returns the corresponding socket_info structure.
  584. * (same as grep_socket_info, but use ip addr instead)
  585. * if port==0, the port number is ignored
  586. * if proto==0 (PROTO_NONE) the protocol is ignored
  587. * returns 0 if not found
  588. * WARNING: uses str2ip6 so it will overwrite any previous
  589. * unsaved result of this function (static buffer)
  590. */
  591. struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
  592. unsigned short proto)
  593. {
  594. struct socket_info* si;
  595. struct socket_info** list;
  596. struct addr_info* ai;
  597. unsigned short c_proto;
  598. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  599. do{
  600. /* get the proper sock_list */
  601. list=get_sock_info_list(c_proto);
  602. if (list==0) /* disabled or unknown protocol */
  603. continue;
  604. for (si=*list; si; si=si->next){
  605. if (port) {
  606. if (si->port_no!=port) {
  607. continue;
  608. }
  609. }
  610. if (ip_addr_cmp(ip, &si->address)
  611. || ip_addr_cmp(ip, &si->useinfo.address))
  612. goto found;
  613. for (ai=si->addr_info_lst; ai; ai=ai->next)
  614. if (ip_addr_cmp(ip, &ai->address))
  615. goto found;
  616. }
  617. }while( (proto==0) && (c_proto=next_proto(c_proto)) );
  618. /* not_found: */
  619. return 0;
  620. found:
  621. return si;
  622. }
  623. /* append a new sock_info structure to the corresponding list
  624. * return new sock info on success, 0 on error */
  625. static struct socket_info* new_sock2list(char* name, struct name_lst* addr_l,
  626. unsigned short port,
  627. unsigned short proto,
  628. char *usename, unsigned short useport,
  629. enum si_flags flags,
  630. struct socket_info** list)
  631. {
  632. struct socket_info* si;
  633. si=new_sock_info(name, addr_l, port, proto, usename, useport, flags);
  634. if (si==0){
  635. LOG(L_ERR, "ERROR: new_sock2list: new_sock_info failed\n");
  636. goto error;
  637. }
  638. if(socket_workers>0) {
  639. si->workers = socket_workers;
  640. socket_workers = 0;
  641. }
  642. sock_listadd(list, si);
  643. return si;
  644. error:
  645. return 0;
  646. }
  647. /* adds a new sock_info structure immediately after "after"
  648. * return new sock info on success, 0 on error */
  649. static struct socket_info* new_sock2list_after(char* name,
  650. struct name_lst* addr_l,
  651. unsigned short port,
  652. unsigned short proto,
  653. char *usename,
  654. unsigned short useport,
  655. enum si_flags flags,
  656. struct socket_info* after)
  657. {
  658. struct socket_info* si;
  659. si=new_sock_info(name, addr_l, port, proto, usename, useport, flags);
  660. if (si==0){
  661. LOG(L_ERR, "ERROR: new_sock2list_after: new_sock_info failed\n");
  662. goto error;
  663. }
  664. sock_listins(si, after);
  665. return si;
  666. error:
  667. return 0;
  668. }
  669. /* adds a sock_info structure to the corresponding proto list
  670. * return 0 on success, -1 on error */
  671. int add_listen_advertise_iface(char* name, struct name_lst* addr_l,
  672. unsigned short port, unsigned short proto,
  673. char *usename, unsigned short useport,
  674. enum si_flags flags)
  675. {
  676. struct socket_info** list;
  677. unsigned short c_proto;
  678. struct name_lst* a_l;
  679. unsigned short c_port;
  680. c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
  681. do{
  682. list=get_sock_info_list(c_proto);
  683. if (list==0) /* disabled or unknown protocol */
  684. continue;
  685. if (port==0){ /* use default port */
  686. c_port=
  687. #ifdef USE_TLS
  688. ((c_proto)==PROTO_TLS)?tls_port_no:
  689. #endif
  690. port_no;
  691. }
  692. #ifdef USE_TLS
  693. else if ((c_proto==PROTO_TLS) && (proto==0)){
  694. /* -l ip:port => on udp:ip:port; tcp:ip:port and tls:ip:port+1?*/
  695. c_port=port+1;
  696. }
  697. #endif
  698. else{
  699. c_port=port;
  700. }
  701. if (c_proto!=PROTO_SCTP){
  702. if (new_sock2list(name, 0, c_port, c_proto, usename, useport,
  703. flags & ~SI_IS_MHOMED, list)==0){
  704. LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
  705. goto error;
  706. }
  707. /* add the other addresses in the list as separate sockets
  708. * since only SCTP can bind to multiple addresses */
  709. for (a_l=addr_l; a_l; a_l=a_l->next){
  710. if (new_sock2list(a_l->name, 0, c_port,
  711. c_proto, usename, useport,
  712. flags & ~SI_IS_MHOMED, list)==0){
  713. LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list"
  714. " failed\n");
  715. goto error;
  716. }
  717. }
  718. }else{
  719. if (new_sock2list(name, addr_l, c_port, c_proto, usename, useport,
  720. flags, list)==0){
  721. LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
  722. goto error;
  723. }
  724. }
  725. }while( (proto==0) && (c_proto=next_proto(c_proto)));
  726. return 0;
  727. error:
  728. return -1;
  729. }
  730. /* adds a sock_info structure to the corresponding proto list
  731. * return 0 on success, -1 on error */
  732. int add_listen_iface(char* name, struct name_lst* addr_l,
  733. unsigned short port, unsigned short proto,
  734. enum si_flags flags)
  735. {
  736. return add_listen_advertise_iface(name, addr_l, port, proto, 0, 0, flags);
  737. }
  738. #ifdef __OS_linux
  739. #include "linux/types.h"
  740. #include "linux/netlink.h"
  741. #include "linux/rtnetlink.h"
  742. #include "arpa/inet.h"
  743. #define MAX_IF_LEN 64
  744. struct idx
  745. {
  746. struct idx * next;
  747. int family;
  748. unsigned ifa_flags;
  749. char addr[MAX_IF_LEN];
  750. };
  751. struct idxlist{
  752. struct idx* addresses;
  753. int index;
  754. char name[MAX_IF_LEN];
  755. unsigned flags;
  756. };
  757. #define MAX_IFACE_NO 32
  758. static struct idxlist *ifaces = NULL;
  759. static int seq = 0;
  760. #define SADDR(s) ((struct sockaddr_in*)s)->sin_addr.s_addr
  761. #define NLMSG_TAIL(nmsg) \
  762. ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
  763. int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
  764. int alen)
  765. {
  766. int len = RTA_LENGTH(alen);
  767. struct rtattr *rta;
  768. if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
  769. fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
  770. return -1;
  771. }
  772. rta = NLMSG_TAIL(n);
  773. rta->rta_type = type;
  774. rta->rta_len = len;
  775. memcpy(RTA_DATA(rta), data, alen);
  776. n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
  777. return 0;
  778. }
  779. static int nl_bound_sock(void)
  780. {
  781. int sock;
  782. struct sockaddr_nl la;
  783. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  784. if(sock <= 0){
  785. LM_ERR("could not create NETLINK sock to get interface list");
  786. goto error;
  787. }
  788. /* bind NETLINK socket to pid */
  789. bzero(&la, sizeof(la));
  790. la.nl_family = AF_NETLINK;
  791. la.nl_pad = 0;
  792. la.nl_pid = getpid();
  793. la.nl_groups = 0;
  794. if ( bind(sock, (struct sockaddr*) &la, sizeof(la)) < 0){
  795. LM_ERR("could not bind NETLINK sock to sockaddr_nl\n");
  796. goto error;
  797. }
  798. return sock;
  799. error:
  800. if(sock > 0) close(sock);
  801. return -1;
  802. }
  803. #define fill_nl_req(req, type, family) do {\
  804. memset(&req, 0, sizeof(req));\
  805. req.nlh.nlmsg_len = sizeof(req);\
  806. req.nlh.nlmsg_type = type;\
  807. req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST|NLM_F_DUMP;\
  808. req.nlh.nlmsg_pid = getpid();\
  809. req.nlh.nlmsg_seq = seq++;\
  810. req.g.rtgen_family = family;\
  811. } while(0);
  812. static int get_flags(int family){
  813. struct {
  814. struct nlmsghdr nlh;
  815. struct rtgenmsg g;
  816. } req;
  817. int rtn = 0;
  818. struct nlmsghdr* nlp;
  819. struct ifinfomsg *ifi;
  820. char buf[8192];
  821. char *p = buf;
  822. int nll = 0;
  823. int nl_sock = 0;
  824. fill_nl_req(req, RTM_GETLINK, AF_INET);
  825. if((nl_sock = nl_bound_sock()) < 0) return -1;
  826. if(send(nl_sock, (void*)&req, sizeof(req), 0) < 0)
  827. {
  828. LM_ERR("error sending NETLINK request\n");
  829. goto error;
  830. }
  831. while(1) {
  832. rtn = recv(nl_sock, p, sizeof(buf) - nll, 0);
  833. nlp = (struct nlmsghdr *) p;
  834. if(nlp->nlmsg_type == NLMSG_DONE){
  835. LM_DBG("done\n");
  836. break;
  837. }
  838. if(nlp->nlmsg_type == NLMSG_ERROR){
  839. LM_DBG("Error on message to netlink");
  840. break;
  841. }
  842. p += rtn;
  843. nll += rtn;
  844. }
  845. nlp = (struct nlmsghdr *) buf;
  846. for(;NLMSG_OK(nlp, nll);nlp=NLMSG_NEXT(nlp, nll)){
  847. ifi = NLMSG_DATA(nlp);
  848. if (nlp->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
  849. goto error;
  850. LM_ERR("Interface with index %d has flags %d\n", ifi->ifi_index, ifi->ifi_flags);
  851. if(ifaces == NULL){
  852. LM_ERR("get_flags must not be called on empty interface list");
  853. goto error;
  854. }
  855. if(ifi->ifi_index >= MAX_IFACE_NO){
  856. LM_ERR("invalid network interface index returned %d", ifi->ifi_index);
  857. goto error;
  858. }
  859. ifaces[ifi->ifi_index].flags = ifi->ifi_flags;
  860. }
  861. if(nl_sock>0) close(nl_sock);
  862. return 0;
  863. error:
  864. if(nl_sock>0) close(nl_sock);
  865. return -1;
  866. }
  867. static int build_iface_list(void)
  868. {
  869. struct {
  870. struct nlmsghdr nlh;
  871. struct rtgenmsg g;
  872. } req;
  873. int seq = 0;
  874. int rtn = 0;
  875. struct nlmsghdr* nlp;
  876. struct ifaddrmsg *ifi;
  877. int rtl;
  878. char buf[8192];
  879. char *p = buf;
  880. int nll = 0;
  881. struct rtattr * rtap;
  882. int index, i;
  883. struct idx* entry;
  884. struct idx* tmp;
  885. int nl_sock = 0;
  886. int families[] = {AF_INET, AF_INET6};
  887. char name[MAX_IF_LEN];
  888. int is_link_local = 0;
  889. if(ifaces == NULL){
  890. if((ifaces = (struct idxlist*)pkg_malloc(MAX_IFACE_NO*sizeof(struct idxlist))) == NULL){
  891. LM_ERR("No more pkg memory\n");
  892. return -1;
  893. }
  894. memset(ifaces, 0, sizeof(struct idxlist)*MAX_IFACE_NO);
  895. }
  896. /* bind netlink socket */
  897. if((nl_sock = nl_bound_sock()) < 0) return -1;
  898. for (i = 0 ; i < sizeof(families)/sizeof(int); i++) {
  899. fill_nl_req(req, RTM_GETADDR, families[i]);
  900. if(send(nl_sock, (void*)&req, sizeof(req), 0) < 0){
  901. LM_ERR("error sending NETLINK request\n");
  902. goto error;
  903. };
  904. memset(buf, 0, sizeof(buf));
  905. nll = 0;
  906. p = buf;
  907. while(1) {
  908. rtn = recv(nl_sock, p, sizeof(buf) - nll, 0);
  909. LM_DBG("received %d byles \n", rtn);
  910. nlp = (struct nlmsghdr *) p;
  911. if(nlp->nlmsg_type == NLMSG_DONE){
  912. LM_DBG("done receiving netlink info \n");
  913. break;
  914. }
  915. if(nlp->nlmsg_type == NLMSG_ERROR){
  916. LM_ERR("Error on message to netlink");
  917. break;
  918. }
  919. p += rtn;
  920. nll += rtn;
  921. }
  922. nlp = (struct nlmsghdr *) buf;
  923. for(;NLMSG_OK(nlp, nll);nlp=NLMSG_NEXT(nlp, nll)){
  924. ifi = NLMSG_DATA(nlp);
  925. if (nlp->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
  926. continue;
  927. // init all the strings
  928. // inner loop: loop thru all the attributes of
  929. // one route entry
  930. rtap = (struct rtattr *) IFA_RTA(ifi);
  931. rtl = IFA_PAYLOAD(nlp);
  932. index = ifi->ifa_index;
  933. if(index >= MAX_IFACE_NO){
  934. LM_ERR("Invalid interface index returned: %d\n", index);
  935. goto error;
  936. }
  937. entry = (struct idx*)pkg_malloc(sizeof(struct idx));
  938. if(entry == 0)
  939. {
  940. LM_ERR("could not allocate memory\n");
  941. goto error;
  942. }
  943. entry->next = 0;
  944. entry->family = families[i];
  945. entry->ifa_flags = ifi->ifa_flags;
  946. is_link_local = 0;
  947. for(;RTA_OK(rtap, rtl);rtap=RTA_NEXT(rtap,rtl)){
  948. switch(rtap->rta_type){
  949. case IFA_ADDRESS:
  950. if((*(int*)RTA_DATA(rtap))== htons(0xfe80)){
  951. LM_DBG("Link Local Address, ignoring ...\n");
  952. is_link_local = 1;
  953. break;
  954. }
  955. inet_ntop(families[i], RTA_DATA(rtap), entry->addr, MAX_IF_LEN);
  956. LM_DBG("iface <IFA_ADDRESS> addr is %s\n", entry->addr);
  957. break;
  958. case IFA_LOCAL:
  959. if((*(int*)RTA_DATA(rtap))== htons(0xfe80)){
  960. LM_DBG("Link Local Address, ignoring ...\n");
  961. is_link_local = 1;
  962. }
  963. inet_ntop(families[i], RTA_DATA(rtap), entry->addr, MAX_IF_LEN);
  964. LM_DBG("iface <IFA_LOCAL> addr is %s\n", entry->addr);
  965. break;
  966. case IFA_LABEL:
  967. LM_DBG("iface name is %s\n", (char*)RTA_DATA(rtap));
  968. strncpy(name, (char*)RTA_DATA(rtap), MAX_IF_LEN);
  969. break;
  970. case IFA_BROADCAST:
  971. case IFA_ANYCAST:
  972. case IFA_UNSPEC:
  973. case IFA_CACHEINFO:
  974. default:
  975. break;
  976. }
  977. }
  978. if(is_link_local) continue; /* link local addresses are not bindable */
  979. if(strlen(ifaces[index].name)==0)
  980. strncpy(ifaces[index].name, name, MAX_IF_LEN);
  981. ifaces[index].index = index;
  982. if(ifaces[index].addresses == 0 )
  983. ifaces[index].addresses = entry;
  984. else {
  985. for(tmp = ifaces[index].addresses; tmp->next ; tmp = tmp->next)/*empty*/;
  986. tmp->next = entry;
  987. }
  988. }
  989. }
  990. if(nl_sock>0) close(nl_sock);
  991. /* the socket should be closed so we can bind again */
  992. for(i = 0; i < sizeof(families)/sizeof(int); i++){
  993. /* get device flags */
  994. get_flags(families[i]); /* AF_INET or AF_INET6 */
  995. }
  996. return 0;
  997. error:
  998. if(nl_sock>0) close(nl_sock);
  999. return -1;
  1000. }
  1001. /* add all family type addresses of interface if_to the socket_info array
  1002. * if if_name==0, adds all addresses on all interfaces
  1003. * uses RTNETLINK sockets to get addresses on the present interface on LINUX
  1004. * return: -1 on error, 0 on success
  1005. */
  1006. int add_interfaces_via_netlink(char* if_name, int family, unsigned short port,
  1007. unsigned short proto,
  1008. struct addr_info** ai_l)
  1009. {
  1010. int i;
  1011. struct idx* tmp;
  1012. enum si_flags flags;
  1013. if(ifaces == NULL && (build_iface_list()!=0)){
  1014. LM_ERR("Could not get network interface list\n");
  1015. return -1;
  1016. }
  1017. flags=SI_NONE;
  1018. for(i=0; i< MAX_IFACE_NO; ++i){
  1019. if(ifaces[i].addresses == NULL) continue; /* not present/configured */
  1020. if ((if_name==0)||
  1021. (strncmp(if_name, ifaces[i].name, strlen(ifaces[i].name))==0)){
  1022. /* check if iface is up */
  1023. //if(! (ifaces[i].flags & IFF_UP) ) continue;
  1024. for(tmp = ifaces[i].addresses; tmp; tmp = tmp->next){
  1025. LM_DBG("\t in add_iface_via_netlink Name %s Address %s\n", ifaces[i].name, tmp->addr);
  1026. /* match family */
  1027. if (family == tmp->family){
  1028. /* check if loopback */
  1029. if (ifaces[i].flags & IFF_LOOPBACK){
  1030. LM_DBG("INTERFACE %s is loopback", ifaces[i].name);
  1031. flags|=SI_IS_LO;
  1032. }
  1033. /* save the info */
  1034. if (new_addr_info2list(tmp->addr, flags, ai_l)!=0){
  1035. LOG(L_ERR, "ERROR: add_interfaces: "
  1036. "new_addr_info2list failed\n");
  1037. goto error;
  1038. }
  1039. }
  1040. }
  1041. }
  1042. }
  1043. return 0;
  1044. error:
  1045. return -1;
  1046. }
  1047. #endif /* __OS_linux */
  1048. /* add all family type addresses of interface if_name to the socket_info array
  1049. * if if_name==0, adds all addresses on all interfaces
  1050. * WARNING: it only works with ipv6 addresses on FreeBSD
  1051. * return: -1 on error, 0 on success
  1052. */
  1053. int add_interfaces(char* if_name, int family, unsigned short port,
  1054. unsigned short proto,
  1055. struct addr_info** ai_l)
  1056. {
  1057. struct ifconf ifc;
  1058. struct ifreq ifr;
  1059. struct ifreq ifrcopy;
  1060. char* last;
  1061. char* p;
  1062. int size;
  1063. int lastlen;
  1064. int s;
  1065. char* tmp;
  1066. struct ip_addr addr;
  1067. int ret;
  1068. enum si_flags flags;
  1069. #ifdef HAVE_SOCKADDR_SA_LEN
  1070. #ifndef MAX
  1071. #define MAX(a,b) ( ((a)>(b))?(a):(b))
  1072. #endif
  1073. #endif
  1074. /* ipv4 or ipv6 only*/
  1075. flags=SI_NONE;
  1076. s=socket(family, SOCK_DGRAM, 0);
  1077. ret=-1;
  1078. lastlen=0;
  1079. ifc.ifc_req=0;
  1080. for (size=100; ; size*=2){
  1081. ifc.ifc_len=size*sizeof(struct ifreq);
  1082. ifc.ifc_req=(struct ifreq*) pkg_malloc(size*sizeof(struct ifreq));
  1083. if (ifc.ifc_req==0){
  1084. LOG(L_ERR, "ERROR: add_interfaces: memory allocation failure\n");
  1085. goto error;
  1086. }
  1087. if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
  1088. if(errno==EBADF) return 0; /* invalid descriptor => no such ifs*/
  1089. LOG(L_ERR, "ERROR: add_interfaces: ioctl failed: %s\n",
  1090. strerror(errno));
  1091. goto error;
  1092. }
  1093. if ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
  1094. len not changed*/
  1095. lastlen=ifc.ifc_len;
  1096. /* try a bigger array*/
  1097. pkg_free(ifc.ifc_req);
  1098. }
  1099. last=(char*)ifc.ifc_req+ifc.ifc_len;
  1100. for(p=(char*)ifc.ifc_req; p<last;
  1101. p+=
  1102. #ifdef __OS_linux
  1103. sizeof(ifr) /* works on x86_64 too */
  1104. #else
  1105. (sizeof(ifr.ifr_name)+
  1106. #ifdef HAVE_SOCKADDR_SA_LEN
  1107. MAX(ifr.ifr_addr.sa_len, sizeof(struct sockaddr))
  1108. #else
  1109. ( (ifr.ifr_addr.sa_family==AF_INET)?
  1110. sizeof(struct sockaddr_in):
  1111. ((ifr.ifr_addr.sa_family==AF_INET6)?
  1112. sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
  1113. #endif
  1114. )
  1115. #endif
  1116. )
  1117. {
  1118. /* copy contents into ifr structure
  1119. * warning: it might be longer (e.g. ipv6 address) */
  1120. memcpy(&ifr, p, sizeof(ifr));
  1121. if (ifr.ifr_addr.sa_family!=family){
  1122. /*printf("strange family %d skipping...\n",
  1123. ifr->ifr_addr.sa_family);*/
  1124. continue;
  1125. }
  1126. /*get flags*/
  1127. ifrcopy=ifr;
  1128. if (ioctl(s, SIOCGIFFLAGS, &ifrcopy)!=-1){ /* ignore errors */
  1129. /* ignore down ifs only if listening on all of them*/
  1130. if (if_name==0){
  1131. /* if if not up, skip it*/
  1132. if (!(ifrcopy.ifr_flags & IFF_UP)) continue;
  1133. }
  1134. }
  1135. if ((if_name==0)||
  1136. (strncmp(if_name, ifr.ifr_name, sizeof(ifr.ifr_name))==0)){
  1137. /*add address*/
  1138. sockaddr2ip_addr(&addr,
  1139. (struct sockaddr*)(p+(long)&((struct ifreq*)0)->ifr_addr));
  1140. if ((tmp=ip_addr2a(&addr))==0) goto error;
  1141. /* check if loopback */
  1142. if (ifrcopy.ifr_flags & IFF_LOOPBACK)
  1143. flags|=SI_IS_LO;
  1144. /* save the info */
  1145. if (new_addr_info2list(tmp, flags, ai_l)!=0){
  1146. LOG(L_ERR, "ERROR: add_interfaces: "
  1147. "new_addr_info2list failed\n");
  1148. goto error;
  1149. }
  1150. ret=0;
  1151. }
  1152. /*
  1153. printf("%s:\n", ifr->ifr_name);
  1154. printf(" ");
  1155. print_sockaddr(&(ifr->ifr_addr));
  1156. printf(" ");
  1157. ls_ifflags(ifr->ifr_name, family, options);
  1158. printf("\n");*/
  1159. }
  1160. pkg_free(ifc.ifc_req); /*clean up*/
  1161. close(s);
  1162. return ret;
  1163. error:
  1164. if (ifc.ifc_req) pkg_free(ifc.ifc_req);
  1165. close(s);
  1166. return -1;
  1167. }
  1168. /* internal helper function: resolve host names and add aliases
  1169. * name is a value result parameter: it should contain the hostname that
  1170. * will be used to fill all the other members, including name itself
  1171. * in some situation (name->s should be a 0 terminated pkg_malloc'ed string)
  1172. * return 0 on success and -1 on error */
  1173. static int fix_hostname(str* name, struct ip_addr* address, str* address_str,
  1174. enum si_flags* flags, int* type_flags,
  1175. struct socket_info* s)
  1176. {
  1177. struct hostent* he;
  1178. char* tmp;
  1179. char** h;
  1180. /* get "official hostnames", all the aliases etc. */
  1181. he=resolvehost(name->s);
  1182. if (he==0){
  1183. LOG(L_ERR, "ERROR: fix_hostname: could not resolve %s\n", name->s);
  1184. goto error;
  1185. }
  1186. /* check if we got the official name */
  1187. if (strcasecmp(he->h_name, name->s)!=0){
  1188. if (sr_auto_aliases &&
  1189. add_alias(name->s, name->len, s->port_no, s->proto)<0){
  1190. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  1191. }
  1192. /* change the official name */
  1193. pkg_free(name->s);
  1194. name->s=(char*)pkg_malloc(strlen(he->h_name)+1);
  1195. if (name->s==0){
  1196. LOG(L_ERR, "ERROR: fix_hostname: out of memory.\n");
  1197. goto error;
  1198. }
  1199. name->len=strlen(he->h_name);
  1200. strncpy(name->s, he->h_name, name->len+1);
  1201. }
  1202. /* add the aliases*/
  1203. for(h=he->h_aliases; sr_auto_aliases && h && *h; h++)
  1204. if (add_alias(*h, strlen(*h), s->port_no, s->proto)<0){
  1205. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  1206. }
  1207. hostent2ip_addr(address, he, 0); /*convert to ip_addr format*/
  1208. if (type_flags){
  1209. *type_flags|=(address->af==AF_INET)?SOCKET_T_IPV4:SOCKET_T_IPV6;
  1210. }
  1211. if ((tmp=ip_addr2a(address))==0) goto error;
  1212. address_str->s=pkg_malloc(strlen(tmp)+1);
  1213. if (address_str->s==0){
  1214. LOG(L_ERR, "ERROR: fix_hostname: out of memory.\n");
  1215. goto error;
  1216. }
  1217. strncpy(address_str->s, tmp, strlen(tmp)+1);
  1218. /* set is_ip (1 if name is an ip address, 0 otherwise) */
  1219. address_str->len=strlen(tmp);
  1220. if (sr_auto_aliases && (address_str->len==name->len) &&
  1221. (strncasecmp(address_str->s, name->s, address_str->len)==0)){
  1222. *flags|=SI_IS_IP;
  1223. /* do rev. DNS on it (for aliases)*/
  1224. he=rev_resolvehost(address);
  1225. if (he==0){
  1226. LOG(L_WARN, "WARNING: fix_hostname: could not rev. resolve %s\n",
  1227. name->s);
  1228. }else{
  1229. /* add the aliases*/
  1230. if (add_alias(he->h_name, strlen(he->h_name), s->port_no,
  1231. s->proto)<0){
  1232. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  1233. }
  1234. for(h=he->h_aliases; h && *h; h++)
  1235. if (add_alias(*h, strlen(*h), s->port_no, s->proto) < 0){
  1236. LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
  1237. }
  1238. }
  1239. }
  1240. #ifdef USE_MCAST
  1241. /* Check if it is an multicast address and
  1242. * set the flag if so
  1243. */
  1244. if (is_mcast(address)){
  1245. *flags |= SI_IS_MCAST;
  1246. }
  1247. #endif /* USE_MCAST */
  1248. /* check if INADDR_ANY */
  1249. if (ip_addr_any(address))
  1250. *flags|=SI_IS_ANY;
  1251. else if (ip_addr_loopback(address)) /* check for loopback */
  1252. *flags|=SI_IS_LO;
  1253. return 0;
  1254. error:
  1255. return -1;
  1256. }
  1257. /* append new elements to a socket_info list after "list"
  1258. * each element is created from addr_info_lst + port, protocol and flags
  1259. * return 0 on succes, -1 on error
  1260. */
  1261. static int addr_info_to_si_lst(struct addr_info* ai_lst, unsigned short port,
  1262. char proto, enum si_flags flags,
  1263. struct socket_info** list)
  1264. {
  1265. struct addr_info* ail;
  1266. for (ail=ai_lst; ail; ail=ail->next){
  1267. if(new_sock2list(ail->name.s, 0, port, proto, 0, 0,
  1268. ail->flags | flags, list)==0)
  1269. return -1;
  1270. }
  1271. return 0;
  1272. }
  1273. /* insert new elements to a socket_info list after "el",
  1274. * each element is created from addr_info_lst + port, * protocol and flags
  1275. * return 0 on succes, -1 on error
  1276. */
  1277. static int addr_info_to_si_lst_after(struct addr_info* ai_lst,
  1278. unsigned short port,
  1279. char proto, enum si_flags flags,
  1280. struct socket_info* el)
  1281. {
  1282. struct addr_info* ail;
  1283. struct socket_info* new_si;
  1284. for (ail=ai_lst; ail; ail=ail->next){
  1285. if((new_si=new_sock2list_after(ail->name.s, 0, port, proto,
  1286. 0, 0, ail->flags | flags, el))==0)
  1287. return -1;
  1288. el=new_si;
  1289. }
  1290. return 0;
  1291. }
  1292. /* fixes a socket list => resolve addresses,
  1293. * interface names, fills missing members, remove duplicates
  1294. * fills type_flags if not null with SOCKET_T_IPV4 and/or SOCKET_T_IPV6*/
  1295. static int fix_socket_list(struct socket_info **list, int* type_flags)
  1296. {
  1297. struct socket_info* si;
  1298. struct socket_info* new_si;
  1299. struct socket_info* l;
  1300. struct socket_info* next;
  1301. struct socket_info* next_si;
  1302. struct socket_info* del_si;
  1303. struct socket_info* keep_si;
  1304. char* tmp;
  1305. int len;
  1306. struct addr_info* ai_lst;
  1307. struct addr_info* ail;
  1308. struct addr_info* tmp_ail;
  1309. struct addr_info* tmp_ail_next;
  1310. struct addr_info* ail_next;
  1311. if (type_flags)
  1312. *type_flags=0;
  1313. /* try to change all the interface names into addresses
  1314. * --ugly hack */
  1315. for (si=*list;si;){
  1316. next=si->next;
  1317. ai_lst=0;
  1318. if (add_interfaces(si->name.s, AF_INET, si->port_no,
  1319. si->proto, &ai_lst)!=-1){
  1320. if (si->flags & SI_IS_MHOMED){
  1321. if((new_si=new_sock2list_after(ai_lst->name.s, 0, si->port_no,
  1322. si->proto, si->useinfo.name.s,
  1323. si->useinfo.port_no,
  1324. ai_lst->flags|si->flags, si))==0)
  1325. break;
  1326. ail=ai_lst;
  1327. ai_lst=ai_lst->next;
  1328. free_addr_info(ail); /* free the first elem. */
  1329. if (ai_lst){
  1330. ai_lst->prev=0;
  1331. /* find the end */
  1332. for (ail=ai_lst; ail->next; ail=ail->next);
  1333. /* add the mh list after the last position in ai_lst */
  1334. addr_info_list_ins_lst(si->addr_info_lst, ail);
  1335. new_si->addr_info_lst=ai_lst;
  1336. si->addr_info_lst=0; /* detached and moved to new_si */
  1337. ail=ail->next; /* ail== old si->addr_info_lst */
  1338. }else{
  1339. ail=si->addr_info_lst;
  1340. new_si->addr_info_lst=ail;
  1341. si->addr_info_lst=0; /* detached and moved to new_si */
  1342. }
  1343. }else{
  1344. /* add all addr. as separate interfaces */
  1345. if (addr_info_to_si_lst_after(ai_lst, si->port_no, si->proto,
  1346. si->flags, si)!=0)
  1347. goto error;
  1348. /* ai_lst not needed anymore */
  1349. free_addr_info_lst(&ai_lst);
  1350. ail=0;
  1351. new_si=0;
  1352. }
  1353. /* success => remove current entry (shift the entire array)*/
  1354. sock_listrm(list, si);
  1355. free_sock_info(si);
  1356. }else{
  1357. new_si=si;
  1358. ail=si->addr_info_lst;
  1359. }
  1360. if (ail){
  1361. if (new_si && (new_si->flags & SI_IS_MHOMED)){
  1362. ai_lst=0;
  1363. for (; ail;){
  1364. ail_next=ail->next;
  1365. if (add_interfaces(ail->name.s, AF_INET, new_si->port_no,
  1366. new_si->proto, &ai_lst)!=-1){
  1367. /* add the resolved list after the current position */
  1368. addr_info_list_ins_lst(ai_lst, ail);
  1369. /* success, remove the current entity */
  1370. addr_info_listrm(&new_si->addr_info_lst, ail);
  1371. free_addr_info(ail);
  1372. ai_lst=0;
  1373. }
  1374. ail=ail_next;
  1375. }
  1376. }
  1377. }
  1378. si=next;
  1379. }
  1380. /* get ips & fill the port numbers*/
  1381. #ifdef EXTRA_DEBUG
  1382. DBG("Listening on \n");
  1383. #endif
  1384. for (si=*list;si;si=si->next){
  1385. /* fix port number, port_no should be !=0 here */
  1386. if (si->port_no==0){
  1387. #ifdef USE_TLS
  1388. si->port_no= (si->proto==PROTO_TLS)?tls_port_no:port_no;
  1389. #else
  1390. si->port_no= port_no;
  1391. #endif
  1392. }
  1393. tmp=int2str(si->port_no, &len);
  1394. if (len>=MAX_PORT_LEN){
  1395. LOG(L_ERR, "ERROR: fix_socket_list: bad port number: %d\n",
  1396. si->port_no);
  1397. goto error;
  1398. }
  1399. si->port_no_str.s=(char*)pkg_malloc(len+1);
  1400. if (si->port_no_str.s==0){
  1401. LOG(L_ERR, "ERROR: fix_socket_list: out of memory.\n");
  1402. goto error;
  1403. }
  1404. strncpy(si->port_no_str.s, tmp, len+1);
  1405. si->port_no_str.len=len;
  1406. if (fix_hostname(&si->name, &si->address, &si->address_str,
  1407. &si->flags, type_flags, si) !=0 )
  1408. goto error;
  1409. /* fix hostnames in mh addresses */
  1410. for (ail=si->addr_info_lst; ail; ail=ail->next){
  1411. if (fix_hostname(&ail->name, &ail->address, &ail->address_str,
  1412. &ail->flags, type_flags, si) !=0 )
  1413. goto error;
  1414. }
  1415. if (fix_sock_str(si) < 0) goto error;
  1416. #ifdef EXTRA_DEBUG
  1417. printf(" %.*s [%s]:%s%s\n", si->name.len,
  1418. si->name.s, si->address_str.s, si->port_no_str.s,
  1419. si->flags & SI_IS_MCAST ? " mcast" : "");
  1420. #endif
  1421. }
  1422. /* removing duplicate addresses*/
  1423. for (si=*list;si; ){
  1424. next_si=si->next;
  1425. for (l=si->next;l;){
  1426. next=l->next;
  1427. if ((si->port_no==l->port_no) &&
  1428. (si->address.af==l->address.af) &&
  1429. (memcmp(si->address.u.addr, l->address.u.addr,
  1430. si->address.len) == 0)
  1431. ){
  1432. /* remove the socket with no extra addresses.,
  1433. * if both of them have extra addresses, remove one of them
  1434. * and merge the extra addresses into the other */
  1435. if (l->addr_info_lst==0){
  1436. del_si=l;
  1437. keep_si=si;
  1438. }else if (si->addr_info_lst==0){
  1439. del_si=si;
  1440. keep_si=l;
  1441. }else{
  1442. /* move l->addr_info_lst to si->addr_info_lst */
  1443. /* find last elem */
  1444. for (ail=si->addr_info_lst; ail->next; ail=ail->next);
  1445. /* add the l list after the last position in si lst */
  1446. addr_info_list_ins_lst(l->addr_info_lst, ail);
  1447. l->addr_info_lst=0; /* detached */
  1448. del_si=l; /* l will be removed */
  1449. keep_si=l;
  1450. }
  1451. #ifdef EXTRA_DEBUG
  1452. printf("removing duplicate %s [%s] == %s [%s]\n",
  1453. keep_si->name.s, keep_si->address_str.s,
  1454. del_si->name.s, del_si->address_str.s);
  1455. #endif
  1456. /* add the name to the alias list*/
  1457. if ((!(del_si->flags& SI_IS_IP)) && (
  1458. (del_si->name.len!=keep_si->name.len)||
  1459. (strncmp(del_si->name.s, keep_si->name.s,
  1460. del_si->name.len)!=0))
  1461. )
  1462. add_alias(del_si->name.s, del_si->name.len,
  1463. l->port_no, l->proto);
  1464. /* make sure next_si doesn't point to del_si */
  1465. if (del_si==next_si)
  1466. next_si=next_si->next;
  1467. /* remove del_si*/
  1468. sock_listrm(list, del_si);
  1469. free_sock_info(del_si);
  1470. }
  1471. l=next;
  1472. }
  1473. si=next_si;
  1474. }
  1475. /* check for duplicates in extra_addresses */
  1476. for (si=*list;si; si=si->next){
  1477. /* check for & remove internal duplicates: */
  1478. for (ail=si->addr_info_lst; ail;){
  1479. ail_next=ail->next;
  1480. /* 1. check if the extra addresses contain a duplicate for the
  1481. * main one */
  1482. if ((ail->address.af==si->address.af) &&
  1483. (memcmp(ail->address.u.addr, si->address.u.addr,
  1484. ail->address.len) == 0)){
  1485. /* add the name to the alias list*/
  1486. if ((!(ail->flags& SI_IS_IP)) && (
  1487. (ail->name.len!=si->name.len)||
  1488. (strncmp(ail->name.s, si->name.s, ail->name.len)!=0)))
  1489. add_alias(ail->name.s, ail->name.len, si->port_no,
  1490. si->proto);
  1491. /* remove ail*/
  1492. addr_info_listrm(&si->addr_info_lst, ail);
  1493. free_addr_info(ail);
  1494. ail=ail_next;
  1495. continue;
  1496. }
  1497. /* 2. check if the extra addresses contain a duplicates for
  1498. * other addresses in the same list */
  1499. for (tmp_ail=ail->next; tmp_ail;){
  1500. tmp_ail_next=tmp_ail->next;
  1501. if ((ail->address.af==tmp_ail->address.af) &&
  1502. (memcmp(ail->address.u.addr, tmp_ail->address.u.addr,
  1503. ail->address.len) == 0)){
  1504. /* add the name to the alias list*/
  1505. if ((!(tmp_ail->flags& SI_IS_IP)) && (
  1506. (ail->name.len!=tmp_ail->name.len)||
  1507. (strncmp(ail->name.s, tmp_ail->name.s,
  1508. tmp_ail->name.len)!=0))
  1509. )
  1510. add_alias(tmp_ail->name.s, tmp_ail->name.len,
  1511. si->port_no, si->proto);
  1512. /* remove tmp_ail*/
  1513. addr_info_listrm(&si->addr_info_lst, tmp_ail);
  1514. free_addr_info(tmp_ail);
  1515. }
  1516. tmp_ail=tmp_ail_next;
  1517. }
  1518. ail=ail_next;
  1519. }
  1520. /* check for duplicates between extra addresses (e.g. sctp MH)
  1521. * and other main addresses, on conflict remove the corresponding
  1522. * extra addresses (another possible solution would be to join
  1523. * the 2 si entries into one). */
  1524. for (ail=si->addr_info_lst; ail;){
  1525. ail_next=ail->next;
  1526. for (l=*list;l; l=l->next){
  1527. if (l==si) continue;
  1528. if (si->port_no==l->port_no){
  1529. if ((ail->address.af==l->address.af) &&
  1530. (memcmp(ail->address.u.addr, l->address.u.addr,
  1531. ail->address.len) == 0)){
  1532. /* add the name to the alias list*/
  1533. if ((!(ail->flags& SI_IS_IP)) && (
  1534. (ail->name.len!=l->name.len)||
  1535. (strncmp(ail->name.s, l->name.s, l->name.len)!=0))
  1536. )
  1537. add_alias(ail->name.s, ail->name.len,
  1538. l->port_no, l->proto);
  1539. /* remove ail*/
  1540. addr_info_listrm(&si->addr_info_lst, ail);
  1541. free_addr_info(ail);
  1542. break;
  1543. }
  1544. /* check for duplicates with other extra addresses
  1545. * lists */
  1546. for (tmp_ail=l->addr_info_lst; tmp_ail; ){
  1547. tmp_ail_next=tmp_ail->next;
  1548. if ((ail->address.af==tmp_ail->address.af) &&
  1549. (memcmp(ail->address.u.addr,
  1550. tmp_ail->address.u.addr,
  1551. ail->address.len) == 0)){
  1552. /* add the name to the alias list*/
  1553. if ((!(tmp_ail->flags& SI_IS_IP)) && (
  1554. (ail->name.len!=tmp_ail->name.len)||
  1555. (strncmp(ail->name.s, tmp_ail->name.s,
  1556. tmp_ail->name.len)!=0))
  1557. )
  1558. add_alias(tmp_ail->name.s, tmp_ail->name.len,
  1559. l->port_no, l->proto);
  1560. /* remove tmp_ail*/
  1561. addr_info_listrm(&l->addr_info_lst, tmp_ail);
  1562. free_addr_info(tmp_ail);
  1563. }
  1564. tmp_ail=tmp_ail_next;
  1565. }
  1566. }
  1567. }
  1568. ail=ail_next;
  1569. }
  1570. }
  1571. #ifdef USE_MCAST
  1572. /* Remove invalid multicast entries */
  1573. si=*list;
  1574. while(si){
  1575. if ((si->proto == PROTO_TCP)
  1576. #ifdef USE_TLS
  1577. || (si->proto == PROTO_TLS)
  1578. #endif /* USE_TLS */
  1579. #ifdef USE_SCTP
  1580. || (si->proto == PROTO_SCTP)
  1581. #endif
  1582. ){
  1583. if (si->flags & SI_IS_MCAST){
  1584. LOG(L_WARN, "WARNING: removing entry %s:%s [%s]:%s\n",
  1585. get_valid_proto_name(si->proto), si->name.s,
  1586. si->address_str.s, si->port_no_str.s);
  1587. l = si;
  1588. si=si->next;
  1589. sock_listrm(list, l);
  1590. free_sock_info(l);
  1591. }else{
  1592. ail=si->addr_info_lst;
  1593. while(ail){
  1594. if (ail->flags & SI_IS_MCAST){
  1595. LOG(L_WARN, "WARNING: removing mh entry %s:%s"
  1596. " [%s]:%s\n",
  1597. get_valid_proto_name(si->proto), ail->name.s,
  1598. ail->address_str.s, si->port_no_str.s);
  1599. tmp_ail=ail;
  1600. ail=ail->next;
  1601. addr_info_listrm(&si->addr_info_lst, tmp_ail);
  1602. free_addr_info(tmp_ail);
  1603. }else{
  1604. ail=ail->next;
  1605. }
  1606. }
  1607. si=si->next;
  1608. }
  1609. } else {
  1610. si=si->next;
  1611. }
  1612. }
  1613. #endif /* USE_MCAST */
  1614. return 0;
  1615. error:
  1616. return -1;
  1617. }
  1618. int socket_types = 0;
  1619. /* fix all 3 socket lists, fills socket_types if non-null
  1620. * return 0 on success, -1 on error */
  1621. int fix_all_socket_lists()
  1622. {
  1623. struct utsname myname;
  1624. int flags;
  1625. struct addr_info* ai_lst;
  1626. ai_lst=0;
  1627. if ((udp_listen==0)
  1628. #ifdef USE_TCP
  1629. && (tcp_listen==0)
  1630. #ifdef USE_TLS
  1631. && (tls_listen==0)
  1632. #endif
  1633. #endif
  1634. #ifdef USE_SCTP
  1635. && (sctp_listen==0)
  1636. #endif
  1637. ){
  1638. /* get all listening ipv4/ipv6 interfaces */
  1639. if ( ( (add_interfaces(0, AF_INET, 0, PROTO_UDP, &ai_lst)==0)
  1640. #ifdef __OS_linux
  1641. && (!auto_bind_ipv6 || add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_UDP, &ai_lst) == 0)
  1642. #else
  1643. && ( !auto_bind_ipv6 || add_interfaces(0, AF_INET6, 0, PROTO_UDP, &ai_lst) !=0 ) /* add_interface does not work for IPv6 on Linux */
  1644. #endif /* __OS_linux */
  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 __OS_linux
  1653. || (auto_bind_ipv6 && add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_TCP, &ai_lst) != 0)
  1654. #else
  1655. || (auto_bind_ipv6 && add_interfaces(0, AF_INET6, 0, PROTO_TCP, &ai_lst) !=0 )
  1656. #endif /* __OS_linux */
  1657. ) || (addr_info_to_si_lst(ai_lst, 0, PROTO_TCP, 0,
  1658. &tcp_listen)!=0))
  1659. goto error;
  1660. free_addr_info_lst(&ai_lst);
  1661. ai_lst=0;
  1662. #ifdef USE_TLS
  1663. if (!tls_disable){
  1664. if (((add_interfaces(0, AF_INET, 0, PROTO_TLS,
  1665. &ai_lst)!=0)
  1666. #ifdef __OS_linux
  1667. || (auto_bind_ipv6 && add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_TLS, &ai_lst) != 0)
  1668. #else
  1669. || (auto_bind_ipv6 && add_interfaces(0, AF_INET6, 0, PROTO_TLS, &ai_lst)!=0)
  1670. #endif /* __OS_linux */
  1671. ) || (addr_info_to_si_lst(ai_lst, 0, PROTO_TLS, 0,
  1672. &tls_listen)!=0))
  1673. goto error;
  1674. }
  1675. free_addr_info_lst(&ai_lst);
  1676. ai_lst=0;
  1677. #endif
  1678. }
  1679. #endif
  1680. #ifdef USE_SCTP
  1681. if (!sctp_disable){
  1682. if (((add_interfaces(0, AF_INET, 0, PROTO_SCTP, &ai_lst)!=0)
  1683. #ifdef __OS_linux
  1684. || (auto_bind_ipv6 && add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_SCTP, &ai_lst) != 0)
  1685. #else
  1686. || (auto_bind_ipv6 && add_interfaces(0, AF_INET6, 0, PROTO_SCTP, &ai_lst) != 0)
  1687. #endif /* __OS_linux */
  1688. ) || (addr_info_to_si_lst(ai_lst, 0, PROTO_SCTP, 0,
  1689. &sctp_listen)!=0))
  1690. goto error;
  1691. free_addr_info_lst(&ai_lst);
  1692. ai_lst=0;
  1693. }
  1694. #endif /* USE_SCTP */
  1695. }else{
  1696. /* if error fall back to get hostname */
  1697. /* get our address, only the first one */
  1698. if (uname (&myname) <0){
  1699. LOG(L_ERR, "ERROR: fix_all_socket_lists: cannot determine"
  1700. " hostname, try -l address\n");
  1701. goto error;
  1702. }
  1703. if (add_listen_iface(myname.nodename, 0, 0, 0, 0)!=0){
  1704. LOG(L_ERR, "ERROR: fix_all_socket_lists: add_listen_iface "
  1705. "failed \n");
  1706. goto error;
  1707. }
  1708. }
  1709. }
  1710. flags=0;
  1711. if (fix_socket_list(&udp_listen, &flags)!=0){
  1712. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1713. " udp failed\n");
  1714. goto error;
  1715. }
  1716. if (flags){
  1717. socket_types|=flags|SOCKET_T_UDP;
  1718. }
  1719. #ifdef USE_TCP
  1720. flags=0;
  1721. if (!tcp_disable && (fix_socket_list(&tcp_listen, &flags)!=0)){
  1722. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1723. " tcp failed\n");
  1724. goto error;
  1725. }
  1726. if (flags){
  1727. socket_types|=flags|SOCKET_T_TCP;
  1728. }
  1729. #ifdef USE_TLS
  1730. flags=0;
  1731. if (!tls_disable && (fix_socket_list(&tls_listen, &flags)!=0)){
  1732. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1733. " tls failed\n");
  1734. goto error;
  1735. }
  1736. if (flags){
  1737. socket_types|=flags|SOCKET_T_TLS;
  1738. }
  1739. #endif
  1740. #endif
  1741. #ifdef USE_SCTP
  1742. flags=0;
  1743. if (!sctp_disable && (fix_socket_list(&sctp_listen, &flags)!=0)){
  1744. LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
  1745. " sctp failed\n");
  1746. goto error;
  1747. }
  1748. if (flags){
  1749. socket_types|=flags|SOCKET_T_SCTP;
  1750. }
  1751. #endif /* USE_SCTP */
  1752. if ((udp_listen==0)
  1753. #ifdef USE_TCP
  1754. && (tcp_listen==0)
  1755. #ifdef USE_TLS
  1756. && (tls_listen==0)
  1757. #endif
  1758. #endif
  1759. #ifdef USE_SCTP
  1760. && (sctp_listen==0)
  1761. #endif
  1762. ){
  1763. LOG(L_ERR, "ERROR: fix_all_socket_lists: no listening sockets\n");
  1764. goto error;
  1765. }
  1766. return 0;
  1767. error:
  1768. if (ai_lst) free_addr_info_lst(&ai_lst);
  1769. return -1;
  1770. }
  1771. void print_all_socket_lists()
  1772. {
  1773. struct socket_info *si;
  1774. struct socket_info** list;
  1775. struct addr_info* ai;
  1776. unsigned short proto;
  1777. proto=PROTO_UDP;
  1778. do{
  1779. list=get_sock_info_list(proto);
  1780. for(si=list?*list:0; si; si=si->next){
  1781. if (si->addr_info_lst){
  1782. printf(" %s: (%s",
  1783. get_valid_proto_name(proto),
  1784. si->address_str.s);
  1785. for (ai=si->addr_info_lst; ai; ai=ai->next)
  1786. printf(", %s", ai->address_str.s);
  1787. printf("):%s%s%s\n",
  1788. si->port_no_str.s,
  1789. si->flags & SI_IS_MCAST ? " mcast" : "",
  1790. si->flags & SI_IS_MHOMED? " mhomed" : "");
  1791. }else{
  1792. printf(" %s: %s",
  1793. get_valid_proto_name(proto),
  1794. si->name.s);
  1795. if (!si->flags & SI_IS_IP)
  1796. printf(" [%s]", si->address_str.s);
  1797. printf( ":%s%s%s",
  1798. si->port_no_str.s,
  1799. si->flags & SI_IS_MCAST ? " mcast" : "",
  1800. si->flags & SI_IS_MHOMED? " mhomed" : "");
  1801. if (si->useinfo.name.s)
  1802. printf(" advertise %s:%d", si->useinfo.name.s, si->useinfo.port_no);
  1803. printf("\n");
  1804. }
  1805. }
  1806. }while((proto=next_proto(proto)));
  1807. }
  1808. void print_aliases()
  1809. {
  1810. struct host_alias* a;
  1811. for(a=aliases; a; a=a->next)
  1812. if (a->port)
  1813. printf(" %s: %.*s:%d\n", get_valid_proto_name(a->proto),
  1814. a->alias.len, a->alias.s, a->port);
  1815. else
  1816. printf(" %s: %.*s:*\n", get_valid_proto_name(a->proto),
  1817. a->alias.len, a->alias.s);
  1818. }
  1819. void init_proto_order()
  1820. {
  1821. int r;
  1822. /* fix proto list (remove disabled protocols)*/
  1823. #ifdef USE_TCP
  1824. if (tcp_disable)
  1825. #endif
  1826. for(r=PROTO_NONE; r<=PROTO_LAST; r++){
  1827. if (nxt_proto[r]==PROTO_TCP)
  1828. nxt_proto[r]=nxt_proto[PROTO_TCP];
  1829. }
  1830. #ifdef USE_TCP
  1831. #ifdef USE_TLS
  1832. if (tls_disable || tcp_disable)
  1833. #endif
  1834. #endif
  1835. for(r=PROTO_NONE; r<=PROTO_LAST; r++){
  1836. if (nxt_proto[r]==PROTO_TLS)
  1837. nxt_proto[r]=nxt_proto[PROTO_TLS];
  1838. }
  1839. #ifdef USE_SCTP
  1840. if (sctp_disable)
  1841. #endif
  1842. for(r=PROTO_NONE; r<=PROTO_LAST; r++){
  1843. if (nxt_proto[r]==PROTO_SCTP)
  1844. nxt_proto[r]=nxt_proto[PROTO_SCTP];
  1845. }
  1846. /* Deliberately skipping PROTO_WS and PROTO_WSS here as these
  1847. are just upgraded TCP and TLS connections */
  1848. }
  1849. /**
  1850. * parse '[port:]host[:port]' string to a broken down structure
  1851. */
  1852. int parse_protohostport(str* ins, sr_phostp_t *r)
  1853. {
  1854. char* first; /* first ':' occurrence */
  1855. char* second; /* second ':' occurrence */
  1856. char* p;
  1857. int bracket;
  1858. char* tmp;
  1859. first=second=0;
  1860. bracket=0;
  1861. memset(r, 0, sizeof(sr_phostp_t));
  1862. /* find the first 2 ':', ignoring possible ipv6 addresses
  1863. * (substrings between [])
  1864. */
  1865. for(p=ins->s; p<ins->s+ins->len; p++){
  1866. switch(*p){
  1867. case '[':
  1868. bracket++;
  1869. if (bracket>1) goto error_brackets;
  1870. break;
  1871. case ']':
  1872. bracket--;
  1873. if (bracket<0) goto error_brackets;
  1874. break;
  1875. case ':':
  1876. if (bracket==0){
  1877. if (first==0) first=p;
  1878. else if( second==0) second=p;
  1879. else goto error_colons;
  1880. }
  1881. break;
  1882. }
  1883. }
  1884. if (p==ins->s) return -1;
  1885. if (*(p-1)==':') goto error_colons;
  1886. if (first==0) { /* no ':' => only host */
  1887. r->host.s=ins->s;
  1888. r->host.len=(int)(p-ins->s);
  1889. goto end;
  1890. }
  1891. if (second) { /* 2 ':' found => check if valid */
  1892. if (parse_proto((unsigned char*)ins->s, first-ins->s, &r->proto)<0)
  1893. goto error_proto;
  1894. r->port=strtol(second+1, &tmp, 10);
  1895. if ((tmp==0)||(*tmp)||(tmp==second+1)) goto error_port;
  1896. r->host.s=first+1;
  1897. r->host.len=(int)(second-r->host.s);
  1898. goto end;
  1899. }
  1900. /* only 1 ':' found => it's either proto:host or host:port */
  1901. r->port=strtol(first+1, &tmp, 10);
  1902. if ((tmp==0)||(*tmp)||(tmp==first+1)){
  1903. /* invalid port => it's proto:host */
  1904. if (parse_proto((unsigned char*)ins->s, first-ins->s, &r->proto)<0)
  1905. goto error_proto;
  1906. r->host.s=first+1;
  1907. r->host.len=(int)(p-r->host.s);
  1908. }else{
  1909. /* valid port => its host:port */
  1910. r->host.s=ins->s;
  1911. r->host.len=(int)(first-r->host.s);
  1912. }
  1913. end:
  1914. return 0;
  1915. error_brackets:
  1916. LOG(L_ERR, "too many brackets in %.*s\n", ins->len, ins->s);
  1917. return -1;
  1918. error_colons:
  1919. LOG(L_ERR, "too many colons in %.*s\n", ins->len, ins->s);
  1920. return -1;
  1921. error_proto:
  1922. LOG(L_ERR, "bad protocol in %.*s\n", ins->len, ins->s);
  1923. return -1;
  1924. error_port:
  1925. LOG(L_ERR, "bad port number in %.*s\n", ins->len, ins->s);
  1926. return -1;
  1927. }
  1928. /**
  1929. * lookup a local socket by '[port:]host[:port]' string
  1930. */
  1931. struct socket_info* lookup_local_socket(str *phostp)
  1932. {
  1933. sr_phostp_t r;
  1934. if(parse_protohostport(phostp, &r)<0)
  1935. return NULL;
  1936. return grep_sock_info(&r.host, (unsigned short)r.port,
  1937. (unsigned short)r.proto);
  1938. }