netengine.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  1. /*
  2. * Copyright (C) 2011, 2012, 2013 Citrix Systems
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include "mainrelay.h"
  31. //////////// Barrier for the threads //////////////
  32. #if !defined(TURN_NO_THREAD_BARRIERS)
  33. static unsigned int barrier_count = 0;
  34. static pthread_barrier_t barrier;
  35. #endif
  36. //////////////////////////////////////////////
  37. #define get_real_general_relay_servers_number() (turn_params.general_relay_servers_number > 1 ? turn_params.general_relay_servers_number : 1)
  38. #define get_real_udp_relay_servers_number() (turn_params.udp_relay_servers_number > 1 ? turn_params.udp_relay_servers_number : 1)
  39. static struct relay_server *general_relay_servers[1+((turnserver_id)-1)];
  40. static struct relay_server *udp_relay_servers[1+((turnserver_id)-1)];
  41. //////////////////////////////////////////////
  42. static void run_events(struct event_base *eb, ioa_engine_handle e);
  43. static void setup_relay_server(struct relay_server *rs, ioa_engine_handle e, int to_set_rfc5780);
  44. /////////////// BARRIERS ///////////////////
  45. #if !defined(PTHREAD_BARRIER_SERIAL_THREAD)
  46. #define PTHREAD_BARRIER_SERIAL_THREAD (-1)
  47. #endif
  48. static void barrier_wait_func(const char* func, int line)
  49. {
  50. #if !defined(TURN_NO_THREAD_BARRIERS)
  51. int br = 0;
  52. do {
  53. br = pthread_barrier_wait(&barrier);
  54. if ((br < 0)&&(br != PTHREAD_BARRIER_SERIAL_THREAD)) {
  55. int err = errno;
  56. perror("barrier wait");
  57. printf("%s:%s:%d: %d\n", __FUNCTION__, func,line,err);
  58. }
  59. } while (((br < 0)&&(br != PTHREAD_BARRIER_SERIAL_THREAD)) && (errno == EINTR));
  60. #else
  61. UNUSED_ARG(func);
  62. UNUSED_ARG(line);
  63. sleep(5);
  64. #endif
  65. }
  66. #define barrier_wait() barrier_wait_func(__FUNCTION__,__LINE__)
  67. /////////////// Bandwidth //////////////////
  68. static pthread_mutex_t mutex_bps;
  69. static band_limit_t allocate_bps(band_limit_t bps, int positive)
  70. {
  71. band_limit_t ret = 0;
  72. if(bps>0) {
  73. pthread_mutex_lock(&mutex_bps);
  74. if(positive) {
  75. if(!(turn_params.bps_capacity)) {
  76. ret = bps;
  77. turn_params.bps_capacity_allocated += ret;
  78. } else if(turn_params.bps_capacity_allocated < turn_params.bps_capacity) {
  79. band_limit_t reserve = turn_params.bps_capacity - turn_params.bps_capacity_allocated;
  80. if(reserve <= bps) {
  81. ret = reserve;
  82. turn_params.bps_capacity_allocated = turn_params.bps_capacity;
  83. } else {
  84. ret = bps;
  85. turn_params.bps_capacity_allocated += ret;
  86. }
  87. }
  88. } else {
  89. if(turn_params.bps_capacity_allocated >= bps) {
  90. turn_params.bps_capacity_allocated -= bps;
  91. } else {
  92. turn_params.bps_capacity_allocated = 0;
  93. }
  94. }
  95. pthread_mutex_unlock(&mutex_bps);
  96. }
  97. return ret;
  98. }
  99. band_limit_t get_bps_capacity_allocated(void)
  100. {
  101. band_limit_t ret = 0;
  102. pthread_mutex_lock(&mutex_bps);
  103. ret = turn_params.bps_capacity_allocated;
  104. pthread_mutex_unlock(&mutex_bps);
  105. return ret;
  106. }
  107. band_limit_t get_bps_capacity(void)
  108. {
  109. band_limit_t ret = 0;
  110. pthread_mutex_lock(&mutex_bps);
  111. ret = turn_params.bps_capacity;
  112. pthread_mutex_unlock(&mutex_bps);
  113. return ret;
  114. }
  115. void set_bps_capacity(band_limit_t value)
  116. {
  117. pthread_mutex_lock(&mutex_bps);
  118. turn_params.bps_capacity = value;
  119. pthread_mutex_unlock(&mutex_bps);
  120. }
  121. band_limit_t get_max_bps(void)
  122. {
  123. band_limit_t ret = 0;
  124. pthread_mutex_lock(&mutex_bps);
  125. ret = turn_params.max_bps;
  126. pthread_mutex_unlock(&mutex_bps);
  127. return ret;
  128. }
  129. void set_max_bps(band_limit_t value)
  130. {
  131. pthread_mutex_lock(&mutex_bps);
  132. turn_params.max_bps = value;
  133. pthread_mutex_unlock(&mutex_bps);
  134. }
  135. /////////////// AUX SERVERS ////////////////
  136. static void add_aux_server_list(const char *saddr, turn_server_addrs_list_t *list)
  137. {
  138. if(saddr && list) {
  139. ioa_addr addr;
  140. if(make_ioa_addr_from_full_string((const u08bits*)saddr, 0, &addr)!=0) {
  141. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong full address format: %s\n",saddr);
  142. } else {
  143. list->addrs = (ioa_addr*)turn_realloc(list->addrs,0,sizeof(ioa_addr)*(list->size+1));
  144. addr_cpy(&(list->addrs[(list->size)++]),&addr);
  145. {
  146. u08bits s[1025];
  147. addr_to_string(&addr, s);
  148. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Aux server: %s\n",s);
  149. }
  150. }
  151. }
  152. }
  153. void add_aux_server(const char *saddr)
  154. {
  155. add_aux_server_list(saddr,&turn_params.aux_servers_list);
  156. }
  157. /////////////// ALTERNATE SERVERS ////////////////
  158. static void add_alt_server(const char *saddr, int default_port, turn_server_addrs_list_t *list)
  159. {
  160. if(saddr && list) {
  161. ioa_addr addr;
  162. turn_mutex_lock(&(list->m));
  163. if(make_ioa_addr_from_full_string((const u08bits*)saddr, default_port, &addr)!=0) {
  164. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong IP address format: %s\n",saddr);
  165. } else {
  166. list->addrs = (ioa_addr*)turn_realloc(list->addrs,0,sizeof(ioa_addr)*(list->size+1));
  167. addr_cpy(&(list->addrs[(list->size)++]),&addr);
  168. {
  169. u08bits s[1025];
  170. addr_to_string(&addr, s);
  171. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Alternate server added: %s\n",s);
  172. }
  173. }
  174. turn_mutex_unlock(&(list->m));
  175. }
  176. }
  177. static void del_alt_server(const char *saddr, int default_port, turn_server_addrs_list_t *list)
  178. {
  179. if(saddr && list && list->size && list->addrs) {
  180. ioa_addr addr;
  181. turn_mutex_lock(&(list->m));
  182. if(make_ioa_addr_from_full_string((const u08bits*)saddr, default_port, &addr)!=0) {
  183. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong IP address format: %s\n",saddr);
  184. } else {
  185. size_t i;
  186. int found = 0;
  187. for(i=0;i<list->size;++i) {
  188. if(addr_eq(&(list->addrs[i]),&addr)) {
  189. found = 1;
  190. break;
  191. }
  192. }
  193. if(found) {
  194. size_t j;
  195. ioa_addr *new_addrs = (ioa_addr*)turn_malloc(sizeof(ioa_addr)*(list->size-1));
  196. for(j=0;j<i;++j) {
  197. addr_cpy(&(new_addrs[j]),&(list->addrs[j]));
  198. }
  199. for(j=i;j<list->size-1;++j) {
  200. addr_cpy(&(new_addrs[j]),&(list->addrs[j+1]));
  201. }
  202. turn_free(list->addrs,0);
  203. list->addrs = new_addrs;
  204. list->size -= 1;
  205. {
  206. u08bits s[1025];
  207. addr_to_string(&addr, s);
  208. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Alternate server removed: %s\n",s);
  209. }
  210. del_alt_server(saddr, default_port, list);
  211. }
  212. }
  213. turn_mutex_unlock(&(list->m));
  214. }
  215. }
  216. void add_alternate_server(const char *saddr)
  217. {
  218. add_alt_server(saddr,DEFAULT_STUN_PORT,&turn_params.alternate_servers_list);
  219. }
  220. void del_alternate_server(const char *saddr)
  221. {
  222. del_alt_server(saddr,DEFAULT_STUN_PORT,&turn_params.alternate_servers_list);
  223. }
  224. void add_tls_alternate_server(const char *saddr)
  225. {
  226. add_alt_server(saddr,DEFAULT_STUN_TLS_PORT,&turn_params.tls_alternate_servers_list);
  227. }
  228. void del_tls_alternate_server(const char *saddr)
  229. {
  230. del_alt_server(saddr,DEFAULT_STUN_TLS_PORT,&turn_params.tls_alternate_servers_list);
  231. }
  232. //////////////////////////////////////////////////
  233. void add_listener_addr(const char* addr) {
  234. ioa_addr baddr;
  235. if(make_ioa_addr((const u08bits*)addr,0,&baddr)<0) {
  236. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,"Cannot add a listener address: %s\n",addr);
  237. } else {
  238. char sbaddr[129];
  239. addr_to_string_no_port(&baddr,(u08bits*)sbaddr);
  240. size_t i = 0;
  241. for(i=0;i<turn_params.listener.addrs_number;++i) {
  242. if(addr_eq(turn_params.listener.encaddrs[turn_params.listener.addrs_number-1],&baddr)) {
  243. return;
  244. }
  245. }
  246. ++turn_params.listener.addrs_number;
  247. ++turn_params.listener.services_number;
  248. turn_params.listener.addrs = (char**)turn_realloc(turn_params.listener.addrs, 0, sizeof(char*)*turn_params.listener.addrs_number);
  249. turn_params.listener.addrs[turn_params.listener.addrs_number-1]=turn_strdup(sbaddr);
  250. turn_params.listener.encaddrs = (ioa_addr**)turn_realloc(turn_params.listener.encaddrs, 0, sizeof(ioa_addr*)*turn_params.listener.addrs_number);
  251. turn_params.listener.encaddrs[turn_params.listener.addrs_number-1]=(ioa_addr*)turn_malloc(sizeof(ioa_addr));
  252. addr_cpy(turn_params.listener.encaddrs[turn_params.listener.addrs_number-1],&baddr);
  253. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Listener address to use: %s\n",sbaddr);
  254. }
  255. }
  256. int add_relay_addr(const char* addr) {
  257. ioa_addr baddr;
  258. if(make_ioa_addr((const u08bits*)addr,0,&baddr)<0) {
  259. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,"Cannot add a relay address: %s\n",addr);
  260. return -1;
  261. } else {
  262. char sbaddr[129];
  263. addr_to_string_no_port(&baddr,(u08bits*)sbaddr);
  264. size_t i = 0;
  265. for(i=0;i<turn_params.relays_number;++i) {
  266. if(!strcmp(turn_params.relay_addrs[turn_params.relays_number-1],sbaddr)) {
  267. return 0;
  268. }
  269. }
  270. ++turn_params.relays_number;
  271. turn_params.relay_addrs = (char**)turn_realloc(turn_params.relay_addrs, 0, sizeof(char*)*turn_params.relays_number);
  272. turn_params.relay_addrs[turn_params.relays_number-1]=turn_strdup(sbaddr);
  273. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Relay address to use: %s\n",sbaddr);
  274. return 1;
  275. }
  276. }
  277. static void allocate_relay_addrs_ports(void) {
  278. int i;
  279. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Wait for relay ports initialization...\n");
  280. for(i=0;i<(int)turn_params.relays_number;i++) {
  281. ioa_addr baddr;
  282. if(make_ioa_addr((const u08bits*)turn_params.relay_addrs[i],0,&baddr)>=0) {
  283. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, " relay %s initialization...\n",turn_params.relay_addrs[i]);
  284. turnipports_add_ip(STUN_ATTRIBUTE_TRANSPORT_UDP_VALUE, &baddr);
  285. turnipports_add_ip(STUN_ATTRIBUTE_TRANSPORT_TCP_VALUE, &baddr);
  286. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, " relay %s initialization done\n",turn_params.relay_addrs[i]);
  287. }
  288. }
  289. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Relay ports initialization done\n");
  290. }
  291. //////////////////////////////////////////////////
  292. // communications between listener and relays ==>>
  293. static int handle_relay_message(relay_server_handle rs, struct message_to_relay *sm);
  294. void send_auth_message_to_auth_server(struct auth_message *am)
  295. {
  296. struct evbuffer *output = bufferevent_get_output(turn_params.authserver.out_buf);
  297. if(evbuffer_add(output,am,sizeof(struct auth_message))<0) {
  298. fprintf(stderr,"%s: Weird buffer error\n",__FUNCTION__);
  299. }
  300. }
  301. static void auth_server_receive_message(struct bufferevent *bev, void *ptr)
  302. {
  303. UNUSED_ARG(ptr);
  304. struct auth_message am;
  305. int n = 0;
  306. struct evbuffer *input = bufferevent_get_input(bev);
  307. while ((n = evbuffer_remove(input, &am, sizeof(struct auth_message))) > 0) {
  308. if (n != sizeof(struct auth_message)) {
  309. fprintf(stderr,"%s: Weird buffer error: size=%d\n",__FUNCTION__,n);
  310. continue;
  311. }
  312. if(am.ct == TURN_CREDENTIALS_SHORT_TERM) {
  313. st_password_t pwd;
  314. am.in_oauth = 0;
  315. am.out_oauth = 0;
  316. if(get_user_pwd(am.username,pwd)<0) {
  317. am.success = 0;
  318. } else {
  319. ns_bcopy(pwd,am.pwd,sizeof(st_password_t));
  320. am.success = 1;
  321. }
  322. } else {
  323. hmackey_t key;
  324. if(get_user_key(am.in_oauth,&(am.out_oauth),&(am.max_session_time),am.username,am.realm,key,am.in_buffer.nbh)<0) {
  325. am.success = 0;
  326. } else {
  327. ns_bcopy(key,am.key,sizeof(hmackey_t));
  328. am.success = 1;
  329. }
  330. }
  331. size_t dest = am.id;
  332. struct evbuffer *output = NULL;
  333. if(dest>=TURNSERVER_ID_BOUNDARY_BETWEEN_TCP_AND_UDP) {
  334. dest -= TURNSERVER_ID_BOUNDARY_BETWEEN_TCP_AND_UDP;
  335. if(dest >= get_real_udp_relay_servers_number()) {
  336. TURN_LOG_FUNC(
  337. TURN_LOG_LEVEL_ERROR,
  338. "%s: Too large UDP relay number: %d\n",
  339. __FUNCTION__,(int)dest);
  340. } else if(!(udp_relay_servers[dest])) {
  341. TURN_LOG_FUNC(
  342. TURN_LOG_LEVEL_ERROR,
  343. "%s: Wrong UDP relay number: %d, total %d\n",
  344. __FUNCTION__,(int)dest, (int)get_real_udp_relay_servers_number());
  345. } else {
  346. output = bufferevent_get_output(udp_relay_servers[dest]->auth_out_buf);
  347. }
  348. } else {
  349. if(dest >= get_real_general_relay_servers_number()) {
  350. TURN_LOG_FUNC(
  351. TURN_LOG_LEVEL_ERROR,
  352. "%s: Too large general relay number: %d, total %d\n",
  353. __FUNCTION__,(int)dest,(int)get_real_general_relay_servers_number());
  354. } else if(!(general_relay_servers[dest])) {
  355. TURN_LOG_FUNC(
  356. TURN_LOG_LEVEL_ERROR,
  357. "%s: Wrong general relay number: %d, total %d\n",
  358. __FUNCTION__,(int)dest,(int)get_real_general_relay_servers_number());
  359. } else {
  360. output = bufferevent_get_output(general_relay_servers[dest]->auth_out_buf);
  361. }
  362. }
  363. if(output)
  364. evbuffer_add(output,&am,sizeof(struct auth_message));
  365. else {
  366. ioa_network_buffer_delete(NULL, am.in_buffer.nbh);
  367. am.in_buffer.nbh = NULL;
  368. }
  369. }
  370. }
  371. static int send_socket_to_general_relay(ioa_engine_handle e, struct message_to_relay *sm)
  372. {
  373. struct relay_server *rdest = sm->relay_server;
  374. if(!rdest) {
  375. size_t dest = (hash_int32(addr_get_port(&(sm->m.sm.nd.src_addr)))) % get_real_general_relay_servers_number();
  376. rdest = general_relay_servers[dest];
  377. }
  378. struct message_to_relay *smptr = sm;
  379. smptr->t = RMT_SOCKET;
  380. struct evbuffer *output = NULL;
  381. int success = 0;
  382. if(!rdest) {
  383. goto label_end;
  384. }
  385. output = bufferevent_get_output(rdest->out_buf);
  386. if(output) {
  387. if(evbuffer_add(output,smptr,sizeof(struct message_to_relay))<0) {
  388. TURN_LOG_FUNC(
  389. TURN_LOG_LEVEL_ERROR,
  390. "%s: Cannot add message to relay output buffer\n",
  391. __FUNCTION__);
  392. } else {
  393. success = 1;
  394. smptr->m.sm.nd.nbh=NULL;
  395. }
  396. }
  397. label_end:
  398. if(!success) {
  399. ioa_network_buffer_delete(e, smptr->m.sm.nd.nbh);
  400. smptr->m.sm.nd.nbh=NULL;
  401. IOA_CLOSE_SOCKET(smptr->m.sm.s);
  402. return -1;
  403. }
  404. return 0;
  405. }
  406. static int send_socket_to_relay(turnserver_id id, u64bits cid, stun_tid *tid, ioa_socket_handle s,
  407. int message_integrity, MESSAGE_TO_RELAY_TYPE rmt, ioa_net_data *nd,
  408. int can_resume)
  409. {
  410. int ret = -1;
  411. struct message_to_relay sm;
  412. ns_bzero(&sm,sizeof(struct message_to_relay));
  413. sm.t = rmt;
  414. ioa_socket_handle s_to_delete = s;
  415. struct relay_server *rs = NULL;
  416. if(id>=TURNSERVER_ID_BOUNDARY_BETWEEN_TCP_AND_UDP) {
  417. size_t dest = id-TURNSERVER_ID_BOUNDARY_BETWEEN_TCP_AND_UDP;
  418. if(dest >= get_real_udp_relay_servers_number()) {
  419. TURN_LOG_FUNC(
  420. TURN_LOG_LEVEL_ERROR,
  421. "%s: Too large UDP relay number: %d, rmt=%d, total=%d\n",
  422. __FUNCTION__,(int)dest,(int)rmt, (int)get_real_udp_relay_servers_number());
  423. goto err;
  424. }
  425. rs = udp_relay_servers[dest];
  426. if(!rs) {
  427. TURN_LOG_FUNC(
  428. TURN_LOG_LEVEL_ERROR,
  429. "%s: Wrong UDP relay number: %d, rmt=%d, total=%d\n",
  430. __FUNCTION__,(int)dest,(int)rmt, (int)get_real_udp_relay_servers_number());
  431. goto err;
  432. }
  433. } else {
  434. size_t dest = id;
  435. if(dest >= get_real_general_relay_servers_number()) {
  436. TURN_LOG_FUNC(
  437. TURN_LOG_LEVEL_ERROR,
  438. "%s: Too large general relay number: %d, rmt=%d, total=%d\n",
  439. __FUNCTION__,(int)dest,(int)rmt, (int)get_real_general_relay_servers_number());
  440. goto err;
  441. }
  442. rs = general_relay_servers[dest];
  443. if(!rs) {
  444. TURN_LOG_FUNC(
  445. TURN_LOG_LEVEL_ERROR,
  446. "%s: Wrong general relay number: %d, rmt=%d, total=%d\n",
  447. __FUNCTION__,(int)dest,(int)rmt, (int)get_real_general_relay_servers_number());
  448. goto err;
  449. }
  450. }
  451. switch (rmt) {
  452. case(RMT_CB_SOCKET): {
  453. if(nd && nd->nbh) {
  454. sm.m.cb_sm.id = id;
  455. sm.m.cb_sm.connection_id = (tcp_connection_id)cid;
  456. stun_tid_cpy(&(sm.m.cb_sm.tid),tid);
  457. sm.m.cb_sm.s = s;
  458. sm.m.cb_sm.message_integrity = message_integrity;
  459. addr_cpy(&(sm.m.cb_sm.nd.src_addr),&(nd->src_addr));
  460. sm.m.cb_sm.nd.recv_tos = nd->recv_tos;
  461. sm.m.cb_sm.nd.recv_ttl = nd->recv_ttl;
  462. sm.m.cb_sm.nd.nbh = nd->nbh;
  463. sm.m.cb_sm.can_resume = can_resume;
  464. nd->nbh = NULL;
  465. s_to_delete = NULL;
  466. ret = 0;
  467. }
  468. break;
  469. }
  470. case (RMT_MOBILE_SOCKET): {
  471. if(nd && nd->nbh) {
  472. sm.m.sm.s = s;
  473. addr_cpy(&(sm.m.sm.nd.src_addr),&(nd->src_addr));
  474. sm.m.sm.nd.recv_tos = nd->recv_tos;
  475. sm.m.sm.nd.recv_ttl = nd->recv_ttl;
  476. sm.m.sm.nd.nbh = nd->nbh;
  477. sm.m.sm.can_resume = can_resume;
  478. nd->nbh = NULL;
  479. s_to_delete = NULL;
  480. ret = 0;
  481. } else {
  482. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: Empty buffer with mobile socket\n",__FUNCTION__);
  483. }
  484. break;
  485. }
  486. default: {
  487. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: UNKNOWN RMT message: %d\n",__FUNCTION__,(int)rmt);
  488. }
  489. }
  490. if(ret == 0) {
  491. struct evbuffer *output = bufferevent_get_output(rs->out_buf);
  492. if(output) {
  493. evbuffer_add(output,&sm,sizeof(struct message_to_relay));
  494. } else {
  495. TURN_LOG_FUNC(
  496. TURN_LOG_LEVEL_ERROR,
  497. "%s: Empty output buffer\n",
  498. __FUNCTION__);
  499. ret = -1;
  500. s_to_delete = s;
  501. }
  502. }
  503. err:
  504. IOA_CLOSE_SOCKET(s_to_delete);
  505. if(nd && nd->nbh) {
  506. ioa_network_buffer_delete(NULL, nd->nbh);
  507. nd->nbh = NULL;
  508. }
  509. if(ret<0) {
  510. if(rmt == RMT_MOBILE_SOCKET) {
  511. ioa_network_buffer_delete(NULL, sm.m.sm.nd.nbh);
  512. sm.m.sm.nd.nbh = NULL;
  513. } else if(rmt == RMT_CB_SOCKET) {
  514. ioa_network_buffer_delete(NULL, sm.m.cb_sm.nd.nbh);
  515. sm.m.cb_sm.nd.nbh = NULL;
  516. }
  517. }
  518. return ret;
  519. }
  520. int send_session_cancellation_to_relay(turnsession_id sid)
  521. {
  522. int ret = 0;
  523. struct message_to_relay sm;
  524. ns_bzero(&sm,sizeof(struct message_to_relay));
  525. sm.t = RMT_CANCEL_SESSION;
  526. turnserver_id id = (turnserver_id)(sid / TURN_SESSION_ID_FACTOR);
  527. struct relay_server *rs = NULL;
  528. if(id>=TURNSERVER_ID_BOUNDARY_BETWEEN_TCP_AND_UDP) {
  529. size_t dest = id-TURNSERVER_ID_BOUNDARY_BETWEEN_TCP_AND_UDP;
  530. if(dest >= get_real_udp_relay_servers_number()) {
  531. TURN_LOG_FUNC(
  532. TURN_LOG_LEVEL_ERROR,
  533. "%s: Too large UDP relay number: %d, total=%d\n",
  534. __FUNCTION__,(int)dest,(int)get_real_udp_relay_servers_number());
  535. ret = -1;
  536. goto err;
  537. }
  538. rs = udp_relay_servers[dest];
  539. if(!rs) {
  540. TURN_LOG_FUNC(
  541. TURN_LOG_LEVEL_ERROR,
  542. "%s: Wrong UDP relay number: %d, total=%d\n",
  543. __FUNCTION__,(int)dest,(int)get_real_udp_relay_servers_number());
  544. ret = -1;
  545. goto err;
  546. }
  547. } else {
  548. size_t dest = id;
  549. if(dest >= get_real_general_relay_servers_number()) {
  550. TURN_LOG_FUNC(
  551. TURN_LOG_LEVEL_ERROR,
  552. "%s: Too large general relay number: %d, total=%d\n",
  553. __FUNCTION__,(int)dest,(int)get_real_general_relay_servers_number());
  554. ret = -1;
  555. goto err;
  556. }
  557. rs = general_relay_servers[dest];
  558. if(!rs) {
  559. TURN_LOG_FUNC(
  560. TURN_LOG_LEVEL_ERROR,
  561. "%s: Wrong general relay number: %d, total=%d\n",
  562. __FUNCTION__,(int)dest,(int)get_real_general_relay_servers_number());
  563. ret = -1;
  564. goto err;
  565. }
  566. }
  567. sm.relay_server = rs;
  568. sm.m.csm.id = sid;
  569. {
  570. struct evbuffer *output = bufferevent_get_output(rs->out_buf);
  571. if(output) {
  572. evbuffer_add(output,&sm,sizeof(struct message_to_relay));
  573. } else {
  574. TURN_LOG_FUNC(
  575. TURN_LOG_LEVEL_ERROR,
  576. "%s: Empty output buffer\n",
  577. __FUNCTION__);
  578. ret = -1;
  579. }
  580. }
  581. err:
  582. return ret;
  583. }
  584. static int handle_relay_message(relay_server_handle rs, struct message_to_relay *sm)
  585. {
  586. if(rs && sm) {
  587. switch (sm->t) {
  588. case RMT_CANCEL_SESSION: {
  589. turn_cancel_session(&(rs->server),sm->m.csm.id);
  590. }
  591. break;
  592. case RMT_SOCKET: {
  593. if (sm->m.sm.s->defer_nbh) {
  594. if (!sm->m.sm.nd.nbh) {
  595. sm->m.sm.nd.nbh = sm->m.sm.s->defer_nbh;
  596. sm->m.sm.s->defer_nbh = NULL;
  597. } else {
  598. ioa_network_buffer_delete(rs->ioa_eng, sm->m.sm.s->defer_nbh);
  599. sm->m.sm.s->defer_nbh = NULL;
  600. }
  601. }
  602. ioa_socket_handle s = sm->m.sm.s;
  603. if (!s) {
  604. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
  605. "%s: socket EMPTY\n",__FUNCTION__);
  606. } else if (s->read_event || s->bev) {
  607. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
  608. "%s: socket wrongly preset: 0x%lx : 0x%lx\n",
  609. __FUNCTION__, (long) s->read_event, (long) s->bev);
  610. IOA_CLOSE_SOCKET(s);
  611. sm->m.sm.s = NULL;
  612. } else {
  613. s->e = rs->ioa_eng;
  614. open_client_connection_session(&(rs->server), &(sm->m.sm));
  615. }
  616. ioa_network_buffer_delete(rs->ioa_eng, sm->m.sm.nd.nbh);
  617. sm->m.sm.nd.nbh = NULL;
  618. }
  619. break;
  620. case RMT_CB_SOCKET:
  621. turnserver_accept_tcp_client_data_connection(&(rs->server), sm->m.cb_sm.connection_id,
  622. &(sm->m.cb_sm.tid), sm->m.cb_sm.s, sm->m.cb_sm.message_integrity, &(sm->m.cb_sm.nd),
  623. /*sm->m.cb_sm.can_resume*/
  624. /* Note: we cannot resume this call, it must be authenticated in-place.
  625. * There are two reasons for that:
  626. * 1) Technical. That's very difficult with the current code structure.
  627. * 2) Security (more important). We do not want 'stealing' connections between the users.
  628. * */
  629. 0);
  630. ioa_network_buffer_delete(rs->ioa_eng, sm->m.cb_sm.nd.nbh);
  631. sm->m.cb_sm.nd.nbh = NULL;
  632. break;
  633. case RMT_MOBILE_SOCKET: {
  634. ioa_socket_handle s = sm->m.sm.s;
  635. if (!s) {
  636. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
  637. "%s: mobile socket EMPTY\n",__FUNCTION__);
  638. } else if (s->read_event || s->bev) {
  639. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
  640. "%s: mobile socket wrongly preset: 0x%lx : 0x%lx\n",
  641. __FUNCTION__, (long) s->read_event, (long) s->bev);
  642. IOA_CLOSE_SOCKET(s);
  643. sm->m.sm.s = NULL;
  644. } else {
  645. s->e = rs->ioa_eng;
  646. open_client_connection_session(&(rs->server), &(sm->m.sm));
  647. }
  648. ioa_network_buffer_delete(rs->ioa_eng, sm->m.sm.nd.nbh);
  649. sm->m.sm.nd.nbh = NULL;
  650. break;
  651. }
  652. default: {
  653. perror("Weird buffer type\n");
  654. }
  655. }
  656. }
  657. return 0;
  658. }
  659. static void handle_relay_auth_message(struct relay_server *rs, struct auth_message *am)
  660. {
  661. am->resume_func(am->success, am->out_oauth, am->max_session_time, am->key, am->pwd,
  662. &(rs->server), am->ctxkey, &(am->in_buffer));
  663. if (am->in_buffer.nbh) {
  664. ioa_network_buffer_delete(rs->ioa_eng, am->in_buffer.nbh);
  665. am->in_buffer.nbh = NULL;
  666. }
  667. }
  668. static void relay_receive_message(struct bufferevent *bev, void *ptr)
  669. {
  670. struct message_to_relay sm;
  671. int n = 0;
  672. struct evbuffer *input = bufferevent_get_input(bev);
  673. struct relay_server *rs = (struct relay_server *)ptr;
  674. while ((n = evbuffer_remove(input, &sm, sizeof(struct message_to_relay))) > 0) {
  675. if (n != sizeof(struct message_to_relay)) {
  676. perror("Weird buffer error\n");
  677. continue;
  678. }
  679. handle_relay_message(rs, &sm);
  680. }
  681. }
  682. static void relay_receive_auth_message(struct bufferevent *bev, void *ptr)
  683. {
  684. struct auth_message am;
  685. int n = 0;
  686. struct evbuffer *input = bufferevent_get_input(bev);
  687. struct relay_server *rs = (struct relay_server *)ptr;
  688. while ((n = evbuffer_remove(input, &am, sizeof(struct auth_message))) > 0) {
  689. if (n != sizeof(struct auth_message)) {
  690. perror("Weird auth_buffer error\n");
  691. continue;
  692. }
  693. handle_relay_auth_message(rs, &am);
  694. }
  695. }
  696. static int send_message_from_listener_to_client(ioa_engine_handle e, ioa_network_buffer_handle nbh, ioa_addr *origin, ioa_addr *destination)
  697. {
  698. struct message_to_listener mm;
  699. mm.t = LMT_TO_CLIENT;
  700. addr_cpy(&(mm.m.tc.origin),origin);
  701. addr_cpy(&(mm.m.tc.destination),destination);
  702. mm.m.tc.nbh = ioa_network_buffer_allocate(e);
  703. ioa_network_buffer_header_init(mm.m.tc.nbh);
  704. ns_bcopy(ioa_network_buffer_data(nbh),ioa_network_buffer_data(mm.m.tc.nbh),ioa_network_buffer_get_size(nbh));
  705. ioa_network_buffer_set_size(mm.m.tc.nbh,ioa_network_buffer_get_size(nbh));
  706. struct evbuffer *output = bufferevent_get_output(turn_params.listener.out_buf);
  707. evbuffer_add(output,&mm,sizeof(struct message_to_listener));
  708. return 0;
  709. }
  710. static void listener_receive_message(struct bufferevent *bev, void *ptr)
  711. {
  712. UNUSED_ARG(ptr);
  713. struct message_to_listener mm;
  714. int n = 0;
  715. struct evbuffer *input = bufferevent_get_input(bev);
  716. while ((n = evbuffer_remove(input, &mm, sizeof(struct message_to_listener))) > 0) {
  717. if (n != sizeof(struct message_to_listener)) {
  718. perror("Weird buffer error\n");
  719. continue;
  720. }
  721. if (mm.t != LMT_TO_CLIENT) {
  722. perror("Weird buffer type\n");
  723. continue;
  724. }
  725. size_t relay_thread_index = 0;
  726. if(turn_params.net_engine_version == NEV_UDP_SOCKET_PER_THREAD) {
  727. size_t ri;
  728. for(ri=0;ri<get_real_general_relay_servers_number();ri++) {
  729. if(!(general_relay_servers[ri])) {
  730. TURN_LOG_FUNC(
  731. TURN_LOG_LEVEL_ERROR,
  732. "%s: Wrong general relay number: %d, total %d\n",
  733. __FUNCTION__,(int)ri,(int)get_real_general_relay_servers_number());
  734. } else if(general_relay_servers[ri]->thr == pthread_self()) {
  735. relay_thread_index=ri;
  736. break;
  737. }
  738. }
  739. }
  740. size_t i;
  741. int found = 0;
  742. for(i=0;i<turn_params.listener.addrs_number;i++) {
  743. if(addr_eq_no_port(turn_params.listener.encaddrs[i],&mm.m.tc.origin)) {
  744. int o_port = addr_get_port(&mm.m.tc.origin);
  745. if(turn_params.listener.addrs_number == turn_params.listener.services_number) {
  746. if(o_port == turn_params.listener_port) {
  747. if(turn_params.listener.udp_services && turn_params.listener.udp_services[i] && turn_params.listener.udp_services[i][relay_thread_index]) {
  748. found = 1;
  749. udp_send_message(turn_params.listener.udp_services[i][relay_thread_index], mm.m.tc.nbh, &mm.m.tc.destination);
  750. }
  751. } else {
  752. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,"%s: Wrong origin port(1): %d\n",__FUNCTION__,o_port);
  753. }
  754. } else if((turn_params.listener.addrs_number * 2) == turn_params.listener.services_number) {
  755. if(o_port == turn_params.listener_port) {
  756. if(turn_params.listener.udp_services && turn_params.listener.udp_services[i*2] && turn_params.listener.udp_services[i*2][relay_thread_index]) {
  757. found = 1;
  758. udp_send_message(turn_params.listener.udp_services[i*2][relay_thread_index], mm.m.tc.nbh, &mm.m.tc.destination);
  759. }
  760. } else if(o_port == get_alt_listener_port()) {
  761. if(turn_params.listener.udp_services && turn_params.listener.udp_services[i*2+1] && turn_params.listener.udp_services[i*2+1][relay_thread_index]) {
  762. found = 1;
  763. udp_send_message(turn_params.listener.udp_services[i*2+1][relay_thread_index], mm.m.tc.nbh, &mm.m.tc.destination);
  764. }
  765. } else {
  766. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,"%s: Wrong origin port(2): %d\n",__FUNCTION__,o_port);
  767. }
  768. } else {
  769. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,"%s: Wrong listener setup\n",__FUNCTION__);
  770. }
  771. break;
  772. }
  773. }
  774. if(!found) {
  775. u08bits saddr[129];
  776. addr_to_string(&mm.m.tc.origin, saddr);
  777. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,"%s: Cannot find local source %s\n",__FUNCTION__,saddr);
  778. }
  779. ioa_network_buffer_delete(turn_params.listener.ioa_eng, mm.m.tc.nbh);
  780. mm.m.tc.nbh = NULL;
  781. }
  782. }
  783. // <<== communications between listener and relays
  784. static ioa_engine_handle create_new_listener_engine(void)
  785. {
  786. struct event_base *eb = turn_event_base_new();
  787. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,"IO method (udp listener/relay thread): %s\n",event_base_get_method(eb));
  788. super_memory_t* sm = new_super_memory_region();
  789. ioa_engine_handle e = create_ioa_engine(sm, eb, turn_params.listener.tp, turn_params.relay_ifname, turn_params.relays_number, turn_params.relay_addrs,
  790. turn_params.default_relays, turn_params.verbose
  791. #if !defined(TURN_NO_HIREDIS)
  792. ,turn_params.redis_statsdb
  793. #endif
  794. );
  795. set_ssl_ctx(e, turn_params.tls_ctx_ssl23, turn_params.tls_ctx_v1_0,
  796. #if defined(SSL_TXT_TLSV1_1)
  797. turn_params.tls_ctx_v1_1,
  798. #if defined(SSL_TXT_TLSV1_2)
  799. turn_params.tls_ctx_v1_2,
  800. #endif
  801. #endif
  802. turn_params.dtls_ctx);
  803. ioa_engine_set_rtcp_map(e, turn_params.listener.rtcpmap);
  804. return e;
  805. }
  806. static void *run_udp_listener_thread(void *arg)
  807. {
  808. static int always_true = 1;
  809. ignore_sigpipe();
  810. barrier_wait();
  811. dtls_listener_relay_server_type *server = (dtls_listener_relay_server_type *)arg;
  812. while(always_true && server) {
  813. run_events(NULL, get_engine(server));
  814. }
  815. return arg;
  816. }
  817. static void setup_listener(void)
  818. {
  819. super_memory_t* sm = new_super_memory_region();
  820. turn_params.listener.tp = turnipports_create(sm, turn_params.min_port, turn_params.max_port);
  821. turn_params.listener.event_base = turn_event_base_new();
  822. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,"IO method (main listener thread): %s\n",event_base_get_method(turn_params.listener.event_base));
  823. turn_params.listener.ioa_eng = create_ioa_engine(sm, turn_params.listener.event_base, turn_params.listener.tp,
  824. turn_params.relay_ifname, turn_params.relays_number, turn_params.relay_addrs,
  825. turn_params.default_relays, turn_params.verbose
  826. #if !defined(TURN_NO_HIREDIS)
  827. ,turn_params.redis_statsdb
  828. #endif
  829. );
  830. if(!turn_params.listener.ioa_eng)
  831. exit(-1);
  832. set_ssl_ctx(turn_params.listener.ioa_eng, turn_params.tls_ctx_ssl23, turn_params.tls_ctx_v1_0,
  833. #if defined(SSL_TXT_TLSV1_1)
  834. turn_params.tls_ctx_v1_1,
  835. #if defined(SSL_TXT_TLSV1_2)
  836. turn_params.tls_ctx_v1_2,
  837. #endif
  838. #endif
  839. turn_params.dtls_ctx);
  840. turn_params.listener.rtcpmap = rtcp_map_create(turn_params.listener.ioa_eng);
  841. ioa_engine_set_rtcp_map(turn_params.listener.ioa_eng, turn_params.listener.rtcpmap);
  842. {
  843. struct bufferevent *pair[2];
  844. bufferevent_pair_new(turn_params.listener.event_base, TURN_BUFFEREVENTS_OPTIONS, pair);
  845. turn_params.listener.in_buf = pair[0];
  846. turn_params.listener.out_buf = pair[1];
  847. bufferevent_setcb(turn_params.listener.in_buf, listener_receive_message, NULL, NULL, &turn_params.listener);
  848. bufferevent_enable(turn_params.listener.in_buf, EV_READ);
  849. }
  850. if(turn_params.listener.addrs_number<2 || turn_params.external_ip) {
  851. turn_params.rfc5780 = 0;
  852. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "WARNING: I cannot support STUN CHANGE_REQUEST functionality because only one IP address is provided\n");
  853. } else {
  854. turn_params.listener.services_number = turn_params.listener.services_number * 2;
  855. }
  856. turn_params.listener.udp_services = (dtls_listener_relay_server_type***)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(dtls_listener_relay_server_type**)*turn_params.listener.services_number);
  857. turn_params.listener.dtls_services = (dtls_listener_relay_server_type***)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(dtls_listener_relay_server_type**)*turn_params.listener.services_number);
  858. turn_params.listener.aux_udp_services = (dtls_listener_relay_server_type***)allocate_super_memory_engine(turn_params.listener.ioa_eng, (sizeof(dtls_listener_relay_server_type**)*turn_params.aux_servers_list.size)+sizeof(void*));
  859. }
  860. static void setup_barriers(void)
  861. {
  862. /* Adjust barriers: */
  863. #if !defined(TURN_NO_THREAD_BARRIERS)
  864. if((turn_params.net_engine_version == NEV_UDP_SOCKET_PER_ENDPOINT) && turn_params.general_relay_servers_number>1) {
  865. /* UDP: */
  866. if(!turn_params.no_udp) {
  867. barrier_count += turn_params.listener.addrs_number;
  868. if(turn_params.rfc5780) {
  869. barrier_count += turn_params.listener.addrs_number;
  870. }
  871. }
  872. if(!turn_params.no_dtls && (turn_params.no_udp || (turn_params.listener_port != turn_params.tls_listener_port))) {
  873. barrier_count += turn_params.listener.addrs_number;
  874. if(turn_params.rfc5780) {
  875. barrier_count += turn_params.listener.addrs_number;
  876. }
  877. }
  878. if(!turn_params.no_udp || !turn_params.no_dtls) {
  879. barrier_count += (unsigned int)turn_params.aux_servers_list.size;
  880. }
  881. }
  882. #endif
  883. #if !defined(TURN_NO_THREAD_BARRIERS)
  884. {
  885. if(pthread_barrier_init(&barrier,NULL,barrier_count)<0)
  886. perror("barrier init");
  887. }
  888. #endif
  889. }
  890. static void setup_socket_per_endpoint_udp_listener_servers(void)
  891. {
  892. size_t i = 0;
  893. /* Adjust udp relay number */
  894. if(turn_params.general_relay_servers_number>1) {
  895. if (!turn_params.no_udp) {
  896. turn_params.udp_relay_servers_number += turn_params.listener.addrs_number;
  897. if (turn_params.rfc5780) {
  898. turn_params.udp_relay_servers_number += turn_params.listener.addrs_number;
  899. }
  900. }
  901. if (!turn_params.no_dtls && (turn_params.no_udp || (turn_params.listener_port != turn_params.tls_listener_port))) {
  902. turn_params.udp_relay_servers_number += turn_params.listener.addrs_number;
  903. if (turn_params.rfc5780) {
  904. turn_params.udp_relay_servers_number += turn_params.listener.addrs_number;
  905. }
  906. }
  907. if (!turn_params.no_udp || !turn_params.no_dtls) {
  908. turn_params.udp_relay_servers_number += (unsigned int) turn_params.aux_servers_list.size;
  909. }
  910. }
  911. {
  912. if (!turn_params.no_udp || !turn_params.no_dtls) {
  913. for (i = 0; i < get_real_udp_relay_servers_number(); i++) {
  914. ioa_engine_handle e = turn_params.listener.ioa_eng;
  915. int is_5780 = turn_params.rfc5780;
  916. if(turn_params.general_relay_servers_number<=1) {
  917. while(!(general_relay_servers[0]->ioa_eng))
  918. sched_yield();
  919. udp_relay_servers[i] = general_relay_servers[0];
  920. continue;
  921. } else if(turn_params.general_relay_servers_number>1) {
  922. e = create_new_listener_engine();
  923. is_5780 = is_5780 && (i >= (size_t) (turn_params.aux_servers_list.size));
  924. }
  925. super_memory_t *sm = new_super_memory_region();
  926. struct relay_server* udp_rs = (struct relay_server*) allocate_super_memory_region(sm, sizeof(struct relay_server));
  927. udp_rs->id = (turnserver_id) i + TURNSERVER_ID_BOUNDARY_BETWEEN_TCP_AND_UDP;
  928. udp_rs->sm = sm;
  929. setup_relay_server(udp_rs, e, is_5780);
  930. udp_relay_servers[i] = udp_rs;
  931. }
  932. }
  933. }
  934. int udp_relay_server_index = 0;
  935. /* Create listeners */
  936. /* Aux UDP servers */
  937. for(i=0; i<turn_params.aux_servers_list.size; i++) {
  938. int index = i;
  939. if(!turn_params.no_udp || !turn_params.no_dtls) {
  940. ioa_addr addr;
  941. char saddr[129];
  942. addr_cpy(&addr,&turn_params.aux_servers_list.addrs[i]);
  943. int port = (int)addr_get_port(&addr);
  944. addr_to_string_no_port(&addr,(u08bits*)saddr);
  945. turn_params.listener.aux_udp_services[index] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(udp_relay_servers[udp_relay_server_index]->ioa_eng, sizeof(dtls_listener_relay_server_type*));
  946. turn_params.listener.aux_udp_services[index][0] = create_dtls_listener_server(turn_params.listener_ifname, saddr, port, turn_params.verbose, udp_relay_servers[udp_relay_server_index]->ioa_eng, &(udp_relay_servers[udp_relay_server_index]->server), 1, NULL);
  947. if(turn_params.general_relay_servers_number>1) {
  948. ++udp_relay_server_index;
  949. pthread_t thr;
  950. if(pthread_create(&thr, NULL, run_udp_listener_thread, turn_params.listener.aux_udp_services[index][0])<0) {
  951. perror("Cannot create aux listener thread\n");
  952. exit(-1);
  953. }
  954. pthread_detach(thr);
  955. }
  956. }
  957. }
  958. /* Main servers */
  959. for(i=0; i<turn_params.listener.addrs_number; i++) {
  960. int index = turn_params.rfc5780 ? i*2 : i;
  961. /* UDP: */
  962. if(!turn_params.no_udp) {
  963. turn_params.listener.udp_services[index] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(udp_relay_servers[udp_relay_server_index]->ioa_eng, sizeof(dtls_listener_relay_server_type*));
  964. turn_params.listener.udp_services[index][0] = create_dtls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], turn_params.listener_port, turn_params.verbose, udp_relay_servers[udp_relay_server_index]->ioa_eng, &(udp_relay_servers[udp_relay_server_index]->server), 1, NULL);
  965. if(turn_params.general_relay_servers_number>1) {
  966. ++udp_relay_server_index;
  967. pthread_t thr;
  968. if(pthread_create(&thr, NULL, run_udp_listener_thread, turn_params.listener.udp_services[index][0])<0) {
  969. perror("Cannot create listener thread\n");
  970. exit(-1);
  971. }
  972. pthread_detach(thr);
  973. }
  974. if(turn_params.rfc5780) {
  975. turn_params.listener.udp_services[index+1] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(udp_relay_servers[udp_relay_server_index]->ioa_eng, sizeof(dtls_listener_relay_server_type*));
  976. turn_params.listener.udp_services[index+1][0] = create_dtls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], get_alt_listener_port(), turn_params.verbose, udp_relay_servers[udp_relay_server_index]->ioa_eng, &(udp_relay_servers[udp_relay_server_index]->server), 1, NULL);
  977. if(turn_params.general_relay_servers_number>1) {
  978. ++udp_relay_server_index;
  979. pthread_t thr;
  980. if(pthread_create(&thr, NULL, run_udp_listener_thread, turn_params.listener.udp_services[index+1][0])<0) {
  981. perror("Cannot create listener thread\n");
  982. exit(-1);
  983. }
  984. pthread_detach(thr);
  985. }
  986. }
  987. } else {
  988. turn_params.listener.udp_services[index] = NULL;
  989. if(turn_params.rfc5780)
  990. turn_params.listener.udp_services[index+1] = NULL;
  991. }
  992. if(!turn_params.no_dtls && (turn_params.no_udp || (turn_params.listener_port != turn_params.tls_listener_port))) {
  993. turn_params.listener.dtls_services[index] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(udp_relay_servers[udp_relay_server_index]->ioa_eng, sizeof(dtls_listener_relay_server_type*));
  994. turn_params.listener.dtls_services[index][0] = create_dtls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], turn_params.tls_listener_port, turn_params.verbose, udp_relay_servers[udp_relay_server_index]->ioa_eng, &(udp_relay_servers[udp_relay_server_index]->server), 1, NULL);
  995. if(turn_params.general_relay_servers_number>1) {
  996. ++udp_relay_server_index;
  997. pthread_t thr;
  998. if(pthread_create(&thr, NULL, run_udp_listener_thread, turn_params.listener.dtls_services[index][0])<0) {
  999. perror("Cannot create listener thread\n");
  1000. exit(-1);
  1001. }
  1002. pthread_detach(thr);
  1003. }
  1004. if(turn_params.rfc5780) {
  1005. turn_params.listener.dtls_services[index+1] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(udp_relay_servers[udp_relay_server_index]->ioa_eng, sizeof(dtls_listener_relay_server_type*));
  1006. turn_params.listener.dtls_services[index+1][0] = create_dtls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], get_alt_tls_listener_port(), turn_params.verbose, udp_relay_servers[udp_relay_server_index]->ioa_eng, &(udp_relay_servers[udp_relay_server_index]->server), 1, NULL);
  1007. if(turn_params.general_relay_servers_number>1) {
  1008. ++udp_relay_server_index;
  1009. pthread_t thr;
  1010. if(pthread_create(&thr, NULL, run_udp_listener_thread, turn_params.listener.dtls_services[index+1][0])<0) {
  1011. perror("Cannot create listener thread\n");
  1012. exit(-1);
  1013. }
  1014. pthread_detach(thr);
  1015. }
  1016. }
  1017. } else {
  1018. turn_params.listener.dtls_services[index] = NULL;
  1019. if(turn_params.rfc5780)
  1020. turn_params.listener.dtls_services[index+1] = NULL;
  1021. }
  1022. }
  1023. }
  1024. static void setup_socket_per_thread_udp_listener_servers(void)
  1025. {
  1026. size_t i = 0;
  1027. size_t relayindex = 0;
  1028. /* Create listeners */
  1029. for(relayindex=0;relayindex<get_real_general_relay_servers_number();relayindex++) {
  1030. while(!(general_relay_servers[relayindex]->ioa_eng) || !(general_relay_servers[relayindex]->server.e))
  1031. sched_yield();
  1032. }
  1033. /* Aux UDP servers */
  1034. for(i=0; i<turn_params.aux_servers_list.size; i++) {
  1035. int index = i;
  1036. if(!turn_params.no_udp || !turn_params.no_dtls) {
  1037. ioa_addr addr;
  1038. char saddr[129];
  1039. addr_cpy(&addr,&turn_params.aux_servers_list.addrs[i]);
  1040. int port = (int)addr_get_port(&addr);
  1041. addr_to_string_no_port(&addr,(u08bits*)saddr);
  1042. turn_params.listener.aux_udp_services[index] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(dtls_listener_relay_server_type*) * get_real_general_relay_servers_number());
  1043. for(relayindex=0;relayindex<get_real_general_relay_servers_number();relayindex++) {
  1044. turn_params.listener.aux_udp_services[index][relayindex] = create_dtls_listener_server(turn_params.listener_ifname, saddr, port, turn_params.verbose,
  1045. general_relay_servers[relayindex]->ioa_eng, &(general_relay_servers[relayindex]->server), !relayindex, NULL);
  1046. }
  1047. }
  1048. }
  1049. /* Main servers */
  1050. for(i=0; i<turn_params.listener.addrs_number; i++) {
  1051. int index = turn_params.rfc5780 ? i*2 : i;
  1052. /* UDP: */
  1053. if(!turn_params.no_udp) {
  1054. turn_params.listener.udp_services[index] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(dtls_listener_relay_server_type*) * get_real_general_relay_servers_number());
  1055. for(relayindex=0;relayindex<get_real_general_relay_servers_number();relayindex++) {
  1056. turn_params.listener.udp_services[index][relayindex] = create_dtls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], turn_params.listener_port, turn_params.verbose,
  1057. general_relay_servers[relayindex]->ioa_eng, &(general_relay_servers[relayindex]->server), !relayindex, NULL);
  1058. }
  1059. if(turn_params.rfc5780) {
  1060. turn_params.listener.udp_services[index+1] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(dtls_listener_relay_server_type*) * get_real_general_relay_servers_number());
  1061. for(relayindex=0;relayindex<get_real_general_relay_servers_number();relayindex++) {
  1062. turn_params.listener.udp_services[index+1][relayindex] = create_dtls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], get_alt_listener_port(), turn_params.verbose,
  1063. general_relay_servers[relayindex]->ioa_eng, &(general_relay_servers[relayindex]->server), !relayindex, NULL);
  1064. }
  1065. }
  1066. } else {
  1067. turn_params.listener.udp_services[index] = NULL;
  1068. if(turn_params.rfc5780)
  1069. turn_params.listener.udp_services[index+1] = NULL;
  1070. }
  1071. if(!turn_params.no_dtls && (turn_params.no_udp || (turn_params.listener_port != turn_params.tls_listener_port))) {
  1072. turn_params.listener.dtls_services[index] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(dtls_listener_relay_server_type*) * get_real_general_relay_servers_number());
  1073. for(relayindex=0;relayindex<get_real_general_relay_servers_number();relayindex++) {
  1074. turn_params.listener.dtls_services[index][relayindex] = create_dtls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], turn_params.tls_listener_port, turn_params.verbose,
  1075. general_relay_servers[relayindex]->ioa_eng, &(general_relay_servers[relayindex]->server), !relayindex, NULL);
  1076. }
  1077. if(turn_params.rfc5780) {
  1078. turn_params.listener.dtls_services[index+1] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(dtls_listener_relay_server_type*) * get_real_general_relay_servers_number());
  1079. for(relayindex=0;relayindex<get_real_general_relay_servers_number();relayindex++) {
  1080. turn_params.listener.dtls_services[index+1][relayindex] = create_dtls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], get_alt_tls_listener_port(), turn_params.verbose,
  1081. general_relay_servers[relayindex]->ioa_eng, &(general_relay_servers[relayindex]->server), !relayindex, NULL);
  1082. }
  1083. }
  1084. } else {
  1085. turn_params.listener.dtls_services[index] = NULL;
  1086. if(turn_params.rfc5780)
  1087. turn_params.listener.dtls_services[index+1] = NULL;
  1088. }
  1089. }
  1090. }
  1091. static void setup_socket_per_session_udp_listener_servers(void)
  1092. {
  1093. size_t i = 0;
  1094. /* Aux UDP servers */
  1095. for(i=0; i<turn_params.aux_servers_list.size; i++) {
  1096. int index = i;
  1097. if(!turn_params.no_udp || !turn_params.no_dtls) {
  1098. ioa_addr addr;
  1099. char saddr[129];
  1100. addr_cpy(&addr,&turn_params.aux_servers_list.addrs[i]);
  1101. int port = (int)addr_get_port(&addr);
  1102. addr_to_string_no_port(&addr,(u08bits*)saddr);
  1103. turn_params.listener.aux_udp_services[index] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(dtls_listener_relay_server_type*));
  1104. turn_params.listener.aux_udp_services[index][0] = create_dtls_listener_server(turn_params.listener_ifname, saddr, port, turn_params.verbose,
  1105. turn_params.listener.ioa_eng, NULL, 1, send_socket_to_general_relay);
  1106. }
  1107. }
  1108. /* Main servers */
  1109. for(i=0; i<turn_params.listener.addrs_number; i++) {
  1110. int index = turn_params.rfc5780 ? i*2 : i;
  1111. /* UDP: */
  1112. if(!turn_params.no_udp) {
  1113. turn_params.listener.udp_services[index] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(dtls_listener_relay_server_type*));
  1114. turn_params.listener.udp_services[index][0] = create_dtls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], turn_params.listener_port, turn_params.verbose,
  1115. turn_params.listener.ioa_eng, NULL, 1, send_socket_to_general_relay);
  1116. if(turn_params.rfc5780) {
  1117. turn_params.listener.udp_services[index+1] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(dtls_listener_relay_server_type*));
  1118. turn_params.listener.udp_services[index+1][0] = create_dtls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], get_alt_listener_port(), turn_params.verbose,
  1119. turn_params.listener.ioa_eng, NULL, 1, send_socket_to_general_relay);
  1120. }
  1121. } else {
  1122. turn_params.listener.udp_services[index] = NULL;
  1123. if(turn_params.rfc5780)
  1124. turn_params.listener.udp_services[index+1] = NULL;
  1125. }
  1126. if(!turn_params.no_dtls && (turn_params.no_udp || (turn_params.listener_port != turn_params.tls_listener_port))) {
  1127. turn_params.listener.dtls_services[index] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(dtls_listener_relay_server_type*));
  1128. turn_params.listener.dtls_services[index][0] = create_dtls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], turn_params.tls_listener_port, turn_params.verbose,
  1129. turn_params.listener.ioa_eng, NULL, 1, send_socket_to_general_relay);
  1130. if(turn_params.rfc5780) {
  1131. turn_params.listener.dtls_services[index+1] = (dtls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(dtls_listener_relay_server_type*));
  1132. turn_params.listener.dtls_services[index+1][0] = create_dtls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], get_alt_tls_listener_port(), turn_params.verbose,
  1133. turn_params.listener.ioa_eng, NULL, 1, send_socket_to_general_relay);
  1134. }
  1135. } else {
  1136. turn_params.listener.dtls_services[index] = NULL;
  1137. if(turn_params.rfc5780)
  1138. turn_params.listener.dtls_services[index+1] = NULL;
  1139. }
  1140. }
  1141. }
  1142. static void setup_tcp_listener_servers(ioa_engine_handle e, struct relay_server *relay_server)
  1143. {
  1144. size_t i = 0;
  1145. tls_listener_relay_server_type **tcp_services = (tls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(tls_listener_relay_server_type*)*turn_params.listener.services_number);
  1146. tls_listener_relay_server_type **tls_services = (tls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(tls_listener_relay_server_type*)*turn_params.listener.services_number);
  1147. tls_listener_relay_server_type **aux_tcp_services = (tls_listener_relay_server_type**)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(tls_listener_relay_server_type*)*turn_params.aux_servers_list.size+1);
  1148. /* Create listeners */
  1149. /* Aux TCP servers */
  1150. if(!turn_params.no_tls || !turn_params.no_tcp) {
  1151. for(i=0; i<turn_params.aux_servers_list.size; i++) {
  1152. ioa_addr addr;
  1153. char saddr[129];
  1154. addr_cpy(&addr,&turn_params.aux_servers_list.addrs[i]);
  1155. int port = (int)addr_get_port(&addr);
  1156. addr_to_string_no_port(&addr,(u08bits*)saddr);
  1157. aux_tcp_services[i] = create_tls_listener_server(turn_params.listener_ifname, saddr, port, turn_params.verbose, e, send_socket_to_general_relay, relay_server);
  1158. }
  1159. }
  1160. /* Main servers */
  1161. for(i=0; i<turn_params.listener.addrs_number; i++) {
  1162. int index = turn_params.rfc5780 ? i*2 : i;
  1163. /* TCP: */
  1164. if(!turn_params.no_tcp) {
  1165. tcp_services[index] = create_tls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], turn_params.listener_port, turn_params.verbose, e, send_socket_to_general_relay, relay_server);
  1166. if(turn_params.rfc5780)
  1167. tcp_services[index+1] = create_tls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], get_alt_listener_port(), turn_params.verbose, e, send_socket_to_general_relay, relay_server);
  1168. } else {
  1169. tcp_services[index] = NULL;
  1170. if(turn_params.rfc5780)
  1171. tcp_services[index+1] = NULL;
  1172. }
  1173. if(!turn_params.no_tls && (turn_params.no_tcp || (turn_params.listener_port != turn_params.tls_listener_port))) {
  1174. tls_services[index] = create_tls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], turn_params.tls_listener_port, turn_params.verbose, e, send_socket_to_general_relay, relay_server);
  1175. if(turn_params.rfc5780)
  1176. tls_services[index+1] = create_tls_listener_server(turn_params.listener_ifname, turn_params.listener.addrs[i], get_alt_tls_listener_port(), turn_params.verbose, e, send_socket_to_general_relay, relay_server);
  1177. } else {
  1178. tls_services[index] = NULL;
  1179. if(turn_params.rfc5780)
  1180. tls_services[index+1] = NULL;
  1181. }
  1182. }
  1183. }
  1184. static int get_alt_addr(ioa_addr *addr, ioa_addr *alt_addr)
  1185. {
  1186. if(!addr || !turn_params.rfc5780 || (turn_params.listener.addrs_number<2))
  1187. ;
  1188. else {
  1189. size_t index = 0xffff;
  1190. size_t i = 0;
  1191. int alt_port = -1;
  1192. int port = addr_get_port(addr);
  1193. if(port == turn_params.listener_port)
  1194. alt_port = get_alt_listener_port();
  1195. else if(port == get_alt_listener_port())
  1196. alt_port = turn_params.listener_port;
  1197. else if(port == turn_params.tls_listener_port)
  1198. alt_port = get_alt_tls_listener_port();
  1199. else if(port == get_alt_tls_listener_port())
  1200. alt_port = turn_params.tls_listener_port;
  1201. else
  1202. return -1;
  1203. for(i=0;i<turn_params.listener.addrs_number;i++) {
  1204. if(turn_params.listener.encaddrs && turn_params.listener.encaddrs[i]) {
  1205. if(addr->ss.sa_family == turn_params.listener.encaddrs[i]->ss.sa_family) {
  1206. index=i;
  1207. break;
  1208. }
  1209. }
  1210. }
  1211. if(index!=0xffff) {
  1212. for(i=0;i<turn_params.listener.addrs_number;i++) {
  1213. size_t ind = (index+i+1) % turn_params.listener.addrs_number;
  1214. if(turn_params.listener.encaddrs && turn_params.listener.encaddrs[ind]) {
  1215. ioa_addr *caddr = turn_params.listener.encaddrs[ind];
  1216. if(caddr->ss.sa_family == addr->ss.sa_family) {
  1217. addr_cpy(alt_addr,caddr);
  1218. addr_set_port(alt_addr, alt_port);
  1219. return 0;
  1220. }
  1221. }
  1222. }
  1223. }
  1224. }
  1225. return -1;
  1226. }
  1227. static void run_events(struct event_base *eb, ioa_engine_handle e)
  1228. {
  1229. if(!eb && e)
  1230. eb = e->event_base;
  1231. if (!eb)
  1232. return;
  1233. struct timeval timeout;
  1234. timeout.tv_sec = 5;
  1235. timeout.tv_usec = 0;
  1236. event_base_loopexit(eb, &timeout);
  1237. event_base_dispatch(eb);
  1238. #if !defined(TURN_NO_HIREDIS)
  1239. if(e)
  1240. send_message_to_redis(e->rch, "publish", "__XXX__", "__YYY__");
  1241. #endif
  1242. }
  1243. void run_listener_server(struct listener_server *ls)
  1244. {
  1245. unsigned int cycle = 0;
  1246. while (!turn_params.stop_turn_server) {
  1247. if (eve(turn_params.verbose)) {
  1248. if ((cycle++ & 15) == 0) {
  1249. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: cycle=%u\n", __FUNCTION__, cycle);
  1250. }
  1251. }
  1252. run_events(ls->event_base, ls->ioa_eng);
  1253. rollover_logfile();
  1254. tm_print();
  1255. }
  1256. }
  1257. static void setup_relay_server(struct relay_server *rs, ioa_engine_handle e, int to_set_rfc5780)
  1258. {
  1259. struct bufferevent *pair[2];
  1260. if(e) {
  1261. rs->event_base = e->event_base;
  1262. rs->ioa_eng = e;
  1263. } else {
  1264. rs->event_base = turn_event_base_new();
  1265. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,"IO method (general relay thread): %s\n",event_base_get_method(rs->event_base));
  1266. rs->ioa_eng = create_ioa_engine(rs->sm, rs->event_base, turn_params.listener.tp, turn_params.relay_ifname,
  1267. turn_params.relays_number, turn_params.relay_addrs, turn_params.default_relays, turn_params.verbose
  1268. #if !defined(TURN_NO_HIREDIS)
  1269. ,turn_params.redis_statsdb
  1270. #endif
  1271. );
  1272. set_ssl_ctx(rs->ioa_eng, turn_params.tls_ctx_ssl23, turn_params.tls_ctx_v1_0,
  1273. #if defined(SSL_TXT_TLSV1_1)
  1274. turn_params.tls_ctx_v1_1,
  1275. #if defined(SSL_TXT_TLSV1_2)
  1276. turn_params.tls_ctx_v1_2,
  1277. #endif
  1278. #endif
  1279. turn_params.dtls_ctx);
  1280. ioa_engine_set_rtcp_map(rs->ioa_eng, turn_params.listener.rtcpmap);
  1281. }
  1282. bufferevent_pair_new(rs->event_base, TURN_BUFFEREVENTS_OPTIONS, pair);
  1283. rs->in_buf = pair[0];
  1284. rs->out_buf = pair[1];
  1285. bufferevent_setcb(rs->in_buf, relay_receive_message, NULL, NULL, rs);
  1286. bufferevent_enable(rs->in_buf, EV_READ);
  1287. bufferevent_pair_new(rs->event_base, TURN_BUFFEREVENTS_OPTIONS, pair);
  1288. rs->auth_in_buf = pair[0];
  1289. rs->auth_out_buf = pair[1];
  1290. bufferevent_setcb(rs->auth_in_buf, relay_receive_auth_message, NULL, NULL, rs);
  1291. bufferevent_enable(rs->auth_in_buf, EV_READ);
  1292. init_turn_server(&(rs->server),
  1293. rs->id, turn_params.verbose,
  1294. rs->ioa_eng, turn_params.ct, 0,
  1295. turn_params.fingerprint, DONT_FRAGMENT_SUPPORTED,
  1296. start_user_check,
  1297. check_new_allocation_quota,
  1298. release_allocation_quota,
  1299. turn_params.external_ip,
  1300. &turn_params.check_origin,
  1301. &turn_params.no_tcp_relay,
  1302. &turn_params.no_udp_relay,
  1303. &turn_params.stale_nonce,
  1304. &turn_params.stun_only,
  1305. &turn_params.no_stun,
  1306. &turn_params.alternate_servers_list,
  1307. &turn_params.tls_alternate_servers_list,
  1308. &turn_params.aux_servers_list,
  1309. turn_params.udp_self_balance,
  1310. &turn_params.no_multicast_peers, &turn_params.no_loopback_peers,
  1311. &turn_params.ip_whitelist, &turn_params.ip_blacklist,
  1312. send_socket_to_relay,
  1313. &turn_params.secure_stun, turn_params.shatype, &turn_params.mobility,
  1314. turn_params.server_relay,
  1315. send_turn_session_info,
  1316. allocate_bps,
  1317. turn_params.oauth, turn_params.oauth_server_name);
  1318. if(to_set_rfc5780) {
  1319. set_rfc5780(&(rs->server), get_alt_addr, send_message_from_listener_to_client);
  1320. }
  1321. if(turn_params.net_engine_version == NEV_UDP_SOCKET_PER_THREAD) {
  1322. setup_tcp_listener_servers(rs->ioa_eng, rs);
  1323. }
  1324. }
  1325. static void *run_general_relay_thread(void *arg)
  1326. {
  1327. static int always_true = 1;
  1328. struct relay_server *rs = (struct relay_server *)arg;
  1329. int udp_reuses_the_same_relay_server = (turn_params.general_relay_servers_number<=1) || (turn_params.net_engine_version == NEV_UDP_SOCKET_PER_THREAD) || (turn_params.net_engine_version == NEV_UDP_SOCKET_PER_SESSION);
  1330. int we_need_rfc5780 = udp_reuses_the_same_relay_server && turn_params.rfc5780;
  1331. ignore_sigpipe();
  1332. setup_relay_server(rs, NULL, we_need_rfc5780);
  1333. barrier_wait();
  1334. while(always_true) {
  1335. run_events(rs->event_base, rs->ioa_eng);
  1336. }
  1337. return arg;
  1338. }
  1339. static void setup_general_relay_servers(void)
  1340. {
  1341. size_t i = 0;
  1342. for(i=0;i<get_real_general_relay_servers_number();i++) {
  1343. if(turn_params.general_relay_servers_number == 0) {
  1344. general_relay_servers[i] = (struct relay_server*)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(struct relay_server));
  1345. general_relay_servers[i]->id = (turnserver_id)i;
  1346. general_relay_servers[i]->sm = NULL;
  1347. setup_relay_server(general_relay_servers[i], turn_params.listener.ioa_eng, ((turn_params.net_engine_version == NEV_UDP_SOCKET_PER_THREAD) || (turn_params.net_engine_version == NEV_UDP_SOCKET_PER_SESSION)) && turn_params.rfc5780);
  1348. general_relay_servers[i]->thr = pthread_self();
  1349. } else {
  1350. super_memory_t *sm = new_super_memory_region();
  1351. general_relay_servers[i] = (struct relay_server*)allocate_super_memory_region(sm,sizeof(struct relay_server));
  1352. general_relay_servers[i]->id = (turnserver_id)i;
  1353. general_relay_servers[i]->sm = sm;
  1354. if(pthread_create(&(general_relay_servers[i]->thr), NULL, run_general_relay_thread, general_relay_servers[i])<0) {
  1355. perror("Cannot create relay thread\n");
  1356. exit(-1);
  1357. }
  1358. pthread_detach(general_relay_servers[i]->thr);
  1359. }
  1360. }
  1361. }
  1362. static int run_auth_server_flag = 1;
  1363. static void* run_auth_server_thread(void *arg)
  1364. {
  1365. ignore_sigpipe();
  1366. ns_bzero(&turn_params.authserver,sizeof(struct auth_server));
  1367. turn_params.authserver.event_base = turn_event_base_new();
  1368. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,"IO method (auth thread): %s\n",event_base_get_method(turn_params.authserver.event_base));
  1369. struct bufferevent *pair[2];
  1370. bufferevent_pair_new(turn_params.authserver.event_base, TURN_BUFFEREVENTS_OPTIONS, pair);
  1371. turn_params.authserver.in_buf = pair[0];
  1372. turn_params.authserver.out_buf = pair[1];
  1373. bufferevent_setcb(turn_params.authserver.in_buf, auth_server_receive_message, NULL, NULL, &turn_params.authserver);
  1374. bufferevent_enable(turn_params.authserver.in_buf, EV_READ);
  1375. #if !defined(TURN_NO_HIREDIS)
  1376. turn_params.authserver.rch = get_redis_async_connection(turn_params.authserver.event_base, turn_params.redis_statsdb, 1);
  1377. #endif
  1378. struct auth_server *authserver = &turn_params.authserver;
  1379. struct event_base *eb = authserver->event_base;
  1380. barrier_wait();
  1381. while(run_auth_server_flag) {
  1382. reread_realms();
  1383. run_events(eb,NULL);
  1384. read_userdb_file(0);
  1385. update_white_and_black_lists();
  1386. auth_ping(authserver->rch);
  1387. #if defined(DB_TEST)
  1388. run_db_test();
  1389. #endif
  1390. }
  1391. return arg;
  1392. }
  1393. static void setup_auth_server(void)
  1394. {
  1395. if(pthread_create(&(turn_params.authserver.thr), NULL, run_auth_server_thread, NULL)<0) {
  1396. perror("Cannot create auth thread\n");
  1397. exit(-1);
  1398. }
  1399. pthread_detach(turn_params.authserver.thr);
  1400. }
  1401. static void* run_cli_server_thread(void *arg)
  1402. {
  1403. ignore_sigpipe();
  1404. setup_cli_thread();
  1405. barrier_wait();
  1406. while(cliserver.event_base) {
  1407. run_events(cliserver.event_base,NULL);
  1408. }
  1409. return arg;
  1410. }
  1411. static void setup_cli_server(void)
  1412. {
  1413. ns_bzero(&cliserver,sizeof(struct cli_server));
  1414. cliserver.listen_fd = -1;
  1415. cliserver.verbose = turn_params.verbose;
  1416. if(pthread_create(&(cliserver.thr), NULL, run_cli_server_thread, &cliserver)<0) {
  1417. perror("Cannot create cli thread\n");
  1418. exit(-1);
  1419. }
  1420. pthread_detach(cliserver.thr);
  1421. }
  1422. void setup_server(void)
  1423. {
  1424. evthread_use_pthreads();
  1425. pthread_mutex_init(&mutex_bps, NULL);
  1426. #if !defined(TURN_NO_THREAD_BARRIERS)
  1427. /* relay threads plus auth thread plus main listener thread */
  1428. /* udp address listener thread(s) will start later */
  1429. barrier_count = turn_params.general_relay_servers_number+2;
  1430. if(use_cli) {
  1431. barrier_count += 1;
  1432. }
  1433. #endif
  1434. setup_listener();
  1435. allocate_relay_addrs_ports();
  1436. setup_barriers();
  1437. setup_general_relay_servers();
  1438. if(turn_params.net_engine_version == NEV_UDP_SOCKET_PER_THREAD)
  1439. setup_socket_per_thread_udp_listener_servers();
  1440. else if(turn_params.net_engine_version == NEV_UDP_SOCKET_PER_ENDPOINT)
  1441. setup_socket_per_endpoint_udp_listener_servers();
  1442. else if(turn_params.net_engine_version == NEV_UDP_SOCKET_PER_SESSION)
  1443. setup_socket_per_session_udp_listener_servers();
  1444. if(turn_params.net_engine_version != NEV_UDP_SOCKET_PER_THREAD) {
  1445. setup_tcp_listener_servers(turn_params.listener.ioa_eng, NULL);
  1446. }
  1447. {
  1448. int tot = 0;
  1449. if(udp_relay_servers[0]) {
  1450. tot = get_real_udp_relay_servers_number();
  1451. }
  1452. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,"Total UDP servers: %d\n",(int)tot);
  1453. }
  1454. {
  1455. int tot = get_real_general_relay_servers_number();
  1456. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,"Total General servers: %d\n",(int)tot);
  1457. int i;
  1458. for(i = 0;i<tot;i++) {
  1459. if(!(general_relay_servers[i])) {
  1460. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,"General server %d is not initialized !\n",(int)i);
  1461. }
  1462. }
  1463. }
  1464. setup_auth_server();
  1465. if(use_cli)
  1466. setup_cli_server();
  1467. barrier_wait();
  1468. }
  1469. void init_listener(void)
  1470. {
  1471. ns_bzero(&turn_params.listener,sizeof(struct listener_server));
  1472. }
  1473. ///////////////////////////////