tcp_main.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. /*
  28. * History:
  29. * --------
  30. * 2002-11-29 created by andrei
  31. * 2002-12-11 added tcp_send (andrei)
  32. * 2003-01-20 locking fixes, hashtables (andrei)
  33. * 2003-02-20 s/lock_t/gen_lock_t/ to avoid a conflict on solaris (andrei)
  34. * 2003-02-25 Nagle is disabled if -DDISABLE_NAGLE (andrei)
  35. * 2003-03-29 SO_REUSEADDR before calling bind to allow
  36. * server restart, Nagle set on the (hopefuly)
  37. * correct socket (jiri)
  38. * 2003-03-31 always try to find the corresponding tcp listen socket for
  39. * a temp. socket and store in in *->bind_address: added
  40. * find_tcp_si, modified tcpconn_connect (andrei)
  41. * 2003-04-14 set sockopts to TOS low delay (andrei)
  42. * 2003-06-30 moved tcp new connect checking & handling to
  43. * handle_new_connect (andrei)
  44. * 2003-07-09 tls_close called before closing the tcp connection (andrei)
  45. * 2003-10-24 converted to the new socket_info lists (andrei)
  46. * 2003-10-27 tcp port aliases support added (andrei)
  47. * 2003-11-04 always lock before manipulating refcnt; sendchild
  48. * does not inc refcnt by itself anymore (andrei)
  49. * 2003-11-07 different unix sockets are used for fd passing
  50. * to/from readers/writers (andrei)
  51. * 2003-11-17 handle_new_connect & tcp_connect will close the
  52. * new socket if tcpconn_new return 0 (e.g. out of mem) (andrei)
  53. * 2003-11-28 tcp_blocking_write & tcp_blocking_connect added (andrei)
  54. * 2004-11-08 dropped find_tcp_si and replaced with find_si (andrei)
  55. * 2005-06-07 new tcp optimized code, supports epoll (LT), sigio + real time
  56. * signals, poll & select (andrei)
  57. * 2005-06-26 *bsd kqueue support (andrei)
  58. * 2005-07-04 solaris /dev/poll support (andrei)
  59. * 2005-07-08 tcp_max_connections, tcp_connection_lifetime, don't accept
  60. * more connections if tcp_max_connections is exceeded (andrei)
  61. * 2005-10-21 cleanup all the open connections on exit
  62. * decrement the no. of open connections on timeout too (andrei) * 2006-01-30 queue send_fd request and execute them at the end of the
  63. * poll loop (#ifdef) (andrei)
  64. * process all children requests, before attempting to send
  65. * them new stuff (fixes some deadlocks) (andrei)
  66. * 2006-02-03 timers are run only once per s (andrei)
  67. * tcp children fds can be non-blocking; send fds are queued on
  68. * EAGAIN; lots of bug fixes (andrei)
  69. * 2006-02-06 better tcp_max_connections checks, tcp_connections_no moved to
  70. * shm (andrei)
  71. * 2006-04-12 tcp_send() changed to use struct dest_info (andrei)
  72. * 2006-11-02 switched to atomic ops for refcnt, locking improvements
  73. * (andrei)
  74. * 2006-11-04 switched to raw ticks (to fix conversion errors which could
  75. * result in inf. lifetime) (andrei)
  76. * 2007-07-25 tcpconn_connect can now bind the socket on a specified
  77. * source addr/port (andrei)
  78. * 2007-07-26 tcp_send() and tcpconn_get() can now use a specified source
  79. * addr./port (andrei)
  80. */
  81. #ifdef USE_TCP
  82. #ifndef SHM_MEM
  83. #error "shared memory support needed (add -DSHM_MEM to Makefile.defs)"
  84. #endif
  85. #include <sys/time.h>
  86. #include <sys/types.h>
  87. #include <sys/select.h>
  88. #include <sys/socket.h>
  89. #include <netinet/in.h>
  90. #include <netinet/in_systm.h>
  91. #include <netinet/ip.h>
  92. #include <netinet/tcp.h>
  93. #include <sys/uio.h> /* writev*/
  94. #include <netdb.h>
  95. #include <stdlib.h> /*exit() */
  96. #include <unistd.h>
  97. #include <errno.h>
  98. #include <string.h>
  99. #ifdef HAVE_SELECT
  100. #include <sys/select.h>
  101. #endif
  102. #include <sys/poll.h>
  103. #include "ip_addr.h"
  104. #include "pass_fd.h"
  105. #include "tcp_conn.h"
  106. #include "globals.h"
  107. #include "pt.h"
  108. #include "locking.h"
  109. #include "mem/mem.h"
  110. #include "mem/shm_mem.h"
  111. #include "timer.h"
  112. #include "sr_module.h"
  113. #include "tcp_server.h"
  114. #include "tcp_init.h"
  115. #include "tsend.h"
  116. #include "timer_ticks.h"
  117. #ifdef CORE_TLS
  118. #include "tls/tls_server.h"
  119. #define tls_loaded() 1
  120. #else
  121. #include "tls_hooks_init.h"
  122. #include "tls_hooks.h"
  123. #endif
  124. #include "tcp_info.h"
  125. #define local_malloc pkg_malloc
  126. #define local_free pkg_free
  127. #define HANDLE_IO_INLINE
  128. #include "io_wait.h"
  129. #include <fcntl.h> /* must be included after io_wait.h if SIGIO_RT is used */
  130. #define TCP_PASS_NEW_CONNECTION_ON_DATA /* don't pass a new connection
  131. immediately to a child, wait for
  132. some data on it first */
  133. #define TCP_LISTEN_BACKLOG 1024
  134. #define SEND_FD_QUEUE /* queue send fd requests on EAGAIN, instead of sending
  135. them immediately */
  136. #define TCP_CHILD_NON_BLOCKING
  137. #ifdef SEND_FD_QUEUE
  138. #ifndef TCP_CHILD_NON_BLOCKING
  139. #define TCP_CHILD_NON_BLOCKING
  140. #endif
  141. #define MAX_SEND_FD_QUEUE_SIZE tcp_main_max_fd_no
  142. #define SEND_FD_QUEUE_SIZE 128 /* initial size */
  143. #define MAX_SEND_FD_RETRIES 96 /* FIXME: not used for now */
  144. #define SEND_FD_QUEUE_TIMEOUT MS_TO_TICKS(2000) /* 2 s */
  145. #endif
  146. /* maximum accepted lifetime (maximum possible is ~ MAXINT/2) */
  147. #define MAX_TCP_CON_LIFETIME ((1U<<(sizeof(ticks_t)*8-1))-1)
  148. /* minimum interval tcpconn_timeout() is allowed to run, in ticks */
  149. #define TCPCONN_TIMEOUT_MIN_RUN S_TO_TICKS(1) /* once per s */
  150. enum fd_types { F_NONE, F_SOCKINFO /* a tcp_listen fd */,
  151. F_TCPCONN, F_TCPCHILD, F_PROC };
  152. static int is_tcp_main=0;
  153. int tcp_accept_aliases=0; /* by default don't accept aliases */
  154. int tcp_connect_timeout=DEFAULT_TCP_CONNECT_TIMEOUT;
  155. int tcp_send_timeout=DEFAULT_TCP_SEND_TIMEOUT;
  156. int tcp_con_lifetime=DEFAULT_TCP_CONNECTION_LIFETIME;
  157. enum poll_types tcp_poll_method=0; /* by default choose the best method */
  158. int tcp_max_connections=DEFAULT_TCP_MAX_CONNECTIONS;
  159. int tcp_main_max_fd_no=0;
  160. static union sockaddr_union tcp_source_ipv4_addr; /* saved bind/srv v4 addr. */
  161. static union sockaddr_union* tcp_source_ipv4=0;
  162. #ifdef USE_IPV6
  163. static union sockaddr_union tcp_source_ipv6_addr; /* saved bind/src v6 addr. */
  164. static union sockaddr_union* tcp_source_ipv6=0;
  165. #endif
  166. static int* tcp_connections_no=0; /* current open connections */
  167. /* connection hash table (after ip&port) , includes also aliases */
  168. struct tcp_conn_alias** tcpconn_aliases_hash=0;
  169. /* connection hash table (after connection id) */
  170. struct tcp_connection** tcpconn_id_hash=0;
  171. gen_lock_t* tcpconn_lock=0;
  172. struct tcp_child* tcp_children;
  173. static int* connection_id=0; /* unique for each connection, used for
  174. quickly finding the corresponding connection
  175. for a reply */
  176. int unix_tcp_sock;
  177. static int tcp_proto_no=-1; /* tcp protocol number as returned by
  178. getprotobyname */
  179. static io_wait_h io_h;
  180. inline static int _tcpconn_add_alias_unsafe(struct tcp_connection* c, int port,
  181. struct ip_addr* l_ip, int l_port);
  182. /* sets source address used when opening new sockets and no source is specified
  183. * (by default the address is choosen by the kernel)
  184. * Should be used only on init.
  185. * returns -1 on error */
  186. int tcp_set_src_addr(struct ip_addr* ip)
  187. {
  188. switch (ip->af){
  189. case AF_INET:
  190. ip_addr2su(&tcp_source_ipv4_addr, ip, 0);
  191. tcp_source_ipv4=&tcp_source_ipv4_addr;
  192. break;
  193. #ifdef USE_IPV6
  194. case AF_INET6:
  195. ip_addr2su(&tcp_source_ipv6_addr, ip, 0);
  196. tcp_source_ipv6=&tcp_source_ipv6_addr;
  197. break;
  198. #endif
  199. default:
  200. return -1;
  201. }
  202. return 0;
  203. }
  204. /* set all socket/fd options: disable nagle, tos lowdelay, non-blocking
  205. * return -1 on error */
  206. static int init_sock_opt(int s)
  207. {
  208. int flags;
  209. int optval;
  210. #ifdef DISABLE_NAGLE
  211. flags=1;
  212. if ( (tcp_proto_no!=-1) && (setsockopt(s, tcp_proto_no , TCP_NODELAY,
  213. &flags, sizeof(flags))<0) ){
  214. LOG(L_WARN, "WARNING: init_sock_opt: could not disable Nagle: %s\n",
  215. strerror(errno));
  216. }
  217. #endif
  218. /* tos*/
  219. optval = tos;
  220. if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval,sizeof(optval)) ==-1){
  221. LOG(L_WARN, "WARNING: init_sock_opt: setsockopt tos: %s\n",
  222. strerror(errno));
  223. /* continue since this is not critical */
  224. }
  225. /* non-blocking */
  226. flags=fcntl(s, F_GETFL);
  227. if (flags==-1){
  228. LOG(L_ERR, "ERROR: init_sock_opt: fnctl failed: (%d) %s\n",
  229. errno, strerror(errno));
  230. goto error;
  231. }
  232. if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){
  233. LOG(L_ERR, "ERROR: init_sock_opt: fcntl: set non-blocking failed:"
  234. " (%d) %s\n", errno, strerror(errno));
  235. goto error;
  236. }
  237. return 0;
  238. error:
  239. return -1;
  240. }
  241. /* blocking connect on a non-blocking fd; it will timeout after
  242. * tcp_connect_timeout
  243. * if BLOCKING_USE_SELECT and HAVE_SELECT are defined it will internally
  244. * use select() instead of poll (bad if fd > FD_SET_SIZE, poll is preferred)
  245. */
  246. static int tcp_blocking_connect(int fd, const struct sockaddr *servaddr,
  247. socklen_t addrlen)
  248. {
  249. int n;
  250. #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
  251. fd_set sel_set;
  252. fd_set orig_set;
  253. struct timeval timeout;
  254. #else
  255. struct pollfd pf;
  256. #endif
  257. int elapsed;
  258. int to;
  259. int ticks;
  260. int err;
  261. unsigned int err_len;
  262. int poll_err;
  263. poll_err=0;
  264. to=tcp_connect_timeout;
  265. ticks=get_ticks();
  266. again:
  267. n=connect(fd, servaddr, addrlen);
  268. if (n==-1){
  269. if (errno==EINTR){
  270. elapsed=(get_ticks()-ticks)*TIMER_TICK;
  271. if (elapsed<to) goto again;
  272. else goto error_timeout;
  273. }
  274. if (errno!=EINPROGRESS && errno!=EALREADY){
  275. LOG(L_ERR, "ERROR: tcp_blocking_connect: (%d) %s\n",
  276. errno, strerror(errno));
  277. goto error;
  278. }
  279. }else goto end;
  280. /* poll/select loop */
  281. #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
  282. FD_ZERO(&orig_set);
  283. FD_SET(fd, &orig_set);
  284. #else
  285. pf.fd=fd;
  286. pf.events=POLLOUT;
  287. #endif
  288. while(1){
  289. elapsed=(get_ticks()-ticks)*TIMER_TICK;
  290. if (elapsed<to)
  291. to-=elapsed;
  292. else
  293. goto error_timeout;
  294. #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
  295. sel_set=orig_set;
  296. timeout.tv_sec=to;
  297. timeout.tv_usec=0;
  298. n=select(fd+1, 0, &sel_set, 0, &timeout);
  299. #else
  300. n=poll(&pf, 1, to*1000);
  301. #endif
  302. if (n<0){
  303. if (errno==EINTR) continue;
  304. LOG(L_ERR, "ERROR: tcp_blocking_connect: poll/select failed:"
  305. " (%d) %s\n", errno, strerror(errno));
  306. goto error;
  307. }else if (n==0) /* timeout */ continue;
  308. #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
  309. if (FD_ISSET(fd, &sel_set))
  310. #else
  311. if (pf.revents&(POLLERR|POLLHUP|POLLNVAL)){
  312. LOG(L_ERR, "ERROR: tcp_blocking_connect: poll error: flags %x\n",
  313. pf.revents);
  314. poll_err=1;
  315. }
  316. #endif
  317. {
  318. err_len=sizeof(err);
  319. getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
  320. if ((err==0) && (poll_err==0)) goto end;
  321. if (err!=EINPROGRESS && err!=EALREADY){
  322. LOG(L_ERR, "ERROR: tcp_blocking_connect: SO_ERROR (%d) %s\n",
  323. err, strerror(err));
  324. goto error;
  325. }
  326. }
  327. }
  328. error_timeout:
  329. /* timeout */
  330. LOG(L_ERR, "ERROR: tcp_blocking_connect: timeout %d s elapsed from %d s\n",
  331. elapsed, tcp_connect_timeout);
  332. error:
  333. return -1;
  334. end:
  335. return 0;
  336. }
  337. #if 0
  338. /* blocking write even on non-blocking sockets
  339. * if TCP_TIMEOUT will return with error */
  340. static int tcp_blocking_write(struct tcp_connection* c, int fd, char* buf,
  341. unsigned int len)
  342. {
  343. int n;
  344. fd_set sel_set;
  345. struct timeval timeout;
  346. int ticks;
  347. int initial_len;
  348. initial_len=len;
  349. again:
  350. n=send(fd, buf, len,
  351. #ifdef HAVE_MSG_NOSIGNAL
  352. MSG_NOSIGNAL
  353. #else
  354. 0
  355. #endif
  356. );
  357. if (n<0){
  358. if (errno==EINTR) goto again;
  359. else if (errno!=EAGAIN && errno!=EWOULDBLOCK){
  360. LOG(L_ERR, "tcp_blocking_write: failed to send: (%d) %s\n",
  361. errno, strerror(errno));
  362. goto error;
  363. }
  364. }else if (n<len){
  365. /* partial write */
  366. buf+=n;
  367. len-=n;
  368. }else{
  369. /* success: full write */
  370. goto end;
  371. }
  372. while(1){
  373. FD_ZERO(&sel_set);
  374. FD_SET(fd, &sel_set);
  375. timeout.tv_sec=tcp_send_timeout;
  376. timeout.tv_usec=0;
  377. ticks=get_ticks();
  378. n=select(fd+1, 0, &sel_set, 0, &timeout);
  379. if (n<0){
  380. if (errno==EINTR) continue; /* signal, ignore */
  381. LOG(L_ERR, "ERROR: tcp_blocking_write: select failed: "
  382. " (%d) %s\n", errno, strerror(errno));
  383. goto error;
  384. }else if (n==0){
  385. /* timeout */
  386. if (get_ticks()-ticks>=tcp_send_timeout){
  387. LOG(L_ERR, "ERROR: tcp_blocking_write: send timeout (%d)\n",
  388. tcp_send_timeout);
  389. goto error;
  390. }
  391. continue;
  392. }
  393. if (FD_ISSET(fd, &sel_set)){
  394. /* we can write again */
  395. goto again;
  396. }
  397. }
  398. error:
  399. return -1;
  400. end:
  401. return initial_len;
  402. }
  403. #endif
  404. struct tcp_connection* tcpconn_new(int sock, union sockaddr_union* su,
  405. union sockaddr_union* local_addr,
  406. struct socket_info* ba, int type,
  407. int state)
  408. {
  409. struct tcp_connection *c;
  410. c=(struct tcp_connection*)shm_malloc(sizeof(struct tcp_connection));
  411. if (c==0){
  412. LOG(L_ERR, "ERROR: tcpconn_new: mem. allocation failure\n");
  413. goto error;
  414. }
  415. memset(c, 0, sizeof(struct tcp_connection)); /* zero init */
  416. c->s=sock;
  417. c->fd=-1; /* not initialized */
  418. if (lock_init(&c->write_lock)==0){
  419. LOG(L_ERR, "ERROR: tcpconn_new: init lock failed\n");
  420. goto error;
  421. }
  422. c->rcv.src_su=*su;
  423. atomic_set(&c->refcnt, 0);
  424. su2ip_addr(&c->rcv.src_ip, su);
  425. c->rcv.src_port=su_getport(su);
  426. c->rcv.bind_address=ba;
  427. if (likely(local_addr)){
  428. su2ip_addr(&c->rcv.dst_ip, local_addr);
  429. c->rcv.dst_port=su_getport(local_addr);
  430. }else if (ba){
  431. c->rcv.dst_ip=ba->address;
  432. c->rcv.dst_port=ba->port_no;
  433. }
  434. print_ip("tcpconn_new: new tcp connection: ", &c->rcv.src_ip, "\n");
  435. DBG( "tcpconn_new: on port %d, type %d\n", c->rcv.src_port, type);
  436. init_tcp_req(&c->req);
  437. c->id=(*connection_id)++;
  438. c->rcv.proto_reserved1=0; /* this will be filled before receive_message*/
  439. c->rcv.proto_reserved2=0;
  440. c->state=state;
  441. c->extra_data=0;
  442. #ifdef USE_TLS
  443. if (type==PROTO_TLS){
  444. if (tls_tcpconn_init(c, sock)==-1) goto error;
  445. }else
  446. #endif /* USE_TLS*/
  447. {
  448. c->type=PROTO_TCP;
  449. c->rcv.proto=PROTO_TCP;
  450. c->timeout=get_ticks_raw()+tcp_con_lifetime;
  451. }
  452. c->flags|=F_CONN_REMOVED;
  453. return c;
  454. error:
  455. if (c) shm_free(c);
  456. return 0;
  457. }
  458. struct tcp_connection* tcpconn_connect( union sockaddr_union* server,
  459. union sockaddr_union* from,
  460. int type)
  461. {
  462. int s;
  463. struct socket_info* si;
  464. union sockaddr_union my_name;
  465. socklen_t my_name_len;
  466. struct tcp_connection* con;
  467. struct ip_addr ip;
  468. s=-1;
  469. if (*tcp_connections_no >= tcp_max_connections){
  470. LOG(L_ERR, "ERROR: tcpconn_connect: maximum number of connections"
  471. " exceeded (%d/%d)\n",
  472. *tcp_connections_no, tcp_max_connections);
  473. goto error;
  474. }
  475. s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
  476. if (s==-1){
  477. LOG(L_ERR, "ERROR: tcpconn_connect: socket: (%d) %s\n",
  478. errno, strerror(errno));
  479. goto error;
  480. }
  481. if (init_sock_opt(s)<0){
  482. LOG(L_ERR, "ERROR: tcpconn_connect: init_sock_opt failed\n");
  483. goto error;
  484. }
  485. if (from && bind(s, &from->s, sockaddru_len(*from)) != 0)
  486. LOG(L_WARN, "WARNING: tcpconn_connect: binding to source address"
  487. " failed: %s [%d]\n", strerror(errno), errno);
  488. if (tcp_blocking_connect(s, &server->s, sockaddru_len(*server))<0){
  489. LOG(L_ERR, "ERROR: tcpconn_connect: tcp_blocking_connect failed\n");
  490. goto error;
  491. }
  492. if (from){
  493. su2ip_addr(&ip, from);
  494. if (!ip_addr_any(&ip))
  495. /* we already know the source ip, skip the sys. call */
  496. goto find_socket;
  497. }
  498. my_name_len=sizeof(my_name);
  499. if (getsockname(s, &my_name.s, &my_name_len)!=0){
  500. LOG(L_ERR, "ERROR: tcp_connect: getsockname failed: %s(%d)\n",
  501. strerror(errno), errno);
  502. si=0; /* try to go on */
  503. goto skip;
  504. }
  505. from=&my_name; /* update from with the real "from" address */
  506. su2ip_addr(&ip, &my_name);
  507. find_socket:
  508. #ifdef USE_TLS
  509. if (type==PROTO_TLS)
  510. si=find_si(&ip, 0, PROTO_TLS);
  511. else
  512. #endif
  513. si=find_si(&ip, 0, PROTO_TCP);
  514. skip:
  515. if (si==0){
  516. LOG(L_WARN, "WARNING: tcp_connect: could not find corresponding"
  517. " listening socket, using default...\n");
  518. if (server->s.sa_family==AF_INET) si=sendipv4_tcp;
  519. #ifdef USE_IPV6
  520. else si=sendipv6_tcp;
  521. #endif
  522. }
  523. con=tcpconn_new(s, server, from, si, type, S_CONN_CONNECT);
  524. if (con==0){
  525. LOG(L_ERR, "ERROR: tcp_connect: tcpconn_new failed, closing the "
  526. " socket\n");
  527. goto error;
  528. }
  529. return con;
  530. /*FIXME: set sock idx! */
  531. error:
  532. if (s!=-1) close(s); /* close the opened socket */
  533. return 0;
  534. }
  535. /* adds a tcp connection to the tcpconn hashes
  536. * Note: it's called _only_ from the tcp_main process */
  537. inline static struct tcp_connection* tcpconn_add(struct tcp_connection *c)
  538. {
  539. struct ip_addr zero_ip;
  540. if (likely(c)){
  541. ip_addr_mk_any(c->rcv.src_ip.af, &zero_ip);
  542. c->id_hash=tcp_id_hash(c->id);
  543. c->aliases=0;
  544. TCPCONN_LOCK;
  545. /* add it at the begining of the list*/
  546. tcpconn_listadd(tcpconn_id_hash[c->id_hash], c, id_next, id_prev);
  547. /* set the aliases */
  548. /* first alias is for (peer_ip, peer_port, 0 ,0) -- for finding
  549. * any connection to peer_ip, peer_port
  550. * the second alias is for (peer_ip, peer_port, local_addr, 0) -- for
  551. * finding any conenction to peer_ip, peer_port from local_addr
  552. * the third alias is for (peer_ip, peer_port, local_addr, local_port)
  553. * -- for finding if a fully specified connection exists */
  554. _tcpconn_add_alias_unsafe(c, c->rcv.src_port, &zero_ip, 0);
  555. _tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip, 0);
  556. _tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip,
  557. c->rcv.dst_port);
  558. /* ignore add_alias errors, there are some valid cases when one
  559. * of the add_alias would fail (e.g. first add_alias for 2 connections
  560. * with the same destination but different src. ip*/
  561. TCPCONN_UNLOCK;
  562. DBG("tcpconn_add: hashes: %d:%d:%d, %d\n",
  563. c->con_aliases[0].hash,
  564. c->con_aliases[1].hash,
  565. c->con_aliases[2].hash,
  566. c->id_hash);
  567. return c;
  568. }else{
  569. LOG(L_CRIT, "tcpconn_add: BUG: null connection pointer\n");
  570. return 0;
  571. }
  572. }
  573. /* unsafe tcpconn_rm version (nolocks) */
  574. void _tcpconn_rm(struct tcp_connection* c)
  575. {
  576. int r;
  577. tcpconn_listrm(tcpconn_id_hash[c->id_hash], c, id_next, id_prev);
  578. /* remove all the aliases */
  579. for (r=0; r<c->aliases; r++)
  580. tcpconn_listrm(tcpconn_aliases_hash[c->con_aliases[r].hash],
  581. &c->con_aliases[r], next, prev);
  582. lock_destroy(&c->write_lock);
  583. #ifdef USE_TLS
  584. if (c->type==PROTO_TLS) tls_tcpconn_clean(c);
  585. #endif
  586. shm_free(c);
  587. }
  588. void tcpconn_rm(struct tcp_connection* c)
  589. {
  590. int r;
  591. TCPCONN_LOCK;
  592. tcpconn_listrm(tcpconn_id_hash[c->id_hash], c, id_next, id_prev);
  593. /* remove all the aliases */
  594. for (r=0; r<c->aliases; r++)
  595. tcpconn_listrm(tcpconn_aliases_hash[c->con_aliases[r].hash],
  596. &c->con_aliases[r], next, prev);
  597. TCPCONN_UNLOCK;
  598. lock_destroy(&c->write_lock);
  599. #ifdef USE_TLS
  600. if ((c->type==PROTO_TLS)&&(c->extra_data)) tls_tcpconn_clean(c);
  601. #endif
  602. shm_free(c);
  603. }
  604. /* finds a connection, if id=0 uses the ip addr, port, local_ip and local port
  605. * (host byte order) and tries to find the connection that matches all of
  606. * them. Wild cards can be used for local_ip and local_port (a 0 filled
  607. * ip address and/or a 0 local port).
  608. * WARNING: unprotected (locks) use tcpconn_get unless you really
  609. * know what you are doing */
  610. struct tcp_connection* _tcpconn_find(int id, struct ip_addr* ip, int port,
  611. struct ip_addr* l_ip, int l_port)
  612. {
  613. struct tcp_connection *c;
  614. struct tcp_conn_alias* a;
  615. unsigned hash;
  616. int is_local_ip_any;
  617. #ifdef EXTRA_DEBUG
  618. DBG("tcpconn_find: %d port %d\n",id, port);
  619. if (ip) print_ip("tcpconn_find: ip ", ip, "\n");
  620. #endif
  621. if (id){
  622. hash=tcp_id_hash(id);
  623. for (c=tcpconn_id_hash[hash]; c; c=c->id_next){
  624. #ifdef EXTRA_DEBUG
  625. DBG("c=%p, c->id=%d, port=%d\n",c, c->id, c->rcv.src_port);
  626. print_ip("ip=", &c->rcv.src_ip, "\n");
  627. #endif
  628. if ((id==c->id)&&(c->state!=S_CONN_BAD)) return c;
  629. }
  630. }else if (ip){
  631. hash=tcp_addr_hash(ip, port, l_ip, l_port);
  632. is_local_ip_any=ip_addr_any(l_ip);
  633. for (a=tcpconn_aliases_hash[hash]; a; a=a->next){
  634. #ifdef EXTRA_DEBUG
  635. DBG("a=%p, c=%p, c->id=%d, alias port= %d port=%d\n", a, a->parent,
  636. a->parent->id, a->port, a->parent->rcv.src_port);
  637. print_ip("ip=",&a->parent->rcv.src_ip,"\n");
  638. #endif
  639. if ( (a->parent->state!=S_CONN_BAD) && (port==a->port) &&
  640. ((l_port==0) || (l_port==a->parent->rcv.dst_port)) &&
  641. (ip_addr_cmp(ip, &a->parent->rcv.src_ip)) &&
  642. (is_local_ip_any ||
  643. ip_addr_cmp(l_ip, &a->parent->rcv.dst_ip))
  644. )
  645. return a->parent;
  646. }
  647. }
  648. return 0;
  649. }
  650. /* _tcpconn_find with locks and timeout
  651. * local_addr contains the desired local ip:port. If null any local address
  652. * will be used. IN*ADDR_ANY or 0 port are wild cards.
  653. */
  654. struct tcp_connection* tcpconn_get(int id, struct ip_addr* ip, int port,
  655. union sockaddr_union* local_addr,
  656. ticks_t timeout)
  657. {
  658. struct tcp_connection* c;
  659. struct ip_addr local_ip;
  660. int local_port;
  661. local_port=0;
  662. if (ip){
  663. if (local_addr){
  664. su2ip_addr(&local_ip, local_addr);
  665. local_port=su_getport(local_addr);
  666. }else{
  667. ip_addr_mk_any(ip->af, &local_ip);
  668. local_port=0;
  669. }
  670. }
  671. TCPCONN_LOCK;
  672. c=_tcpconn_find(id, ip, port, &local_ip, local_port);
  673. if (c){
  674. atomic_inc(&c->refcnt);
  675. c->timeout=get_ticks_raw()+timeout;
  676. }
  677. TCPCONN_UNLOCK;
  678. return c;
  679. }
  680. /* add c->dst:port, local_addr as an alias for the "id" connection,
  681. * returns 0 on success, <0 on failure ( -1 - null c, -2 too many aliases,
  682. * -3 alias already present and pointing to another connection)
  683. * WARNING: must be called with TCPCONN_LOCK held */
  684. inline static int _tcpconn_add_alias_unsafe(struct tcp_connection* c, int port,
  685. struct ip_addr* l_ip, int l_port)
  686. {
  687. unsigned hash;
  688. struct tcp_conn_alias* a;
  689. int is_local_ip_any;
  690. a=0;
  691. is_local_ip_any=ip_addr_any(l_ip);
  692. if (c){
  693. hash=tcp_addr_hash(&c->rcv.src_ip, port, l_ip, l_port);
  694. /* search the aliases for an already existing one */
  695. for (a=tcpconn_aliases_hash[hash]; a; a=a->next){
  696. if ( (a->parent->state!=S_CONN_BAD) && (port==a->port) &&
  697. ( (l_port==0) || (l_port==a->parent->rcv.dst_port)) &&
  698. (ip_addr_cmp(&c->rcv.src_ip, &a->parent->rcv.src_ip)) &&
  699. ( is_local_ip_any ||
  700. ip_addr_cmp(&a->parent->rcv.dst_ip, l_ip))
  701. ){
  702. /* found */
  703. if (a->parent!=c) goto error_sec;
  704. else goto ok;
  705. }
  706. }
  707. if (c->aliases>=TCP_CON_MAX_ALIASES) goto error_aliases;
  708. c->con_aliases[c->aliases].parent=c;
  709. c->con_aliases[c->aliases].port=port;
  710. c->con_aliases[c->aliases].hash=hash;
  711. tcpconn_listadd(tcpconn_aliases_hash[hash],
  712. &c->con_aliases[c->aliases], next, prev);
  713. c->aliases++;
  714. }else goto error_not_found;
  715. ok:
  716. #ifdef EXTRA_DEBUG
  717. if (a) DBG("_tcpconn_add_alias_unsafe: alias already present\n");
  718. else DBG("_tcpconn_add_alias_unsafe: alias port %d for hash %d, id %d\n",
  719. port, hash, c->id);
  720. #endif
  721. return 0;
  722. error_aliases:
  723. /* too many aliases */
  724. return -2;
  725. error_not_found:
  726. /* null connection */
  727. return -1;
  728. error_sec:
  729. /* alias already present and pointing to a different connection
  730. * (hijack attempt?) */
  731. return -3;
  732. }
  733. /* add port as an alias for the "id" connection,
  734. * returns 0 on success,-1 on failure */
  735. int tcpconn_add_alias(int id, int port, int proto)
  736. {
  737. struct tcp_connection* c;
  738. int ret;
  739. struct ip_addr zero_ip;
  740. /* fix the port */
  741. port=port?port:((proto==PROTO_TLS)?SIPS_PORT:SIP_PORT);
  742. TCPCONN_LOCK;
  743. /* check if alias already exists */
  744. c=_tcpconn_find(id, 0, 0, 0, 0);
  745. if (c){
  746. ip_addr_mk_any(c->rcv.src_ip.af, &zero_ip);
  747. /* alias src_ip:port, 0, 0 */
  748. ret=_tcpconn_add_alias_unsafe(c, port, &zero_ip, 0);
  749. if (ret<0 && ret!=-3) goto error;
  750. /* alias src_ip:port, local_ip, 0 */
  751. ret=_tcpconn_add_alias_unsafe(c, port, &c->rcv.dst_ip, 0);
  752. if (ret<0 && ret!=-3) goto error;
  753. /* alias src_ip:port, local_ip, local_port */
  754. ret=_tcpconn_add_alias_unsafe(c, port, &c->rcv.dst_ip,
  755. c->rcv.dst_port);
  756. if (ret<0) goto error;
  757. }else goto error_not_found;
  758. TCPCONN_UNLOCK;
  759. return 0;
  760. error_not_found:
  761. TCPCONN_UNLOCK;
  762. LOG(L_ERR, "ERROR: tcpconn_add_alias: no connection found for id %d\n",id);
  763. return -1;
  764. error:
  765. TCPCONN_UNLOCK;
  766. switch(ret){
  767. case -2:
  768. LOG(L_ERR, "ERROR: tcpconn_add_alias: too many aliases"
  769. " for connection %p (%d)\n", c, c->id);
  770. break;
  771. case -3:
  772. LOG(L_ERR, "ERROR: tcpconn_add_alias: possible port"
  773. " hijack attempt\n");
  774. LOG(L_ERR, "ERROR: tcpconn_add_alias: alias for %d port %d already"
  775. " present and points to another connection \n",
  776. c->id, port);
  777. break;
  778. default:
  779. LOG(L_ERR, "ERROR: tcpconn_add_alias: unkown error %d\n", ret);
  780. }
  781. return -1;
  782. }
  783. /* finds a tcpconn & sends on it
  784. * uses the dst members to, proto (TCP|TLS) and id and tries to send
  785. * from the "from" address (if non null and id==0)
  786. * returns: number of bytes written (>=0) on success
  787. * <0 on error */
  788. int tcp_send(struct dest_info* dst, union sockaddr_union* from,
  789. char* buf, unsigned len)
  790. {
  791. struct tcp_connection *c;
  792. struct tcp_connection *tmp;
  793. struct ip_addr ip;
  794. int port;
  795. int fd;
  796. long response[2];
  797. int n;
  798. port=su_getport(&dst->to);
  799. if (port){
  800. su2ip_addr(&ip, &dst->to);
  801. c=tcpconn_get(dst->id, &ip, port, from, tcp_con_lifetime);
  802. }else if (dst->id){
  803. c=tcpconn_get(dst->id, 0, 0, 0, tcp_con_lifetime);
  804. }else{
  805. LOG(L_CRIT, "BUG: tcp_send called with null id & to\n");
  806. return -1;
  807. }
  808. if (dst->id){
  809. if (c==0) {
  810. if (port){
  811. /* try again w/o id */
  812. c=tcpconn_get(0, &ip, port, from, tcp_con_lifetime);
  813. goto no_id;
  814. }else{
  815. LOG(L_ERR, "ERROR: tcp_send: id %d not found, dropping\n",
  816. dst->id);
  817. return -1;
  818. }
  819. }else goto get_fd;
  820. }
  821. no_id:
  822. if (c==0){
  823. DBG("tcp_send: no open tcp connection found, opening new one\n");
  824. /* create tcp connection */
  825. if (from==0){
  826. /* check to see if we have to use a specific source addr. */
  827. switch (dst->to.s.sa_family) {
  828. case AF_INET:
  829. from = tcp_source_ipv4;
  830. break;
  831. #ifdef USE_IPV6
  832. case AF_INET6:
  833. from = tcp_source_ipv6;
  834. break;
  835. #endif
  836. default:
  837. /* error, bad af, ignore ... */
  838. break;
  839. }
  840. }
  841. if ((c=tcpconn_connect(&dst->to, from, dst->proto))==0){
  842. LOG(L_ERR, "ERROR: tcp_send: connect failed\n");
  843. return -1;
  844. }
  845. atomic_set(&c->refcnt, 1); /* ref. only from here for now */
  846. fd=c->s;
  847. /* send the new tcpconn to "tcp main" */
  848. response[0]=(long)c;
  849. response[1]=CONN_NEW;
  850. n=send_fd(unix_tcp_sock, response, sizeof(response), c->s);
  851. if (n<=0){
  852. LOG(L_ERR, "BUG: tcp_send: failed send_fd: %s (%d)\n",
  853. strerror(errno), errno);
  854. n=-1;
  855. goto end;
  856. }
  857. goto send_it;
  858. }
  859. get_fd:
  860. /* todo: see if this is not the same process holding
  861. * c and if so send directly on c->fd */
  862. DBG("tcp_send: tcp connection found (%p), acquiring fd\n", c);
  863. /* get the fd */
  864. response[0]=(long)c;
  865. response[1]=CONN_GET_FD;
  866. n=send_all(unix_tcp_sock, response, sizeof(response));
  867. if (n<=0){
  868. LOG(L_ERR, "BUG: tcp_send: failed to get fd(write):%s (%d)\n",
  869. strerror(errno), errno);
  870. n=-1;
  871. goto release_c;
  872. }
  873. DBG("tcp_send, c= %p, n=%d\n", c, n);
  874. n=receive_fd(unix_tcp_sock, &tmp, sizeof(tmp), &fd, MSG_WAITALL);
  875. if (n<=0){
  876. LOG(L_ERR, "BUG: tcp_send: failed to get fd(receive_fd):"
  877. " %s (%d)\n", strerror(errno), errno);
  878. n=-1;
  879. goto release_c;
  880. }
  881. if (c!=tmp){
  882. LOG(L_CRIT, "BUG: tcp_send: get_fd: got different connection:"
  883. " %p (id= %d, refcnt=%d state=%d) != "
  884. " %p (n=%d)\n",
  885. c, c->id, atomic_get(&c->refcnt), c->state,
  886. tmp, n
  887. );
  888. n=-1; /* fail */
  889. goto end;
  890. }
  891. DBG("tcp_send: after receive_fd: c= %p n=%d fd=%d\n",c, n, fd);
  892. send_it:
  893. DBG("tcp_send: sending...\n");
  894. lock_get(&c->write_lock);
  895. #ifdef USE_TLS
  896. if (c->type==PROTO_TLS)
  897. n=tls_blocking_write(c, fd, buf, len);
  898. else
  899. #endif
  900. /* n=tcp_blocking_write(c, fd, buf, len); */
  901. n=tsend_stream(fd, buf, len, tcp_send_timeout*1000);
  902. lock_release(&c->write_lock);
  903. DBG("tcp_send: after write: c= %p n=%d fd=%d\n",c, n, fd);
  904. DBG("tcp_send: buf=\n%.*s\n", (int)len, buf);
  905. if (n<0){
  906. LOG(L_ERR, "ERROR: tcp_send: failed to send\n");
  907. /* error on the connection , mark it as bad and set 0 timeout */
  908. c->state=S_CONN_BAD;
  909. c->timeout=get_ticks_raw();
  910. /* tell "main" it should drop this (optional it will t/o anyway?)*/
  911. response[0]=(long)c;
  912. response[1]=CONN_ERROR;
  913. if (send_all(unix_tcp_sock, response, sizeof(response))<=0){
  914. LOG(L_ERR, "BUG: tcp_send: error return failed (write):%s (%d)\n",
  915. strerror(errno), errno);
  916. tcpconn_put(c); /* deref. it manually */
  917. n=-1;
  918. }
  919. /* CONN_ERROR will auto-dec refcnt => we must not call tcpconn_put
  920. * if it succeeds */
  921. close(fd);
  922. return n; /* error return, no tcpconn_put */
  923. }
  924. end:
  925. close(fd);
  926. release_c:
  927. tcpconn_put(c); /* release c (lock; dec refcnt; unlock) */
  928. return n;
  929. }
  930. int tcp_init(struct socket_info* sock_info)
  931. {
  932. union sockaddr_union* addr;
  933. int optval;
  934. #ifdef DISABLE_NAGLE
  935. int flag;
  936. struct protoent* pe;
  937. if (tcp_proto_no==-1){ /* if not already set */
  938. pe=getprotobyname("tcp");
  939. if (pe==0){
  940. LOG(L_ERR, "ERROR: tcp_init: could not get TCP protocol number\n");
  941. tcp_proto_no=-1;
  942. }else{
  943. tcp_proto_no=pe->p_proto;
  944. }
  945. }
  946. #endif
  947. addr=&sock_info->su;
  948. /* sock_info->proto=PROTO_TCP; */
  949. if (init_su(addr, &sock_info->address, sock_info->port_no)<0){
  950. LOG(L_ERR, "ERROR: tcp_init: could no init sockaddr_union\n");
  951. goto error;
  952. }
  953. sock_info->socket=socket(AF2PF(addr->s.sa_family), SOCK_STREAM, 0);
  954. if (sock_info->socket==-1){
  955. LOG(L_ERR, "ERROR: tcp_init: socket: %s\n", strerror(errno));
  956. goto error;
  957. }
  958. #ifdef DISABLE_NAGLE
  959. flag=1;
  960. if ( (tcp_proto_no!=-1) &&
  961. (setsockopt(sock_info->socket, tcp_proto_no , TCP_NODELAY,
  962. &flag, sizeof(flag))<0) ){
  963. LOG(L_ERR, "ERROR: tcp_init: could not disable Nagle: %s\n",
  964. strerror(errno));
  965. }
  966. #endif
  967. #if !defined(TCP_DONT_REUSEADDR)
  968. /* Stevens, "Network Programming", Section 7.5, "Generic Socket
  969. * Options": "...server started,..a child continues..on existing
  970. * connection..listening server is restarted...call to bind fails
  971. * ... ALL TCP servers should specify the SO_REUSEADDRE option
  972. * to allow the server to be restarted in this situation
  973. *
  974. * Indeed, without this option, the server can't restart.
  975. * -jiri
  976. */
  977. optval=1;
  978. if (setsockopt(sock_info->socket, SOL_SOCKET, SO_REUSEADDR,
  979. (void*)&optval, sizeof(optval))==-1) {
  980. LOG(L_ERR, "ERROR: tcp_init: setsockopt %s\n",
  981. strerror(errno));
  982. goto error;
  983. }
  984. #endif
  985. /* tos */
  986. optval = tos;
  987. if (setsockopt(sock_info->socket, IPPROTO_IP, IP_TOS, (void*)&optval,
  988. sizeof(optval)) ==-1){
  989. LOG(L_WARN, "WARNING: tcp_init: setsockopt tos: %s\n", strerror(errno));
  990. /* continue since this is not critical */
  991. }
  992. if (bind(sock_info->socket, &addr->s, sockaddru_len(*addr))==-1){
  993. LOG(L_ERR, "ERROR: tcp_init: bind(%x, %p, %d) on %s:%d : %s\n",
  994. sock_info->socket, &addr->s,
  995. (unsigned)sockaddru_len(*addr),
  996. sock_info->address_str.s,
  997. sock_info->port_no,
  998. strerror(errno));
  999. goto error;
  1000. }
  1001. if (listen(sock_info->socket, TCP_LISTEN_BACKLOG)==-1){
  1002. LOG(L_ERR, "ERROR: tcp_init: listen(%x, %p, %d) on %s: %s\n",
  1003. sock_info->socket, &addr->s,
  1004. (unsigned)sockaddru_len(*addr),
  1005. sock_info->address_str.s,
  1006. strerror(errno));
  1007. goto error;
  1008. }
  1009. return 0;
  1010. error:
  1011. if (sock_info->socket!=-1){
  1012. close(sock_info->socket);
  1013. sock_info->socket=-1;
  1014. }
  1015. return -1;
  1016. }
  1017. /* used internally by tcp_main_loop()
  1018. * tries to destroy a tcp connection (if it cannot it will force a timeout)
  1019. * Note: it's called _only_ from the tcp_main process */
  1020. static void tcpconn_destroy(struct tcp_connection* tcpconn)
  1021. {
  1022. int fd;
  1023. TCPCONN_LOCK; /*avoid races w/ tcp_send*/
  1024. if (atomic_dec_and_test(&tcpconn->refcnt)){
  1025. DBG("tcpconn_destroy: destroying connection %p, flags %04x\n",
  1026. tcpconn, tcpconn->flags);
  1027. fd=tcpconn->s;
  1028. #ifdef USE_TLS
  1029. /*FIXME: lock ->writelock ? */
  1030. if (tcpconn->type==PROTO_TLS)
  1031. tls_close(tcpconn, fd);
  1032. #endif
  1033. _tcpconn_rm(tcpconn);
  1034. close(fd);
  1035. (*tcp_connections_no)--;
  1036. }else{
  1037. /* force timeout */
  1038. tcpconn->timeout=get_ticks_raw();
  1039. tcpconn->state=S_CONN_BAD;
  1040. DBG("tcpconn_destroy: delaying (%p, flags %04x) ...\n",
  1041. tcpconn, tcpconn->flags);
  1042. }
  1043. TCPCONN_UNLOCK;
  1044. }
  1045. #ifdef SEND_FD_QUEUE
  1046. struct send_fd_info{
  1047. struct tcp_connection* tcp_conn;
  1048. ticks_t expire;
  1049. int unix_sock;
  1050. unsigned int retries; /* debugging */
  1051. };
  1052. struct tcp_send_fd_q{
  1053. struct send_fd_info* data; /* buffer */
  1054. struct send_fd_info* crt; /* pointer inside the buffer */
  1055. struct send_fd_info* end; /* points after the last valid position */
  1056. };
  1057. static struct tcp_send_fd_q send2child_q;
  1058. static int send_fd_queue_init(struct tcp_send_fd_q *q, unsigned int size)
  1059. {
  1060. q->data=pkg_malloc(size*sizeof(struct send_fd_info));
  1061. if (q->data==0){
  1062. LOG(L_ERR, "ERROR: send_fd_queue_init: out of memory\n");
  1063. return -1;
  1064. }
  1065. q->crt=&q->data[0];
  1066. q->end=&q->data[size];
  1067. return 0;
  1068. }
  1069. static void send_fd_queue_destroy(struct tcp_send_fd_q *q)
  1070. {
  1071. if (q->data){
  1072. pkg_free(q->data);
  1073. q->data=0;
  1074. q->crt=q->end=0;
  1075. }
  1076. }
  1077. static int init_send_fd_queues()
  1078. {
  1079. if (send_fd_queue_init(&send2child_q, SEND_FD_QUEUE_SIZE)!=0)
  1080. goto error;
  1081. return 0;
  1082. error:
  1083. LOG(L_ERR, "ERROR: init_send_fd_queues: init failed\n");
  1084. return -1;
  1085. }
  1086. static void destroy_send_fd_queues()
  1087. {
  1088. send_fd_queue_destroy(&send2child_q);
  1089. }
  1090. inline static int send_fd_queue_add( struct tcp_send_fd_q* q,
  1091. int unix_sock,
  1092. struct tcp_connection *t)
  1093. {
  1094. struct send_fd_info* tmp;
  1095. unsigned long new_size;
  1096. if (q->crt>=q->end){
  1097. new_size=q->end-&q->data[0];
  1098. if (new_size< MAX_SEND_FD_QUEUE_SIZE/2){
  1099. new_size*=2;
  1100. }else new_size=MAX_SEND_FD_QUEUE_SIZE;
  1101. if (q->crt>=&q->data[new_size]){
  1102. LOG(L_ERR, "ERROR: send_fd_queue_add: queue full: %ld/%ld\n",
  1103. (long)(q->crt-&q->data[0]-1), new_size);
  1104. goto error;
  1105. }
  1106. LOG(L_CRIT, "INFO: send_fd_queue: queue full: %ld, extending to %ld\n",
  1107. (long)(q->end-&q->data[0]), new_size);
  1108. tmp=pkg_realloc(q->data, new_size*sizeof(struct send_fd_info));
  1109. if (tmp==0){
  1110. LOG(L_ERR, "ERROR: send_fd_queue_add: out of memory\n");
  1111. goto error;
  1112. }
  1113. q->crt=(q->crt-&q->data[0])+tmp;
  1114. q->data=tmp;
  1115. q->end=&q->data[new_size];
  1116. }
  1117. q->crt->tcp_conn=t;
  1118. q->crt->unix_sock=unix_sock;
  1119. q->crt->expire=get_ticks_raw()+SEND_FD_QUEUE_TIMEOUT;
  1120. q->crt->retries=0;
  1121. q->crt++;
  1122. return 0;
  1123. error:
  1124. return -1;
  1125. }
  1126. inline static void send_fd_queue_run(struct tcp_send_fd_q* q)
  1127. {
  1128. struct send_fd_info* p;
  1129. struct send_fd_info* t;
  1130. for (p=t=&q->data[0]; p<q->crt; p++){
  1131. if (send_fd(p->unix_sock, &(p->tcp_conn),
  1132. sizeof(struct tcp_connection*), p->tcp_conn->s)<=0){
  1133. if ( ((errno==EAGAIN)||(errno==EWOULDBLOCK)) &&
  1134. ((s_ticks_t)(p->expire-get_ticks_raw())>0)){
  1135. /* leave in queue for a future try */
  1136. *t=*p;
  1137. t->retries++;
  1138. t++;
  1139. }else{
  1140. LOG(L_ERR, "ERROR: run_send_fd_queue: send_fd failed"
  1141. " on socket %d , queue entry %ld, retries %d,"
  1142. " connection %p, tcp socket %d, errno=%d (%s) \n",
  1143. p->unix_sock, (long)(p-&q->data[0]), p->retries,
  1144. p->tcp_conn, p->tcp_conn->s, errno,
  1145. strerror(errno));
  1146. tcpconn_destroy(p->tcp_conn);
  1147. }
  1148. }
  1149. }
  1150. q->crt=t;
  1151. }
  1152. #else
  1153. #define send_fd_queue_run(q)
  1154. #endif
  1155. /* handles io from a tcp child process
  1156. * params: tcp_c - pointer in the tcp_children array, to the entry for
  1157. * which an io event was detected
  1158. * fd_i - fd index in the fd_array (usefull for optimizing
  1159. * io_watch_deletes)
  1160. * returns: handle_* return convention: -1 on error, 0 on EAGAIN (no more
  1161. * io events queued), >0 on success. success/error refer only to
  1162. * the reads from the fd.
  1163. */
  1164. inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
  1165. {
  1166. struct tcp_connection* tcpconn;
  1167. long response[2];
  1168. int cmd;
  1169. int bytes;
  1170. if (tcp_c->unix_sock<=0){
  1171. /* (we can't have a fd==0, 0 is never closed )*/
  1172. LOG(L_CRIT, "BUG: handle_tcp_child: fd %d for %d "
  1173. "(pid %d, ser no %d)\n", tcp_c->unix_sock,
  1174. (int)(tcp_c-&tcp_children[0]), tcp_c->pid, tcp_c->proc_no);
  1175. goto error;
  1176. }
  1177. /* read until sizeof(response)
  1178. * (this is a SOCK_STREAM so read is not atomic) */
  1179. bytes=recv_all(tcp_c->unix_sock, response, sizeof(response), MSG_DONTWAIT);
  1180. if (bytes<(int)sizeof(response)){
  1181. if (bytes==0){
  1182. /* EOF -> bad, child has died */
  1183. DBG("DBG: handle_tcp_child: dead tcp child %d (pid %d, no %d)"
  1184. " (shutting down?)\n", (int)(tcp_c-&tcp_children[0]),
  1185. tcp_c->pid, tcp_c->proc_no );
  1186. /* don't listen on it any more */
  1187. io_watch_del(&io_h, tcp_c->unix_sock, fd_i, 0);
  1188. goto error; /* eof. so no more io here, it's ok to return error */
  1189. }else if (bytes<0){
  1190. /* EAGAIN is ok if we try to empty the buffer
  1191. * e.g.: SIGIO_RT overflow mode or EPOLL ET */
  1192. if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){
  1193. LOG(L_CRIT, "ERROR: handle_tcp_child: read from tcp child %ld "
  1194. " (pid %d, no %d) %s [%d]\n",
  1195. (long)(tcp_c-&tcp_children[0]), tcp_c->pid,
  1196. tcp_c->proc_no, strerror(errno), errno );
  1197. }else{
  1198. bytes=0;
  1199. }
  1200. /* try to ignore ? */
  1201. goto end;
  1202. }else{
  1203. /* should never happen */
  1204. LOG(L_CRIT, "BUG: handle_tcp_child: too few bytes received (%d)\n",
  1205. bytes );
  1206. bytes=0; /* something was read so there is no error; otoh if
  1207. receive_fd returned less then requested => the receive
  1208. buffer is empty => no more io queued on this fd */
  1209. goto end;
  1210. }
  1211. }
  1212. DBG("handle_tcp_child: reader response= %lx, %ld from %d \n",
  1213. response[0], response[1], (int)(tcp_c-&tcp_children[0]));
  1214. cmd=response[1];
  1215. tcpconn=(struct tcp_connection*)response[0];
  1216. if (tcpconn==0){
  1217. /* should never happen */
  1218. LOG(L_CRIT, "BUG: handle_tcp_child: null tcpconn pointer received"
  1219. " from tcp child %d (pid %d): %lx, %lx\n",
  1220. (int)(tcp_c-&tcp_children[0]), tcp_c->pid,
  1221. response[0], response[1]) ;
  1222. goto end;
  1223. }
  1224. switch(cmd){
  1225. case CONN_RELEASE:
  1226. tcp_c->busy--;
  1227. if (tcpconn->state==S_CONN_BAD){
  1228. tcpconn_destroy(tcpconn);
  1229. break;
  1230. }
  1231. /* update the timeout*/
  1232. tcpconn->timeout=get_ticks_raw()+tcp_con_lifetime;
  1233. tcpconn_put(tcpconn);
  1234. /* must be after the de-ref*/
  1235. io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
  1236. tcpconn->flags&=~F_CONN_REMOVED;
  1237. DBG("handle_tcp_child: CONN_RELEASE %p refcnt= %d\n",
  1238. tcpconn, atomic_get(&tcpconn->refcnt));
  1239. break;
  1240. case CONN_ERROR:
  1241. case CONN_DESTROY:
  1242. case CONN_EOF:
  1243. /* WARNING: this will auto-dec. refcnt! */
  1244. tcp_c->busy--;
  1245. /* main doesn't listen on it => we don't have to delete it
  1246. if (tcpconn->s!=-1)
  1247. io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
  1248. */
  1249. tcpconn_destroy(tcpconn); /* closes also the fd */
  1250. break;
  1251. default:
  1252. LOG(L_CRIT, "BUG: handle_tcp_child: unknown cmd %d"
  1253. " from tcp reader %d\n",
  1254. cmd, (int)(tcp_c-&tcp_children[0]));
  1255. }
  1256. end:
  1257. return bytes;
  1258. error:
  1259. return -1;
  1260. }
  1261. /* handles io from a "generic" ser process (get fd or new_fd from a tcp_send)
  1262. *
  1263. * params: p - pointer in the ser processes array (pt[]), to the entry for
  1264. * which an io event was detected
  1265. * fd_i - fd index in the fd_array (usefull for optimizing
  1266. * io_watch_deletes)
  1267. * returns: handle_* return convention:
  1268. * -1 on error reading from the fd,
  1269. * 0 on EAGAIN or when no more io events are queued
  1270. * (receive buffer empty),
  1271. * >0 on successfull reads from the fd (the receive buffer might
  1272. * be non-empty).
  1273. */
  1274. inline static int handle_ser_child(struct process_table* p, int fd_i)
  1275. {
  1276. struct tcp_connection* tcpconn;
  1277. long response[2];
  1278. int cmd;
  1279. int bytes;
  1280. int ret;
  1281. int fd;
  1282. ret=-1;
  1283. if (p->unix_sock<=0){
  1284. /* (we can't have a fd==0, 0 is never closed )*/
  1285. LOG(L_CRIT, "BUG: handle_ser_child: fd %d for %d "
  1286. "(pid %d)\n", p->unix_sock, (int)(p-&pt[0]), p->pid);
  1287. goto error;
  1288. }
  1289. /* get all bytes and the fd (if transmitted)
  1290. * (this is a SOCK_STREAM so read is not atomic) */
  1291. bytes=receive_fd(p->unix_sock, response, sizeof(response), &fd,
  1292. MSG_DONTWAIT);
  1293. if (bytes<(int)sizeof(response)){
  1294. /* too few bytes read */
  1295. if (bytes==0){
  1296. /* EOF -> bad, child has died */
  1297. DBG("DBG: handle_ser_child: dead child %d, pid %d"
  1298. " (shutting down?)\n", (int)(p-&pt[0]), p->pid);
  1299. /* don't listen on it any more */
  1300. io_watch_del(&io_h, p->unix_sock, fd_i, 0);
  1301. goto error; /* child dead => no further io events from it */
  1302. }else if (bytes<0){
  1303. /* EAGAIN is ok if we try to empty the buffer
  1304. * e.g: SIGIO_RT overflow mode or EPOLL ET */
  1305. if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){
  1306. LOG(L_CRIT, "ERROR: handle_ser_child: read from child %d "
  1307. "(pid %d): %s [%d]\n", (int)(p-&pt[0]), p->pid,
  1308. strerror(errno), errno);
  1309. ret=-1;
  1310. }else{
  1311. ret=0;
  1312. }
  1313. /* try to ignore ? */
  1314. goto end;
  1315. }else{
  1316. /* should never happen */
  1317. LOG(L_CRIT, "BUG: handle_ser_child: too few bytes received (%d)\n",
  1318. bytes );
  1319. ret=0; /* something was read so there is no error; otoh if
  1320. receive_fd returned less then requested => the receive
  1321. buffer is empty => no more io queued on this fd */
  1322. goto end;
  1323. }
  1324. }
  1325. ret=1; /* something was received, there might be more queued */
  1326. DBG("handle_ser_child: read response= %lx, %ld, fd %d from %d (%d)\n",
  1327. response[0], response[1], fd, (int)(p-&pt[0]), p->pid);
  1328. cmd=response[1];
  1329. tcpconn=(struct tcp_connection*)response[0];
  1330. if (tcpconn==0){
  1331. LOG(L_CRIT, "BUG: handle_ser_child: null tcpconn pointer received"
  1332. " from child %d (pid %d): %lx, %lx\n",
  1333. (int)(p-&pt[0]), p->pid, response[0], response[1]) ;
  1334. goto end;
  1335. }
  1336. switch(cmd){
  1337. case CONN_ERROR:
  1338. if (!(tcpconn->flags & F_CONN_REMOVED) && (tcpconn->s!=-1)){
  1339. io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
  1340. tcpconn->flags|=F_CONN_REMOVED;
  1341. }
  1342. tcpconn_destroy(tcpconn); /* will close also the fd */
  1343. break;
  1344. case CONN_GET_FD:
  1345. /* send the requested FD */
  1346. /* WARNING: take care of setting refcnt properly to
  1347. * avoid race condition */
  1348. if (send_fd(p->unix_sock, &tcpconn, sizeof(tcpconn),
  1349. tcpconn->s)<=0){
  1350. LOG(L_ERR, "ERROR: handle_ser_child: send_fd failed\n");
  1351. }
  1352. break;
  1353. case CONN_NEW:
  1354. /* update the fd in the requested tcpconn*/
  1355. /* WARNING: take care of setting refcnt properly to
  1356. * avoid race condition */
  1357. if (fd==-1){
  1358. LOG(L_CRIT, "BUG: handle_ser_child: CONN_NEW:"
  1359. " no fd received\n");
  1360. break;
  1361. }
  1362. (*tcp_connections_no)++;
  1363. tcpconn->s=fd;
  1364. /* add tcpconn to the list*/
  1365. tcpconn_add(tcpconn);
  1366. /* update the timeout*/
  1367. tcpconn->timeout=get_ticks_raw()+tcp_con_lifetime;
  1368. io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
  1369. tcpconn->flags&=~F_CONN_REMOVED;
  1370. break;
  1371. default:
  1372. LOG(L_CRIT, "BUG: handle_ser_child: unknown cmd %d\n", cmd);
  1373. }
  1374. end:
  1375. return ret;
  1376. error:
  1377. return -1;
  1378. }
  1379. /* sends a tcpconn + fd to a choosen child */
  1380. inline static int send2child(struct tcp_connection* tcpconn)
  1381. {
  1382. int i;
  1383. int min_busy;
  1384. int idx;
  1385. static int crt=0; /* current child */
  1386. int last;
  1387. min_busy=tcp_children[0].busy;
  1388. idx=0;
  1389. last=crt+tcp_children_no;
  1390. for (; crt<last; crt++){
  1391. i=crt%tcp_children_no;
  1392. if (!tcp_children[i].busy){
  1393. idx=i;
  1394. min_busy=0;
  1395. break;
  1396. }else if (min_busy>tcp_children[i].busy){
  1397. min_busy=tcp_children[i].busy;
  1398. idx=i;
  1399. }
  1400. }
  1401. crt=idx+1; /* next time we start with crt%tcp_children_no */
  1402. tcp_children[idx].busy++;
  1403. tcp_children[idx].n_reqs++;
  1404. if (min_busy){
  1405. DBG("WARNING: send2child: no free tcp receiver, "
  1406. " connection passed to the least busy one (%d)\n",
  1407. min_busy);
  1408. }
  1409. DBG("send2child: to tcp child %d %d(%d), %p\n", idx,
  1410. tcp_children[idx].proc_no,
  1411. tcp_children[idx].pid, tcpconn);
  1412. /* first make sure this child doesn't have pending request for
  1413. * tcp_main (to avoid a possible deadlock: e.g. child wants to
  1414. * send a release command, but the master fills its socket buffer
  1415. * with new connection commands => deadlock) */
  1416. /* answer tcp_send requests first */
  1417. while(handle_ser_child(&pt[tcp_children[idx].proc_no], -1)>0);
  1418. /* process tcp readers requests */
  1419. while(handle_tcp_child(&tcp_children[idx], -1)>0);
  1420. #ifdef SEND_FD_QUEUE
  1421. /* if queue full, try to queue the io */
  1422. if (send_fd(tcp_children[idx].unix_sock, &tcpconn, sizeof(tcpconn),
  1423. tcpconn->s)<=0){
  1424. if ((errno==EAGAIN)||(errno==EWOULDBLOCK)){
  1425. /* FIXME: remove after debugging */
  1426. LOG(L_CRIT, "INFO: tcp child %d, socket %d: queue full,"
  1427. " %d requests queued (total handled %d)\n",
  1428. idx, tcp_children[idx].unix_sock, min_busy,
  1429. tcp_children[idx].n_reqs-1);
  1430. if (send_fd_queue_add(&send2child_q, tcp_children[idx].unix_sock,
  1431. tcpconn)!=0){
  1432. LOG(L_ERR, "ERROR: send2child: queue send op. failed\n");
  1433. return -1;
  1434. }
  1435. }else{
  1436. LOG(L_ERR, "ERROR: send2child: send_fd failed\n");
  1437. return -1;
  1438. }
  1439. }
  1440. #else
  1441. if (send_fd(tcp_children[idx].unix_sock, &tcpconn, sizeof(tcpconn),
  1442. tcpconn->s)<=0){
  1443. LOG(L_ERR, "ERROR: send2child: send_fd failed\n");
  1444. return -1;
  1445. }
  1446. #endif
  1447. return 0;
  1448. }
  1449. /* handles a new connection, called internally by tcp_main_loop/handle_io.
  1450. * params: si - pointer to one of the tcp socket_info structures on which
  1451. * an io event was detected (connection attempt)
  1452. * returns: handle_* return convention: -1 on error, 0 on EAGAIN (no more
  1453. * io events queued), >0 on success. success/error refer only to
  1454. * the accept.
  1455. */
  1456. static inline int handle_new_connect(struct socket_info* si)
  1457. {
  1458. union sockaddr_union su;
  1459. struct tcp_connection* tcpconn;
  1460. socklen_t su_len;
  1461. int new_sock;
  1462. /* got a connection on r */
  1463. su_len=sizeof(su);
  1464. new_sock=accept(si->socket, &(su.s), &su_len);
  1465. if (new_sock==-1){
  1466. if ((errno==EAGAIN)||(errno==EWOULDBLOCK))
  1467. return 0;
  1468. LOG(L_ERR, "WARNING: handle_new_connect: error while accepting"
  1469. " connection(%d): %s\n", errno, strerror(errno));
  1470. return -1;
  1471. }
  1472. if (*tcp_connections_no>=tcp_max_connections){
  1473. LOG(L_ERR, "ERROR: maximum number of connections exceeded: %d/%d\n",
  1474. *tcp_connections_no, tcp_max_connections);
  1475. close(new_sock);
  1476. return 1; /* success, because the accept was succesfull */
  1477. }
  1478. if (init_sock_opt(new_sock)<0){
  1479. LOG(L_ERR, "ERROR: handle_new_connect: init_sock_opt failed\n");
  1480. close(new_sock);
  1481. return 1; /* success, because the accept was succesfull */
  1482. }
  1483. (*tcp_connections_no)++;
  1484. /* add socket to list */
  1485. tcpconn=tcpconn_new(new_sock, &su, &si->su, si, si->proto, S_CONN_ACCEPT);
  1486. if (tcpconn){
  1487. #ifdef TCP_PASS_NEW_CONNECTION_ON_DATA
  1488. io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
  1489. tcpconn->flags&=~F_CONN_REMOVED;
  1490. tcpconn_add(tcpconn);
  1491. #else
  1492. atomic_set(&tcpconn->refcnt, 1); /* safe, not yet available to the
  1493. outside world */
  1494. tcpconn_add(tcpconn);
  1495. DBG("handle_new_connect: new connection: %p %d flags: %04x\n",
  1496. tcpconn, tcpconn->s, tcpconn->flags);
  1497. /* pass it to a child */
  1498. if(send2child(tcpconn)<0){
  1499. LOG(L_ERR,"ERROR: handle_new_connect: no children "
  1500. "available\n");
  1501. tcpconn_destroy(tcpconn);
  1502. }
  1503. #endif
  1504. }else{ /*tcpconn==0 */
  1505. LOG(L_ERR, "ERROR: handle_new_connect: tcpconn_new failed, "
  1506. "closing socket\n");
  1507. close(new_sock);
  1508. (*tcp_connections_no)--;
  1509. }
  1510. return 1; /* accept() was succesfull */
  1511. }
  1512. /* handles an io event on one of the watched tcp connections
  1513. *
  1514. * params: tcpconn - pointer to the tcp_connection for which we have an io ev.
  1515. * fd_i - index in the fd_array table (needed for delete)
  1516. * returns: handle_* return convention, but on success it always returns 0
  1517. * (because it's one-shot, after a succesfull execution the fd is
  1518. * removed from tcp_main's watch fd list and passed to a child =>
  1519. * tcp_main is not interested in further io events that might be
  1520. * queued for this fd)
  1521. */
  1522. inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, int fd_i)
  1523. {
  1524. /* is refcnt!=0 really necessary?
  1525. * No, in fact it's a bug: I can have the following situation: a send only
  1526. * tcp connection used by n processes simultaneously => refcnt = n. In
  1527. * the same time I can have a read event and this situation is perfectly
  1528. * valid. -- andrei
  1529. */
  1530. #if 0
  1531. if ((tcpconn->refcnt!=0)){
  1532. /* FIXME: might be valid for sigio_rt iff fd flags are not cleared
  1533. * (there is a short window in which it could generate a sig
  1534. * that would be catched by tcp_main) */
  1535. LOG(L_CRIT, "BUG: handle_tcpconn_ev: io event on referenced"
  1536. " tcpconn (%p), refcnt=%d, fd=%d\n",
  1537. tcpconn, tcpconn->refcnt, tcpconn->s);
  1538. return -1;
  1539. }
  1540. #endif
  1541. /* pass it to child, so remove it from the io watch list */
  1542. DBG("handle_tcpconn_ev: data available on %p %d\n", tcpconn, tcpconn->s);
  1543. if (io_watch_del(&io_h, tcpconn->s, fd_i, 0)==-1) goto error;
  1544. tcpconn->flags|=F_CONN_REMOVED;
  1545. tcpconn_ref(tcpconn); /* refcnt ++ */
  1546. if (send2child(tcpconn)<0){
  1547. LOG(L_ERR,"ERROR: handle_tcpconn_ev: no children available\n");
  1548. tcpconn_destroy(tcpconn);
  1549. }
  1550. return 0; /* we are not interested in possibly queued io events,
  1551. the fd was either passed to a child, or closed */
  1552. error:
  1553. return -1;
  1554. }
  1555. /* generic handle io routine, it will call the appropiate
  1556. * handle_xxx() based on the fd_map type
  1557. *
  1558. * params: fm - pointer to a fd hash entry
  1559. * idx - index in the fd_array (or -1 if not known)
  1560. * return: -1 on error
  1561. * 0 on EAGAIN or when by some other way it is known that no more
  1562. * io events are queued on the fd (the receive buffer is empty).
  1563. * Usefull to detect when there are no more io events queued for
  1564. * sigio_rt, epoll_et, kqueue.
  1565. * >0 on successfull read from the fd (when there might be more io
  1566. * queued -- the receive buffer might still be non-empty)
  1567. */
  1568. inline static int handle_io(struct fd_map* fm, int idx)
  1569. {
  1570. int ret;
  1571. switch(fm->type){
  1572. case F_SOCKINFO:
  1573. ret=handle_new_connect((struct socket_info*)fm->data);
  1574. break;
  1575. case F_TCPCONN:
  1576. ret=handle_tcpconn_ev((struct tcp_connection*)fm->data, idx);
  1577. break;
  1578. case F_TCPCHILD:
  1579. ret=handle_tcp_child((struct tcp_child*)fm->data, idx);
  1580. break;
  1581. case F_PROC:
  1582. ret=handle_ser_child((struct process_table*)fm->data, idx);
  1583. break;
  1584. case F_NONE:
  1585. LOG(L_CRIT, "BUG: handle_io: empty fd map: %p {%d, %d, %p},"
  1586. " idx %d\n", fm, fm->fd, fm->type, fm->data, idx);
  1587. goto error;
  1588. default:
  1589. LOG(L_CRIT, "BUG: handle_io: uknown fd type %d\n", fm->type);
  1590. goto error;
  1591. }
  1592. return ret;
  1593. error:
  1594. return -1;
  1595. }
  1596. /* very inefficient for now - FIXME
  1597. * keep in sync with tcpconn_destroy, the "delete" part should be
  1598. * the same except for io_watch_del..
  1599. * Note: this function is called only from the tcp_main process with 1
  1600. * exception: on shutdown it's called also by the main ser process via
  1601. * cleanup() => with the ser shutdown exception, it cannot execute in parallel
  1602. * with tcpconn_add() or tcpconn_destroy()*/
  1603. static inline void tcpconn_timeout(int force)
  1604. {
  1605. static ticks_t prev_ticks=0;
  1606. struct tcp_connection *c, *next;
  1607. ticks_t ticks;
  1608. unsigned h;
  1609. int fd;
  1610. ticks=get_ticks_raw();
  1611. if (((ticks-prev_ticks)<TCPCONN_TIMEOUT_MIN_RUN) && !force) return;
  1612. prev_ticks=ticks;
  1613. TCPCONN_LOCK; /* fixme: we can lock only on delete IMO */
  1614. for(h=0; h<TCP_ID_HASH_SIZE; h++){
  1615. c=tcpconn_id_hash[h];
  1616. while(c){
  1617. next=c->id_next;
  1618. if (force ||((atomic_get(&c->refcnt)==0) &&
  1619. ((s_ticks_t)(ticks-c->timeout)>=0))){
  1620. if (!force)
  1621. DBG("tcpconn_timeout: timeout for hash=%d - %p"
  1622. " (%d > %d)\n", h, c, ticks, c->timeout);
  1623. if (c->s>0 && is_tcp_main){
  1624. /* we cannot close or remove the fd if we are not in the
  1625. * tcp main proc.*/
  1626. fd=c->s;
  1627. if (!(c->flags & F_CONN_REMOVED)){
  1628. io_watch_del(&io_h, fd, -1, IO_FD_CLOSING);
  1629. c->flags|=F_CONN_REMOVED;
  1630. }
  1631. }else{
  1632. fd=-1;
  1633. }
  1634. #ifdef USE_TLS
  1635. if (c->type==PROTO_TLS)
  1636. tls_close(c, fd);
  1637. #endif
  1638. _tcpconn_rm(c);
  1639. if (fd>0) {
  1640. close(fd);
  1641. }
  1642. (*tcp_connections_no)--;
  1643. }
  1644. c=next;
  1645. }
  1646. }
  1647. TCPCONN_UNLOCK;
  1648. }
  1649. /* tcp main loop */
  1650. void tcp_main_loop()
  1651. {
  1652. struct socket_info* si;
  1653. int r;
  1654. is_tcp_main=1; /* mark this process as tcp main */
  1655. tcp_main_max_fd_no=get_max_open_fds();
  1656. /* init send fd queues (here because we want mem. alloc only in the tcp
  1657. * process */
  1658. #ifdef SEND_FD_QUEUE
  1659. if (init_send_fd_queues()<0){
  1660. LOG(L_CRIT, "ERROR: init_tcp: could not init send fd queues\n");
  1661. goto error;
  1662. }
  1663. #endif
  1664. /* init io_wait (here because we want the memory allocated only in
  1665. * the tcp_main process) */
  1666. if (init_io_wait(&io_h, tcp_main_max_fd_no, tcp_poll_method)<0)
  1667. goto error;
  1668. /* init: start watching all the fds*/
  1669. /* add all the sockets we listen on for connections */
  1670. for (si=tcp_listen; si; si=si->next){
  1671. if ((si->proto==PROTO_TCP) &&(si->socket!=-1)){
  1672. if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si)<0){
  1673. LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
  1674. "listen socket to the fd list\n");
  1675. goto error;
  1676. }
  1677. }else{
  1678. LOG(L_CRIT, "BUG: tcp_main_loop: non tcp address in tcp_listen\n");
  1679. }
  1680. }
  1681. #ifdef USE_TLS
  1682. if (!tls_disable && tls_loaded()){
  1683. for (si=tls_listen; si; si=si->next){
  1684. if ((si->proto==PROTO_TLS) && (si->socket!=-1)){
  1685. if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si)<0){
  1686. LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
  1687. "tls listen socket to the fd list\n");
  1688. goto error;
  1689. }
  1690. }else{
  1691. LOG(L_CRIT, "BUG: tcp_main_loop: non tls address"
  1692. " in tls_listen\n");
  1693. }
  1694. }
  1695. }
  1696. #endif
  1697. /* add all the unix sockets used for communcation with other ser processes
  1698. * (get fd, new connection a.s.o) */
  1699. for (r=1; r<process_no; r++){
  1700. if (pt[r].unix_sock>0) /* we can't have 0, we never close it!*/
  1701. if (io_watch_add(&io_h, pt[r].unix_sock, F_PROC, &pt[r])<0){
  1702. LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
  1703. "process %d unix socket to the fd list\n", r);
  1704. goto error;
  1705. }
  1706. }
  1707. /* add all the unix sokets used for communication with the tcp childs */
  1708. for (r=0; r<tcp_children_no; r++){
  1709. if (tcp_children[r].unix_sock>0)/*we can't have 0, we never close it!*/
  1710. if (io_watch_add(&io_h, tcp_children[r].unix_sock, F_TCPCHILD,
  1711. &tcp_children[r]) <0){
  1712. LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
  1713. "tcp child %d unix socket to the fd list\n", r);
  1714. goto error;
  1715. }
  1716. }
  1717. /* main loop */
  1718. switch(io_h.poll_method){
  1719. case POLL_POLL:
  1720. while(1){
  1721. /* wait and process IO */
  1722. io_wait_loop_poll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1723. send_fd_queue_run(&send2child_q); /* then new io */
  1724. /* remove old connections */
  1725. tcpconn_timeout(0);
  1726. }
  1727. break;
  1728. #ifdef HAVE_SELECT
  1729. case POLL_SELECT:
  1730. while(1){
  1731. io_wait_loop_select(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1732. send_fd_queue_run(&send2child_q); /* then new io */
  1733. tcpconn_timeout(0);
  1734. }
  1735. break;
  1736. #endif
  1737. #ifdef HAVE_SIGIO_RT
  1738. case POLL_SIGIO_RT:
  1739. while(1){
  1740. io_wait_loop_sigio_rt(&io_h, TCP_MAIN_SELECT_TIMEOUT);
  1741. send_fd_queue_run(&send2child_q); /* then new io */
  1742. tcpconn_timeout(0);
  1743. }
  1744. break;
  1745. #endif
  1746. #ifdef HAVE_EPOLL
  1747. case POLL_EPOLL_LT:
  1748. while(1){
  1749. io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1750. send_fd_queue_run(&send2child_q); /* then new io */
  1751. tcpconn_timeout(0);
  1752. }
  1753. break;
  1754. case POLL_EPOLL_ET:
  1755. while(1){
  1756. io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 1);
  1757. send_fd_queue_run(&send2child_q); /* then new io */
  1758. tcpconn_timeout(0);
  1759. }
  1760. break;
  1761. #endif
  1762. #ifdef HAVE_KQUEUE
  1763. case POLL_KQUEUE:
  1764. while(1){
  1765. io_wait_loop_kqueue(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1766. send_fd_queue_run(&send2child_q); /* then new io */
  1767. tcpconn_timeout(0);
  1768. }
  1769. break;
  1770. #endif
  1771. #ifdef HAVE_DEVPOLL
  1772. case POLL_DEVPOLL:
  1773. while(1){
  1774. io_wait_loop_devpoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1775. send_fd_queue_run(&send2child_q); /* then new io */
  1776. tcpconn_timeout(0);
  1777. }
  1778. break;
  1779. #endif
  1780. default:
  1781. LOG(L_CRIT, "BUG: tcp_main_loop: no support for poll method "
  1782. " %s (%d)\n",
  1783. poll_method_name(io_h.poll_method), io_h.poll_method);
  1784. goto error;
  1785. }
  1786. error:
  1787. #ifdef SEND_FD_QUEUE
  1788. destroy_send_fd_queues();
  1789. #endif
  1790. destroy_io_wait(&io_h);
  1791. LOG(L_CRIT, "ERROR: tcp_main_loop: exiting...");
  1792. exit(-1);
  1793. }
  1794. /* cleanup before exit */
  1795. void destroy_tcp()
  1796. {
  1797. if (tcpconn_id_hash){
  1798. if (tcpconn_lock)
  1799. TCPCONN_UNLOCK; /* hack: force-unlock the tcp lock in case
  1800. some process was terminated while holding
  1801. it; this will allow an almost gracious
  1802. shutdown */
  1803. tcpconn_timeout(1); /* force close/expire for all active tcpconns*/
  1804. shm_free(tcpconn_id_hash);
  1805. tcpconn_id_hash=0;
  1806. }
  1807. if (tcp_connections_no){
  1808. shm_free(tcp_connections_no);
  1809. tcp_connections_no=0;
  1810. }
  1811. if (connection_id){
  1812. shm_free(connection_id);
  1813. connection_id=0;
  1814. }
  1815. if (tcpconn_aliases_hash){
  1816. shm_free(tcpconn_aliases_hash);
  1817. tcpconn_aliases_hash=0;
  1818. }
  1819. if (tcpconn_lock){
  1820. lock_destroy(tcpconn_lock);
  1821. lock_dealloc((void*)tcpconn_lock);
  1822. tcpconn_lock=0;
  1823. }
  1824. if (tcp_children){
  1825. pkg_free(tcp_children);
  1826. tcp_children=0;
  1827. }
  1828. }
  1829. int init_tcp()
  1830. {
  1831. char* poll_err;
  1832. /* init lock */
  1833. tcpconn_lock=lock_alloc();
  1834. if (tcpconn_lock==0){
  1835. LOG(L_CRIT, "ERROR: init_tcp: could not alloc lock\n");
  1836. goto error;
  1837. }
  1838. if (lock_init(tcpconn_lock)==0){
  1839. LOG(L_CRIT, "ERROR: init_tcp: could not init lock\n");
  1840. lock_dealloc((void*)tcpconn_lock);
  1841. tcpconn_lock=0;
  1842. goto error;
  1843. }
  1844. /* init globals */
  1845. tcp_connections_no=shm_malloc(sizeof(int));
  1846. if (tcp_connections_no==0){
  1847. LOG(L_CRIT, "ERROR: init_tcp: could not alloc globals\n");
  1848. goto error;
  1849. }
  1850. *tcp_connections_no=0;
  1851. connection_id=shm_malloc(sizeof(int));
  1852. if (connection_id==0){
  1853. LOG(L_CRIT, "ERROR: init_tcp: could not alloc globals\n");
  1854. goto error;
  1855. }
  1856. *connection_id=1;
  1857. /* alloc hashtables*/
  1858. tcpconn_aliases_hash=(struct tcp_conn_alias**)
  1859. shm_malloc(TCP_ALIAS_HASH_SIZE* sizeof(struct tcp_conn_alias*));
  1860. if (tcpconn_aliases_hash==0){
  1861. LOG(L_CRIT, "ERROR: init_tcp: could not alloc address hashtable\n");
  1862. goto error;
  1863. }
  1864. tcpconn_id_hash=(struct tcp_connection**)shm_malloc(TCP_ID_HASH_SIZE*
  1865. sizeof(struct tcp_connection*));
  1866. if (tcpconn_id_hash==0){
  1867. LOG(L_CRIT, "ERROR: init_tcp: could not alloc id hashtable\n");
  1868. goto error;
  1869. }
  1870. /* init hashtables*/
  1871. memset((void*)tcpconn_aliases_hash, 0,
  1872. TCP_ALIAS_HASH_SIZE * sizeof(struct tcp_conn_alias*));
  1873. memset((void*)tcpconn_id_hash, 0,
  1874. TCP_ID_HASH_SIZE * sizeof(struct tcp_connection*));
  1875. /* fix config variables */
  1876. if (tcp_connect_timeout<0)
  1877. tcp_connect_timeout=DEFAULT_TCP_CONNECT_TIMEOUT;
  1878. if (tcp_send_timeout<0)
  1879. tcp_send_timeout=DEFAULT_TCP_SEND_TIMEOUT;
  1880. if (tcp_con_lifetime<0){
  1881. /* set to max value (~ 1/2 MAX_INT) */
  1882. tcp_con_lifetime=MAX_TCP_CON_LIFETIME;
  1883. }else{
  1884. if ((unsigned)tcp_con_lifetime >
  1885. (unsigned)TICKS_TO_S(MAX_TCP_CON_LIFETIME)){
  1886. LOG(L_WARN, "init_tcp: tcp_con_lifetime too big (%u s), "
  1887. " the maximum value is %u\n", tcp_con_lifetime,
  1888. TICKS_TO_S(MAX_TCP_CON_LIFETIME));
  1889. tcp_con_lifetime=MAX_TCP_CON_LIFETIME;
  1890. }else{
  1891. tcp_con_lifetime=S_TO_TICKS(tcp_con_lifetime);
  1892. }
  1893. }
  1894. poll_err=check_poll_method(tcp_poll_method);
  1895. /* set an appropriate poll method */
  1896. if (poll_err || (tcp_poll_method==0)){
  1897. tcp_poll_method=choose_poll_method();
  1898. if (poll_err){
  1899. LOG(L_ERR, "ERROR: init_tcp: %s, using %s instead\n",
  1900. poll_err, poll_method_name(tcp_poll_method));
  1901. }else{
  1902. LOG(L_INFO, "init_tcp: using %s as the io watch method"
  1903. " (auto detected)\n", poll_method_name(tcp_poll_method));
  1904. }
  1905. }else{
  1906. LOG(L_INFO, "init_tcp: using %s io watch method (config)\n",
  1907. poll_method_name(tcp_poll_method));
  1908. }
  1909. return 0;
  1910. error:
  1911. /* clean-up */
  1912. destroy_tcp();
  1913. return -1;
  1914. }
  1915. #ifdef TCP_CHILD_NON_BLOCKING
  1916. /* returns -1 on error */
  1917. static int set_non_blocking(int s)
  1918. {
  1919. int flags;
  1920. /* non-blocking */
  1921. flags=fcntl(s, F_GETFL);
  1922. if (flags==-1){
  1923. LOG(L_ERR, "ERROR: set_non_blocking: fnctl failed: (%d) %s\n",
  1924. errno, strerror(errno));
  1925. goto error;
  1926. }
  1927. if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){
  1928. LOG(L_ERR, "ERROR: set_non_blocking: fcntl: set non-blocking failed:"
  1929. " (%d) %s\n", errno, strerror(errno));
  1930. goto error;
  1931. }
  1932. return 0;
  1933. error:
  1934. return -1;
  1935. }
  1936. #endif
  1937. /* returns -1 on error, 0 on success */
  1938. int tcp_fix_child_sockets(int* fd)
  1939. {
  1940. #ifdef TCP_CHILD_NON_BLOCKING
  1941. if ((set_non_blocking(fd[0])<0) ||
  1942. (set_non_blocking(fd[1])<0)){
  1943. return -1;
  1944. }
  1945. #endif
  1946. return 0;
  1947. }
  1948. /* starts the tcp processes */
  1949. int tcp_init_children()
  1950. {
  1951. int r;
  1952. int reader_fd_1; /* for comm. with the tcp children read */
  1953. pid_t pid;
  1954. struct socket_info *si;
  1955. /* estimate max fd. no:
  1956. * 1 tcp send unix socket/all_proc,
  1957. * + 1 udp sock/udp proc + 1 tcp_child sock/tcp child*
  1958. * + no_listen_tcp */
  1959. for(r=0, si=tcp_listen; si; si=si->next, r++);
  1960. #ifdef USE_TLS
  1961. if (! tls_disable)
  1962. for (si=tls_listen; si; si=si->next, r++);
  1963. #endif
  1964. register_fds(r+tcp_max_connections+get_max_procs()-1 /* tcp main */);
  1965. #if 0
  1966. tcp_max_fd_no=get_max_procs()*2 +r-1 /* timer */ +3; /* stdin/out/err*/
  1967. /* max connections can be temporarily exceeded with estimated_process_count
  1968. * - tcp_main (tcpconn_connect called simultaneously in all all the
  1969. * processes) */
  1970. tcp_max_fd_no+=tcp_max_connections+get_max_procs()-1 /* tcp main */;
  1971. #endif
  1972. /* alloc the children array */
  1973. tcp_children=pkg_malloc(sizeof(struct tcp_child)*tcp_children_no);
  1974. if (tcp_children==0){
  1975. LOG(L_ERR, "ERROR: tcp_init_children: out of memory\n");
  1976. goto error;
  1977. }
  1978. /* create the tcp sock_info structures */
  1979. /* copy the sockets --moved to main_loop*/
  1980. /* fork children & create the socket pairs*/
  1981. for(r=0; r<tcp_children_no; r++){
  1982. child_rank++;
  1983. pid=fork_tcp_process(child_rank, "tcp receiver", r, &reader_fd_1);
  1984. if (pid<0){
  1985. LOG(L_ERR, "ERROR: tcp_main: fork failed: %s\n",
  1986. strerror(errno));
  1987. goto error;
  1988. }else if (pid>0){
  1989. /* parent */
  1990. }else{
  1991. /* child */
  1992. bind_address=0; /* force a SEGFAULT if someone uses a non-init.
  1993. bind address on tcp */
  1994. tcp_receive_loop(reader_fd_1);
  1995. }
  1996. }
  1997. return 0;
  1998. error:
  1999. return -1;
  2000. }
  2001. void tcp_get_info(struct tcp_gen_info *ti)
  2002. {
  2003. ti->tcp_readers=tcp_children_no;
  2004. ti->tcp_max_connections=tcp_max_connections;
  2005. ti->tcp_connections_no=*tcp_connections_no;
  2006. }
  2007. #endif