service.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/service.c
  18. * @brief functions related to starting services (redesign)
  19. * @author Christian Grothoff
  20. * @author Florian Dold
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_protocols.h"
  25. #include "gnunet_constants.h"
  26. #include "gnunet_resolver_service.h"
  27. #include "speedup.h"
  28. #if HAVE_MALLINFO
  29. #include <malloc.h>
  30. #include "gauger.h"
  31. #endif
  32. #define LOG(kind, ...) GNUNET_log_from (kind, "util-service", __VA_ARGS__)
  33. #define LOG_STRERROR(kind, syscall) \
  34. GNUNET_log_from_strerror (kind, "util-service", syscall)
  35. #define LOG_STRERROR_FILE(kind, syscall, filename) \
  36. GNUNET_log_from_strerror_file (kind, "util-service", syscall, filename)
  37. /**
  38. * Information the service tracks per listen operation.
  39. */
  40. struct ServiceListenContext
  41. {
  42. /**
  43. * Kept in a DLL.
  44. */
  45. struct ServiceListenContext *next;
  46. /**
  47. * Kept in a DLL.
  48. */
  49. struct ServiceListenContext *prev;
  50. /**
  51. * Service this listen context belongs to.
  52. */
  53. struct GNUNET_SERVICE_Handle *sh;
  54. /**
  55. * Socket we are listening on.
  56. */
  57. struct GNUNET_NETWORK_Handle *listen_socket;
  58. /**
  59. * Task scheduled to do the listening.
  60. */
  61. struct GNUNET_SCHEDULER_Task *listen_task;
  62. };
  63. /**
  64. * Reasons why we might be suspended.
  65. */
  66. enum SuspendReason
  67. {
  68. /**
  69. * We are running normally.
  70. */
  71. SUSPEND_STATE_NONE = 0,
  72. /**
  73. * Application requested it.
  74. */
  75. SUSPEND_STATE_APP = 1,
  76. /**
  77. * OS ran out of file descriptors.
  78. */
  79. SUSPEND_STATE_EMFILE = 2,
  80. /**
  81. * Both reasons, APP and EMFILE apply.
  82. */
  83. SUSPEND_STATE_APP_AND_EMFILE = 3,
  84. /**
  85. * Suspension because service was permanently shutdown.
  86. */
  87. SUSPEND_STATE_SHUTDOWN = 4
  88. };
  89. /**
  90. * Handle to a service.
  91. */
  92. struct GNUNET_SERVICE_Handle
  93. {
  94. /**
  95. * Our configuration.
  96. */
  97. const struct GNUNET_CONFIGURATION_Handle *cfg;
  98. /**
  99. * Name of our service.
  100. */
  101. const char *service_name;
  102. /**
  103. * Main service-specific task to run.
  104. */
  105. GNUNET_SERVICE_InitCallback service_init_cb;
  106. /**
  107. * Function to call when clients connect.
  108. */
  109. GNUNET_SERVICE_ConnectHandler connect_cb;
  110. /**
  111. * Function to call when clients disconnect / are disconnected.
  112. */
  113. GNUNET_SERVICE_DisconnectHandler disconnect_cb;
  114. /**
  115. * Closure for @e service_init_cb, @e connect_cb, @e disconnect_cb.
  116. */
  117. void *cb_cls;
  118. /**
  119. * DLL of listen sockets used to accept new connections.
  120. */
  121. struct ServiceListenContext *slc_head;
  122. /**
  123. * DLL of listen sockets used to accept new connections.
  124. */
  125. struct ServiceListenContext *slc_tail;
  126. /**
  127. * Our clients, kept in a DLL.
  128. */
  129. struct GNUNET_SERVICE_Client *clients_head;
  130. /**
  131. * Our clients, kept in a DLL.
  132. */
  133. struct GNUNET_SERVICE_Client *clients_tail;
  134. /**
  135. * Message handlers to use for all clients.
  136. */
  137. struct GNUNET_MQ_MessageHandler *handlers;
  138. /**
  139. * Closure for @e task.
  140. */
  141. void *task_cls;
  142. /**
  143. * IPv4 addresses that are not allowed to connect.
  144. */
  145. struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_denied;
  146. /**
  147. * IPv6 addresses that are not allowed to connect.
  148. */
  149. struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_denied;
  150. /**
  151. * IPv4 addresses that are allowed to connect (if not
  152. * set, all are allowed).
  153. */
  154. struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_allowed;
  155. /**
  156. * IPv6 addresses that are allowed to connect (if not
  157. * set, all are allowed).
  158. */
  159. struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_allowed;
  160. /**
  161. * Do we require a matching UID for UNIX domain socket connections?
  162. * #GNUNET_NO means that the UID does not have to match (however,
  163. * @e match_gid may still impose other access control checks).
  164. */
  165. int match_uid;
  166. /**
  167. * Do we require a matching GID for UNIX domain socket connections?
  168. * Ignored if @e match_uid is #GNUNET_YES. Note that this is about
  169. * checking that the client's UID is in our group OR that the
  170. * client's GID is our GID. If both "match_gid" and @e match_uid are
  171. * #GNUNET_NO, all users on the local system have access.
  172. */
  173. int match_gid;
  174. /**
  175. * Are we suspended, and if so, why?
  176. */
  177. enum SuspendReason suspend_state;
  178. /**
  179. * Our options.
  180. */
  181. enum GNUNET_SERVICE_Options options;
  182. /**
  183. * If we are daemonizing, this FD is set to the
  184. * pipe to the parent. Send '.' if we started
  185. * ok, '!' if not. -1 if we are not daemonizing.
  186. */
  187. int ready_confirm_fd;
  188. /**
  189. * Overall success/failure of the service start.
  190. */
  191. int ret;
  192. /**
  193. * If #GNUNET_YES, consider unknown message types an error where the
  194. * client is disconnected.
  195. */
  196. int require_found;
  197. };
  198. /**
  199. * Handle to a client that is connected to a service.
  200. */
  201. struct GNUNET_SERVICE_Client
  202. {
  203. /**
  204. * Kept in a DLL.
  205. */
  206. struct GNUNET_SERVICE_Client *next;
  207. /**
  208. * Kept in a DLL.
  209. */
  210. struct GNUNET_SERVICE_Client *prev;
  211. /**
  212. * Service that this client belongs to.
  213. */
  214. struct GNUNET_SERVICE_Handle *sh;
  215. /**
  216. * Socket of this client.
  217. */
  218. struct GNUNET_NETWORK_Handle *sock;
  219. /**
  220. * Message queue for the client.
  221. */
  222. struct GNUNET_MQ_Handle *mq;
  223. /**
  224. * Tokenizer we use for processing incoming data.
  225. */
  226. struct GNUNET_MessageStreamTokenizer *mst;
  227. /**
  228. * Task that warns about missing calls to
  229. * #GNUNET_SERVICE_client_continue().
  230. */
  231. struct GNUNET_SCHEDULER_Task *warn_task;
  232. /**
  233. * Task run to finish dropping the client after the stack has
  234. * properly unwound.
  235. */
  236. struct GNUNET_SCHEDULER_Task *drop_task;
  237. /**
  238. * Task that receives data from the client to
  239. * pass it to the handlers.
  240. */
  241. struct GNUNET_SCHEDULER_Task *recv_task;
  242. /**
  243. * Task that transmit data to the client.
  244. */
  245. struct GNUNET_SCHEDULER_Task *send_task;
  246. /**
  247. * Pointer to the message to be transmitted by @e send_task.
  248. */
  249. const struct GNUNET_MessageHeader *msg;
  250. /**
  251. * User context value, value returned from
  252. * the connect callback.
  253. */
  254. void *user_context;
  255. /**
  256. * Time when we last gave a message from this client
  257. * to the application.
  258. */
  259. struct GNUNET_TIME_Absolute warn_start;
  260. /**
  261. * Current position in @e msg at which we are transmitting.
  262. */
  263. size_t msg_pos;
  264. /**
  265. * Persist the file handle for this client no matter what happens,
  266. * force the OS to close once the process actually dies. Should only
  267. * be used in special cases!
  268. */
  269. int persist;
  270. /**
  271. * Is this client a 'monitor' client that should not be counted
  272. * when deciding on destroying the server during soft shutdown?
  273. * (see also #GNUNET_SERVICE_start)
  274. */
  275. int is_monitor;
  276. /**
  277. * Are we waiting for the application to call #GNUNET_SERVICE_client_continue()?
  278. */
  279. int needs_continue;
  280. /**
  281. * Type of last message processed (for warn_no_receive_done).
  282. */
  283. uint16_t warn_type;
  284. };
  285. /**
  286. * Check if any of the clients we have left are unrelated to
  287. * monitoring.
  288. *
  289. * @param sh service to check clients for
  290. * @return #GNUNET_YES if we have non-monitoring clients left
  291. */
  292. static int
  293. have_non_monitor_clients (struct GNUNET_SERVICE_Handle *sh)
  294. {
  295. for (struct GNUNET_SERVICE_Client *client = sh->clients_head; NULL != client;
  296. client = client->next)
  297. {
  298. if (client->is_monitor)
  299. continue;
  300. return GNUNET_YES;
  301. }
  302. return GNUNET_NO;
  303. }
  304. /**
  305. * Suspend accepting connections from the listen socket temporarily.
  306. * Resume activity using #do_resume.
  307. *
  308. * @param sh service to stop accepting connections.
  309. * @param sr reason for suspending accepting connections
  310. */
  311. static void
  312. do_suspend (struct GNUNET_SERVICE_Handle *sh, enum SuspendReason sr)
  313. {
  314. struct ServiceListenContext *slc;
  315. GNUNET_assert (0 == (sh->suspend_state & sr));
  316. sh->suspend_state |= sr;
  317. for (slc = sh->slc_head; NULL != slc; slc = slc->next)
  318. {
  319. if (NULL != slc->listen_task)
  320. {
  321. GNUNET_SCHEDULER_cancel (slc->listen_task);
  322. slc->listen_task = NULL;
  323. }
  324. }
  325. }
  326. /**
  327. * Shutdown task triggered when a service should be terminated.
  328. * This considers active clients and the service options to see
  329. * how this specific service is to be terminated, and depending
  330. * on this proceeds with the shutdown logic.
  331. *
  332. * @param cls our `struct GNUNET_SERVICE_Handle`
  333. */
  334. static void
  335. service_shutdown (void *cls)
  336. {
  337. struct GNUNET_SERVICE_Handle *sh = cls;
  338. switch (sh->options & GNUNET_SERVICE_OPTION_SHUTDOWN_BITMASK)
  339. {
  340. case GNUNET_SERVICE_OPTION_NONE:
  341. GNUNET_SERVICE_shutdown (sh);
  342. break;
  343. case GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN:
  344. /* This task should never be run if we are using
  345. the manual shutdown. */
  346. GNUNET_assert (0);
  347. break;
  348. case GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN:
  349. if (0 == (sh->suspend_state & SUSPEND_STATE_SHUTDOWN))
  350. do_suspend (sh, SUSPEND_STATE_SHUTDOWN);
  351. if (GNUNET_NO == have_non_monitor_clients (sh))
  352. GNUNET_SERVICE_shutdown (sh);
  353. break;
  354. }
  355. }
  356. /**
  357. * Check if the given IP address is in the list of IP addresses.
  358. *
  359. * @param list a list of networks
  360. * @param add the IP to check (in network byte order)
  361. * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
  362. */
  363. static int
  364. check_ipv4_listed (const struct GNUNET_STRINGS_IPv4NetworkPolicy *list,
  365. const struct in_addr *add)
  366. {
  367. unsigned int i;
  368. if (NULL == list)
  369. return GNUNET_NO;
  370. i = 0;
  371. while ((0 != list[i].network.s_addr) || (0 != list[i].netmask.s_addr))
  372. {
  373. if ((add->s_addr & list[i].netmask.s_addr) ==
  374. (list[i].network.s_addr & list[i].netmask.s_addr))
  375. return GNUNET_YES;
  376. i++;
  377. }
  378. return GNUNET_NO;
  379. }
  380. /**
  381. * Check if the given IP address is in the list of IP addresses.
  382. *
  383. * @param list a list of networks
  384. * @param ip the IP to check (in network byte order)
  385. * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
  386. */
  387. static int
  388. check_ipv6_listed (const struct GNUNET_STRINGS_IPv6NetworkPolicy *list,
  389. const struct in6_addr *ip)
  390. {
  391. unsigned int i;
  392. if (NULL == list)
  393. return GNUNET_NO;
  394. i = 0;
  395. NEXT:
  396. while (GNUNET_NO == GNUNET_is_zero (&list[i].network))
  397. {
  398. for (unsigned int j = 0; j < sizeof(struct in6_addr) / sizeof(int); j++)
  399. if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
  400. (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
  401. {
  402. i++;
  403. goto NEXT;
  404. }
  405. return GNUNET_YES;
  406. }
  407. return GNUNET_NO;
  408. }
  409. /**
  410. * Task run when we are ready to transmit data to the
  411. * client.
  412. *
  413. * @param cls the `struct GNUNET_SERVICE_Client *` to send to
  414. */
  415. static void
  416. do_send (void *cls)
  417. {
  418. struct GNUNET_SERVICE_Client *client = cls;
  419. ssize_t ret;
  420. size_t left;
  421. const char *buf;
  422. LOG (GNUNET_ERROR_TYPE_DEBUG,
  423. "service: sending message with type %u\n",
  424. ntohs (client->msg->type));
  425. client->send_task = NULL;
  426. buf = (const char *) client->msg;
  427. left = ntohs (client->msg->size) - client->msg_pos;
  428. ret = GNUNET_NETWORK_socket_send (client->sock,
  429. &buf[client->msg_pos],
  430. left);
  431. GNUNET_assert (ret <= (ssize_t) left);
  432. if (0 == ret)
  433. {
  434. LOG (GNUNET_ERROR_TYPE_DEBUG, "no data send");
  435. GNUNET_MQ_inject_error (client->mq, GNUNET_MQ_ERROR_WRITE);
  436. return;
  437. }
  438. if (-1 == ret)
  439. {
  440. if ((EAGAIN == errno) || (EINTR == errno))
  441. {
  442. /* ignore */
  443. ret = 0;
  444. }
  445. else
  446. {
  447. if (EPIPE != errno)
  448. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send");
  449. LOG (GNUNET_ERROR_TYPE_DEBUG,
  450. "socket send returned with error code %i",
  451. errno);
  452. GNUNET_MQ_inject_error (client->mq, GNUNET_MQ_ERROR_WRITE);
  453. return;
  454. }
  455. }
  456. if (0 == client->msg_pos)
  457. {
  458. GNUNET_MQ_impl_send_in_flight (client->mq);
  459. }
  460. client->msg_pos += ret;
  461. if (left > (size_t) ret)
  462. {
  463. GNUNET_assert (NULL == client->drop_task);
  464. client->send_task =
  465. GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
  466. client->sock,
  467. &do_send,
  468. client);
  469. return;
  470. }
  471. GNUNET_MQ_impl_send_continue (client->mq);
  472. }
  473. /**
  474. * Signature of functions implementing the sending functionality of a
  475. * message queue.
  476. *
  477. * @param mq the message queue
  478. * @param msg the message to send
  479. * @param impl_state our `struct GNUNET_SERVICE_Client *`
  480. */
  481. static void
  482. service_mq_send (struct GNUNET_MQ_Handle *mq,
  483. const struct GNUNET_MessageHeader *msg,
  484. void *impl_state)
  485. {
  486. struct GNUNET_SERVICE_Client *client = impl_state;
  487. (void) mq;
  488. if (NULL != client->drop_task)
  489. return; /* we're going down right now, do not try to send */
  490. GNUNET_assert (NULL == client->send_task);
  491. LOG (GNUNET_ERROR_TYPE_DEBUG,
  492. "Sending message of type %u and size %u to client\n",
  493. ntohs (msg->type),
  494. ntohs (msg->size));
  495. client->msg = msg;
  496. client->msg_pos = 0;
  497. client->send_task = GNUNET_SCHEDULER_add_now (&do_send,
  498. client);
  499. }
  500. /**
  501. * Implementation function that cancels the currently sent message.
  502. *
  503. * @param mq message queue
  504. * @param impl_state state specific to the implementation
  505. */
  506. static void
  507. service_mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
  508. {
  509. struct GNUNET_SERVICE_Client *client = impl_state;
  510. (void) mq;
  511. GNUNET_assert (0 == client->msg_pos);
  512. client->msg = NULL;
  513. GNUNET_SCHEDULER_cancel (client->send_task);
  514. client->send_task = NULL;
  515. }
  516. /**
  517. * Generic error handler, called with the appropriate
  518. * error code and the same closure specified at the creation of
  519. * the message queue.
  520. * Not every message queue implementation supports an error handler.
  521. *
  522. * @param cls closure with our `struct GNUNET_SERVICE_Client`
  523. * @param error error code
  524. */
  525. static void
  526. service_mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
  527. {
  528. struct GNUNET_SERVICE_Client *client = cls;
  529. struct GNUNET_SERVICE_Handle *sh = client->sh;
  530. if ((GNUNET_MQ_ERROR_NO_MATCH == error) && (GNUNET_NO == sh->require_found))
  531. {
  532. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  533. "No handler for message of type %u found\n",
  534. (unsigned int) client->warn_type);
  535. GNUNET_SERVICE_client_continue (client);
  536. return; /* ignore error */
  537. }
  538. GNUNET_SERVICE_client_drop (client);
  539. }
  540. /**
  541. * Task run to warn about missing calls to #GNUNET_SERVICE_client_continue().
  542. *
  543. * @param cls our `struct GNUNET_SERVICE_Client *` to process more requests from
  544. */
  545. static void
  546. warn_no_client_continue (void *cls)
  547. {
  548. struct GNUNET_SERVICE_Client *client = cls;
  549. GNUNET_break (
  550. 0 !=
  551. client->warn_type); /* type should never be 0 here, as we don't use 0 */
  552. client->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
  553. &warn_no_client_continue,
  554. client);
  555. LOG (
  556. GNUNET_ERROR_TYPE_WARNING,
  557. _ (
  558. "Processing code for message of type %u did not call `GNUNET_SERVICE_client_continue' after %s\n"),
  559. (unsigned int) client->warn_type,
  560. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (
  561. client->warn_start),
  562. GNUNET_YES));
  563. }
  564. /**
  565. * Functions with this signature are called whenever a
  566. * complete message is received by the tokenizer for a client.
  567. *
  568. * Do not call #GNUNET_MST_destroy() from within
  569. * the scope of this callback.
  570. *
  571. * @param cls closure with the `struct GNUNET_SERVICE_Client *`
  572. * @param message the actual message
  573. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the client was dropped
  574. */
  575. static int
  576. service_client_mst_cb (void *cls, const struct GNUNET_MessageHeader *message)
  577. {
  578. struct GNUNET_SERVICE_Client *client = cls;
  579. LOG (GNUNET_ERROR_TYPE_DEBUG,
  580. "Received message of type %u and size %u from client\n",
  581. ntohs (message->type),
  582. ntohs (message->size));
  583. GNUNET_assert (GNUNET_NO == client->needs_continue);
  584. client->needs_continue = GNUNET_YES;
  585. client->warn_type = ntohs (message->type);
  586. client->warn_start = GNUNET_TIME_absolute_get ();
  587. GNUNET_assert (NULL == client->warn_task);
  588. client->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
  589. &warn_no_client_continue,
  590. client);
  591. GNUNET_MQ_inject_message (client->mq, message);
  592. if (NULL != client->drop_task)
  593. return GNUNET_SYSERR;
  594. return GNUNET_OK;
  595. }
  596. /**
  597. * A client sent us data. Receive and process it. If we are done,
  598. * reschedule this task.
  599. *
  600. * @param cls the `struct GNUNET_SERVICE_Client` that sent us data.
  601. */
  602. static void
  603. service_client_recv (void *cls)
  604. {
  605. struct GNUNET_SERVICE_Client *client = cls;
  606. int ret;
  607. client->recv_task = NULL;
  608. ret = GNUNET_MST_read (client->mst, client->sock, GNUNET_NO, GNUNET_YES);
  609. if (GNUNET_SYSERR == ret)
  610. {
  611. /* client closed connection (or IO error) */
  612. if (NULL == client->drop_task)
  613. {
  614. GNUNET_assert (GNUNET_NO == client->needs_continue);
  615. GNUNET_SERVICE_client_drop (client);
  616. }
  617. return;
  618. }
  619. if (GNUNET_NO == ret)
  620. return; /* more messages in buffer, wait for application
  621. to be done processing */
  622. GNUNET_assert (GNUNET_OK == ret);
  623. if (GNUNET_YES == client->needs_continue)
  624. return;
  625. if (NULL != client->recv_task)
  626. return;
  627. /* MST needs more data, re-schedule read job */
  628. client->recv_task =
  629. GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  630. client->sock,
  631. &service_client_recv,
  632. client);
  633. }
  634. /**
  635. * We have successfully accepted a connection from a client. Now
  636. * setup the client (with the scheduler) and tell the application.
  637. *
  638. * @param sh service that accepted the client
  639. * @param sock socket associated with the client
  640. */
  641. static void
  642. start_client (struct GNUNET_SERVICE_Handle *sh,
  643. struct GNUNET_NETWORK_Handle *csock)
  644. {
  645. struct GNUNET_SERVICE_Client *client;
  646. client = GNUNET_new (struct GNUNET_SERVICE_Client);
  647. GNUNET_CONTAINER_DLL_insert (sh->clients_head, sh->clients_tail, client);
  648. client->sh = sh;
  649. client->sock = csock;
  650. client->mq = GNUNET_MQ_queue_for_callbacks (&service_mq_send,
  651. NULL,
  652. &service_mq_cancel,
  653. client,
  654. sh->handlers,
  655. &service_mq_error_handler,
  656. client);
  657. client->mst = GNUNET_MST_create (&service_client_mst_cb, client);
  658. if (NULL != sh->connect_cb)
  659. client->user_context = sh->connect_cb (sh->cb_cls, client, client->mq);
  660. GNUNET_MQ_set_handlers_closure (client->mq, client->user_context);
  661. client->recv_task =
  662. GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  663. client->sock,
  664. &service_client_recv,
  665. client);
  666. }
  667. /**
  668. * We have a client. Accept the incoming socket(s) (and reschedule
  669. * the listen task).
  670. *
  671. * @param cls the `struct ServiceListenContext` of the ready listen socket
  672. */
  673. static void
  674. accept_client (void *cls)
  675. {
  676. struct ServiceListenContext *slc = cls;
  677. struct GNUNET_SERVICE_Handle *sh = slc->sh;
  678. slc->listen_task = NULL;
  679. while (1)
  680. {
  681. struct GNUNET_NETWORK_Handle *sock;
  682. const struct sockaddr_in *v4;
  683. const struct sockaddr_in6 *v6;
  684. struct sockaddr_storage sa;
  685. socklen_t addrlen;
  686. int ok;
  687. addrlen = sizeof(sa);
  688. sock = GNUNET_NETWORK_socket_accept (slc->listen_socket,
  689. (struct sockaddr *) &sa,
  690. &addrlen);
  691. if (NULL == sock)
  692. {
  693. if (EMFILE == errno)
  694. do_suspend (sh, SUSPEND_STATE_EMFILE);
  695. else if (EAGAIN != errno)
  696. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
  697. break;
  698. }
  699. switch (sa.ss_family)
  700. {
  701. case AF_INET:
  702. GNUNET_assert (addrlen == sizeof(struct sockaddr_in));
  703. v4 = (const struct sockaddr_in *) &sa;
  704. ok = (((NULL == sh->v4_allowed) ||
  705. (check_ipv4_listed (sh->v4_allowed, &v4->sin_addr))) &&
  706. ((NULL == sh->v4_denied) ||
  707. (! check_ipv4_listed (sh->v4_denied, &v4->sin_addr))));
  708. break;
  709. case AF_INET6:
  710. GNUNET_assert (addrlen == sizeof(struct sockaddr_in6));
  711. v6 = (const struct sockaddr_in6 *) &sa;
  712. ok = (((NULL == sh->v6_allowed) ||
  713. (check_ipv6_listed (sh->v6_allowed, &v6->sin6_addr))) &&
  714. ((NULL == sh->v6_denied) ||
  715. (! check_ipv6_listed (sh->v6_denied, &v6->sin6_addr))));
  716. break;
  717. case AF_UNIX:
  718. ok = GNUNET_OK; /* controlled using file-system ACL now */
  719. break;
  720. default:
  721. LOG (GNUNET_ERROR_TYPE_WARNING,
  722. _ ("Unknown address family %d\n"),
  723. sa.ss_family);
  724. return;
  725. }
  726. if (! ok)
  727. {
  728. LOG (GNUNET_ERROR_TYPE_DEBUG,
  729. "Service rejected incoming connection from %s due to policy.\n",
  730. GNUNET_a2s ((const struct sockaddr *) &sa, addrlen));
  731. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
  732. continue;
  733. }
  734. LOG (GNUNET_ERROR_TYPE_DEBUG,
  735. "Service accepted incoming connection from %s.\n",
  736. GNUNET_a2s ((const struct sockaddr *) &sa, addrlen));
  737. start_client (slc->sh, sock);
  738. }
  739. if (0 != sh->suspend_state)
  740. return;
  741. slc->listen_task =
  742. GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  743. slc->listen_socket,
  744. &accept_client,
  745. slc);
  746. }
  747. /**
  748. * Resume accepting connections from the listen socket.
  749. *
  750. * @param sh service to resume accepting connections.
  751. * @param sr reason that is no longer causing the suspension,
  752. * or #SUSPEND_STATE_NONE on first startup
  753. */
  754. static void
  755. do_resume (struct GNUNET_SERVICE_Handle *sh, enum SuspendReason sr)
  756. {
  757. struct ServiceListenContext *slc;
  758. GNUNET_assert ((SUSPEND_STATE_NONE == sr) || (0 != (sh->suspend_state & sr)));
  759. sh->suspend_state -= sr;
  760. if (SUSPEND_STATE_NONE != sh->suspend_state)
  761. return;
  762. for (slc = sh->slc_head; NULL != slc; slc = slc->next)
  763. {
  764. GNUNET_assert (NULL == slc->listen_task);
  765. slc->listen_task =
  766. GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  767. slc->listen_socket,
  768. &accept_client,
  769. slc);
  770. }
  771. }
  772. /**
  773. * First task run by any service. Initializes our shutdown task,
  774. * starts the listening operation on our listen sockets and launches
  775. * the custom logic of the application service.
  776. *
  777. * @param cls our `struct GNUNET_SERVICE_Handle`
  778. */
  779. static void
  780. service_main (void *cls)
  781. {
  782. struct GNUNET_SERVICE_Handle *sh = cls;
  783. if (GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN !=
  784. (sh->options & GNUNET_SERVICE_OPTION_SHUTDOWN_BITMASK))
  785. GNUNET_SCHEDULER_add_shutdown (&service_shutdown, sh);
  786. do_resume (sh, SUSPEND_STATE_NONE);
  787. if (-1 != sh->ready_confirm_fd)
  788. {
  789. GNUNET_break (1 == write (sh->ready_confirm_fd, ".", 1));
  790. GNUNET_break (0 == close (sh->ready_confirm_fd));
  791. sh->ready_confirm_fd = -1;
  792. }
  793. if (NULL != sh->service_init_cb)
  794. sh->service_init_cb (sh->cb_cls, sh->cfg, sh);
  795. }
  796. /**
  797. * Parse an IPv4 access control list.
  798. *
  799. * @param ret location where to write the ACL (set)
  800. * @param sh service context to use to get the configuration
  801. * @param option name of the ACL option to parse
  802. * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
  803. * no ACL configured)
  804. */
  805. static int
  806. process_acl4 (struct GNUNET_STRINGS_IPv4NetworkPolicy **ret,
  807. struct GNUNET_SERVICE_Handle *sh,
  808. const char *option)
  809. {
  810. char *opt;
  811. if (! GNUNET_CONFIGURATION_have_value (sh->cfg, sh->service_name, option))
  812. {
  813. *ret = NULL;
  814. return GNUNET_OK;
  815. }
  816. GNUNET_break (GNUNET_OK ==
  817. GNUNET_CONFIGURATION_get_value_string (sh->cfg,
  818. sh->service_name,
  819. option,
  820. &opt));
  821. if (NULL == (*ret = GNUNET_STRINGS_parse_ipv4_policy (opt)))
  822. {
  823. LOG (GNUNET_ERROR_TYPE_WARNING,
  824. _ ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
  825. opt,
  826. sh->service_name,
  827. option);
  828. GNUNET_free (opt);
  829. return GNUNET_SYSERR;
  830. }
  831. GNUNET_free (opt);
  832. return GNUNET_OK;
  833. }
  834. /**
  835. * Parse an IPv6 access control list.
  836. *
  837. * @param ret location where to write the ACL (set)
  838. * @param sh service context to use to get the configuration
  839. * @param option name of the ACL option to parse
  840. * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
  841. * no ACL configured)
  842. */
  843. static int
  844. process_acl6 (struct GNUNET_STRINGS_IPv6NetworkPolicy **ret,
  845. struct GNUNET_SERVICE_Handle *sh,
  846. const char *option)
  847. {
  848. char *opt;
  849. if (! GNUNET_CONFIGURATION_have_value (sh->cfg, sh->service_name, option))
  850. {
  851. *ret = NULL;
  852. return GNUNET_OK;
  853. }
  854. GNUNET_break (GNUNET_OK ==
  855. GNUNET_CONFIGURATION_get_value_string (sh->cfg,
  856. sh->service_name,
  857. option,
  858. &opt));
  859. if (NULL == (*ret = GNUNET_STRINGS_parse_ipv6_policy (opt)))
  860. {
  861. LOG (GNUNET_ERROR_TYPE_WARNING,
  862. _ ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
  863. opt,
  864. sh->service_name,
  865. option);
  866. GNUNET_free (opt);
  867. return GNUNET_SYSERR;
  868. }
  869. GNUNET_free (opt);
  870. return GNUNET_OK;
  871. }
  872. /**
  873. * Add the given UNIX domain path as an address to the
  874. * list (as the first entry).
  875. *
  876. * @param saddrs array to update
  877. * @param saddrlens where to store the address length
  878. * @param unixpath path to add
  879. */
  880. static void
  881. add_unixpath (struct sockaddr **saddrs,
  882. socklen_t *saddrlens,
  883. const char *unixpath)
  884. {
  885. #ifdef AF_UNIX
  886. struct sockaddr_un *un;
  887. un = GNUNET_new (struct sockaddr_un);
  888. un->sun_family = AF_UNIX;
  889. GNUNET_strlcpy (un->sun_path, unixpath, sizeof(un->sun_path));
  890. #if HAVE_SOCKADDR_UN_SUN_LEN
  891. un->sun_len = (u_char) sizeof(struct sockaddr_un);
  892. #endif
  893. *saddrs = (struct sockaddr *) un;
  894. *saddrlens = sizeof(struct sockaddr_un);
  895. #else
  896. /* this function should never be called
  897. * unless AF_UNIX is defined! */
  898. GNUNET_assert (0);
  899. #endif
  900. }
  901. /**
  902. * Get the list of addresses that a server for the given service
  903. * should bind to.
  904. *
  905. * @param service_name name of the service
  906. * @param cfg configuration (which specifies the addresses)
  907. * @param addrs set (call by reference) to an array of pointers to the
  908. * addresses the server should bind to and listen on; the
  909. * array will be NULL-terminated (on success)
  910. * @param addr_lens set (call by reference) to an array of the lengths
  911. * of the respective `struct sockaddr` struct in the @a addrs
  912. * array (on success)
  913. * @return number of addresses found on success,
  914. * #GNUNET_SYSERR if the configuration
  915. * did not specify reasonable finding information or
  916. * if it specified a hostname that could not be resolved;
  917. * #GNUNET_NO if the number of addresses configured is
  918. * zero (in this case, `*addrs` and `*addr_lens` will be
  919. * set to NULL).
  920. */
  921. static int
  922. get_server_addresses (const char *service_name,
  923. const struct GNUNET_CONFIGURATION_Handle *cfg,
  924. struct sockaddr ***addrs,
  925. socklen_t **addr_lens)
  926. {
  927. int disablev6;
  928. struct GNUNET_NETWORK_Handle *desc;
  929. unsigned long long port;
  930. char *unixpath;
  931. struct addrinfo hints;
  932. struct addrinfo *res;
  933. struct addrinfo *pos;
  934. struct addrinfo *next;
  935. unsigned int i;
  936. int resi;
  937. int ret;
  938. struct sockaddr **saddrs;
  939. socklen_t *saddrlens;
  940. char *hostname;
  941. *addrs = NULL;
  942. *addr_lens = NULL;
  943. desc = NULL;
  944. disablev6 = GNUNET_NO;
  945. if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
  946. (GNUNET_YES ==
  947. GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
  948. disablev6 = GNUNET_YES;
  949. port = 0;
  950. if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
  951. {
  952. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
  953. service_name,
  954. "PORT",
  955. &port))
  956. {
  957. LOG (GNUNET_ERROR_TYPE_ERROR,
  958. _ ("Require valid port number for service `%s' in configuration!\n"),
  959. service_name);
  960. }
  961. if (port > 65535)
  962. {
  963. LOG (GNUNET_ERROR_TYPE_ERROR,
  964. _ ("Require valid port number for service `%s' in configuration!\n"),
  965. service_name);
  966. return GNUNET_SYSERR;
  967. }
  968. }
  969. if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
  970. {
  971. GNUNET_break (GNUNET_OK ==
  972. GNUNET_CONFIGURATION_get_value_string (cfg,
  973. service_name,
  974. "BINDTO",
  975. &hostname));
  976. }
  977. else
  978. hostname = NULL;
  979. unixpath = NULL;
  980. #ifdef AF_UNIX
  981. if ((GNUNET_YES ==
  982. GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
  983. (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg,
  984. service_name,
  985. "UNIXPATH",
  986. &unixpath)) &&
  987. (0 < strlen (unixpath)))
  988. {
  989. /* probe UNIX support */
  990. struct sockaddr_un s_un;
  991. if (strlen (unixpath) >= sizeof(s_un.sun_path))
  992. {
  993. LOG (GNUNET_ERROR_TYPE_WARNING,
  994. _ ("UNIXPATH `%s' too long, maximum length is %llu\n"),
  995. unixpath,
  996. (unsigned long long) sizeof(s_un.sun_path));
  997. unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
  998. LOG (GNUNET_ERROR_TYPE_INFO, _ ("Using `%s' instead\n"), unixpath);
  999. }
  1000. if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (unixpath))
  1001. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "mkdir", unixpath);
  1002. }
  1003. if (NULL != unixpath)
  1004. {
  1005. desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
  1006. if (NULL == desc)
  1007. {
  1008. if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
  1009. (EACCES == errno))
  1010. {
  1011. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
  1012. GNUNET_free (hostname);
  1013. GNUNET_free (unixpath);
  1014. return GNUNET_SYSERR;
  1015. }
  1016. LOG (GNUNET_ERROR_TYPE_INFO,
  1017. _ (
  1018. "Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
  1019. service_name,
  1020. strerror (errno));
  1021. GNUNET_free (unixpath);
  1022. unixpath = NULL;
  1023. }
  1024. else
  1025. {
  1026. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
  1027. desc = NULL;
  1028. }
  1029. }
  1030. #endif
  1031. if ((0 == port) && (NULL == unixpath))
  1032. {
  1033. LOG (GNUNET_ERROR_TYPE_ERROR,
  1034. _ (
  1035. "Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
  1036. service_name);
  1037. GNUNET_free (hostname);
  1038. return GNUNET_SYSERR;
  1039. }
  1040. if (0 == port)
  1041. {
  1042. saddrs = GNUNET_new_array (2, struct sockaddr *);
  1043. saddrlens = GNUNET_new_array (2, socklen_t);
  1044. add_unixpath (saddrs, saddrlens, unixpath);
  1045. GNUNET_free (unixpath);
  1046. GNUNET_free (hostname);
  1047. *addrs = saddrs;
  1048. *addr_lens = saddrlens;
  1049. return 1;
  1050. }
  1051. if (NULL != hostname)
  1052. {
  1053. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1054. "Resolving `%s' since that is where `%s' will bind to.\n",
  1055. hostname,
  1056. service_name);
  1057. memset (&hints, 0, sizeof(struct addrinfo));
  1058. if (disablev6)
  1059. hints.ai_family = AF_INET;
  1060. hints.ai_protocol = IPPROTO_TCP;
  1061. if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
  1062. (NULL == res))
  1063. {
  1064. LOG (GNUNET_ERROR_TYPE_ERROR,
  1065. _ ("Failed to resolve `%s': %s\n"),
  1066. hostname,
  1067. gai_strerror (ret));
  1068. GNUNET_free (hostname);
  1069. GNUNET_free (unixpath);
  1070. return GNUNET_SYSERR;
  1071. }
  1072. next = res;
  1073. i = 0;
  1074. while (NULL != (pos = next))
  1075. {
  1076. next = pos->ai_next;
  1077. if ((disablev6) && (pos->ai_family == AF_INET6))
  1078. continue;
  1079. i++;
  1080. }
  1081. if (0 == i)
  1082. {
  1083. LOG (GNUNET_ERROR_TYPE_ERROR,
  1084. _ ("Failed to find %saddress for `%s'.\n"),
  1085. disablev6 ? "IPv4 " : "",
  1086. hostname);
  1087. freeaddrinfo (res);
  1088. GNUNET_free (hostname);
  1089. GNUNET_free (unixpath);
  1090. return GNUNET_SYSERR;
  1091. }
  1092. resi = i;
  1093. if (NULL != unixpath)
  1094. resi++;
  1095. saddrs = GNUNET_new_array (resi + 1, struct sockaddr *);
  1096. saddrlens = GNUNET_new_array (resi + 1, socklen_t);
  1097. i = 0;
  1098. if (NULL != unixpath)
  1099. {
  1100. add_unixpath (saddrs, saddrlens, unixpath);
  1101. i++;
  1102. }
  1103. next = res;
  1104. while (NULL != (pos = next))
  1105. {
  1106. next = pos->ai_next;
  1107. if ((disablev6) && (AF_INET6 == pos->ai_family))
  1108. continue;
  1109. if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
  1110. continue; /* not TCP */
  1111. if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
  1112. continue; /* huh? */
  1113. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1114. "Service `%s' will bind to `%s'\n",
  1115. service_name,
  1116. GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
  1117. if (AF_INET == pos->ai_family)
  1118. {
  1119. GNUNET_assert (sizeof(struct sockaddr_in) == pos->ai_addrlen);
  1120. saddrlens[i] = pos->ai_addrlen;
  1121. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  1122. GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
  1123. ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
  1124. }
  1125. else
  1126. {
  1127. GNUNET_assert (AF_INET6 == pos->ai_family);
  1128. GNUNET_assert (sizeof(struct sockaddr_in6) == pos->ai_addrlen);
  1129. saddrlens[i] = pos->ai_addrlen;
  1130. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  1131. GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
  1132. ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
  1133. }
  1134. i++;
  1135. }
  1136. GNUNET_free (hostname);
  1137. freeaddrinfo (res);
  1138. resi = i;
  1139. }
  1140. else
  1141. {
  1142. /* will bind against everything, just set port */
  1143. if (disablev6)
  1144. {
  1145. /* V4-only */
  1146. resi = 1;
  1147. if (NULL != unixpath)
  1148. resi++;
  1149. i = 0;
  1150. saddrs = GNUNET_new_array (resi + 1, struct sockaddr *);
  1151. saddrlens = GNUNET_new_array (resi + 1, socklen_t);
  1152. if (NULL != unixpath)
  1153. {
  1154. add_unixpath (saddrs, saddrlens, unixpath);
  1155. i++;
  1156. }
  1157. saddrlens[i] = sizeof(struct sockaddr_in);
  1158. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  1159. #if HAVE_SOCKADDR_IN_SIN_LEN
  1160. ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
  1161. #endif
  1162. ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
  1163. ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
  1164. }
  1165. else
  1166. {
  1167. /* dual stack */
  1168. resi = 2;
  1169. if (NULL != unixpath)
  1170. resi++;
  1171. saddrs = GNUNET_new_array (resi + 1, struct sockaddr *);
  1172. saddrlens = GNUNET_new_array (resi + 1, socklen_t);
  1173. i = 0;
  1174. if (NULL != unixpath)
  1175. {
  1176. add_unixpath (saddrs, saddrlens, unixpath);
  1177. i++;
  1178. }
  1179. saddrlens[i] = sizeof(struct sockaddr_in6);
  1180. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  1181. #if HAVE_SOCKADDR_IN_SIN_LEN
  1182. ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
  1183. #endif
  1184. ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
  1185. ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
  1186. i++;
  1187. saddrlens[i] = sizeof(struct sockaddr_in);
  1188. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  1189. #if HAVE_SOCKADDR_IN_SIN_LEN
  1190. ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
  1191. #endif
  1192. ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
  1193. ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
  1194. }
  1195. }
  1196. GNUNET_free (unixpath);
  1197. *addrs = saddrs;
  1198. *addr_lens = saddrlens;
  1199. return resi;
  1200. }
  1201. /**
  1202. * Create and initialize a listen socket for the server.
  1203. *
  1204. * @param server_addr address to listen on
  1205. * @param socklen length of @a server_addr
  1206. * @return NULL on error, otherwise the listen socket
  1207. */
  1208. static struct GNUNET_NETWORK_Handle *
  1209. open_listen_socket (const struct sockaddr *server_addr,
  1210. socklen_t socklen)
  1211. {
  1212. struct GNUNET_NETWORK_Handle *sock;
  1213. uint16_t port;
  1214. int eno;
  1215. switch (server_addr->sa_family)
  1216. {
  1217. case AF_INET:
  1218. port = ntohs (((const struct sockaddr_in *) server_addr)->sin_port);
  1219. break;
  1220. case AF_INET6:
  1221. port = ntohs (((const struct sockaddr_in6 *) server_addr)->sin6_port);
  1222. break;
  1223. case AF_UNIX:
  1224. port = 0;
  1225. break;
  1226. default:
  1227. GNUNET_break (0);
  1228. port = 0;
  1229. break;
  1230. }
  1231. sock = GNUNET_NETWORK_socket_create (server_addr->sa_family,
  1232. SOCK_STREAM,
  1233. 0);
  1234. if (NULL == sock)
  1235. {
  1236. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
  1237. "socket");
  1238. errno = 0;
  1239. return NULL;
  1240. }
  1241. /* bind the socket */
  1242. if (GNUNET_OK !=
  1243. GNUNET_NETWORK_socket_bind (sock,
  1244. server_addr,
  1245. socklen))
  1246. {
  1247. eno = errno;
  1248. if (EADDRINUSE != errno)
  1249. {
  1250. /* we don't log 'EADDRINUSE' here since an IPv4 bind may
  1251. * fail if we already took the port on IPv6; if both IPv4 and
  1252. * IPv6 binds fail, then our caller will log using the
  1253. * errno preserved in 'eno' */
  1254. if (0 != port)
  1255. LOG (GNUNET_ERROR_TYPE_ERROR,
  1256. _ ("`%s' failed for port %d (%s).\n"),
  1257. "bind",
  1258. port,
  1259. (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
  1260. else
  1261. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
  1262. eno = 0;
  1263. }
  1264. else
  1265. {
  1266. if (0 != port)
  1267. LOG (GNUNET_ERROR_TYPE_WARNING,
  1268. _ ("`%s' failed for port %d (%s): address already in use\n"),
  1269. "bind",
  1270. port,
  1271. (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
  1272. else if (AF_UNIX == server_addr->sa_family)
  1273. {
  1274. LOG (GNUNET_ERROR_TYPE_WARNING,
  1275. _ ("`%s' failed for `%s': address already in use\n"),
  1276. "bind",
  1277. GNUNET_a2s (server_addr, socklen));
  1278. }
  1279. }
  1280. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
  1281. errno = eno;
  1282. return NULL;
  1283. }
  1284. if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
  1285. {
  1286. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "listen");
  1287. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
  1288. errno = 0;
  1289. return NULL;
  1290. }
  1291. if (0 != port)
  1292. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1293. "Server starts to listen on port %u.\n",
  1294. port);
  1295. return sock;
  1296. }
  1297. /**
  1298. * Setup service handle
  1299. *
  1300. * Configuration may specify:
  1301. * - PORT (where to bind to for TCP)
  1302. * - UNIXPATH (where to bind to for UNIX domain sockets)
  1303. * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
  1304. * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
  1305. * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
  1306. * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
  1307. * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
  1308. * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
  1309. *
  1310. * @param sh service context to initialize
  1311. * @return #GNUNET_OK if configuration succeeded
  1312. */
  1313. static int
  1314. setup_service (struct GNUNET_SERVICE_Handle *sh)
  1315. {
  1316. int tolerant;
  1317. struct GNUNET_NETWORK_Handle **csocks = NULL;
  1318. struct GNUNET_NETWORK_Handle **lsocks;
  1319. const char *nfds;
  1320. unsigned int cnt;
  1321. int flags;
  1322. char dummy[2];
  1323. if (GNUNET_CONFIGURATION_have_value (sh->cfg,
  1324. sh->service_name,
  1325. "TOLERANT"))
  1326. {
  1327. if (GNUNET_SYSERR ==
  1328. (tolerant = GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
  1329. sh->service_name,
  1330. "TOLERANT")))
  1331. {
  1332. LOG (GNUNET_ERROR_TYPE_ERROR,
  1333. _ ("Specified value for `%s' of service `%s' is invalid\n"),
  1334. "TOLERANT",
  1335. sh->service_name);
  1336. return GNUNET_SYSERR;
  1337. }
  1338. }
  1339. else
  1340. tolerant = GNUNET_NO;
  1341. lsocks = NULL;
  1342. errno = 0;
  1343. if ((NULL != (nfds = getenv ("LISTEN_FDS"))) &&
  1344. (1 == sscanf (nfds, "%u%1s", &cnt, dummy)) && (cnt > 0) &&
  1345. (cnt < FD_SETSIZE) && (cnt + 4 < FD_SETSIZE))
  1346. {
  1347. lsocks = GNUNET_new_array (cnt + 1, struct GNUNET_NETWORK_Handle *);
  1348. while (0 < cnt--)
  1349. {
  1350. flags = fcntl (3 + cnt, F_GETFD);
  1351. if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
  1352. (NULL == (lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
  1353. {
  1354. LOG (GNUNET_ERROR_TYPE_ERROR,
  1355. _ (
  1356. "Could not access pre-bound socket %u, will try to bind myself\n"),
  1357. (unsigned int) 3 + cnt);
  1358. cnt++;
  1359. while (NULL != lsocks[cnt])
  1360. GNUNET_break (GNUNET_OK ==
  1361. GNUNET_NETWORK_socket_close (lsocks[cnt++]));
  1362. GNUNET_free (lsocks);
  1363. lsocks = NULL;
  1364. break;
  1365. }
  1366. }
  1367. unsetenv ("LISTEN_FDS");
  1368. }
  1369. if ( (0 != (GNUNET_SERVICE_OPTION_CLOSE_LSOCKS & sh->options)) &&
  1370. (NULL != lsocks) )
  1371. {
  1372. csocks = lsocks;
  1373. lsocks = NULL;
  1374. }
  1375. if (NULL != lsocks)
  1376. {
  1377. /* listen only on inherited sockets if we have any */
  1378. for (struct GNUNET_NETWORK_Handle **ls = lsocks; NULL != *ls; ls++)
  1379. {
  1380. struct ServiceListenContext *slc;
  1381. slc = GNUNET_new (struct ServiceListenContext);
  1382. slc->sh = sh;
  1383. slc->listen_socket = *ls;
  1384. GNUNET_CONTAINER_DLL_insert (sh->slc_head, sh->slc_tail, slc);
  1385. }
  1386. GNUNET_free (lsocks);
  1387. }
  1388. else
  1389. {
  1390. struct sockaddr **addrs;
  1391. socklen_t *addrlens;
  1392. int num;
  1393. num = get_server_addresses (sh->service_name, sh->cfg, &addrs, &addrlens);
  1394. if (GNUNET_SYSERR == num)
  1395. return GNUNET_SYSERR;
  1396. for (int i = 0; i < num; i++)
  1397. {
  1398. struct ServiceListenContext *slc;
  1399. slc = GNUNET_new (struct ServiceListenContext);
  1400. slc->sh = sh;
  1401. slc->listen_socket = open_listen_socket (addrs[i], addrlens[i]);
  1402. GNUNET_free (addrs[i]);
  1403. if (NULL == slc->listen_socket)
  1404. {
  1405. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
  1406. GNUNET_free (slc);
  1407. continue;
  1408. }
  1409. GNUNET_CONTAINER_DLL_insert (sh->slc_head, sh->slc_tail, slc);
  1410. }
  1411. GNUNET_free (addrlens);
  1412. GNUNET_free (addrs);
  1413. if ((0 != num) && (NULL == sh->slc_head))
  1414. {
  1415. /* All attempts to bind failed, hard failure */
  1416. GNUNET_log (
  1417. GNUNET_ERROR_TYPE_ERROR,
  1418. _ (
  1419. "Could not bind to any of the ports I was supposed to, refusing to run!\n"));
  1420. GNUNET_free (csocks);
  1421. return GNUNET_SYSERR;
  1422. }
  1423. }
  1424. if (NULL != csocks)
  1425. {
  1426. /* close inherited sockets to signal parent that we are ready */
  1427. for (struct GNUNET_NETWORK_Handle **ls = csocks; NULL != *ls; ls++)
  1428. GNUNET_NETWORK_socket_close (*ls);
  1429. GNUNET_free (csocks);
  1430. }
  1431. sh->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
  1432. sh->match_uid = GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
  1433. sh->service_name,
  1434. "UNIX_MATCH_UID");
  1435. sh->match_gid = GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
  1436. sh->service_name,
  1437. "UNIX_MATCH_GID");
  1438. process_acl4 (&sh->v4_denied, sh, "REJECT_FROM");
  1439. process_acl4 (&sh->v4_allowed, sh, "ACCEPT_FROM");
  1440. process_acl6 (&sh->v6_denied, sh, "REJECT_FROM6");
  1441. process_acl6 (&sh->v6_allowed, sh, "ACCEPT_FROM6");
  1442. return GNUNET_OK;
  1443. }
  1444. /**
  1445. * Get the name of the user that'll be used
  1446. * to provide the service.
  1447. *
  1448. * @param sh service context
  1449. * @return value of the 'USERNAME' option
  1450. */
  1451. static char *
  1452. get_user_name (struct GNUNET_SERVICE_Handle *sh)
  1453. {
  1454. char *un;
  1455. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (sh->cfg,
  1456. sh->service_name,
  1457. "USERNAME",
  1458. &un))
  1459. return NULL;
  1460. return un;
  1461. }
  1462. /**
  1463. * Set user ID.
  1464. *
  1465. * @param sh service context
  1466. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  1467. */
  1468. static int
  1469. set_user_id (struct GNUNET_SERVICE_Handle *sh)
  1470. {
  1471. char *user;
  1472. if (NULL == (user = get_user_name (sh)))
  1473. return GNUNET_OK; /* keep */
  1474. struct passwd *pws;
  1475. errno = 0;
  1476. pws = getpwnam (user);
  1477. if (NULL == pws)
  1478. {
  1479. LOG (GNUNET_ERROR_TYPE_ERROR,
  1480. _ ("Cannot obtain information about user `%s': %s\n"),
  1481. user,
  1482. errno == 0 ? _ ("No such user") : strerror (errno));
  1483. GNUNET_free (user);
  1484. return GNUNET_SYSERR;
  1485. }
  1486. if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
  1487. #if HAVE_INITGROUPS
  1488. (0 != initgroups (user, pws->pw_gid)) ||
  1489. #endif
  1490. (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
  1491. {
  1492. if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
  1493. (0 != setreuid (pws->pw_uid, pws->pw_uid)))
  1494. {
  1495. LOG (GNUNET_ERROR_TYPE_ERROR,
  1496. _ ("Cannot change user/group to `%s': %s\n"),
  1497. user,
  1498. strerror (errno));
  1499. GNUNET_free (user);
  1500. return GNUNET_SYSERR;
  1501. }
  1502. }
  1503. GNUNET_free (user);
  1504. return GNUNET_OK;
  1505. }
  1506. /**
  1507. * Get the name of the file where we will
  1508. * write the PID of the service.
  1509. *
  1510. * @param sh service context
  1511. * @return name of the file for the process ID
  1512. */
  1513. static char *
  1514. get_pid_file_name (struct GNUNET_SERVICE_Handle *sh)
  1515. {
  1516. char *pif;
  1517. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (sh->cfg,
  1518. sh->service_name,
  1519. "PIDFILE",
  1520. &pif))
  1521. return NULL;
  1522. return pif;
  1523. }
  1524. /**
  1525. * Delete the PID file that was created by our parent.
  1526. *
  1527. * @param sh service context
  1528. */
  1529. static void
  1530. pid_file_delete (struct GNUNET_SERVICE_Handle *sh)
  1531. {
  1532. char *pif = get_pid_file_name (sh);
  1533. if (NULL == pif)
  1534. return; /* no PID file */
  1535. if (0 != unlink (pif))
  1536. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
  1537. GNUNET_free (pif);
  1538. }
  1539. /**
  1540. * Detach from terminal.
  1541. *
  1542. * @param sh service context
  1543. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  1544. */
  1545. static int
  1546. detach_terminal (struct GNUNET_SERVICE_Handle *sh)
  1547. {
  1548. pid_t pid;
  1549. int nullfd;
  1550. int filedes[2];
  1551. if (0 != pipe (filedes))
  1552. {
  1553. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
  1554. return GNUNET_SYSERR;
  1555. }
  1556. pid = fork ();
  1557. if (pid < 0)
  1558. {
  1559. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
  1560. return GNUNET_SYSERR;
  1561. }
  1562. if (0 != pid)
  1563. {
  1564. /* Parent */
  1565. char c;
  1566. GNUNET_break (0 == close (filedes[1]));
  1567. c = 'X';
  1568. if (1 != read (filedes[0], &c, sizeof(char)))
  1569. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
  1570. fflush (stdout);
  1571. switch (c)
  1572. {
  1573. case '.':
  1574. exit (0);
  1575. case 'I':
  1576. LOG (GNUNET_ERROR_TYPE_INFO,
  1577. _ ("Service process failed to initialize\n"));
  1578. break;
  1579. case 'S':
  1580. LOG (GNUNET_ERROR_TYPE_INFO,
  1581. _ ("Service process could not initialize server function\n"));
  1582. break;
  1583. case 'X':
  1584. LOG (GNUNET_ERROR_TYPE_INFO,
  1585. _ ("Service process failed to report status\n"));
  1586. break;
  1587. }
  1588. exit (1); /* child reported error */
  1589. }
  1590. GNUNET_break (0 == close (0));
  1591. GNUNET_break (0 == close (1));
  1592. GNUNET_break (0 == close (filedes[0]));
  1593. nullfd = open ("/dev/null", O_RDWR | O_APPEND);
  1594. if (nullfd < 0)
  1595. return GNUNET_SYSERR;
  1596. /* set stdin/stdout to /dev/null */
  1597. if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
  1598. {
  1599. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
  1600. (void) close (nullfd);
  1601. return GNUNET_SYSERR;
  1602. }
  1603. (void) close (nullfd);
  1604. /* Detach from controlling terminal */
  1605. pid = setsid ();
  1606. if (-1 == pid)
  1607. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
  1608. sh->ready_confirm_fd = filedes[1];
  1609. return GNUNET_OK;
  1610. }
  1611. /**
  1612. * Tear down the service, closing the listen sockets and
  1613. * freeing the ACLs.
  1614. *
  1615. * @param sh handle to the service to tear down.
  1616. */
  1617. static void
  1618. teardown_service (struct GNUNET_SERVICE_Handle *sh)
  1619. {
  1620. struct ServiceListenContext *slc;
  1621. GNUNET_free (sh->v4_denied);
  1622. GNUNET_free (sh->v6_denied);
  1623. GNUNET_free (sh->v4_allowed);
  1624. GNUNET_free (sh->v6_allowed);
  1625. while (NULL != (slc = sh->slc_head))
  1626. {
  1627. GNUNET_CONTAINER_DLL_remove (sh->slc_head, sh->slc_tail, slc);
  1628. if (NULL != slc->listen_task)
  1629. GNUNET_SCHEDULER_cancel (slc->listen_task);
  1630. GNUNET_break (GNUNET_OK ==
  1631. GNUNET_NETWORK_socket_close (slc->listen_socket));
  1632. GNUNET_free (slc);
  1633. }
  1634. }
  1635. /**
  1636. * Function to return link to AGPL source upon request.
  1637. *
  1638. * @param cls closure with the identification of the client
  1639. * @param msg AGPL request
  1640. */
  1641. static void
  1642. return_agpl (void *cls, const struct GNUNET_MessageHeader *msg)
  1643. {
  1644. struct GNUNET_SERVICE_Client *client = cls;
  1645. struct GNUNET_MQ_Handle *mq;
  1646. struct GNUNET_MQ_Envelope *env;
  1647. struct GNUNET_MessageHeader *res;
  1648. size_t slen;
  1649. const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get ();
  1650. (void) msg;
  1651. slen = strlen (pd->agpl_url) + 1;
  1652. env = GNUNET_MQ_msg_extra (res, GNUNET_MESSAGE_TYPE_RESPONSE_AGPL, slen);
  1653. memcpy (&res[1], GNUNET_AGPL_URL, slen);
  1654. mq = GNUNET_SERVICE_client_get_mq (client);
  1655. GNUNET_MQ_send (mq, env);
  1656. GNUNET_SERVICE_client_continue (client);
  1657. }
  1658. /**
  1659. * Low-level function to start a service if the scheduler
  1660. * is already running. Should only be used directly in
  1661. * special cases.
  1662. *
  1663. * The function will launch the service with the name @a service_name
  1664. * using the @a service_options to configure its shutdown
  1665. * behavior. When clients connect or disconnect, the respective
  1666. * @a connect_cb or @a disconnect_cb functions will be called. For
  1667. * messages received from the clients, the respective @a handlers will
  1668. * be invoked; for the closure of the handlers we use the return value
  1669. * from the @a connect_cb invocation of the respective client.
  1670. *
  1671. * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
  1672. * message to receive further messages from this client. If
  1673. * #GNUNET_SERVICE_client_continue() is not called within a short
  1674. * time, a warning will be logged. If delays are expected, services
  1675. * should call #GNUNET_SERVICE_client_disable_continue_warning() to
  1676. * disable the warning.
  1677. *
  1678. * Clients sending invalid messages (based on @a handlers) will be
  1679. * dropped. Additionally, clients can be dropped at any time using
  1680. * #GNUNET_SERVICE_client_drop().
  1681. *
  1682. * The service must be stopped using #GNUNET_SERVICE_stop().
  1683. *
  1684. * @param service_name name of the service to run
  1685. * @param cfg configuration to use
  1686. * @param connect_cb function to call whenever a client connects
  1687. * @param disconnect_cb function to call whenever a client disconnects
  1688. * @param cls closure argument for @a connect_cb and @a disconnect_cb
  1689. * @param handlers NULL-terminated array of message handlers for the service,
  1690. * the closure will be set to the value returned by
  1691. * the @a connect_cb for the respective connection
  1692. * @return NULL on error
  1693. */
  1694. struct GNUNET_SERVICE_Handle *
  1695. GNUNET_SERVICE_start (const char *service_name,
  1696. const struct GNUNET_CONFIGURATION_Handle *cfg,
  1697. GNUNET_SERVICE_ConnectHandler connect_cb,
  1698. GNUNET_SERVICE_DisconnectHandler disconnect_cb,
  1699. void *cls,
  1700. const struct GNUNET_MQ_MessageHandler *handlers)
  1701. {
  1702. struct GNUNET_SERVICE_Handle *sh;
  1703. sh = GNUNET_new (struct GNUNET_SERVICE_Handle);
  1704. sh->service_name = service_name;
  1705. sh->cfg = cfg;
  1706. sh->connect_cb = connect_cb;
  1707. sh->disconnect_cb = disconnect_cb;
  1708. sh->cb_cls = cls;
  1709. sh->handlers = GNUNET_MQ_copy_handlers2 (handlers, &return_agpl, NULL);
  1710. if (GNUNET_OK != setup_service (sh))
  1711. {
  1712. GNUNET_free (sh->handlers);
  1713. GNUNET_free (sh);
  1714. return NULL;
  1715. }
  1716. do_resume (sh, SUSPEND_STATE_NONE);
  1717. return sh;
  1718. }
  1719. /**
  1720. * Stops a service that was started with #GNUNET_SERVICE_start().
  1721. *
  1722. * @param srv service to stop
  1723. */
  1724. void
  1725. GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Handle *srv)
  1726. {
  1727. struct GNUNET_SERVICE_Client *client;
  1728. GNUNET_SERVICE_suspend (srv);
  1729. while (NULL != (client = srv->clients_head))
  1730. GNUNET_SERVICE_client_drop (client);
  1731. teardown_service (srv);
  1732. GNUNET_free (srv->handlers);
  1733. GNUNET_free (srv);
  1734. }
  1735. /**
  1736. * Creates the "main" function for a GNUnet service. You
  1737. * should almost always use the #GNUNET_SERVICE_MAIN macro
  1738. * instead of calling this function directly (except
  1739. * for ARM, which should call this function directly).
  1740. *
  1741. * The function will launch the service with the name @a service_name
  1742. * using the @a service_options to configure its shutdown
  1743. * behavior. Once the service is ready, the @a init_cb will be called
  1744. * for service-specific initialization. @a init_cb will be given the
  1745. * service handler which can be used to control the service's
  1746. * availability. When clients connect or disconnect, the respective
  1747. * @a connect_cb or @a disconnect_cb functions will be called. For
  1748. * messages received from the clients, the respective @a handlers will
  1749. * be invoked; for the closure of the handlers we use the return value
  1750. * from the @a connect_cb invocation of the respective client.
  1751. *
  1752. * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
  1753. * message to receive further messages from this client. If
  1754. * #GNUNET_SERVICE_client_continue() is not called within a short
  1755. * time, a warning will be logged. If delays are expected, services
  1756. * should call #GNUNET_SERVICE_client_disable_continue_warning() to
  1757. * disable the warning.
  1758. *
  1759. * Clients sending invalid messages (based on @a handlers) will be
  1760. * dropped. Additionally, clients can be dropped at any time using
  1761. * #GNUNET_SERVICE_client_drop().
  1762. *
  1763. * @param argc number of command-line arguments in @a argv
  1764. * @param argv array of command-line arguments
  1765. * @param service_name name of the service to run
  1766. * @param options options controlling shutdown of the service
  1767. * @param service_init_cb function to call once the service is ready
  1768. * @param connect_cb function to call whenever a client connects
  1769. * @param disconnect_cb function to call whenever a client disconnects
  1770. * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
  1771. * @param handlers NULL-terminated array of message handlers for the service,
  1772. * the closure will be set to the value returned by
  1773. * the @a connect_cb for the respective connection
  1774. * @return 0 on success, non-zero on error
  1775. */
  1776. int
  1777. GNUNET_SERVICE_run_ (int argc,
  1778. char *const *argv,
  1779. const char *service_name,
  1780. enum GNUNET_SERVICE_Options options,
  1781. GNUNET_SERVICE_InitCallback service_init_cb,
  1782. GNUNET_SERVICE_ConnectHandler connect_cb,
  1783. GNUNET_SERVICE_DisconnectHandler disconnect_cb,
  1784. void *cls,
  1785. const struct GNUNET_MQ_MessageHandler *handlers)
  1786. {
  1787. struct GNUNET_SERVICE_Handle sh;
  1788. #if ENABLE_NLS
  1789. char *path;
  1790. #endif
  1791. char *cfg_filename;
  1792. char *opt_cfg_filename;
  1793. char *loglev;
  1794. const char *xdg;
  1795. char *logfile;
  1796. int do_daemonize;
  1797. unsigned long long skew_offset;
  1798. unsigned long long skew_variance;
  1799. long long clock_offset;
  1800. struct GNUNET_CONFIGURATION_Handle *cfg;
  1801. int ret;
  1802. int err;
  1803. const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get ();
  1804. struct GNUNET_GETOPT_CommandLineOption service_options[] = {
  1805. GNUNET_GETOPT_option_cfgfile (&opt_cfg_filename),
  1806. GNUNET_GETOPT_option_flag ('d',
  1807. "daemonize",
  1808. gettext_noop (
  1809. "do daemonize (detach from terminal)"),
  1810. &do_daemonize),
  1811. GNUNET_GETOPT_option_help (NULL),
  1812. GNUNET_GETOPT_option_loglevel (&loglev),
  1813. GNUNET_GETOPT_option_logfile (&logfile),
  1814. GNUNET_GETOPT_option_version (pd->version),
  1815. GNUNET_GETOPT_OPTION_END
  1816. };
  1817. err = 1;
  1818. memset (&sh, 0, sizeof(sh));
  1819. xdg = getenv ("XDG_CONFIG_HOME");
  1820. if (NULL != xdg)
  1821. GNUNET_asprintf (&cfg_filename,
  1822. "%s%s%s",
  1823. xdg,
  1824. DIR_SEPARATOR_STR,
  1825. pd->config_file);
  1826. else
  1827. cfg_filename = GNUNET_strdup (pd->user_config_file);
  1828. sh.ready_confirm_fd = -1;
  1829. sh.options = options;
  1830. sh.cfg = cfg = GNUNET_CONFIGURATION_create ();
  1831. sh.service_init_cb = service_init_cb;
  1832. sh.connect_cb = connect_cb;
  1833. sh.disconnect_cb = disconnect_cb;
  1834. sh.cb_cls = cls;
  1835. sh.handlers = (NULL == pd->agpl_url)
  1836. ? GNUNET_MQ_copy_handlers (handlers)
  1837. : GNUNET_MQ_copy_handlers2 (handlers, &return_agpl, NULL);
  1838. sh.service_name = service_name;
  1839. sh.ret = 0;
  1840. /* setup subsystems */
  1841. loglev = NULL;
  1842. logfile = NULL;
  1843. opt_cfg_filename = NULL;
  1844. do_daemonize = 0;
  1845. #if ENABLE_NLS
  1846. if (NULL != pd->gettext_domain)
  1847. {
  1848. setlocale (LC_ALL, "");
  1849. path = (NULL == pd->gettext_path) ?
  1850. GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR) :
  1851. GNUNET_strdup (pd->gettext_path);
  1852. if (NULL != path)
  1853. {
  1854. bindtextdomain (pd->gettext_domain, path);
  1855. GNUNET_free (path);
  1856. }
  1857. textdomain (pd->gettext_domain);
  1858. }
  1859. #endif
  1860. ret = GNUNET_GETOPT_run (service_name,
  1861. service_options,
  1862. argc,
  1863. argv);
  1864. if (GNUNET_SYSERR == ret)
  1865. goto shutdown;
  1866. if (GNUNET_NO == ret)
  1867. {
  1868. err = 0;
  1869. goto shutdown;
  1870. }
  1871. if (GNUNET_OK != GNUNET_log_setup (service_name,
  1872. loglev,
  1873. logfile))
  1874. {
  1875. GNUNET_break (0);
  1876. goto shutdown;
  1877. }
  1878. if (NULL != opt_cfg_filename)
  1879. {
  1880. if ((GNUNET_YES != GNUNET_DISK_file_test (opt_cfg_filename)) ||
  1881. (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, opt_cfg_filename)))
  1882. {
  1883. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1884. _ ("Malformed configuration file `%s', exit ...\n"),
  1885. opt_cfg_filename);
  1886. goto shutdown;
  1887. }
  1888. }
  1889. else
  1890. {
  1891. if (GNUNET_YES == GNUNET_DISK_file_test (cfg_filename))
  1892. {
  1893. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, cfg_filename))
  1894. {
  1895. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1896. _ ("Malformed configuration file `%s', exit ...\n"),
  1897. cfg_filename);
  1898. goto shutdown;
  1899. }
  1900. }
  1901. else
  1902. {
  1903. if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, NULL))
  1904. {
  1905. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1906. _ ("Malformed configuration, exit ...\n"));
  1907. goto shutdown;
  1908. }
  1909. }
  1910. }
  1911. if (GNUNET_OK != setup_service (&sh))
  1912. goto shutdown;
  1913. if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sh)))
  1914. {
  1915. GNUNET_break (0);
  1916. goto shutdown;
  1917. }
  1918. if (GNUNET_OK != set_user_id (&sh))
  1919. goto shutdown;
  1920. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1921. "Service `%s' runs with configuration from `%s'\n",
  1922. service_name,
  1923. (NULL != opt_cfg_filename) ? opt_cfg_filename : cfg_filename);
  1924. if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (sh.cfg,
  1925. "TESTING",
  1926. "SKEW_OFFSET",
  1927. &skew_offset)) &&
  1928. (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (sh.cfg,
  1929. "TESTING",
  1930. "SKEW_VARIANCE",
  1931. &skew_variance)))
  1932. {
  1933. clock_offset = skew_offset - skew_variance;
  1934. GNUNET_TIME_set_offset (clock_offset);
  1935. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1936. "Skewing clock by %lld ms\n",
  1937. (long long) clock_offset);
  1938. }
  1939. GNUNET_RESOLVER_connect (sh.cfg);
  1940. /* actually run service */
  1941. err = 0;
  1942. GNUNET_SCHEDULER_run (&service_main, &sh);
  1943. /* shutdown */
  1944. if (1 == do_daemonize)
  1945. pid_file_delete (&sh);
  1946. shutdown:
  1947. if (-1 != sh.ready_confirm_fd)
  1948. {
  1949. if (1 != write (sh.ready_confirm_fd, err ? "I" : "S", 1))
  1950. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
  1951. GNUNET_break (0 == close (sh.ready_confirm_fd));
  1952. }
  1953. #if HAVE_MALLINFO
  1954. {
  1955. char *counter;
  1956. if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (sh.cfg,
  1957. service_name,
  1958. "GAUGER_HEAP")) &&
  1959. (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (sh.cfg,
  1960. service_name,
  1961. "GAUGER_HEAP",
  1962. &counter)))
  1963. {
  1964. struct mallinfo mi;
  1965. mi = mallinfo ();
  1966. GAUGER (service_name, counter, mi.usmblks, "blocks");
  1967. GNUNET_free (counter);
  1968. }
  1969. }
  1970. #endif
  1971. teardown_service (&sh);
  1972. GNUNET_free (sh.handlers);
  1973. GNUNET_SPEEDUP_stop_ ();
  1974. GNUNET_CONFIGURATION_destroy (cfg);
  1975. GNUNET_free (logfile);
  1976. GNUNET_free (loglev);
  1977. GNUNET_free (cfg_filename);
  1978. GNUNET_free (opt_cfg_filename);
  1979. return err ? GNUNET_SYSERR : sh.ret;
  1980. }
  1981. /**
  1982. * Suspend accepting connections from the listen socket temporarily.
  1983. * Resume activity using #GNUNET_SERVICE_resume.
  1984. *
  1985. * @param sh service to stop accepting connections.
  1986. */
  1987. void
  1988. GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh)
  1989. {
  1990. do_suspend (sh, SUSPEND_STATE_APP);
  1991. }
  1992. /**
  1993. * Resume accepting connections from the listen socket.
  1994. *
  1995. * @param sh service to resume accepting connections.
  1996. */
  1997. void
  1998. GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh)
  1999. {
  2000. do_resume (sh, SUSPEND_STATE_APP);
  2001. }
  2002. /**
  2003. * Task run to resume receiving data from the client after
  2004. * the client called #GNUNET_SERVICE_client_continue().
  2005. *
  2006. * @param cls our `struct GNUNET_SERVICE_Client`
  2007. */
  2008. static void
  2009. resume_client_receive (void *cls)
  2010. {
  2011. struct GNUNET_SERVICE_Client *c = cls;
  2012. int ret;
  2013. c->recv_task = NULL;
  2014. /* first, check if there is still something in the buffer */
  2015. ret = GNUNET_MST_next (c->mst, GNUNET_YES);
  2016. if (GNUNET_SYSERR == ret)
  2017. {
  2018. if (NULL == c->drop_task)
  2019. GNUNET_SERVICE_client_drop (c);
  2020. return;
  2021. }
  2022. if (GNUNET_NO == ret)
  2023. return; /* done processing, wait for more later */
  2024. GNUNET_assert (GNUNET_OK == ret);
  2025. if (GNUNET_YES == c->needs_continue)
  2026. return; /* #GNUNET_MST_next() did give a message to the client */
  2027. /* need to receive more data from the network first */
  2028. if (NULL != c->recv_task)
  2029. return;
  2030. c->recv_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  2031. c->sock,
  2032. &service_client_recv,
  2033. c);
  2034. }
  2035. /**
  2036. * Continue receiving further messages from the given client.
  2037. * Must be called after each message received.
  2038. *
  2039. * @param c the client to continue receiving from
  2040. */
  2041. void
  2042. GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c)
  2043. {
  2044. GNUNET_assert (NULL == c->drop_task);
  2045. GNUNET_assert (GNUNET_YES == c->needs_continue);
  2046. GNUNET_assert (NULL == c->recv_task);
  2047. c->needs_continue = GNUNET_NO;
  2048. if (NULL != c->warn_task)
  2049. {
  2050. GNUNET_SCHEDULER_cancel (c->warn_task);
  2051. c->warn_task = NULL;
  2052. }
  2053. c->recv_task = GNUNET_SCHEDULER_add_now (&resume_client_receive, c);
  2054. }
  2055. /**
  2056. * Disable the warning the server issues if a message is not
  2057. * acknowledged in a timely fashion. Use this call if a client is
  2058. * intentionally delayed for a while. Only applies to the current
  2059. * message.
  2060. *
  2061. * @param c client for which to disable the warning
  2062. */
  2063. void
  2064. GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c)
  2065. {
  2066. GNUNET_break (NULL != c->warn_task);
  2067. if (NULL != c->warn_task)
  2068. {
  2069. GNUNET_SCHEDULER_cancel (c->warn_task);
  2070. c->warn_task = NULL;
  2071. }
  2072. }
  2073. /**
  2074. * Asynchronously finish dropping the client.
  2075. *
  2076. * @param cls the `struct GNUNET_SERVICE_Client`.
  2077. */
  2078. static void
  2079. finish_client_drop (void *cls)
  2080. {
  2081. struct GNUNET_SERVICE_Client *c = cls;
  2082. struct GNUNET_SERVICE_Handle *sh = c->sh;
  2083. c->drop_task = NULL;
  2084. GNUNET_assert (NULL == c->send_task);
  2085. GNUNET_assert (NULL == c->recv_task);
  2086. GNUNET_assert (NULL == c->warn_task);
  2087. GNUNET_MST_destroy (c->mst);
  2088. GNUNET_MQ_destroy (c->mq);
  2089. if (GNUNET_NO == c->persist)
  2090. {
  2091. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (c->sock));
  2092. if ((0 != (SUSPEND_STATE_EMFILE & sh->suspend_state)) &&
  2093. (0 == (SUSPEND_STATE_SHUTDOWN & sh->suspend_state)))
  2094. do_resume (sh, SUSPEND_STATE_EMFILE);
  2095. }
  2096. else
  2097. {
  2098. GNUNET_NETWORK_socket_free_memory_only_ (c->sock);
  2099. }
  2100. GNUNET_free (c);
  2101. if ((0 != (SUSPEND_STATE_SHUTDOWN & sh->suspend_state)) &&
  2102. (GNUNET_NO == have_non_monitor_clients (sh)))
  2103. GNUNET_SERVICE_shutdown (sh);
  2104. }
  2105. /**
  2106. * Ask the server to disconnect from the given client. This is the
  2107. * same as returning #GNUNET_SYSERR within the check procedure when
  2108. * handling a message, wexcept that it allows dropping of a client even
  2109. * when not handling a message from that client. The `disconnect_cb`
  2110. * will be called on @a c even if the application closes the connection
  2111. * using this function.
  2112. *
  2113. * @param c client to disconnect now
  2114. */
  2115. void
  2116. GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c)
  2117. {
  2118. struct GNUNET_SERVICE_Handle *sh = c->sh;
  2119. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2120. "Client dropped: %p (MQ: %p)\n",
  2121. c,
  2122. c->mq);
  2123. #if EXECINFO
  2124. {
  2125. void *backtrace_array[MAX_TRACE_DEPTH];
  2126. int num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
  2127. char **backtrace_strings =
  2128. backtrace_symbols (backtrace_array, t->num_backtrace_strings);
  2129. for (unsigned int i = 0; i < num_backtrace_strings; i++)
  2130. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2131. "client drop trace %u: %s\n",
  2132. i,
  2133. backtrace_strings[i]);
  2134. }
  2135. #endif
  2136. if (NULL != c->drop_task)
  2137. {
  2138. /* asked to drop twice! */
  2139. GNUNET_assert (0);
  2140. return;
  2141. }
  2142. GNUNET_CONTAINER_DLL_remove (sh->clients_head,
  2143. sh->clients_tail,
  2144. c);
  2145. if (NULL != sh->disconnect_cb)
  2146. sh->disconnect_cb (sh->cb_cls,
  2147. c,
  2148. c->user_context);
  2149. if (NULL != c->warn_task)
  2150. {
  2151. GNUNET_SCHEDULER_cancel (c->warn_task);
  2152. c->warn_task = NULL;
  2153. }
  2154. if (NULL != c->recv_task)
  2155. {
  2156. GNUNET_SCHEDULER_cancel (c->recv_task);
  2157. c->recv_task = NULL;
  2158. }
  2159. if (NULL != c->send_task)
  2160. {
  2161. GNUNET_SCHEDULER_cancel (c->send_task);
  2162. c->send_task = NULL;
  2163. }
  2164. c->drop_task = GNUNET_SCHEDULER_add_now (&finish_client_drop, c);
  2165. }
  2166. /**
  2167. * Explicitly stops the service.
  2168. *
  2169. * @param sh server to shutdown
  2170. */
  2171. void
  2172. GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh)
  2173. {
  2174. struct GNUNET_SERVICE_Client *client;
  2175. if (0 == (sh->suspend_state & SUSPEND_STATE_SHUTDOWN))
  2176. do_suspend (sh, SUSPEND_STATE_SHUTDOWN);
  2177. while (NULL != (client = sh->clients_head))
  2178. GNUNET_SERVICE_client_drop (client);
  2179. }
  2180. /**
  2181. * Set the 'monitor' flag on this client. Clients which have been
  2182. * marked as 'monitors' won't prevent the server from shutting down
  2183. * once #GNUNET_SERVICE_stop_listening() has been invoked. The idea is
  2184. * that for "normal" clients we likely want to allow them to process
  2185. * their requests; however, monitor-clients are likely to 'never'
  2186. * disconnect during shutdown and thus will not be considered when
  2187. * determining if the server should continue to exist after
  2188. * shutdown has been triggered.
  2189. *
  2190. * @param c client to mark as a monitor
  2191. */
  2192. void
  2193. GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c)
  2194. {
  2195. c->is_monitor = GNUNET_YES;
  2196. if (((0 != (SUSPEND_STATE_SHUTDOWN & c->sh->suspend_state)) &&
  2197. (GNUNET_NO == have_non_monitor_clients (c->sh))))
  2198. GNUNET_SERVICE_shutdown (c->sh);
  2199. }
  2200. /**
  2201. * Set the persist option on this client. Indicates that the
  2202. * underlying socket or fd should never really be closed. Used for
  2203. * indicating process death.
  2204. *
  2205. * @param c client to persist the socket (never to be closed)
  2206. */
  2207. void
  2208. GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c)
  2209. {
  2210. c->persist = GNUNET_YES;
  2211. }
  2212. /**
  2213. * Obtain the message queue of @a c. Convenience function.
  2214. *
  2215. * @param c the client to continue receiving from
  2216. * @return the message queue of @a c
  2217. */
  2218. struct GNUNET_MQ_Handle *
  2219. GNUNET_SERVICE_client_get_mq (struct GNUNET_SERVICE_Client *c)
  2220. {
  2221. return c->mq;
  2222. }
  2223. /* end of service.c */