Socket.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2025 IBM Corp., Ian Craggs and others
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial implementation and documentation
  15. * Ian Craggs - async client updates
  16. * Ian Craggs - fix for bug 484496
  17. * Juergen Kosel, Ian Craggs - fix for issue #135
  18. * Ian Craggs - issue #217
  19. * Ian Craggs - fix for issue #186
  20. * Ian Craggs - remove StackTrace print debugging calls
  21. *******************************************************************************/
  22. /**
  23. * @file
  24. * \brief Socket related functions
  25. *
  26. * Some other related functions are in the SocketBuffer module
  27. */
  28. #include "Socket.h"
  29. #include "Log.h"
  30. #include "SocketBuffer.h"
  31. #include "Messages.h"
  32. #include "StackTrace.h"
  33. #if defined(OPENSSL)
  34. #include "SSLSocket.h"
  35. #endif
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <signal.h>
  39. #include <ctype.h>
  40. #include "Heap.h"
  41. #if defined(UNIXSOCK)
  42. #include <sys/un.h>
  43. #endif
  44. #if defined(USE_SELECT)
  45. int isReady(int socket, fd_set* read_set, fd_set* write_set);
  46. int Socket_continueWrites(fd_set* pwset, SOCKET* socket, mutex_type mutex);
  47. #else
  48. int isReady(int index);
  49. int Socket_continueWrites(SOCKET* socket, mutex_type mutex);
  50. #endif
  51. int Socket_setnonblocking(SOCKET sock);
  52. int Socket_error(char* aString, SOCKET sock);
  53. int Socket_addSocket(SOCKET newSd);
  54. int Socket_writev(SOCKET socket, iobuf* iovecs, int count, unsigned long* bytes);
  55. int Socket_close_only(SOCKET socket);
  56. int Socket_continueWrite(SOCKET socket);
  57. char* Socket_getaddrname(struct sockaddr* sa, SOCKET sock);
  58. int Socket_abortWrite(SOCKET socket);
  59. #if defined(_WIN32) || defined(_WIN64)
  60. #define iov_len len
  61. #define iov_base buf
  62. #define snprintf _snprintf
  63. #endif
  64. /**
  65. * Structure to hold all socket data for this module
  66. */
  67. Sockets mod_s;
  68. #if defined(USE_SELECT)
  69. static fd_set wset;
  70. #endif
  71. extern mutex_type socket_mutex;
  72. /**
  73. * Set a socket non-blocking, OS independently
  74. * @param sock the socket to set non-blocking
  75. * @return TCP call error code
  76. */
  77. int Socket_setnonblocking(SOCKET sock)
  78. {
  79. int rc;
  80. #if defined(_WIN32) || defined(_WIN64)
  81. u_long flag = 1L;
  82. FUNC_ENTRY;
  83. rc = ioctl(sock, FIONBIO, &flag);
  84. #else
  85. int flags;
  86. FUNC_ENTRY;
  87. if ((flags = fcntl(sock, F_GETFL, 0)))
  88. flags = 0;
  89. rc = fcntl(sock, F_SETFL, flags | O_NONBLOCK);
  90. #endif
  91. FUNC_EXIT_RC(rc);
  92. return rc;
  93. }
  94. /**
  95. * Gets the specific error corresponding to SOCKET_ERROR
  96. * @param aString the function that was being used when the error occurred
  97. * @param sock the socket on which the error occurred
  98. * @return the specific TCP error code
  99. */
  100. int Socket_error(char* aString, SOCKET sock)
  101. {
  102. int err;
  103. #if defined(_WIN32) || defined(_WIN64)
  104. err = WSAGetLastError();
  105. #else
  106. err = errno;
  107. #endif
  108. if (err != EINTR && err != EAGAIN && err != EINPROGRESS && err != EWOULDBLOCK)
  109. {
  110. if (strcmp(aString, "shutdown") != 0 || (err != ENOTCONN && err != ECONNRESET))
  111. Log(TRACE_MINIMUM, -1, "Socket error %s(%d) in %s for socket %d", strerror(err), err, aString, sock);
  112. }
  113. return err;
  114. }
  115. #if !defined(_WIN32) && !defined(_WIN64)
  116. void SIGPIPE_ignore()
  117. {
  118. #if defined(PAHO_IGNORE_WITH_SIGNAL)
  119. if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
  120. Log(LOG_ERROR, -1, "Failed to ignore SIG_PIPE, errno %d", errno);
  121. #else
  122. struct sigaction action;
  123. if (sigaction(SIGPIPE, NULL, &action) != 0) /* get current action */
  124. Log(LOG_ERROR, -1, "sigaction failed to get SIG_PIPE action8, errno %d", errno);
  125. action.sa_handler = SIG_IGN;
  126. if (sigaction(SIGPIPE, &action, NULL) != 0)
  127. Log(LOG_ERROR, -1, "sigaction failed to ignore SIG_PIPE, errno %d", errno);
  128. #endif
  129. }
  130. #endif
  131. /**
  132. * Initialize the socket module
  133. */
  134. void Socket_outInitialize(void)
  135. {
  136. #if defined(_WIN32) || defined(_WIN64)
  137. WORD winsockVer = 0x0202;
  138. WSADATA wsd;
  139. FUNC_ENTRY;
  140. WSAStartup(winsockVer, &wsd);
  141. #else
  142. FUNC_ENTRY;
  143. SIGPIPE_ignore();
  144. #endif
  145. SocketBuffer_initialize();
  146. mod_s.connect_pending = ListInitialize();
  147. mod_s.write_pending = ListInitialize();
  148. #if defined(USE_SELECT)
  149. mod_s.clientsds = ListInitialize();
  150. mod_s.cur_clientsds = NULL;
  151. FD_ZERO(&(mod_s.rset)); /* Initialize the descriptor set */
  152. FD_ZERO(&(mod_s.pending_wset));
  153. mod_s.maxfdp1 = 0;
  154. memcpy((void*)&(mod_s.rset_saved), (void*)&(mod_s.rset), sizeof(mod_s.rset_saved));
  155. #else
  156. mod_s.nfds = 0;
  157. mod_s.fds_read = NULL;
  158. mod_s.fds_write = NULL;
  159. mod_s.saved.cur_fd = -1;
  160. mod_s.saved.fds_write = NULL;
  161. mod_s.saved.fds_read = NULL;
  162. mod_s.saved.nfds = 0;
  163. #endif
  164. FUNC_EXIT;
  165. }
  166. /**
  167. * Terminate the socket module
  168. */
  169. void Socket_outTerminate(void)
  170. {
  171. FUNC_ENTRY;
  172. ListFree(mod_s.connect_pending);
  173. ListFree(mod_s.write_pending);
  174. #if defined(USE_SELECT)
  175. ListFree(mod_s.clientsds);
  176. #else
  177. if (mod_s.fds_read)
  178. free(mod_s.fds_read);
  179. if (mod_s.fds_write)
  180. free(mod_s.fds_write);
  181. if (mod_s.saved.fds_write)
  182. free(mod_s.saved.fds_write);
  183. if (mod_s.saved.fds_read)
  184. free(mod_s.saved.fds_read);
  185. #endif
  186. SocketBuffer_terminate();
  187. #if defined(_WIN32) || defined(_WIN64)
  188. WSACleanup();
  189. #endif
  190. FUNC_EXIT;
  191. }
  192. #if defined(USE_SELECT)
  193. /**
  194. * Add a socket to the list of socket to check with select
  195. * @param newSd the new socket to add
  196. */
  197. int Socket_addSocket(SOCKET newSd)
  198. {
  199. int rc = 0;
  200. FUNC_ENTRY;
  201. if (ListFindItem(mod_s.clientsds, &newSd, intcompare) == NULL) /* make sure we don't add the same socket twice */
  202. {
  203. if (mod_s.clientsds->count >= FD_SETSIZE)
  204. {
  205. Log(LOG_ERROR, -1, "addSocket: exceeded FD_SETSIZE %d", FD_SETSIZE);
  206. rc = SOCKET_ERROR;
  207. }
  208. else
  209. {
  210. SOCKET* pnewSd = (SOCKET*)malloc(sizeof(newSd));
  211. if (!pnewSd)
  212. {
  213. rc = PAHO_MEMORY_ERROR;
  214. goto exit;
  215. }
  216. *pnewSd = newSd;
  217. if (!ListAppend(mod_s.clientsds, pnewSd, sizeof(newSd)))
  218. {
  219. free(pnewSd);
  220. rc = PAHO_MEMORY_ERROR;
  221. goto exit;
  222. }
  223. FD_SET(newSd, &(mod_s.rset_saved));
  224. mod_s.maxfdp1 = max(mod_s.maxfdp1, (int)newSd + 1);
  225. rc = Socket_setnonblocking(newSd);
  226. if (rc == SOCKET_ERROR)
  227. Log(LOG_ERROR, -1, "addSocket: setnonblocking");
  228. }
  229. }
  230. else
  231. Log(LOG_ERROR, -1, "addSocket: socket %d already in the list", newSd);
  232. exit:
  233. FUNC_EXIT_RC(rc);
  234. return rc;
  235. }
  236. #else
  237. static int cmpfds(const void *p1, const void *p2)
  238. {
  239. SOCKET key1 = ((struct pollfd*)p1)->fd;
  240. SOCKET key2 = ((struct pollfd*)p2)->fd;
  241. return (key1 == key2) ? 0 : ((key1 < key2) ? -1 : 1);
  242. }
  243. static int cmpsockfds(const void *p1, const void *p2)
  244. {
  245. int key1 = *(int*)p1;
  246. SOCKET key2 = ((struct pollfd*)p2)->fd;
  247. return (key1 == key2) ? 0 : ((key1 < key2) ? -1 : 1);
  248. }
  249. /**
  250. * Add a socket to the list of socket to check with select
  251. * @param newSd the new socket to add
  252. */
  253. int Socket_addSocket(SOCKET newSd)
  254. {
  255. int rc = 0;
  256. FUNC_ENTRY;
  257. Paho_thread_lock_mutex(socket_mutex);
  258. mod_s.nfds++;
  259. if (mod_s.fds_read)
  260. {
  261. void* newPtr = realloc(mod_s.fds_read, mod_s.nfds * sizeof(mod_s.fds_read[0]));
  262. if (newPtr == NULL)
  263. {
  264. free(mod_s.fds_read);
  265. mod_s.fds_read = NULL;
  266. }
  267. else
  268. {
  269. mod_s.fds_read = newPtr;
  270. }
  271. }
  272. else
  273. mod_s.fds_read = malloc(mod_s.nfds * sizeof(mod_s.fds_read[0]));
  274. if (!mod_s.fds_read)
  275. {
  276. rc = PAHO_MEMORY_ERROR;
  277. goto exit;
  278. }
  279. if (mod_s.fds_write)
  280. {
  281. void* newPtr = realloc(mod_s.fds_write, mod_s.nfds * sizeof(mod_s.fds_write[0]));
  282. if (newPtr == NULL)
  283. {
  284. free(mod_s.fds_write);
  285. mod_s.fds_write = NULL;
  286. }
  287. else
  288. {
  289. mod_s.fds_write = newPtr;
  290. }
  291. }
  292. else
  293. mod_s.fds_write = malloc(mod_s.nfds * sizeof(mod_s.fds_write[0]));
  294. if (!mod_s.fds_write)
  295. {
  296. rc = PAHO_MEMORY_ERROR;
  297. goto exit;
  298. }
  299. mod_s.fds_read[mod_s.nfds - 1].fd = newSd;
  300. mod_s.fds_write[mod_s.nfds - 1].fd = newSd;
  301. #if defined(_WIN32) || defined(_WIN64)
  302. mod_s.fds_read[mod_s.nfds - 1].events = POLLIN;
  303. mod_s.fds_write[mod_s.nfds - 1].events = POLLOUT;
  304. #else
  305. mod_s.fds_read[mod_s.nfds - 1].events = POLLIN | POLLNVAL;
  306. mod_s.fds_write[mod_s.nfds - 1].events = POLLOUT;
  307. #endif
  308. /* sort the poll fds array by socket number */
  309. qsort(mod_s.fds_read, (size_t)mod_s.nfds, sizeof(mod_s.fds_read[0]), cmpfds);
  310. qsort(mod_s.fds_write, (size_t)mod_s.nfds, sizeof(mod_s.fds_write[0]), cmpfds);
  311. rc = Socket_setnonblocking(newSd);
  312. if (rc == SOCKET_ERROR)
  313. Log(LOG_ERROR, -1, "addSocket: setnonblocking");
  314. exit:
  315. Paho_thread_unlock_mutex(socket_mutex);
  316. FUNC_EXIT_RC(rc);
  317. return rc;
  318. }
  319. #endif
  320. #if defined(USE_SELECT)
  321. /**
  322. * Don't accept work from a client unless it is accepting work back, i.e. its socket is writeable
  323. * this seems like a reasonable form of flow control, and practically, seems to work.
  324. * @param socket the socket to check
  325. * @param read_set the socket read set (see select doc)
  326. * @param write_set the socket write set (see select doc)
  327. * @return boolean - is the socket ready to go?
  328. */
  329. int isReady(int socket, fd_set* read_set, fd_set* write_set)
  330. {
  331. int rc = 1;
  332. FUNC_ENTRY;
  333. if (ListFindItem(mod_s.connect_pending, &socket, intcompare) && FD_ISSET(socket, write_set))
  334. ListRemoveItem(mod_s.connect_pending, &socket, intcompare);
  335. else
  336. rc = FD_ISSET(socket, read_set) && FD_ISSET(socket, write_set) && Socket_noPendingWrites(socket);
  337. FUNC_EXIT_RC(rc);
  338. return rc;
  339. }
  340. #else
  341. /**
  342. * Don't accept work from a client unless it is accepting work back, i.e. its socket is writeable
  343. * this seems like a reasonable form of flow control, and practically, seems to work.
  344. * @param index the socket index to check
  345. * @return boolean - is the socket ready to go?
  346. */
  347. int isReady(int index)
  348. {
  349. int rc = 1;
  350. SOCKET* socket = &mod_s.saved.fds_write[index].fd;
  351. FUNC_ENTRY;
  352. if ((mod_s.saved.fds_read[index].revents & POLLHUP) || (mod_s.saved.fds_read[index].revents & POLLNVAL))
  353. ; /* signal work to be done if there is an error on the socket */
  354. else if (ListFindItem(mod_s.connect_pending, socket, intcompare) &&
  355. (mod_s.saved.fds_write[index].revents & POLLOUT))
  356. ListRemoveItem(mod_s.connect_pending, socket, intcompare);
  357. else
  358. rc = (mod_s.saved.fds_read[index].revents & POLLIN) &&
  359. (mod_s.saved.fds_write[index].revents & POLLOUT) &&
  360. Socket_noPendingWrites(*socket);
  361. FUNC_EXIT_RC(rc);
  362. return rc;
  363. }
  364. #endif
  365. #if defined(USE_SELECT)
  366. /**
  367. * Returns the next socket ready for communications as indicated by select
  368. * @param more_work flag to indicate more work is waiting, and thus a timeout value of 0 should
  369. * be used for the select
  370. * @param timeout the timeout to be used for the select, unless overridden
  371. * @param rc a value other than 0 indicates an error of the returned socket
  372. * @return the socket next ready, or 0 if none is ready
  373. */
  374. SOCKET Socket_getReadySocket(int more_work, int timeout, mutex_type mutex, int* rc)
  375. {
  376. SOCKET sock = 0;
  377. *rc = 0;
  378. int timeout_ms = 1000;
  379. FUNC_ENTRY;
  380. Paho_thread_lock_mutex(mutex);
  381. if (mod_s.clientsds->count == 0)
  382. goto exit;
  383. if (more_work)
  384. timeout_ms = 0;
  385. else if (timeout >= 0)
  386. timeout_ms = timeout;
  387. while (mod_s.cur_clientsds != NULL)
  388. {
  389. if (isReady(*((int*)(mod_s.cur_clientsds->content)), &(mod_s.rset), &wset))
  390. break;
  391. ListNextElement(mod_s.clientsds, &mod_s.cur_clientsds);
  392. }
  393. if (mod_s.cur_clientsds == NULL)
  394. {
  395. static struct timeval zero = {0L, 0L}; /* 0 seconds */
  396. int rc1, maxfdp1_saved;
  397. fd_set pwset;
  398. struct timeval timeout_tv = {0L, 0L};
  399. if (timeout_ms > 0L)
  400. {
  401. timeout_tv.tv_sec = timeout_ms / 1000;
  402. timeout_tv.tv_usec = (timeout_ms % 1000) * 1000; /* this field is microseconds! */
  403. }
  404. memcpy((void*)&(mod_s.rset), (void*)&(mod_s.rset_saved), sizeof(mod_s.rset));
  405. memcpy((void*)&(pwset), (void*)&(mod_s.pending_wset), sizeof(pwset));
  406. maxfdp1_saved = mod_s.maxfdp1;
  407. if (maxfdp1_saved == 0)
  408. {
  409. sock = 0;
  410. goto exit; /* no work to do */
  411. }
  412. /* Prevent performance issue by unlocking the socket_mutex while waiting for a ready socket. */
  413. Paho_thread_unlock_mutex(mutex);
  414. *rc = select(maxfdp1_saved, &(mod_s.rset), &pwset, NULL, &timeout_tv);
  415. Paho_thread_lock_mutex(mutex);
  416. if (*rc == SOCKET_ERROR)
  417. {
  418. Socket_error("read select", 0);
  419. goto exit;
  420. }
  421. Log(TRACE_MAX, -1, "Return code %d from read select", *rc);
  422. if (Socket_continueWrites(&pwset, &sock, mutex) == SOCKET_ERROR)
  423. {
  424. *rc = SOCKET_ERROR;
  425. goto exit;
  426. }
  427. memcpy((void*)&wset, (void*)&(mod_s.rset_saved), sizeof(wset));
  428. if ((rc1 = select(mod_s.maxfdp1, NULL, &(wset), NULL, &zero)) == SOCKET_ERROR)
  429. {
  430. Socket_error("write select", 0);
  431. *rc = rc1;
  432. goto exit;
  433. }
  434. Log(TRACE_MAX, -1, "Return code %d from write select", rc1);
  435. if (*rc == 0 && rc1 == 0)
  436. {
  437. sock = 0;
  438. goto exit; /* no work to do */
  439. }
  440. mod_s.cur_clientsds = mod_s.clientsds->first;
  441. while (mod_s.cur_clientsds != NULL)
  442. {
  443. int cursock = *((int*)(mod_s.cur_clientsds->content));
  444. if (isReady(cursock, &(mod_s.rset), &wset))
  445. break;
  446. ListNextElement(mod_s.clientsds, &mod_s.cur_clientsds);
  447. }
  448. }
  449. *rc = 0;
  450. if (mod_s.cur_clientsds == NULL)
  451. sock = 0;
  452. else
  453. {
  454. sock = *((int*)(mod_s.cur_clientsds->content));
  455. ListNextElement(mod_s.clientsds, &mod_s.cur_clientsds);
  456. }
  457. exit:
  458. Paho_thread_unlock_mutex(mutex);
  459. FUNC_EXIT_RC(sock);
  460. return sock;
  461. } /* end getReadySocket */
  462. #else
  463. /**
  464. * Returns the next socket ready for communications as indicated by select
  465. * @param more_work flag to indicate more work is waiting, and thus a timeout value of 0 should
  466. * be used for the select
  467. * @param timeout the timeout to be used in ms
  468. * @param rc a value other than 0 indicates an error of the returned socket
  469. * @return the socket next ready, or 0 if none is ready
  470. */
  471. SOCKET Socket_getReadySocket(int more_work, int timeout, mutex_type mutex, int* rc)
  472. {
  473. SOCKET sock = 0;
  474. *rc = 0;
  475. int timeout_ms = 1000;
  476. FUNC_ENTRY;
  477. Paho_thread_lock_mutex(mutex);
  478. if (mod_s.nfds == 0 && mod_s.saved.nfds == 0)
  479. goto exit;
  480. if (more_work)
  481. timeout_ms = 0;
  482. else if (timeout >= 0)
  483. timeout_ms = timeout;
  484. while (mod_s.saved.cur_fd != -1)
  485. {
  486. if (isReady(mod_s.saved.cur_fd))
  487. break;
  488. mod_s.saved.cur_fd = (mod_s.saved.cur_fd == mod_s.saved.nfds - 1) ? -1 : mod_s.saved.cur_fd + 1;
  489. }
  490. if (mod_s.saved.cur_fd == -1)
  491. {
  492. int rc1 = 0;
  493. if (mod_s.nfds != mod_s.saved.nfds)
  494. {
  495. mod_s.saved.nfds = mod_s.nfds;
  496. if (mod_s.nfds == 0)
  497. {
  498. if (mod_s.saved.fds_read)
  499. {
  500. free(mod_s.saved.fds_read);
  501. mod_s.saved.fds_read = NULL;
  502. }
  503. }
  504. else if (mod_s.saved.fds_read)
  505. {
  506. void* newPtr = realloc(mod_s.saved.fds_read, mod_s.nfds * sizeof(struct pollfd));
  507. if (newPtr == NULL)
  508. {
  509. free(mod_s.saved.fds_read);
  510. mod_s.saved.fds_read = NULL;
  511. }
  512. else
  513. {
  514. mod_s.saved.fds_read = newPtr;
  515. }
  516. }
  517. else
  518. mod_s.saved.fds_read = malloc(mod_s.nfds * sizeof(struct pollfd));
  519. if (mod_s.nfds == 0)
  520. {
  521. if (mod_s.saved.fds_write)
  522. {
  523. free(mod_s.saved.fds_write);
  524. mod_s.saved.fds_write = NULL;
  525. }
  526. }
  527. else if (mod_s.saved.fds_write)
  528. {
  529. void* newPtr = realloc(mod_s.saved.fds_write, mod_s.nfds * sizeof(struct pollfd));
  530. if (newPtr == NULL)
  531. {
  532. free(mod_s.saved.fds_write);
  533. mod_s.saved.fds_write = NULL;
  534. }
  535. else
  536. {
  537. mod_s.saved.fds_write = newPtr;
  538. }
  539. }
  540. else
  541. mod_s.saved.fds_write = malloc(mod_s.nfds * sizeof(struct pollfd));
  542. }
  543. if (mod_s.fds_read == NULL)
  544. mod_s.saved.fds_read = NULL;
  545. else
  546. memcpy(mod_s.saved.fds_read, mod_s.fds_read, mod_s.nfds * sizeof(struct pollfd));
  547. if (mod_s.fds_write == NULL)
  548. mod_s.saved.fds_write = NULL;
  549. else
  550. memcpy(mod_s.saved.fds_write, mod_s.fds_write, mod_s.nfds * sizeof(struct pollfd));
  551. if (mod_s.saved.nfds == 0)
  552. {
  553. sock = 0;
  554. goto exit; /* no work to do */
  555. }
  556. /* Check pending write set for writeable sockets */
  557. rc1 = poll(mod_s.saved.fds_write, mod_s.saved.nfds, 0);
  558. if (rc1 > 0 && Socket_continueWrites(&sock, mutex) == SOCKET_ERROR)
  559. {
  560. *rc = SOCKET_ERROR;
  561. goto exit;
  562. }
  563. /* Prevent performance issue by unlocking the socket_mutex while waiting for a ready socket. */
  564. Paho_thread_unlock_mutex(mutex);
  565. *rc = poll(mod_s.saved.fds_read, mod_s.saved.nfds, timeout_ms);
  566. Paho_thread_lock_mutex(mutex);
  567. if (*rc == SOCKET_ERROR)
  568. {
  569. Socket_error("poll", 0);
  570. goto exit;
  571. }
  572. Log(TRACE_MAX, -1, "Return code %d from poll", *rc);
  573. if (rc1 == 0 && *rc == 0)
  574. {
  575. sock = 0;
  576. goto exit; /* no work to do */
  577. }
  578. mod_s.saved.cur_fd = 0;
  579. while (mod_s.saved.cur_fd != -1)
  580. {
  581. if (isReady(mod_s.saved.cur_fd))
  582. break;
  583. mod_s.saved.cur_fd = (mod_s.saved.cur_fd == mod_s.saved.nfds - 1) ? -1 : mod_s.saved.cur_fd + 1;
  584. }
  585. }
  586. *rc = 0;
  587. if (mod_s.saved.cur_fd == -1)
  588. sock = 0;
  589. else
  590. {
  591. sock = mod_s.saved.fds_read[mod_s.saved.cur_fd].fd;
  592. mod_s.saved.cur_fd = (mod_s.saved.cur_fd == mod_s.saved.nfds - 1) ? -1 : mod_s.saved.cur_fd + 1;
  593. }
  594. exit:
  595. Paho_thread_unlock_mutex(mutex);
  596. FUNC_EXIT_RC(sock);
  597. return sock;
  598. } /* end getReadySocket */
  599. #endif
  600. /**
  601. * Reads one byte from a socket
  602. * @param socket the socket to read from
  603. * @param c the character read, returned
  604. * @return completion code
  605. */
  606. int Socket_getch(SOCKET socket, char* c)
  607. {
  608. int rc = SOCKET_ERROR;
  609. FUNC_ENTRY;
  610. if ((rc = SocketBuffer_getQueuedChar(socket, c)) != SOCKETBUFFER_INTERRUPTED)
  611. goto exit;
  612. if ((rc = recv(socket, c, (size_t)1, 0)) == SOCKET_ERROR)
  613. {
  614. int err = Socket_error("recv - getch", socket);
  615. if (err == EWOULDBLOCK || err == EAGAIN)
  616. {
  617. rc = TCPSOCKET_INTERRUPTED;
  618. SocketBuffer_interrupted(socket, 0);
  619. }
  620. }
  621. else if (rc == 0)
  622. rc = SOCKET_ERROR; /* The return value from recv is 0 when the peer has performed an orderly shutdown. */
  623. else if (rc == 1)
  624. {
  625. SocketBuffer_queueChar(socket, *c);
  626. rc = TCPSOCKET_COMPLETE;
  627. }
  628. exit:
  629. FUNC_EXIT_RC(rc);
  630. return rc;
  631. }
  632. /**
  633. * Attempts to read a number of bytes from a socket, non-blocking. If a previous read did not
  634. * finish, then retrieve that data.
  635. * @param socket the socket to read from
  636. * @param bytes the number of bytes to read
  637. * @param actual_len the actual number of bytes read
  638. * @return completion code
  639. */
  640. char *Socket_getdata(SOCKET socket, size_t bytes, size_t* actual_len, int *rc)
  641. {
  642. char* buf;
  643. FUNC_ENTRY;
  644. if (bytes == 0)
  645. {
  646. buf = SocketBuffer_complete(socket);
  647. goto exit;
  648. }
  649. buf = SocketBuffer_getQueuedData(socket, bytes, actual_len);
  650. if ((*rc = recv(socket, buf + (*actual_len), (int)(bytes - (*actual_len)), 0)) == SOCKET_ERROR)
  651. {
  652. *rc = Socket_error("recv - getdata", socket);
  653. if (*rc != EAGAIN && *rc != EWOULDBLOCK)
  654. {
  655. buf = NULL;
  656. goto exit;
  657. }
  658. }
  659. else if (*rc == 0) /* rc 0 means the other end closed the socket, albeit "gracefully" */
  660. {
  661. buf = NULL;
  662. goto exit;
  663. }
  664. else
  665. *actual_len += *rc;
  666. if (*actual_len == bytes)
  667. SocketBuffer_complete(socket);
  668. else /* we didn't read the whole packet */
  669. {
  670. SocketBuffer_interrupted(socket, *actual_len);
  671. Log(TRACE_MAX, -1, "%d bytes expected but %d bytes now received", (int)bytes, (int)*actual_len);
  672. }
  673. exit:
  674. FUNC_EXIT;
  675. return buf;
  676. }
  677. /**
  678. * Indicate whether any data is pending outbound for a socket.
  679. * @return boolean - true == no pending data.
  680. */
  681. int Socket_noPendingWrites(SOCKET socket)
  682. {
  683. SOCKET cursock = socket;
  684. return ListFindItem(mod_s.write_pending, &cursock, intcompare) == NULL;
  685. }
  686. /**
  687. * Attempts to write a series of iovec buffers to a socket in *one* system call so that
  688. * they are sent as one packet.
  689. * @param socket the socket to write to
  690. * @param iovecs an array of buffers to write
  691. * @param count number of buffers in iovecs
  692. * @param bytes number of bytes actually written returned
  693. * @return completion code, especially TCPSOCKET_INTERRUPTED
  694. */
  695. int Socket_writev(SOCKET socket, iobuf* iovecs, int count, unsigned long* bytes)
  696. {
  697. int rc;
  698. FUNC_ENTRY;
  699. *bytes = 0L;
  700. #if defined(_WIN32) || defined(_WIN64)
  701. rc = WSASend(socket, iovecs, count, (LPDWORD)bytes, 0, NULL, NULL);
  702. if (rc == SOCKET_ERROR)
  703. {
  704. int err = Socket_error("WSASend - putdatas", socket);
  705. if (err == EWOULDBLOCK || err == EAGAIN)
  706. rc = TCPSOCKET_INTERRUPTED;
  707. }
  708. #else
  709. /*#define TCPSOCKET_INTERRUPTED_TESTING
  710. This section forces the occasional return of TCPSOCKET_INTERRUPTED,
  711. for testing purposes only!
  712. */
  713. #if defined(TCPSOCKET_INTERRUPTED_TESTING)
  714. static int i = 0;
  715. if (++i >= 10 && i < 21)
  716. {
  717. if (1)
  718. {
  719. printf("Deliberately simulating TCPSOCKET_INTERRUPTED\n");
  720. rc = TCPSOCKET_INTERRUPTED; /* simulate a network wait */
  721. }
  722. else
  723. {
  724. printf("Deliberately simulating SOCKET_ERROR\n");
  725. rc = SOCKET_ERROR;
  726. }
  727. /* should *bytes always be 0? */
  728. if (i == 20)
  729. {
  730. printf("Shutdown socket\n");
  731. shutdown(socket, SHUT_WR);
  732. }
  733. }
  734. else
  735. {
  736. #endif
  737. rc = writev(socket, iovecs, count);
  738. if (rc == SOCKET_ERROR)
  739. {
  740. int err = Socket_error("writev - putdatas", socket);
  741. if (err == EWOULDBLOCK || err == EAGAIN)
  742. rc = TCPSOCKET_INTERRUPTED;
  743. }
  744. else
  745. *bytes = rc;
  746. #if defined(TCPSOCKET_INTERRUPTED_TESTING)
  747. }
  748. #endif
  749. #endif
  750. FUNC_EXIT_RC(rc);
  751. return rc;
  752. }
  753. /**
  754. * Attempts to write a series of buffers to a socket in *one* system call so that they are
  755. * sent as one packet.
  756. * @param socket the socket to write to
  757. * @param buf0 the first buffer
  758. * @param buf0len the length of data in the first buffer
  759. * @param count number of buffers
  760. * @param buffers an array of buffers to write
  761. * @param buflens an array of corresponding buffer lengths
  762. * @return completion code, especially TCPSOCKET_INTERRUPTED
  763. */
  764. int Socket_putdatas(SOCKET socket, char* buf0, size_t buf0len, PacketBuffers bufs)
  765. {
  766. unsigned long bytes = 0L;
  767. iobuf iovecs[5];
  768. int frees1[5];
  769. int rc = TCPSOCKET_INTERRUPTED, i;
  770. size_t total = buf0len;
  771. FUNC_ENTRY;
  772. if (!Socket_noPendingWrites(socket))
  773. {
  774. Log(LOG_SEVERE, -1, "Trying to write to socket %d for which there is already pending output", socket);
  775. rc = SOCKET_ERROR;
  776. goto exit;
  777. }
  778. for (i = 0; i < bufs.count; i++)
  779. total += bufs.buflens[i];
  780. iovecs[0].iov_base = buf0;
  781. iovecs[0].iov_len = (ULONG)buf0len;
  782. frees1[0] = 1; /* this buffer should be freed by SocketBuffer if the write is interrupted */
  783. for (i = 0; i < bufs.count; i++)
  784. {
  785. iovecs[i+1].iov_base = bufs.buffers[i];
  786. iovecs[i+1].iov_len = (ULONG)bufs.buflens[i];
  787. frees1[i+1] = bufs.frees[i];
  788. }
  789. if ((rc = Socket_writev(socket, iovecs, bufs.count+1, &bytes)) != SOCKET_ERROR)
  790. {
  791. if (bytes == total)
  792. rc = TCPSOCKET_COMPLETE;
  793. else
  794. {
  795. SOCKET* sockmem = (SOCKET*)malloc(sizeof(SOCKET));
  796. if (!sockmem)
  797. {
  798. rc = PAHO_MEMORY_ERROR;
  799. goto exit;
  800. }
  801. Log(TRACE_MIN, -1, "Partial write: %lu bytes of %lu actually written on socket %d",
  802. bytes, total, socket);
  803. #if defined(OPENSSL)
  804. SocketBuffer_pendingWrite(socket, NULL, bufs.count+1, iovecs, frees1, total, bytes);
  805. #else
  806. SocketBuffer_pendingWrite(socket, bufs.count+1, iovecs, frees1, total, bytes);
  807. #endif
  808. *sockmem = socket;
  809. if (!ListAppend(mod_s.write_pending, sockmem, sizeof(int)))
  810. {
  811. free(sockmem);
  812. rc = PAHO_MEMORY_ERROR;
  813. goto exit;
  814. }
  815. #if defined(USE_SELECT)
  816. FD_SET(socket, &(mod_s.pending_wset));
  817. #endif
  818. rc = TCPSOCKET_INTERRUPTED;
  819. }
  820. }
  821. exit:
  822. FUNC_EXIT_RC(rc);
  823. return rc;
  824. }
  825. /**
  826. * Add a socket to the pending write list, so that it is checked for writing in select. This is used
  827. * in connect processing when the TCP connect is incomplete, as we need to check the socket for both
  828. * ready to read and write states.
  829. * @param socket the socket to add
  830. */
  831. void Socket_addPendingWrite(SOCKET socket)
  832. {
  833. #if defined(USE_SELECT)
  834. FD_SET(socket, &(mod_s.pending_wset));
  835. #endif
  836. }
  837. /**
  838. * Clear a socket from the pending write list - if one was added with Socket_addPendingWrite
  839. * @param socket the socket to remove
  840. */
  841. void Socket_clearPendingWrite(SOCKET socket)
  842. {
  843. #if defined(USE_SELECT)
  844. if (FD_ISSET(socket, &(mod_s.pending_wset)))
  845. FD_CLR(socket, &(mod_s.pending_wset));
  846. #endif
  847. }
  848. /**
  849. * Close a socket without removing it from the select list.
  850. * @param socket the socket to close
  851. * @return completion code
  852. */
  853. int Socket_close_only(SOCKET socket)
  854. {
  855. int rc;
  856. FUNC_ENTRY;
  857. #if defined(_WIN32) || defined(_WIN64)
  858. if (shutdown(socket, SD_BOTH) == SOCKET_ERROR)
  859. Socket_error("shutdown", socket);
  860. if ((rc = closesocket(socket)) == SOCKET_ERROR)
  861. Socket_error("close", socket);
  862. #else
  863. if (shutdown(socket, SHUT_WR) == SOCKET_ERROR)
  864. Socket_error("shutdown", socket);
  865. if ((rc = recv(socket, NULL, (size_t)0, 0)) == SOCKET_ERROR)
  866. Socket_error("shutdown", socket);
  867. if ((rc = close(socket)) == SOCKET_ERROR)
  868. Socket_error("close", socket);
  869. #endif
  870. FUNC_EXIT_RC(rc);
  871. return rc;
  872. }
  873. #if defined(USE_SELECT)
  874. /**
  875. * Close a socket and remove it from the select list.
  876. * @param socket the socket to close
  877. * @return completion code
  878. */
  879. int Socket_close(SOCKET socket)
  880. {
  881. int rc = 0;
  882. FUNC_ENTRY;
  883. Socket_close_only(socket);
  884. FD_CLR(socket, &(mod_s.rset_saved));
  885. if (FD_ISSET(socket, &(mod_s.pending_wset)))
  886. FD_CLR(socket, &(mod_s.pending_wset));
  887. if (mod_s.cur_clientsds != NULL && *(int*)(mod_s.cur_clientsds->content) == socket)
  888. mod_s.cur_clientsds = mod_s.cur_clientsds->next;
  889. Socket_abortWrite(socket);
  890. SocketBuffer_cleanup(socket);
  891. ListRemoveItem(mod_s.connect_pending, &socket, intcompare);
  892. ListRemoveItem(mod_s.write_pending, &socket, intcompare);
  893. if (ListRemoveItem(mod_s.clientsds, &socket, intcompare))
  894. Log(TRACE_MIN, -1, "Removed socket %d", socket);
  895. else
  896. {
  897. Log(LOG_ERROR, -1, "Failed to remove socket %d", socket);
  898. rc = SOCKET_ERROR;
  899. goto exit;
  900. }
  901. if (socket + 1 >= mod_s.maxfdp1)
  902. {
  903. /* now we have to reset mod_s.maxfdp1 */
  904. ListElement* cur_clientsds = NULL;
  905. mod_s.maxfdp1 = 0;
  906. while (ListNextElement(mod_s.clientsds, &cur_clientsds))
  907. mod_s.maxfdp1 = max(*((int*)(cur_clientsds->content)), mod_s.maxfdp1);
  908. ++(mod_s.maxfdp1);
  909. Log(TRACE_MAX, -1, "Reset max fdp1 to %d", mod_s.maxfdp1);
  910. }
  911. exit:
  912. FUNC_EXIT_RC(rc);
  913. return rc;
  914. }
  915. #else
  916. /**
  917. * Close a socket and remove it from the select list.
  918. * @param socket the socket to close
  919. * @return completion code
  920. */
  921. int Socket_close(SOCKET socket)
  922. {
  923. struct pollfd* fd;
  924. int rc = 0;
  925. FUNC_ENTRY;
  926. Paho_thread_lock_mutex(socket_mutex);
  927. Socket_close_only(socket);
  928. Socket_abortWrite(socket);
  929. SocketBuffer_cleanup(socket);
  930. ListRemoveItem(mod_s.connect_pending, &socket, intcompare);
  931. ListRemoveItem(mod_s.write_pending, &socket, intcompare);
  932. if (mod_s.nfds == 0)
  933. goto exit;
  934. fd = bsearch(&socket, mod_s.fds_read, (size_t)mod_s.nfds, sizeof(mod_s.fds_read[0]), cmpsockfds);
  935. if (fd)
  936. {
  937. struct pollfd* last_fd = &mod_s.fds_read[mod_s.nfds - 1];
  938. if (--mod_s.nfds == 0)
  939. {
  940. free(mod_s.fds_read);
  941. mod_s.fds_read = NULL;
  942. }
  943. else
  944. {
  945. if (fd != last_fd)
  946. {
  947. /* shift array to remove the socket in question */
  948. memmove(fd, fd + 1, (mod_s.nfds - (fd - mod_s.fds_read)) * sizeof(mod_s.fds_read[0]));
  949. }
  950. void* newPtr = realloc(mod_s.fds_read, sizeof(mod_s.fds_read[0]) * mod_s.nfds);
  951. if (newPtr == NULL)
  952. {
  953. free(mod_s.fds_read);
  954. mod_s.fds_read = NULL;
  955. rc = PAHO_MEMORY_ERROR;
  956. goto exit;
  957. }
  958. else
  959. {
  960. mod_s.fds_read = newPtr;
  961. }
  962. }
  963. Log(TRACE_MIN, -1, "Removed socket %d", socket);
  964. }
  965. else
  966. Log(LOG_ERROR, -1, "Failed to remove socket %d", socket);
  967. fd = bsearch(&socket, mod_s.fds_write, (size_t)(mod_s.nfds+1), sizeof(mod_s.fds_write[0]), cmpsockfds);
  968. if (fd)
  969. {
  970. struct pollfd* last_fd = &mod_s.fds_write[mod_s.nfds];
  971. if (mod_s.nfds == 0)
  972. {
  973. free(mod_s.fds_write);
  974. mod_s.fds_write = NULL;
  975. }
  976. else
  977. {
  978. if (fd != last_fd)
  979. {
  980. /* shift array to remove the socket in question */
  981. memmove(fd, fd + 1, (mod_s.nfds - (fd - mod_s.fds_write)) * sizeof(mod_s.fds_write[0]));
  982. }
  983. void* newPtr = realloc(mod_s.fds_write, sizeof(mod_s.fds_write[0]) * mod_s.nfds);
  984. if (newPtr == NULL)
  985. {
  986. free(mod_s.fds_write);
  987. mod_s.fds_write = NULL;
  988. rc = PAHO_MEMORY_ERROR;
  989. goto exit;
  990. }
  991. else
  992. {
  993. mod_s.fds_write = newPtr;
  994. }
  995. }
  996. Log(TRACE_MIN, -1, "Removed socket %d", socket);
  997. }
  998. else
  999. Log(LOG_ERROR, -1, "Failed to remove socket %d", socket);
  1000. exit:
  1001. Paho_thread_unlock_mutex(socket_mutex);
  1002. FUNC_EXIT_RC(rc);
  1003. return rc;
  1004. }
  1005. #endif
  1006. /**
  1007. * Create a new socket and TCP connect to an address/port
  1008. * @param addr the address string
  1009. * @param assr_len the length of the address string
  1010. * @param port the TCP port
  1011. * @param sock returns the new socket
  1012. * @param timeout the timeout in milliseconds
  1013. * @return completion code 0=good, SOCKET_ERROR=fail
  1014. */
  1015. #if defined(__GNUC__) && defined(__linux__)
  1016. int Socket_new(const char* addr, size_t addr_len, int port, SOCKET* sock, long timeout)
  1017. #else
  1018. int Socket_new(const char* addr, size_t addr_len, int port, SOCKET* sock)
  1019. #endif
  1020. {
  1021. int type = SOCK_STREAM;
  1022. char *addr_mem;
  1023. struct sockaddr_in address;
  1024. #if defined(AF_INET6)
  1025. struct sockaddr_in6 address6;
  1026. #endif
  1027. int rc = SOCKET_ERROR;
  1028. #if defined(_WIN32) || defined(_WIN64)
  1029. short family = AF_INET;
  1030. #else
  1031. sa_family_t family = AF_INET;
  1032. #endif
  1033. struct addrinfo *result = NULL;
  1034. struct addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL, NULL};
  1035. FUNC_ENTRY;
  1036. *sock = SOCKET_ERROR;
  1037. memset(&address6, '\0', sizeof(address6));
  1038. if (addr[0] == '[')
  1039. {
  1040. ++addr;
  1041. --addr_len;
  1042. }
  1043. if ((addr_mem = malloc( addr_len + 1u )) == NULL)
  1044. {
  1045. rc = PAHO_MEMORY_ERROR;
  1046. goto exit;
  1047. }
  1048. memcpy( addr_mem, addr, addr_len );
  1049. addr_mem[addr_len] = '\0';
  1050. #if 0 /*defined(__GNUC__) && defined(__linux__)*/
  1051. /* Commented out because the CI tests get intermittent ECONNABORTED return values
  1052. * and I don't know why yet.
  1053. */
  1054. /* set getaddrinfo timeout if available */
  1055. struct gaicb ar = {addr_mem, NULL, &hints, NULL};
  1056. struct gaicb *reqs[] = {&ar};
  1057. unsigned long int seconds = timeout / 1000L;
  1058. unsigned long int nanos = (timeout - (seconds * 1000L)) * 1000000L;
  1059. struct timespec timeoutspec = {seconds, nanos};
  1060. rc = getaddrinfo_a(GAI_NOWAIT, reqs, 1, NULL);
  1061. if (rc == 0)
  1062. rc = gai_suspend((const struct gaicb* const *) reqs, 1, &timeoutspec);
  1063. if (rc == 0)
  1064. {
  1065. rc = gai_error(reqs[0]);
  1066. result = ar.ar_result;
  1067. }
  1068. #else
  1069. rc = getaddrinfo(addr_mem, NULL, &hints, &result);
  1070. #endif
  1071. if (rc == 0)
  1072. {
  1073. struct addrinfo* res = result;
  1074. while (res)
  1075. { /* prefer ip4 addresses */
  1076. if (res->ai_family == AF_INET || res->ai_next == NULL)
  1077. break;
  1078. res = res->ai_next;
  1079. }
  1080. if (res == NULL)
  1081. rc = SOCKET_ERROR;
  1082. else
  1083. #if defined(AF_INET6)
  1084. if (res->ai_family == AF_INET6)
  1085. {
  1086. address6.sin6_port = htons(port);
  1087. address6.sin6_family = family = AF_INET6;
  1088. struct sockaddr_in6* res6 = (struct sockaddr_in6*)(res->ai_addr);
  1089. memcpy(&address6.sin6_addr, &res6->sin6_addr, sizeof(address6.sin6_addr));
  1090. memcpy(&address6.sin6_scope_id, &res6->sin6_scope_id, sizeof(address6.sin6_scope_id));
  1091. memcpy(&address6.sin6_flowinfo, &res6->sin6_flowinfo, sizeof(address6.sin6_flowinfo));
  1092. }
  1093. else
  1094. #endif
  1095. if (res->ai_family == AF_INET)
  1096. {
  1097. memset(&address.sin_zero, 0, sizeof(address.sin_zero));
  1098. address.sin_port = htons(port);
  1099. address.sin_family = family = AF_INET;
  1100. address.sin_addr = ((struct sockaddr_in*)(res->ai_addr))->sin_addr;
  1101. }
  1102. else
  1103. rc = SOCKET_ERROR;
  1104. freeaddrinfo(result);
  1105. }
  1106. else
  1107. {
  1108. Log(LOG_ERROR, -1, "getaddrinfo failed for addr %s with rc %d", addr_mem, rc);
  1109. rc = SOCKET_ERROR;
  1110. }
  1111. if (rc != 0)
  1112. Log(LOG_ERROR, -1, "%s is not a valid IP address", addr_mem);
  1113. else
  1114. {
  1115. *sock = socket(family, type, 0);
  1116. if (*sock == INVALID_SOCKET)
  1117. rc = Socket_error("socket", *sock);
  1118. else
  1119. {
  1120. #if defined(NOSIGPIPE)
  1121. int opt = 1;
  1122. if (setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt)) != 0)
  1123. Log(LOG_ERROR, -1, "Could not set SO_NOSIGPIPE for socket %d", *sock);
  1124. #endif
  1125. /*#define SMALL_TCP_BUFFER_TESTING
  1126. This section sets the TCP send buffer to a small amount to provoke TCPSOCKET_INTERRUPTED
  1127. return codes from send, for testing only!
  1128. */
  1129. #if defined(SMALL_TCP_BUFFER_TESTING)
  1130. if (1)
  1131. {
  1132. int optsend = 100; //2 * 1440;
  1133. printf("Setting optsend to %d\n", optsend);
  1134. if (setsockopt(*sock, SOL_SOCKET, SO_SNDBUF, (void*)&optsend, sizeof(optsend)) != 0)
  1135. Log(LOG_ERROR, -1, "Could not set SO_SNDBUF for socket %d", *sock);
  1136. }
  1137. #endif
  1138. Log(TRACE_MIN, -1, "New socket %d for %s, port %d", *sock, addr, port);
  1139. if (Socket_addSocket(*sock) == SOCKET_ERROR)
  1140. rc = Socket_error("addSocket", *sock);
  1141. else
  1142. {
  1143. /* this could complete immediately, even though we are non-blocking */
  1144. if (family == AF_INET)
  1145. rc = connect(*sock, (struct sockaddr*)&address, sizeof(address));
  1146. #if defined(AF_INET6)
  1147. else
  1148. rc = connect(*sock, (struct sockaddr*)&address6, sizeof(address6));
  1149. #endif
  1150. if (rc == SOCKET_ERROR)
  1151. rc = Socket_error("connect", *sock);
  1152. if (rc == EINPROGRESS || rc == EWOULDBLOCK)
  1153. {
  1154. SOCKET* pnewSd = (SOCKET*)malloc(sizeof(SOCKET));
  1155. ListElement* listResult = NULL;
  1156. if (!pnewSd)
  1157. {
  1158. rc = PAHO_MEMORY_ERROR;
  1159. goto exit;
  1160. }
  1161. *pnewSd = *sock;
  1162. Paho_thread_lock_mutex(socket_mutex);
  1163. listResult = ListAppend(mod_s.connect_pending, pnewSd, sizeof(SOCKET));
  1164. Paho_thread_unlock_mutex(socket_mutex);
  1165. if (!listResult)
  1166. {
  1167. free(pnewSd);
  1168. rc = PAHO_MEMORY_ERROR;
  1169. goto exit;
  1170. }
  1171. Log(TRACE_MIN, 15, "Connect pending");
  1172. }
  1173. }
  1174. /* Prevent socket leak by closing unusable sockets,
  1175. as reported in https://github.com/eclipse/paho.mqtt.c/issues/135 */
  1176. if (rc != 0 && (rc != EINPROGRESS) && (rc != EWOULDBLOCK))
  1177. {
  1178. Socket_close(*sock); /* close socket and remove from our list of sockets */
  1179. *sock = SOCKET_ERROR; /* as initialized before */
  1180. }
  1181. }
  1182. }
  1183. exit:
  1184. if (addr_mem)
  1185. free(addr_mem);
  1186. FUNC_EXIT_RC(rc);
  1187. return rc;
  1188. }
  1189. #if defined(UNIXSOCK)
  1190. /**
  1191. * Create a new socket and TCP connect to an address/port
  1192. * @param addr the address string, which is a file path
  1193. * @param assr_len the length of the address string
  1194. * @param sock returns the new socket
  1195. * @return completion code 0=good, SOCKET_ERROR=fail
  1196. */
  1197. int Socket_unix_new(const char* addr, size_t addr_len, SOCKET* sock)
  1198. {
  1199. struct sockaddr_un address;
  1200. int rc = SOCKET_ERROR;
  1201. FUNC_ENTRY;
  1202. if (addr_len >= sizeof(address.sun_path)) {
  1203. rc = PAHO_MEMORY_ERROR;
  1204. }
  1205. else {
  1206. address.sun_family = AF_UNIX;
  1207. memcpy(&address.sun_path, addr, addr_len);
  1208. address.sun_path[addr_len] = '\0';
  1209. *sock = socket(AF_UNIX, SOCK_STREAM, 0);
  1210. if (*sock == INVALID_SOCKET)
  1211. rc = Socket_error("socket", *sock);
  1212. else
  1213. {
  1214. #if defined(NOSIGPIPE)
  1215. int opt = 1;
  1216. if (setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt)) != 0)
  1217. Log(LOG_ERROR, -1, "Could not set SO_NOSIGPIPE for socket %d", *sock);
  1218. #endif
  1219. Log(TRACE_MIN, -1, "New UNIX socket %d for %s", *sock, addr);
  1220. if (Socket_addSocket(*sock) == SOCKET_ERROR)
  1221. rc = Socket_error("addSocket", *sock);
  1222. else
  1223. {
  1224. /* this will complete immediately, even though we are non-blocking */
  1225. rc = connect(*sock, (struct sockaddr*)&address, sizeof(address));
  1226. if (rc == SOCKET_ERROR)
  1227. rc = Socket_error("connect", *sock);
  1228. }
  1229. }
  1230. }
  1231. exit:
  1232. FUNC_EXIT_RC(rc);
  1233. return rc;
  1234. }
  1235. #endif
  1236. static Socket_writeContinue* writecontinue = NULL;
  1237. void Socket_setWriteContinueCallback(Socket_writeContinue* mywritecontinue)
  1238. {
  1239. writecontinue = mywritecontinue;
  1240. }
  1241. static Socket_writeComplete* writecomplete = NULL;
  1242. void Socket_setWriteCompleteCallback(Socket_writeComplete* mywritecomplete)
  1243. {
  1244. writecomplete = mywritecomplete;
  1245. }
  1246. static Socket_writeAvailable* writeAvailable = NULL;
  1247. void Socket_setWriteAvailableCallback(Socket_writeAvailable* mywriteavailable)
  1248. {
  1249. writeAvailable = mywriteavailable;
  1250. }
  1251. /**
  1252. * Continue an outstanding write for a particular socket
  1253. * @param socket that socket
  1254. * @return completion code: 0=incomplete, 1=complete, -1=socket error
  1255. */
  1256. int Socket_continueWrite(SOCKET socket)
  1257. {
  1258. int rc = 0;
  1259. pending_writes* pw;
  1260. unsigned long curbuflen = 0L, /* cumulative total of buffer lengths */
  1261. bytes = 0L;
  1262. int curbuf = -1, i;
  1263. iobuf iovecs1[5];
  1264. FUNC_ENTRY;
  1265. pw = SocketBuffer_getWrite(socket);
  1266. #if defined(OPENSSL)
  1267. if (pw->ssl)
  1268. {
  1269. rc = SSLSocket_continueWrite(pw);
  1270. goto exit;
  1271. }
  1272. #endif
  1273. for (i = 0; i < pw->count; ++i)
  1274. {
  1275. if (pw->bytes <= curbuflen)
  1276. { /* if previously written length is less than the buffer we are currently looking at,
  1277. add the whole buffer */
  1278. iovecs1[++curbuf].iov_len = pw->iovecs[i].iov_len;
  1279. iovecs1[curbuf].iov_base = pw->iovecs[i].iov_base;
  1280. }
  1281. else if (pw->bytes < curbuflen + pw->iovecs[i].iov_len)
  1282. { /* if previously written length is in the middle of the buffer we are currently looking at,
  1283. add some of the buffer */
  1284. size_t offset = pw->bytes - curbuflen;
  1285. iovecs1[++curbuf].iov_len = pw->iovecs[i].iov_len - (ULONG)offset;
  1286. iovecs1[curbuf].iov_base = (char*)pw->iovecs[i].iov_base + offset;
  1287. }
  1288. curbuflen += pw->iovecs[i].iov_len;
  1289. }
  1290. if ((rc = Socket_writev(socket, iovecs1, curbuf+1, &bytes)) != SOCKET_ERROR)
  1291. {
  1292. pw->bytes += bytes;
  1293. if ((rc = (pw->bytes == pw->total)))
  1294. { /* topic and payload buffers are freed elsewhere, when all references to them have been removed */
  1295. for (i = 0; i < pw->count; i++)
  1296. {
  1297. if (pw->frees[i])
  1298. {
  1299. free(pw->iovecs[i].iov_base);
  1300. pw->iovecs[i].iov_base = NULL;
  1301. }
  1302. }
  1303. rc = 1; /* signal complete */
  1304. Log(TRACE_MIN, -1, "ContinueWrite: partial write now complete for socket %d", socket);
  1305. }
  1306. else
  1307. {
  1308. rc = 0; /* signal not complete */
  1309. Log(TRACE_MIN, -1, "ContinueWrite wrote +%lu bytes on socket %d", bytes, socket);
  1310. }
  1311. }
  1312. else /* if we got SOCKET_ERROR we need to clean up anyway - a partial write is no good anymore */
  1313. {
  1314. for (i = 0; i < pw->count; i++)
  1315. {
  1316. if (pw->frees[i])
  1317. {
  1318. free(pw->iovecs[i].iov_base);
  1319. pw->iovecs[i].iov_base = NULL;
  1320. }
  1321. }
  1322. }
  1323. #if defined(OPENSSL)
  1324. exit:
  1325. #endif
  1326. FUNC_EXIT_RC(rc);
  1327. return rc;
  1328. }
  1329. /**
  1330. * Continue an outstanding write for a particular socket
  1331. * @param socket that socket
  1332. * @return completion code: 0=incomplete, 1=complete, -1=socket error
  1333. */
  1334. int Socket_abortWrite(SOCKET socket)
  1335. {
  1336. int i = -1, rc = 0;
  1337. pending_writes* pw;
  1338. FUNC_ENTRY;
  1339. if ((pw = SocketBuffer_getWrite(socket)) == NULL)
  1340. goto exit;
  1341. #if defined(OPENSSL)
  1342. if (pw->ssl)
  1343. {
  1344. rc = SSLSocket_abortWrite(pw);
  1345. goto exit;
  1346. }
  1347. #endif
  1348. for (i = 0; i < pw->count; i++)
  1349. {
  1350. if (pw->frees[i])
  1351. {
  1352. Log(TRACE_MIN, -1, "Cleaning in abortWrite for socket %d", socket);
  1353. free(pw->iovecs[i].iov_base);
  1354. }
  1355. }
  1356. exit:
  1357. FUNC_EXIT_RC(rc);
  1358. return rc;
  1359. }
  1360. #if defined(USE_SELECT)
  1361. /**
  1362. * Continue any outstanding writes for a socket set
  1363. * @param pwset the set of sockets
  1364. * @param sock in case of a socket error contains the affected socket
  1365. * @return completion code, 0 or SOCKET_ERROR
  1366. */
  1367. int Socket_continueWrites(fd_set* pwset, SOCKET* sock, mutex_type mutex)
  1368. #else
  1369. /**
  1370. * Continue any outstanding socket writes
  1371. * @param sock in case of a socket error contains the affected socket
  1372. * @return completion code, 0 or SOCKET_ERROR
  1373. */
  1374. int Socket_continueWrites(SOCKET* sock, mutex_type mutex)
  1375. #endif
  1376. {
  1377. int rc1 = 0;
  1378. ListElement* curpending = mod_s.write_pending->first;
  1379. FUNC_ENTRY;
  1380. while (curpending && curpending->content)
  1381. {
  1382. int socket = *(int*)(curpending->content);
  1383. int rc = 0;
  1384. #if defined(USE_SELECT)
  1385. if (FD_ISSET(socket, pwset) && ((rc = Socket_continueWrite(socket)) != 0))
  1386. #else
  1387. struct pollfd* fd;
  1388. /* find the socket in the fds structure */
  1389. fd = bsearch(&socket, mod_s.saved.fds_write, (size_t)mod_s.saved.nfds, sizeof(mod_s.saved.fds_write[0]), cmpsockfds);
  1390. if ((fd->revents & POLLOUT) && ((rc = Socket_continueWrite(socket)) != 0))
  1391. #endif
  1392. {
  1393. if (!SocketBuffer_writeComplete(socket))
  1394. Log(LOG_SEVERE, -1, "Failed to remove pending write from socket buffer list");
  1395. #if defined(USE_SELECT)
  1396. FD_CLR(socket, &(mod_s.pending_wset));
  1397. #endif
  1398. if (!ListRemove(mod_s.write_pending, curpending->content))
  1399. {
  1400. Log(LOG_SEVERE, -1, "Failed to remove pending write from list");
  1401. ListNextElement(mod_s.write_pending, &curpending);
  1402. }
  1403. curpending = mod_s.write_pending->current;
  1404. if (writeAvailable && rc > 0)
  1405. (*writeAvailable)(socket);
  1406. if (writecomplete)
  1407. {
  1408. Paho_thread_unlock_mutex(mutex);
  1409. (*writecomplete)(socket, rc);
  1410. Paho_thread_lock_mutex(mutex);
  1411. }
  1412. }
  1413. else
  1414. ListNextElement(mod_s.write_pending, &curpending);
  1415. if (writecontinue && rc == 0)
  1416. (*writecontinue)(socket);
  1417. if (rc == SOCKET_ERROR)
  1418. {
  1419. *sock = socket;
  1420. rc1 = SOCKET_ERROR;
  1421. }
  1422. }
  1423. FUNC_EXIT_RC(rc1);
  1424. return rc1;
  1425. }
  1426. /**
  1427. * Convert a numeric address to character string
  1428. * @param sa socket numerical address
  1429. * @param sock socket
  1430. * @return the peer information
  1431. */
  1432. char* Socket_getaddrname(struct sockaddr* sa, SOCKET sock)
  1433. {
  1434. /**
  1435. * maximum length of the address string
  1436. */
  1437. #define ADDRLEN INET6_ADDRSTRLEN+1
  1438. /**
  1439. * maximum length of the port string
  1440. */
  1441. #define PORTLEN 10
  1442. static char addr_string[ADDRLEN + PORTLEN];
  1443. #if defined(_WIN32) || defined(_WIN64)
  1444. int buflen = ADDRLEN*2;
  1445. wchar_t buf[ADDRLEN*2];
  1446. if (WSAAddressToStringW(sa, sizeof(struct sockaddr_in6), NULL, buf, (LPDWORD)&buflen) == SOCKET_ERROR)
  1447. Socket_error("WSAAddressToString", sock);
  1448. else
  1449. wcstombs(addr_string, buf, sizeof(addr_string));
  1450. /* TODO: append the port information - format: [00:00:00::]:port */
  1451. /* strcpy(&addr_string[strlen(addr_string)], "what?"); */
  1452. #else
  1453. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  1454. size_t buflen = sizeof(addr_string) - strlen(addr_string);
  1455. inet_ntop(sin->sin_family, &sin->sin_addr, addr_string, ADDRLEN);
  1456. if (snprintf(&addr_string[strlen(addr_string)], buflen, ":%d", ntohs(sin->sin_port)) >= buflen)
  1457. addr_string[sizeof(addr_string)-1] = '\0'; /* just in case of snprintf buffer filling */
  1458. #endif
  1459. return addr_string;
  1460. }
  1461. /**
  1462. * Get information about the other end connected to a socket
  1463. * @param sock the socket to inquire on
  1464. * @return the peer information
  1465. */
  1466. char* Socket_getpeer(SOCKET sock)
  1467. {
  1468. struct sockaddr_in6 sa;
  1469. socklen_t sal = sizeof(sa);
  1470. if (getpeername(sock, (struct sockaddr*)&sa, &sal) == SOCKET_ERROR)
  1471. {
  1472. Socket_error("getpeername", sock);
  1473. return "unknown";
  1474. }
  1475. return Socket_getaddrname((struct sockaddr*)&sa, sock);
  1476. }
  1477. #if defined(Socket_TEST)
  1478. int main(int argc, char *argv[])
  1479. {
  1480. Socket_connect("127.0.0.1", 1883);
  1481. Socket_connect("localhost", 1883);
  1482. Socket_connect("loadsadsacalhost", 1883);
  1483. }
  1484. #endif