socket_info.c 53 KB

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