socket_info.c 53 KB

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