network.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. #ifndef CONFIG
  2. #define CONFIG "config.h"
  3. #endif // CONFIG
  4. #include CONFIG
  5. #ifndef USE_MSRPC
  6. #ifndef _GNU_SOURCE
  7. #define _GNU_SOURCE
  8. #endif
  9. #include <string.h>
  10. #ifndef _WIN32
  11. #include <signal.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <errno.h>
  15. #include <netinet/in.h>
  16. #endif // WIN32
  17. #include "network.h"
  18. #include "endian.h"
  19. #include "output.h"
  20. #include "helpers.h"
  21. #include "shared_globals.h"
  22. #include "rpc.h"
  23. #ifndef _WIN32
  24. typedef ssize_t (*sendrecv_t)(int, void*, size_t, int);
  25. #else
  26. typedef int (WINAPI *sendrecv_t)(SOCKET, void*, int, int);
  27. #endif
  28. // Send or receive a fixed number of bytes regardless if received in one or more chunks
  29. int_fast8_t sendrecv(SOCKET sock, BYTE *data, int len, int_fast8_t do_send)
  30. {
  31. int n;
  32. sendrecv_t f = do_send
  33. ? (sendrecv_t) send
  34. : (sendrecv_t) recv;
  35. do
  36. {
  37. n = f(sock, data, len, 0);
  38. }
  39. while (
  40. ( n < 0 && socket_errno == VLMCSD_EINTR ) || ( n > 0 && ( data += n, (len -= n) > 0 ) ));
  41. return ! len;
  42. }
  43. static int_fast8_t ip2str(char *restrict result, const size_t resultLength, const struct sockaddr *const restrict socketAddress, const socklen_t socketLength)
  44. {
  45. static const char *const fIPv4 = "%s:%s";
  46. static const char *const fIPv6 = "[%s]:%s";
  47. char ipAddress[64], portNumber[8];
  48. if (getnameinfo
  49. (
  50. socketAddress,
  51. socketLength,
  52. ipAddress,
  53. sizeof(ipAddress),
  54. portNumber,
  55. sizeof(portNumber),
  56. NI_NUMERICHOST | NI_NUMERICSERV
  57. ))
  58. {
  59. return FALSE;
  60. }
  61. if ((unsigned int)snprintf(result, resultLength, socketAddress->sa_family == AF_INET6 ? fIPv6 : fIPv4, ipAddress, portNumber) > resultLength) return FALSE;
  62. return TRUE;
  63. }
  64. static int_fast8_t getSocketList(struct addrinfo **saList, const char *const addr, const int flags, const int AddressFamily)
  65. {
  66. int status;
  67. char *szHost, *szPort;
  68. size_t len = strlen(addr) + 1;
  69. // Don't alloca too much
  70. if (len > 264) return FALSE;
  71. char *addrcopy = (char*)alloca(len);
  72. memcpy(addrcopy, addr, len);
  73. parseAddress(addrcopy, &szHost, &szPort);
  74. struct addrinfo hints;
  75. memset(&hints, 0, sizeof(struct addrinfo));
  76. hints.ai_family = AddressFamily;
  77. hints.ai_socktype = SOCK_STREAM;
  78. hints.ai_protocol = IPPROTO_TCP;
  79. hints.ai_flags = flags;
  80. if ((status = getaddrinfo(szHost, szPort, &hints, saList)))
  81. {
  82. printerrorf("Warning: %s: %s\n", addr, gai_strerror(status));
  83. return FALSE;
  84. }
  85. return TRUE;
  86. }
  87. static int_fast8_t setBlockingEnabled(SOCKET fd, int_fast8_t blocking)
  88. {
  89. if (fd == INVALID_SOCKET) return FALSE;
  90. #ifdef _WIN32
  91. unsigned long mode = blocking ? 0 : 1;
  92. return (ioctlsocket(fd, FIONBIO, &mode) == 0) ? TRUE : FALSE;
  93. #else // POSIX
  94. int flags = fcntl(fd, F_GETFL, 0);
  95. if (flags < 0) return FALSE;
  96. flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
  97. return (fcntl(fd, F_SETFL, flags) == 0) ? TRUE : FALSE;
  98. #endif // POSIX
  99. }
  100. int_fast8_t isDisconnected(const SOCKET s)
  101. {
  102. char buffer[1];
  103. if (!setBlockingEnabled(s, FALSE)) return TRUE;
  104. int n = recv(s, buffer, 1, MSG_PEEK);
  105. if (!setBlockingEnabled(s, TRUE)) return TRUE;
  106. if (n == 0) return TRUE;
  107. return FALSE;
  108. }
  109. // Connect to TCP address addr (e.g. "kms.example.com:1688") and return an
  110. // open socket for the connection if successful or INVALID_SOCKET otherwise
  111. SOCKET connectToAddress(const char *const addr, const int AddressFamily, int_fast8_t showHostName)
  112. {
  113. struct addrinfo *saList, *sa;
  114. SOCKET s = INVALID_SOCKET;
  115. char szAddr[128];
  116. if (!getSocketList(&saList, addr, 0, AddressFamily)) return INVALID_SOCKET;
  117. for (sa = saList; sa; sa = sa->ai_next)
  118. {
  119. // struct sockaddr_in* addr4 = (struct sockaddr_in*)sa->ai_addr;
  120. // struct sockaddr_in6* addr6 = (struct sockaddr_in6*)sa->ai_addr;
  121. if (ip2str(szAddr, sizeof(szAddr), sa->ai_addr, sa->ai_addrlen))
  122. {
  123. if (showHostName)
  124. printf("Connecting to %s (%s) ... ", addr, szAddr);
  125. else
  126. printf("Connecting to %s ... ", szAddr);
  127. fflush(stdout);
  128. }
  129. s = socket(sa->ai_family, SOCK_STREAM, IPPROTO_TCP);
  130. # if !defined(NO_TIMEOUT) && !__minix__
  131. # ifndef _WIN32 // Standard Posix timeout structure
  132. struct timeval to;
  133. to.tv_sec = 10;
  134. to.tv_usec = 0;
  135. # else // Windows requires a DWORD with milliseconds
  136. DWORD to = 10000;
  137. # endif // _WIN32
  138. setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (sockopt_t)&to, sizeof(to));
  139. setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (sockopt_t)&to, sizeof(to));
  140. # endif // !defined(NO_TIMEOUT) && !__minix__
  141. if (!connect(s, sa->ai_addr, sa->ai_addrlen))
  142. {
  143. printf("successful\n");
  144. break;
  145. }
  146. errorout("%s\n", socket_errno == VLMCSD_EINPROGRESS ? "Timed out" : vlmcsd_strerror(socket_errno));
  147. socketclose(s);
  148. s = INVALID_SOCKET;
  149. }
  150. freeaddrinfo(saList);
  151. return s;
  152. }
  153. #ifndef NO_SOCKETS
  154. // Create a Listening socket for addrinfo sa and return socket s
  155. // szHost and szPort are for logging only
  156. static int listenOnAddress(const struct addrinfo *const ai, SOCKET *s)
  157. {
  158. int error;
  159. char ipstr[64];
  160. ip2str(ipstr, sizeof(ipstr), ai->ai_addr, ai->ai_addrlen);
  161. //*s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  162. *s = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
  163. if (*s == INVALID_SOCKET)
  164. {
  165. error = socket_errno;
  166. printerrorf("Warning: %s error. %s\n", ai->ai_family == AF_INET6 ? cIPv6 : cIPv4, vlmcsd_strerror(error));
  167. return error;
  168. }
  169. # if !defined(_WIN32) && !defined(NO_SIGHUP)
  170. int flags = fcntl(*s, F_GETFD, 0);
  171. if (flags != -1)
  172. {
  173. flags |= FD_CLOEXEC;
  174. fcntl(*s, F_SETFD, flags);
  175. }
  176. # ifdef _PEDANTIC
  177. else
  178. {
  179. printerrorf("Warning: Could not set FD_CLOEXEC flag on %s: %s\n", ipstr, vlmcsd_strerror(errno));
  180. }
  181. # endif // _PEDANTIC
  182. # endif // !defined(_WIN32) && !defined(NO_SIGHUP)
  183. BOOL socketOption = TRUE;
  184. // fix for lame tomato toolchain
  185. # ifndef IPV6_V6ONLY
  186. # ifdef __linux__
  187. # define IPV6_V6ONLY (26)
  188. # endif // __linux__
  189. # endif // IPV6_V6ONLY
  190. # ifdef IPV6_V6ONLY
  191. if (ai->ai_family == AF_INET6) setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_t)&socketOption, sizeof(socketOption));
  192. # endif
  193. # ifndef _WIN32
  194. setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, (sockopt_t)&socketOption, sizeof(socketOption));
  195. # endif
  196. if (bind(*s, ai->ai_addr, ai->ai_addrlen) || listen(*s, SOMAXCONN))
  197. {
  198. error = socket_errno;
  199. printerrorf("Warning: %s: %s\n", ipstr, vlmcsd_strerror(error));
  200. socketclose(*s);
  201. return error;
  202. }
  203. # ifndef NO_LOG
  204. logger("Listening on %s\n", ipstr);
  205. # endif
  206. return 0;
  207. }
  208. // Adds a listening socket for an address string,
  209. // e.g. 127.0.0.1:1688 or [2001:db8:dead:beef::1]:1688
  210. BOOL addListeningSocket(const char *const addr)
  211. {
  212. struct addrinfo *aiList, *ai;
  213. int result = FALSE;
  214. SOCKET *s = SocketList + numsockets;
  215. if (getSocketList(&aiList, addr, AI_PASSIVE | AI_NUMERICHOST, AF_UNSPEC))
  216. {
  217. for (ai = aiList; ai; ai = ai->ai_next)
  218. {
  219. // struct sockaddr_in* addr4 = (struct sockaddr_in*)sa->ai_addr;
  220. // struct sockaddr_in6* addr6 = (struct sockaddr_in6*)sa->ai_addr;
  221. if (numsockets >= FD_SETSIZE)
  222. {
  223. #ifdef _PEDANTIC // Do not report this error in normal builds to keep file size low
  224. printerrorf("Warning: Cannot listen on %s. Your OS only supports %u listening sockets in an FD_SET.\n", addr, FD_SETSIZE);
  225. #endif
  226. break;
  227. }
  228. if (!listenOnAddress(ai, s))
  229. {
  230. numsockets++;
  231. result = TRUE;
  232. }
  233. }
  234. freeaddrinfo(aiList);
  235. }
  236. return result;
  237. }
  238. // Just create some dummy sockets to see if we have a specific protocol (IPv4 or IPv6)
  239. __pure int_fast8_t checkProtocolStack(const int addressfamily)
  240. {
  241. SOCKET s; // = INVALID_SOCKET;
  242. s = socket(addressfamily, SOCK_STREAM, 0);
  243. int_fast8_t success = (s != INVALID_SOCKET);
  244. socketclose(s);
  245. return success;
  246. }
  247. // Build an fd_set of all listening socket then use select to wait for an incoming connection
  248. static SOCKET network_accept_any()
  249. {
  250. fd_set ListeningSocketsList;
  251. SOCKET maxSocket, sock;
  252. int i;
  253. int status;
  254. FD_ZERO(&ListeningSocketsList);
  255. maxSocket = 0;
  256. for (i = 0; i < numsockets; i++)
  257. {
  258. FD_SET(SocketList[i], &ListeningSocketsList);
  259. if (SocketList[i] > maxSocket) maxSocket = SocketList[i];
  260. }
  261. status = select(maxSocket + 1, &ListeningSocketsList, NULL, NULL, NULL);
  262. if (status < 0) return INVALID_SOCKET;
  263. sock = INVALID_SOCKET;
  264. for (i = 0; i < numsockets; i++)
  265. {
  266. if (FD_ISSET(SocketList[i], &ListeningSocketsList))
  267. {
  268. sock = SocketList[i];
  269. break;
  270. }
  271. }
  272. if (sock == INVALID_SOCKET)
  273. return INVALID_SOCKET;
  274. else
  275. return accept(sock, NULL, NULL);
  276. }
  277. void closeAllListeningSockets()
  278. {
  279. int i;
  280. for (i = 0; i < numsockets; i++)
  281. {
  282. shutdown(SocketList[i], VLMCSD_SHUT_RDWR);
  283. socketclose(SocketList[i]);
  284. }
  285. }
  286. #endif // NO_SOCKETS
  287. static void serveClient(const SOCKET s_client, const DWORD RpcAssocGroup)
  288. {
  289. # if !defined(NO_TIMEOUT) && !__minix__
  290. # ifndef _WIN32 // Standard Posix timeout structure
  291. struct timeval to;
  292. to.tv_sec = ServerTimeout;
  293. to.tv_usec = 0;
  294. #else // Windows requires a DWORD with milliseconds
  295. DWORD to = ServerTimeout * 1000;
  296. # endif // _WIN32
  297. # if !defined(NO_LOG) && defined(_PEDANTIC)
  298. int result =
  299. setsockopt(s_client, SOL_SOCKET, SO_RCVTIMEO, (sockopt_t)&to, sizeof(to)) ||
  300. setsockopt(s_client, SOL_SOCKET, SO_SNDTIMEO, (sockopt_t)&to, sizeof(to));
  301. if (result) logger("Warning: Set timeout failed: %s\n", vlmcsd_strerror(socket_errno));
  302. # else // !(!defined(NO_LOG) && defined(_PEDANTIC))
  303. setsockopt(s_client, SOL_SOCKET, SO_RCVTIMEO, (sockopt_t)&to, sizeof(to));
  304. setsockopt(s_client, SOL_SOCKET, SO_SNDTIMEO, (sockopt_t)&to, sizeof(to));
  305. # endif // !(!defined(NO_LOG) && defined(_PEDANTIC))
  306. # endif // !defined(NO_TIMEOUT) && !__minix__
  307. char ipstr[64];
  308. socklen_t len;
  309. struct sockaddr_storage addr;
  310. len = sizeof addr;
  311. if (getpeername(s_client, (struct sockaddr*)&addr, &len) ||
  312. !ip2str(ipstr, sizeof(ipstr), (struct sockaddr*)&addr, len))
  313. {
  314. # if !defined(NO_LOG) && defined(_PEDANTIC)
  315. logger("Fatal: Cannot determine client's IP address: %s\n", vlmcsd_strerror(errno));
  316. # endif // !defined(NO_LOG) && defined(_PEDANTIC)
  317. socketclose(s_client);
  318. return;
  319. }
  320. # ifndef NO_LOG
  321. const char *const connection_type = addr.ss_family == AF_INET6 ? cIPv6 : cIPv4;
  322. static const char *const cAccepted = "accepted";
  323. static const char *const cClosed = "closed";
  324. static const char *const fIP = "%s connection %s: %s.\n";
  325. logger(fIP, connection_type, cAccepted, ipstr);
  326. #endif // NO_LOG
  327. rpcServer(s_client, RpcAssocGroup, ipstr);
  328. # ifndef NO_LOG
  329. logger(fIP, connection_type, cClosed, ipstr);
  330. # endif // NO_LOG
  331. socketclose(s_client);
  332. }
  333. #ifndef NO_SOCKETS
  334. static void post_sem(void)
  335. {
  336. #if !defined(NO_LIMIT) && !__minix__
  337. if (!InetdMode && MaxTasks != SEM_VALUE_MAX)
  338. {
  339. semaphore_post(Semaphore);
  340. }
  341. #endif // !defined(NO_LIMIT) && !__minix__
  342. }
  343. static void wait_sem(void)
  344. {
  345. #if !defined(NO_LIMIT) && !__minix__
  346. if (!InetdMode && MaxTasks != SEM_VALUE_MAX)
  347. {
  348. semaphore_wait(Semaphore);
  349. }
  350. #endif // !defined(NO_LIMIT) && !__minix__
  351. }
  352. #endif // NO_SOCKETS
  353. #if defined(USE_THREADS) && !defined(NO_SOCKETS)
  354. #if defined(_WIN32) || defined(__CYGWIN__) // Win32 Threads
  355. static DWORD WINAPI serveClientThreadProc(PCLDATA clData)
  356. #else // Posix threads
  357. static void *serveClientThreadProc (PCLDATA clData)
  358. #endif // Thread proc is identical in WIN32 and Posix threads
  359. {
  360. serveClient(clData->socket, clData->RpcAssocGroup);
  361. free(clData);
  362. post_sem();
  363. return 0;
  364. }
  365. #endif // USE_THREADS
  366. #ifndef NO_SOCKETS
  367. #if defined(USE_THREADS) && (defined(_WIN32) || defined(__CYGWIN__)) // Windows Threads
  368. static int serveClientAsyncWinThreads(const PCLDATA thr_CLData)
  369. {
  370. wait_sem();
  371. HANDLE h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)serveClientThreadProc, thr_CLData, 0, NULL);
  372. if (h)
  373. CloseHandle(h);
  374. else
  375. {
  376. socketclose(thr_CLData->socket);
  377. free(thr_CLData);
  378. post_sem();
  379. return GetLastError();
  380. }
  381. return NO_ERROR;
  382. }
  383. #endif // defined(USE_THREADS) && defined(_WIN32) // Windows Threads
  384. #if defined(USE_THREADS) && !defined(_WIN32) && !defined(__CYGWIN__) // Posix Threads
  385. static int ServeClientAsyncPosixThreads(const PCLDATA thr_CLData)
  386. {
  387. pthread_t p_thr;
  388. pthread_attr_t attr;
  389. wait_sem();
  390. // Must set detached state to avoid memory leak
  391. if (pthread_attr_init(&attr) ||
  392. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) ||
  393. pthread_create(&p_thr, &attr, (void * (*)(void *))serveClientThreadProc, thr_CLData))
  394. {
  395. socketclose(thr_CLData->socket);
  396. free(thr_CLData);
  397. post_sem();
  398. return !0;
  399. }
  400. return 0;
  401. }
  402. #endif // defined(USE_THREADS) && !defined(_WIN32) // Posix Threads
  403. #ifndef USE_THREADS // fork() implementation
  404. static void ChildSignalHandler(const int signal)
  405. {
  406. if (signal == SIGHUP) return;
  407. post_sem();
  408. #ifndef NO_LOG
  409. logger("Warning: Child killed/crashed by %s\n", strsignal(signal));
  410. #endif // NO_LOG
  411. exit(!0);
  412. }
  413. static int ServeClientAsyncFork(const SOCKET s_client, const DWORD RpcAssocGroup)
  414. {
  415. int pid;
  416. wait_sem();
  417. if ((pid = fork()) < 0)
  418. {
  419. return errno;
  420. }
  421. else if ( pid )
  422. {
  423. // Parent process
  424. socketclose(s_client);
  425. return 0;
  426. }
  427. else
  428. {
  429. // Child process
  430. // Setup a Child Handler for most common termination signals
  431. struct sigaction sa;
  432. sa.sa_flags = 0;
  433. sa.sa_handler = ChildSignalHandler;
  434. static int signallist[] = { SIGHUP, SIGINT, SIGTERM, SIGSEGV, SIGILL, SIGFPE, SIGBUS };
  435. if (!sigemptyset(&sa.sa_mask))
  436. {
  437. uint_fast8_t i;
  438. for (i = 0; i < _countof(signallist); i++)
  439. {
  440. sigaction(signallist[i], &sa, NULL);
  441. }
  442. }
  443. serveClient(s_client, RpcAssocGroup);
  444. post_sem();
  445. exit(0);
  446. }
  447. }
  448. #endif
  449. int serveClientAsync(const SOCKET s_client, const DWORD RpcAssocGroup)
  450. {
  451. #ifndef USE_THREADS // fork() implementation
  452. return ServeClientAsyncFork(s_client, RpcAssocGroup);
  453. #else // threads implementation
  454. PCLDATA thr_CLData = (PCLDATA)vlmcsd_malloc(sizeof(CLDATA));
  455. thr_CLData->socket = s_client;
  456. thr_CLData->RpcAssocGroup = RpcAssocGroup;
  457. #if defined(_WIN32) || defined (__CYGWIN__) // Windows threads
  458. return serveClientAsyncWinThreads(thr_CLData);
  459. #else // Posix Threads
  460. return ServeClientAsyncPosixThreads(thr_CLData);
  461. #endif // Posix Threads
  462. #endif // USE_THREADS
  463. }
  464. #endif // NO_SOCKETS
  465. int runServer()
  466. {
  467. DWORD RpcAssocGroup = rand32();
  468. // If compiled for inetd-only mode just serve the stdin socket
  469. #ifdef NO_SOCKETS
  470. serveClient(STDIN_FILENO, RpcAssocGroup);
  471. return 0;
  472. #else
  473. // In inetd mode just handle the stdin socket
  474. if (InetdMode)
  475. {
  476. serveClient(STDIN_FILENO, RpcAssocGroup);
  477. return 0;
  478. }
  479. // Standalone mode
  480. for (;;)
  481. {
  482. int error;
  483. SOCKET s_client;
  484. if ( (s_client = network_accept_any()) == INVALID_SOCKET )
  485. {
  486. error = socket_errno;
  487. if (error == VLMCSD_EINTR || error == VLMCSD_ECONNABORTED) continue;
  488. #ifdef _NTSERVICE
  489. if (ServiceShutdown) return 0;
  490. #endif
  491. #ifndef NO_LOG
  492. logger("Fatal: %s\n",vlmcsd_strerror(error));
  493. #endif
  494. return error;
  495. }
  496. RpcAssocGroup++;
  497. serveClientAsync(s_client, RpcAssocGroup);
  498. }
  499. #endif // NO_SOCKETS
  500. return 0;
  501. }
  502. #endif // USE_MSRPC