network.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2013 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/network.c
  18. * @brief basic, low-level networking interface
  19. * @author Nils Durner
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "disk.h"
  25. #define LOG(kind, ...) GNUNET_log_from (kind, "util-network", __VA_ARGS__)
  26. #define LOG_STRERROR_FILE(kind, syscall, \
  27. filename) GNUNET_log_from_strerror_file (kind, \
  28. "util-network", \
  29. syscall, \
  30. filename)
  31. #define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror (kind, \
  32. "util-network", \
  33. syscall)
  34. #define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
  35. #ifndef INVALID_SOCKET
  36. #define INVALID_SOCKET -1
  37. #endif
  38. /**
  39. * @brief handle to a socket
  40. */
  41. struct GNUNET_NETWORK_Handle
  42. {
  43. int fd;
  44. /**
  45. * Address family / domain.
  46. */
  47. int af;
  48. /**
  49. * Type of the socket
  50. */
  51. int type;
  52. /**
  53. * Number of bytes in addr.
  54. */
  55. socklen_t addrlen;
  56. /**
  57. * Address we were bound to, or NULL.
  58. */
  59. struct sockaddr *addr;
  60. };
  61. /**
  62. * Test if the given protocol family is supported by this system.
  63. *
  64. * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
  65. * @return #GNUNET_OK if the PF is supported
  66. */
  67. int
  68. GNUNET_NETWORK_test_pf (int pf)
  69. {
  70. static int cache_v4 = -1;
  71. static int cache_v6 = -1;
  72. static int cache_un = -1;
  73. int s;
  74. int ret;
  75. switch (pf)
  76. {
  77. case PF_INET:
  78. if (-1 != cache_v4)
  79. return cache_v4;
  80. break;
  81. case PF_INET6:
  82. if (-1 != cache_v6)
  83. return cache_v6;
  84. break;
  85. #ifdef PF_UNIX
  86. case PF_UNIX:
  87. if (-1 != cache_un)
  88. return cache_un;
  89. break;
  90. #endif
  91. }
  92. s = socket (pf, SOCK_STREAM, 0);
  93. if (-1 == s)
  94. {
  95. if (EAFNOSUPPORT != errno)
  96. {
  97. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
  98. "socket");
  99. return GNUNET_SYSERR;
  100. }
  101. ret = GNUNET_NO;
  102. }
  103. else
  104. {
  105. close (s);
  106. ret = GNUNET_OK;
  107. }
  108. switch (pf)
  109. {
  110. case PF_INET:
  111. cache_v4 = ret;
  112. break;
  113. case PF_INET6:
  114. cache_v6 = ret;
  115. break;
  116. #ifdef PF_UNIX
  117. case PF_UNIX:
  118. cache_un = ret;
  119. break;
  120. #endif
  121. }
  122. return ret;
  123. }
  124. /**
  125. * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
  126. * shorten it to an acceptable length while keeping it unique
  127. * and making sure it remains a valid filename (if possible).
  128. *
  129. * @param unixpath long path, will be freed (or same pointer returned
  130. * with moved 0-termination).
  131. * @return shortened unixpath, NULL on error
  132. */
  133. char *
  134. GNUNET_NETWORK_shorten_unixpath (char *unixpath)
  135. {
  136. struct sockaddr_un dummy;
  137. size_t slen;
  138. char *end;
  139. struct GNUNET_HashCode sh;
  140. struct GNUNET_CRYPTO_HashAsciiEncoded ae;
  141. size_t upm;
  142. upm = sizeof(dummy.sun_path);
  143. slen = strlen (unixpath);
  144. if (slen < upm)
  145. return unixpath; /* no shortening required */
  146. GNUNET_CRYPTO_hash (unixpath, slen, &sh);
  147. while (16 + strlen (unixpath) >= upm)
  148. {
  149. if (NULL == (end = strrchr (unixpath, '/')))
  150. {
  151. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  152. _ (
  153. "Unable to shorten unix path `%s' while keeping name unique\n"),
  154. unixpath);
  155. GNUNET_free (unixpath);
  156. return NULL;
  157. }
  158. *end = '\0';
  159. }
  160. GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
  161. ae.encoding[16] = '\0';
  162. strcat (unixpath, (char *) ae.encoding);
  163. return unixpath;
  164. }
  165. /**
  166. * If services crash, they can leave a unix domain socket file on the
  167. * disk. This needs to be manually removed, because otherwise both
  168. * bind() and connect() for the respective address will fail. In this
  169. * function, we test if such a left-over file exists, and if so,
  170. * remove it (unless there is a listening service at the address).
  171. *
  172. * @param un unix domain socket address to check
  173. */
  174. void
  175. GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
  176. {
  177. int s;
  178. int eno;
  179. struct stat sbuf;
  180. int ret;
  181. s = socket (AF_UNIX, SOCK_STREAM, 0);
  182. if (-1 == s)
  183. {
  184. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
  185. "Failed to open AF_UNIX socket");
  186. return;
  187. }
  188. ret = connect (s,
  189. (struct sockaddr *) un,
  190. sizeof(struct sockaddr_un));
  191. eno = errno;
  192. GNUNET_break (0 == close (s));
  193. if (0 == ret)
  194. return; /* another process is listening, do not remove! */
  195. if (ECONNREFUSED != eno)
  196. return; /* some other error, likely "no such file or directory" -- all well */
  197. /* should unlink, but sanity checks first */
  198. if (0 != stat (un->sun_path,
  199. &sbuf))
  200. return; /* failed to 'stat', likely does not exist after all */
  201. if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
  202. return; /* refuse to unlink anything except sockets */
  203. /* finally, really unlink */
  204. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  205. "Removing left-over `%s' from previous exeuction\n",
  206. un->sun_path);
  207. if (0 != unlink (un->sun_path))
  208. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  209. "unlink",
  210. un->sun_path);
  211. }
  212. #ifndef FD_COPY
  213. #define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof(fd_set)); } while (0)
  214. #endif
  215. /**
  216. * Set if a socket should use blocking or non-blocking IO.
  217. *
  218. * @param fd socket
  219. * @param doBlock blocking mode
  220. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  221. */
  222. int
  223. GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
  224. int doBlock)
  225. {
  226. int flags = fcntl (fd->fd, F_GETFL);
  227. if (flags == -1)
  228. {
  229. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
  230. "fcntl");
  231. return GNUNET_SYSERR;
  232. }
  233. if (doBlock)
  234. flags &= ~O_NONBLOCK;
  235. else
  236. flags |= O_NONBLOCK;
  237. if (0 != fcntl (fd->fd,
  238. F_SETFL,
  239. flags))
  240. {
  241. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
  242. "fcntl");
  243. return GNUNET_SYSERR;
  244. }
  245. return GNUNET_OK;
  246. }
  247. /**
  248. * Make a socket non-inheritable to child processes
  249. *
  250. * @param h the socket to make non-inheritable
  251. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  252. * @warning Not implemented on Windows
  253. */
  254. static int
  255. socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
  256. {
  257. int i;
  258. i = fcntl (h->fd, F_GETFD);
  259. if (i < 0)
  260. return GNUNET_SYSERR;
  261. if (i == (i | FD_CLOEXEC))
  262. return GNUNET_OK;
  263. i |= FD_CLOEXEC;
  264. if (fcntl (h->fd, F_SETFD, i) < 0)
  265. return GNUNET_SYSERR;
  266. return GNUNET_OK;
  267. }
  268. #ifdef DARWIN
  269. /**
  270. * The MSG_NOSIGNAL equivalent on Mac OS X
  271. *
  272. * @param h the socket to make non-delaying
  273. */
  274. static int
  275. socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
  276. {
  277. int abs_value = 1;
  278. if (0 !=
  279. setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
  280. (const void *) &abs_value,
  281. sizeof(abs_value)))
  282. return GNUNET_SYSERR;
  283. return GNUNET_OK;
  284. }
  285. #endif
  286. /**
  287. * Disable delays when sending data via the socket.
  288. * (GNUnet makes sure that messages are as big as
  289. * possible already).
  290. *
  291. * @param h the socket to make non-delaying
  292. */
  293. static void
  294. socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
  295. {
  296. int value = 1;
  297. if (0 !=
  298. setsockopt (h->fd,
  299. IPPROTO_TCP,
  300. TCP_NODELAY,
  301. &value, sizeof(value)))
  302. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
  303. "setsockopt");
  304. }
  305. /**
  306. * Perform proper canonical initialization for a network handle.
  307. * Set it to non-blocking, make it non-inheritable to child
  308. * processes, disable SIGPIPE, enable "nodelay" (if non-UNIX
  309. * stream socket) and check that it is smaller than FD_SETSIZE.
  310. *
  311. * @param h socket to initialize
  312. * @param af address family of the socket
  313. * @param type socket type
  314. * @return #GNUNET_OK on success, #GNUNET_SYSERR if initialization
  315. * failed and the handle was destroyed
  316. */
  317. static int
  318. initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
  319. int af,
  320. int type)
  321. {
  322. int eno;
  323. h->af = af;
  324. h->type = type;
  325. if (h->fd == INVALID_SOCKET)
  326. {
  327. eno = errno;
  328. GNUNET_free (h);
  329. errno = eno;
  330. return GNUNET_SYSERR;
  331. }
  332. if (h->fd >= FD_SETSIZE)
  333. {
  334. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
  335. errno = EMFILE;
  336. return GNUNET_SYSERR;
  337. }
  338. if (GNUNET_OK != socket_set_inheritable (h))
  339. LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  340. "socket_set_inheritable");
  341. if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
  342. {
  343. eno = errno;
  344. GNUNET_break (0);
  345. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
  346. errno = eno;
  347. return GNUNET_SYSERR;
  348. }
  349. #ifdef DARWIN
  350. if (GNUNET_SYSERR == socket_set_nosigpipe (h))
  351. {
  352. eno = errno;
  353. GNUNET_break (0);
  354. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
  355. errno = eno;
  356. return GNUNET_SYSERR;
  357. }
  358. #endif
  359. if ((type == SOCK_STREAM)
  360. #ifdef AF_UNIX
  361. && (af != AF_UNIX)
  362. #endif
  363. )
  364. socket_set_nodelay (h);
  365. return GNUNET_OK;
  366. }
  367. /**
  368. * accept a new connection on a socket
  369. *
  370. * @param desc bound socket
  371. * @param address address of the connecting peer, may be NULL
  372. * @param address_len length of @a address
  373. * @return client socket
  374. */
  375. struct GNUNET_NETWORK_Handle *
  376. GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
  377. struct sockaddr *address,
  378. socklen_t *address_len)
  379. {
  380. struct GNUNET_NETWORK_Handle *ret;
  381. int eno;
  382. ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
  383. #if DEBUG_NETWORK
  384. {
  385. struct sockaddr_storage name;
  386. socklen_t namelen = sizeof(name);
  387. int gsn = getsockname (desc->fd,
  388. (struct sockaddr *) &name,
  389. &namelen);
  390. if (0 == gsn)
  391. LOG (GNUNET_ERROR_TYPE_DEBUG,
  392. "Accepting connection on `%s'\n",
  393. GNUNET_a2s ((const struct sockaddr *) &name,
  394. namelen));
  395. }
  396. #endif
  397. ret->fd = accept (desc->fd,
  398. address,
  399. address_len);
  400. if (-1 == ret->fd)
  401. {
  402. eno = errno;
  403. GNUNET_free (ret);
  404. errno = eno;
  405. return NULL;
  406. }
  407. if (GNUNET_OK !=
  408. initialize_network_handle (ret,
  409. (NULL != address) ? address->sa_family :
  410. desc->af,
  411. SOCK_STREAM))
  412. {
  413. return NULL;
  414. }
  415. return ret;
  416. }
  417. /**
  418. * Bind a socket to a particular address.
  419. *
  420. * @param desc socket to bind
  421. * @param address address to be bound
  422. * @param address_len length of @a address
  423. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  424. */
  425. int
  426. GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
  427. const struct sockaddr *address,
  428. socklen_t address_len)
  429. {
  430. int ret;
  431. #ifdef IPV6_V6ONLY
  432. #ifdef IPPROTO_IPV6
  433. {
  434. const int on = 1;
  435. if (AF_INET6 == desc->af)
  436. if (setsockopt (desc->fd,
  437. IPPROTO_IPV6,
  438. IPV6_V6ONLY,
  439. (const void *) &on,
  440. sizeof(on)))
  441. LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
  442. "setsockopt");
  443. }
  444. #endif
  445. #endif
  446. if (AF_UNIX == address->sa_family)
  447. GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
  448. {
  449. const int on = 1;
  450. /* This is required here for TCP sockets, but only on UNIX */
  451. if ((SOCK_STREAM == desc->type) &&
  452. (0 != setsockopt (desc->fd,
  453. SOL_SOCKET,
  454. SO_REUSEADDR,
  455. &on, sizeof(on))))
  456. LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
  457. "setsockopt");
  458. }
  459. {
  460. /* set permissions of newly created non-abstract UNIX domain socket to
  461. "user-only"; applications can choose to relax this later */
  462. mode_t old_mask = 0; /* assigned to make compiler happy */
  463. const struct sockaddr_un *un = (const struct sockaddr_un *) address;
  464. int not_abstract = 0;
  465. if ((AF_UNIX == address->sa_family)
  466. && ('\0' != un->sun_path[0])) /* Not an abstract socket */
  467. not_abstract = 1;
  468. if (not_abstract)
  469. old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH
  470. | S_IXOTH);
  471. ret = bind (desc->fd,
  472. address,
  473. address_len);
  474. if (not_abstract)
  475. (void) umask (old_mask);
  476. }
  477. if (0 != ret)
  478. return GNUNET_SYSERR;
  479. desc->addr = GNUNET_malloc (address_len);
  480. GNUNET_memcpy (desc->addr, address, address_len);
  481. desc->addrlen = address_len;
  482. return GNUNET_OK;
  483. }
  484. /**
  485. * Close a socket
  486. *
  487. * @param desc socket
  488. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  489. */
  490. int
  491. GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
  492. {
  493. int ret;
  494. ret = close (desc->fd);
  495. const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
  496. /* Cleanup the UNIX domain socket and its parent directories in case of non
  497. abstract sockets */
  498. if ((AF_UNIX == desc->af) &&
  499. (NULL != desc->addr) &&
  500. ('\0' != un->sun_path[0]))
  501. {
  502. char *dirname = GNUNET_strndup (un->sun_path,
  503. sizeof(un->sun_path));
  504. if (0 != unlink (dirname))
  505. {
  506. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
  507. "unlink",
  508. dirname);
  509. }
  510. else
  511. {
  512. size_t len;
  513. len = strlen (dirname);
  514. while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
  515. len--;
  516. dirname[len] = '\0';
  517. if ((0 != len) && (0 != rmdir (dirname)))
  518. {
  519. switch (errno)
  520. {
  521. case EACCES:
  522. case ENOTEMPTY:
  523. case EPERM:
  524. /* these are normal and can just be ignored */
  525. break;
  526. default:
  527. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  528. "rmdir",
  529. dirname);
  530. break;
  531. }
  532. }
  533. }
  534. GNUNET_free (dirname);
  535. }
  536. GNUNET_NETWORK_socket_free_memory_only_ (desc);
  537. return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
  538. }
  539. /**
  540. * Only free memory of a socket, keep the file descriptor untouched.
  541. *
  542. * @param desc socket
  543. */
  544. void
  545. GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
  546. {
  547. GNUNET_free_non_null (desc->addr);
  548. GNUNET_free (desc);
  549. }
  550. /**
  551. * Box a native socket (and check that it is a socket).
  552. *
  553. * @param fd socket to box
  554. * @return NULL on error (including not supported on target platform)
  555. */
  556. struct GNUNET_NETWORK_Handle *
  557. GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
  558. {
  559. struct GNUNET_NETWORK_Handle *ret;
  560. if (fcntl (fd, F_GETFD) < 0)
  561. return NULL; /* invalid FD */
  562. ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
  563. ret->fd = fd;
  564. ret->af = AF_UNSPEC;
  565. return ret;
  566. }
  567. /**
  568. * Connect a socket to some remote address.
  569. *
  570. * @param desc socket
  571. * @param address peer address
  572. * @param address_len length of @a address
  573. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  574. */
  575. int
  576. GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
  577. const struct sockaddr *address,
  578. socklen_t address_len)
  579. {
  580. int ret;
  581. ret = connect (desc->fd,
  582. address,
  583. address_len);
  584. return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
  585. }
  586. /**
  587. * Get socket options
  588. *
  589. * @param desc socket
  590. * @param level protocol level of the option
  591. * @param optname identifier of the option
  592. * @param optval options
  593. * @param optlen length of @a optval
  594. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  595. */
  596. int
  597. GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
  598. int level,
  599. int optname,
  600. void *optval,
  601. socklen_t *optlen)
  602. {
  603. int ret;
  604. ret = getsockopt (desc->fd,
  605. level,
  606. optname,
  607. optval, optlen);
  608. return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
  609. }
  610. /**
  611. * Listen on a socket
  612. *
  613. * @param desc socket
  614. * @param backlog length of the listen queue
  615. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  616. */
  617. int
  618. GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
  619. int backlog)
  620. {
  621. int ret;
  622. ret = listen (desc->fd,
  623. backlog);
  624. return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
  625. }
  626. /**
  627. * How much data is available to be read on this descriptor?
  628. *
  629. * @param desc socket
  630. * @returns #GNUNET_SYSERR if no data is available, or on error!
  631. */
  632. ssize_t
  633. GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
  634. {
  635. int error;
  636. /* How much is there to be read? */
  637. int pending;
  638. error = ioctl (desc->fd,
  639. FIONREAD,
  640. &pending);
  641. if (0 == error)
  642. return (ssize_t) pending;
  643. return GNUNET_SYSERR;
  644. }
  645. /**
  646. * Read data from a socket (always non-blocking).
  647. *
  648. * @param desc socket
  649. * @param buffer buffer
  650. * @param length length of @a buffer
  651. * @param src_addr either the source to recv from, or all zeroes
  652. * to be filled in by recvfrom
  653. * @param addrlen length of the @a src_addr
  654. */
  655. ssize_t
  656. GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
  657. void *buffer,
  658. size_t length,
  659. struct sockaddr *src_addr,
  660. socklen_t *addrlen)
  661. {
  662. int ret;
  663. int flags;
  664. flags = 0;
  665. #ifdef MSG_DONTWAIT
  666. flags |= MSG_DONTWAIT;
  667. #endif
  668. ret = recvfrom (desc->fd,
  669. buffer,
  670. length,
  671. flags,
  672. src_addr,
  673. addrlen);
  674. return ret;
  675. }
  676. /**
  677. * Read data from a connected socket (always non-blocking).
  678. *
  679. * @param desc socket
  680. * @param buffer buffer
  681. * @param length length of @a buffer
  682. * @return number of bytes received, -1 on error
  683. */
  684. ssize_t
  685. GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
  686. void *buffer,
  687. size_t length)
  688. {
  689. int ret;
  690. int flags;
  691. flags = 0;
  692. #ifdef MSG_DONTWAIT
  693. flags |= MSG_DONTWAIT;
  694. #endif
  695. ret = recv (desc->fd,
  696. buffer,
  697. length,
  698. flags);
  699. return ret;
  700. }
  701. /**
  702. * Send data (always non-blocking).
  703. *
  704. * @param desc socket
  705. * @param buffer data to send
  706. * @param length size of the @a buffer
  707. * @return number of bytes sent, #GNUNET_SYSERR on error
  708. */
  709. ssize_t
  710. GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
  711. const void *buffer,
  712. size_t length)
  713. {
  714. int ret;
  715. int flags;
  716. flags = 0;
  717. #ifdef MSG_DONTWAIT
  718. flags |= MSG_DONTWAIT;
  719. #endif
  720. #ifdef MSG_NOSIGNAL
  721. flags |= MSG_NOSIGNAL;
  722. #endif
  723. ret = send (desc->fd,
  724. buffer,
  725. length,
  726. flags);
  727. return ret;
  728. }
  729. /**
  730. * Send data to a particular destination (always non-blocking).
  731. * This function only works for UDP sockets.
  732. *
  733. * @param desc socket
  734. * @param message data to send
  735. * @param length size of the @a message
  736. * @param dest_addr destination address
  737. * @param dest_len length of @a address
  738. * @return number of bytes sent, #GNUNET_SYSERR on error
  739. */
  740. ssize_t
  741. GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
  742. const void *message,
  743. size_t length,
  744. const struct sockaddr *dest_addr,
  745. socklen_t dest_len)
  746. {
  747. int ret;
  748. int flags;
  749. flags = 0;
  750. #ifdef MSG_DONTWAIT
  751. flags |= MSG_DONTWAIT;
  752. #endif
  753. #ifdef MSG_NOSIGNAL
  754. flags |= MSG_NOSIGNAL;
  755. #endif
  756. ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
  757. return ret;
  758. }
  759. /**
  760. * Set socket option
  761. *
  762. * @param fd socket
  763. * @param level protocol level of the option
  764. * @param option_name option identifier
  765. * @param option_value value to set
  766. * @param option_len size of @a option_value
  767. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  768. */
  769. int
  770. GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
  771. int level,
  772. int option_name,
  773. const void *option_value,
  774. socklen_t option_len)
  775. {
  776. int ret;
  777. ret = setsockopt (fd->fd,
  778. level,
  779. option_name,
  780. option_value,
  781. option_len);
  782. return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
  783. }
  784. /**
  785. * Create a new socket. Configure it for non-blocking IO and
  786. * mark it as non-inheritable to child processes (set the
  787. * close-on-exec flag).
  788. *
  789. * @param domain domain of the socket
  790. * @param type socket type
  791. * @param protocol network protocol
  792. * @return new socket, NULL on error
  793. */
  794. struct GNUNET_NETWORK_Handle *
  795. GNUNET_NETWORK_socket_create (int domain,
  796. int type,
  797. int protocol)
  798. {
  799. struct GNUNET_NETWORK_Handle *ret;
  800. int fd;
  801. fd = socket (domain, type, protocol);
  802. if (-1 == fd)
  803. return NULL;
  804. ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
  805. ret->fd = fd;
  806. if (GNUNET_OK !=
  807. initialize_network_handle (ret,
  808. domain,
  809. type))
  810. return NULL;
  811. return ret;
  812. }
  813. /**
  814. * Shut down socket operations
  815. * @param desc socket
  816. * @param how type of shutdown
  817. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  818. */
  819. int
  820. GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
  821. int how)
  822. {
  823. int ret;
  824. ret = shutdown (desc->fd, how);
  825. return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
  826. }
  827. /**
  828. * Disable the "CORK" feature for communication with the given socket,
  829. * forcing the OS to immediately flush the buffer on transmission
  830. * instead of potentially buffering multiple messages. Essentially
  831. * reduces the OS send buffers to zero.
  832. *
  833. * @param desc socket
  834. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  835. */
  836. int
  837. GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
  838. {
  839. int ret = 0;
  840. #if LINUX
  841. int value = 0;
  842. if (0 !=
  843. (ret =
  844. setsockopt (desc->fd,
  845. SOL_SOCKET,
  846. SO_SNDBUF,
  847. &value,
  848. sizeof(value))))
  849. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
  850. "setsockopt");
  851. if (0 !=
  852. (ret =
  853. setsockopt (desc->fd,
  854. SOL_SOCKET,
  855. SO_RCVBUF,
  856. &value,
  857. sizeof(value))))
  858. LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
  859. "setsockopt");
  860. #endif
  861. return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
  862. }
  863. /**
  864. * Reset FD set
  865. *
  866. * @param fds fd set
  867. */
  868. void
  869. GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
  870. {
  871. FD_ZERO (&fds->sds);
  872. fds->nsds = 0;
  873. }
  874. /**
  875. * Add a socket to the FD set
  876. *
  877. * @param fds fd set
  878. * @param desc socket to add
  879. */
  880. void
  881. GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
  882. const struct GNUNET_NETWORK_Handle *desc)
  883. {
  884. FD_SET (desc->fd,
  885. &fds->sds);
  886. fds->nsds = GNUNET_MAX (fds->nsds,
  887. desc->fd + 1);
  888. }
  889. /**
  890. * Check whether a socket is part of the fd set
  891. *
  892. * @param fds fd set
  893. * @param desc socket
  894. * @return 0 if the FD is not set
  895. */
  896. int
  897. GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
  898. const struct GNUNET_NETWORK_Handle *desc)
  899. {
  900. return FD_ISSET (desc->fd,
  901. &fds->sds);
  902. }
  903. /**
  904. * Add one fd set to another
  905. *
  906. * @param dst the fd set to add to
  907. * @param src the fd set to add from
  908. */
  909. void
  910. GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
  911. const struct GNUNET_NETWORK_FDSet *src)
  912. {
  913. int nfds;
  914. for (nfds = src->nsds; nfds >= 0; nfds--)
  915. if (FD_ISSET (nfds, &src->sds))
  916. FD_SET (nfds, &dst->sds);
  917. dst->nsds = GNUNET_MAX (dst->nsds,
  918. src->nsds);
  919. }
  920. /**
  921. * Copy one fd set to another
  922. *
  923. * @param to destination
  924. * @param from source
  925. */
  926. void
  927. GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
  928. const struct GNUNET_NETWORK_FDSet *from)
  929. {
  930. FD_COPY (&from->sds,
  931. &to->sds);
  932. to->nsds = from->nsds;
  933. }
  934. /**
  935. * Return file descriptor for this network handle
  936. *
  937. * @param desc wrapper to process
  938. * @return POSIX file descriptor
  939. */
  940. int
  941. GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc)
  942. {
  943. return desc->fd;
  944. }
  945. /**
  946. * Return sockaddr for this network handle
  947. *
  948. * @param desc wrapper to process
  949. * @return sockaddr
  950. */
  951. struct sockaddr*
  952. GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc)
  953. {
  954. return desc->addr;
  955. }
  956. /**
  957. * Return sockaddr length for this network handle
  958. *
  959. * @param desc wrapper to process
  960. * @return socklen_t for sockaddr
  961. */
  962. socklen_t
  963. GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc)
  964. {
  965. return desc->addrlen;
  966. }
  967. /**
  968. * Copy a native fd set
  969. *
  970. * @param to destination
  971. * @param from native source set
  972. * @param nfds the biggest socket number in from + 1
  973. */
  974. void
  975. GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
  976. const fd_set *from,
  977. int nfds)
  978. {
  979. FD_COPY (from,
  980. &to->sds);
  981. to->nsds = nfds;
  982. }
  983. /**
  984. * Set a native fd in a set
  985. *
  986. * @param to destination
  987. * @param nfd native FD to set
  988. */
  989. void
  990. GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
  991. int nfd)
  992. {
  993. GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
  994. FD_SET (nfd, &to->sds);
  995. to->nsds = GNUNET_MAX (nfd + 1,
  996. to->nsds);
  997. }
  998. /**
  999. * Test native fd in a set
  1000. *
  1001. * @param to set to test, NULL for empty set
  1002. * @param nfd native FD to test, or -1 for none
  1003. * @return #GNUNET_YES if FD is set in the set
  1004. */
  1005. int
  1006. GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
  1007. int nfd)
  1008. {
  1009. if ((-1 == nfd) ||
  1010. (NULL == to))
  1011. return GNUNET_NO;
  1012. return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
  1013. }
  1014. /**
  1015. * Add a file handle to the fd set
  1016. * @param fds fd set
  1017. * @param h the file handle to add
  1018. */
  1019. void
  1020. GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
  1021. const struct GNUNET_DISK_FileHandle *h)
  1022. {
  1023. int fd;
  1024. GNUNET_assert (GNUNET_OK ==
  1025. GNUNET_DISK_internal_file_handle_ (h,
  1026. &fd,
  1027. sizeof(int)));
  1028. FD_SET (fd,
  1029. &fds->sds);
  1030. fds->nsds = GNUNET_MAX (fd + 1,
  1031. fds->nsds);
  1032. }
  1033. /**
  1034. * Add a file handle to the fd set
  1035. * @param fds fd set
  1036. * @param h the file handle to add
  1037. */
  1038. void
  1039. GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
  1040. const struct GNUNET_DISK_FileHandle *h)
  1041. {
  1042. GNUNET_NETWORK_fdset_handle_set (fds, h);
  1043. }
  1044. /**
  1045. * Check if a file handle is part of an fd set
  1046. *
  1047. * @param fds fd set
  1048. * @param h file handle
  1049. * @return #GNUNET_YES if the file handle is part of the set
  1050. */
  1051. int
  1052. GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
  1053. const struct GNUNET_DISK_FileHandle *h)
  1054. {
  1055. return FD_ISSET (h->fd,
  1056. &fds->sds);
  1057. }
  1058. /**
  1059. * Checks if two fd sets overlap
  1060. *
  1061. * @param fds1 first fd set
  1062. * @param fds2 second fd set
  1063. * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
  1064. */
  1065. int
  1066. GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
  1067. const struct GNUNET_NETWORK_FDSet *fds2)
  1068. {
  1069. int nfds;
  1070. nfds = GNUNET_MIN (fds1->nsds,
  1071. fds2->nsds);
  1072. while (nfds > 0)
  1073. {
  1074. nfds--;
  1075. if ((FD_ISSET (nfds,
  1076. &fds1->sds)) &&
  1077. (FD_ISSET (nfds,
  1078. &fds2->sds)))
  1079. return GNUNET_YES;
  1080. }
  1081. return GNUNET_NO;
  1082. }
  1083. /**
  1084. * Creates an fd set
  1085. *
  1086. * @return a new fd set
  1087. */
  1088. struct GNUNET_NETWORK_FDSet *
  1089. GNUNET_NETWORK_fdset_create ()
  1090. {
  1091. struct GNUNET_NETWORK_FDSet *fds;
  1092. fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
  1093. GNUNET_NETWORK_fdset_zero (fds);
  1094. return fds;
  1095. }
  1096. /**
  1097. * Releases the associated memory of an fd set
  1098. *
  1099. * @param fds fd set
  1100. */
  1101. void
  1102. GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
  1103. {
  1104. GNUNET_free (fds);
  1105. }
  1106. /**
  1107. * Test if the given @a port is available.
  1108. *
  1109. * @param ipproto transport protocol to test (i.e. IPPROTO_TCP)
  1110. * @param port port number to test
  1111. * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
  1112. */
  1113. int
  1114. GNUNET_NETWORK_test_port_free (int ipproto,
  1115. uint16_t port)
  1116. {
  1117. struct GNUNET_NETWORK_Handle *socket;
  1118. int bind_status;
  1119. int socktype;
  1120. char open_port_str[6];
  1121. struct addrinfo hint;
  1122. struct addrinfo *ret;
  1123. struct addrinfo *ai;
  1124. GNUNET_snprintf (open_port_str,
  1125. sizeof(open_port_str),
  1126. "%u",
  1127. (unsigned int) port);
  1128. socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
  1129. ret = NULL;
  1130. memset (&hint, 0, sizeof(hint));
  1131. hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
  1132. hint.ai_socktype = socktype;
  1133. hint.ai_protocol = ipproto;
  1134. hint.ai_addrlen = 0;
  1135. hint.ai_addr = NULL;
  1136. hint.ai_canonname = NULL;
  1137. hint.ai_next = NULL;
  1138. hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
  1139. GNUNET_assert (0 == getaddrinfo (NULL,
  1140. open_port_str,
  1141. &hint,
  1142. &ret));
  1143. bind_status = GNUNET_NO;
  1144. for (ai = ret; NULL != ai; ai = ai->ai_next)
  1145. {
  1146. socket = GNUNET_NETWORK_socket_create (ai->ai_family,
  1147. ai->ai_socktype,
  1148. ai->ai_protocol);
  1149. if (NULL == socket)
  1150. continue;
  1151. bind_status = GNUNET_NETWORK_socket_bind (socket,
  1152. ai->ai_addr,
  1153. ai->ai_addrlen);
  1154. GNUNET_NETWORK_socket_close (socket);
  1155. if (GNUNET_OK != bind_status)
  1156. break;
  1157. }
  1158. freeaddrinfo (ret);
  1159. return bind_status;
  1160. }
  1161. /**
  1162. * Check if sockets or pipes meet certain conditions
  1163. *
  1164. * @param rfds set of sockets or pipes to be checked for readability
  1165. * @param wfds set of sockets or pipes to be checked for writability
  1166. * @param efds set of sockets or pipes to be checked for exceptions
  1167. * @param timeout relative value when to return
  1168. * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
  1169. */
  1170. int
  1171. GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
  1172. struct GNUNET_NETWORK_FDSet *wfds,
  1173. struct GNUNET_NETWORK_FDSet *efds,
  1174. const struct GNUNET_TIME_Relative timeout)
  1175. {
  1176. int nfds;
  1177. struct timeval tv;
  1178. if (NULL != rfds)
  1179. nfds = rfds->nsds;
  1180. else
  1181. nfds = 0;
  1182. if (NULL != wfds)
  1183. nfds = GNUNET_MAX (nfds,
  1184. wfds->nsds);
  1185. if (NULL != efds)
  1186. nfds = GNUNET_MAX (nfds,
  1187. efds->nsds);
  1188. if ((0 == nfds) &&
  1189. (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
  1190. {
  1191. GNUNET_break (0);
  1192. LOG (GNUNET_ERROR_TYPE_ERROR,
  1193. _ (
  1194. "Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
  1195. "select");
  1196. }
  1197. if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned
  1198. long long)
  1199. LONG_MAX)
  1200. {
  1201. tv.tv_sec = LONG_MAX;
  1202. tv.tv_usec = 999999L;
  1203. }
  1204. else
  1205. {
  1206. tv.tv_sec = (long) (timeout.rel_value_us
  1207. / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
  1208. tv.tv_usec =
  1209. (timeout.rel_value_us
  1210. - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
  1211. }
  1212. return select (nfds,
  1213. (NULL != rfds) ? &rfds->sds : NULL,
  1214. (NULL != wfds) ? &wfds->sds : NULL,
  1215. (NULL != efds) ? &efds->sds : NULL,
  1216. (timeout.rel_value_us ==
  1217. GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
  1218. }
  1219. /* end of network.c */