PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/libr/socket/socket.c

https://gitlab.com/unlogic/radare-mirror
C | 775 lines | 709 code | 41 blank | 25 comment | 160 complexity | d140b26955acac86c3491b7a858f4c75 MD5 | raw file
  1. /* radare - LGPL - Copyright 2006-2015 - pancake */
  2. #include <errno.h>
  3. #include <r_types.h>
  4. #include <r_util.h>
  5. #include <r_socket.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <signal.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #if EMSCRIPTEN
  15. #define NETWORK_DISABLED 1
  16. #else
  17. #define NETWORK_DISABLED 0
  18. #endif
  19. R_LIB_VERSION(r_socket);
  20. #if NETWORK_DISABLED
  21. /* no network */
  22. R_API RSocket *r_socket_new (int is_ssl) {
  23. return NULL;
  24. }
  25. R_API bool r_socket_is_connected (RSocket *s) {
  26. return false;
  27. }
  28. static int r_socket_unix_connect(RSocket *s, const char *file) {
  29. return -1;
  30. }
  31. R_API int r_socket_unix_listen (RSocket *s, const char *file) {
  32. return -1;
  33. }
  34. R_API bool r_socket_connect (RSocket *s, const char *host, const char *port, int proto, unsigned int timeout) {
  35. return false;
  36. }
  37. R_API int r_socket_close_fd (RSocket *s) {
  38. return -1;
  39. }
  40. R_API int r_socket_close (RSocket *s) {
  41. return -1;
  42. }
  43. R_API int r_socket_free (RSocket *s) {
  44. return -1;
  45. }
  46. R_API int r_socket_port_by_name(const char *name) {
  47. return -1;
  48. }
  49. R_API bool r_socket_listen (RSocket *s, const char *port, const char *certfile) {
  50. return false;
  51. }
  52. R_API RSocket *r_socket_accept(RSocket *s) {
  53. return NULL;
  54. }
  55. R_API int r_socket_block_time (RSocket *s, int block, int sec) {
  56. return -1;
  57. }
  58. R_API int r_socket_flush(RSocket *s) {
  59. return -1;
  60. }
  61. R_API int r_socket_ready(RSocket *s, int secs, int usecs) {
  62. return -1;
  63. }
  64. R_API char *r_socket_to_string(RSocket *s) {
  65. return NULL;
  66. }
  67. R_API int r_socket_write(RSocket *s, void *buf, int len) {
  68. return -1;
  69. }
  70. R_API int r_socket_puts(RSocket *s, char *buf) {
  71. return -1;
  72. }
  73. R_API void r_socket_printf(RSocket *s, const char *fmt, ...) {
  74. /* nothing here */
  75. }
  76. R_API int r_socket_read(RSocket *s, unsigned char *buf, int len) {
  77. return -1;
  78. }
  79. R_API int r_socket_read_block(RSocket *s, unsigned char *buf, int len) {
  80. return -1;
  81. }
  82. R_API int r_socket_gets(RSocket *s, char *buf, int size) {
  83. return -1;
  84. }
  85. R_API RSocket *r_socket_new_from_fd (int fd) {
  86. return NULL;
  87. }
  88. R_API ut8* r_socket_slurp(RSocket *s, int *len) {
  89. return NULL;
  90. }
  91. #else
  92. #if 0
  93. winsock api notes
  94. =================
  95. close: closes the socket without flushing the data
  96. WSACleanup: closes all network connections
  97. #endif
  98. #define BUFFER_SIZE 4096
  99. R_API bool r_socket_is_connected (RSocket *s) {
  100. #if __WINDOWS__ && !defined(__CYGWIN__) //&& !defined(__MINGW64__)
  101. char buf[2];
  102. r_socket_block_time (s, 0, 0);
  103. ssize_t ret = recv (s->fd, (char*)&buf, 1, MSG_PEEK);
  104. r_socket_block_time (s, 1, 0);
  105. return ret? true: false;
  106. #else
  107. char buf[2];
  108. int ret = recv (s->fd, &buf, 1, MSG_PEEK | MSG_DONTWAIT);
  109. return ret? true: false;
  110. #endif
  111. }
  112. #if __UNIX__ || defined(__CYGWIN__)
  113. static int r_socket_unix_connect(RSocket *s, const char *file) {
  114. struct sockaddr_un addr;
  115. int sock = socket (PF_UNIX, SOCK_STREAM, 0);
  116. if (sock < 0) {
  117. free (s);
  118. return false;
  119. }
  120. // TODO: set socket options
  121. addr.sun_family = AF_UNIX;
  122. strncpy (addr.sun_path, file, sizeof (addr.sun_path)-1);
  123. if (connect (sock, (struct sockaddr *)&addr, sizeof(addr))==-1) {
  124. close (sock);
  125. free (s);
  126. return false;
  127. }
  128. s->fd = sock;
  129. s->is_ssl = false;
  130. return true;
  131. }
  132. R_API int r_socket_unix_listen (RSocket *s, const char *file) {
  133. struct sockaddr_un unix_name;
  134. int sock = socket (PF_UNIX, SOCK_STREAM, 0);
  135. if (sock < 0) {
  136. return false;
  137. }
  138. // TODO: set socket options
  139. unix_name.sun_family = AF_UNIX;
  140. strncpy (unix_name.sun_path, file, sizeof (unix_name.sun_path)-1);
  141. /* just to make sure there is no other socket file */
  142. unlink (unix_name.sun_path);
  143. if (bind (sock, (struct sockaddr *) &unix_name, sizeof (unix_name)) < 0) {
  144. close (sock);
  145. return false;
  146. }
  147. signal (SIGPIPE, SIG_IGN);
  148. /* change permissions */
  149. if (chmod (unix_name.sun_path, 0777) != 0) {
  150. close (sock);
  151. return false;
  152. }
  153. if (listen (sock, 1)) {
  154. close (sock);
  155. return false;
  156. }
  157. s->fd = sock;
  158. return true;
  159. }
  160. #endif
  161. R_API RSocket *r_socket_new (int is_ssl) {
  162. RSocket *s = R_NEW0 (RSocket);
  163. if (!s) return NULL;
  164. s->is_ssl = is_ssl;
  165. s->port = 0;
  166. #if __UNIX_
  167. signal (SIGPIPE, SIG_IGN);
  168. #endif
  169. s->local = 0;
  170. s->fd = -1;
  171. #if HAVE_LIB_SSL
  172. if (is_ssl) {
  173. s->sfd = NULL;
  174. s->ctx = NULL;
  175. s->bio = NULL;
  176. #if OPENSSL_VERSION_NUMBER < 0x1010000fL
  177. if (!SSL_library_init ()) {
  178. r_socket_free (s);
  179. return NULL;
  180. }
  181. SSL_load_error_strings ();
  182. #endif
  183. }
  184. #endif
  185. return s;
  186. }
  187. R_API bool r_socket_connect (RSocket *s, const char *host, const char *port, int proto, unsigned int timeout) {
  188. #if __WINDOWS__ && !defined(__CYGWIN__) //&& !defined(__MINGW64__)
  189. struct sockaddr_in sa;
  190. struct hostent *he;
  191. WSADATA wsadata;
  192. TIMEVAL Timeout;
  193. Timeout.tv_sec = timeout;
  194. Timeout.tv_usec = 0;
  195. if (WSAStartup (MAKEWORD (1, 1), &wsadata) == SOCKET_ERROR) {
  196. eprintf ("Error creating socket.");
  197. return false;
  198. }
  199. s->fd = socket (AF_INET, SOCK_STREAM, 0);
  200. if (s->fd == -1)
  201. return false;
  202. unsigned long iMode = 1;
  203. int iResult = ioctlsocket (s->fd, FIONBIO, &iMode);
  204. if (iResult != NO_ERROR) {
  205. eprintf ("ioctlsocket error: %d\n", iResult);
  206. }
  207. memset (&sa, 0, sizeof(sa));
  208. sa.sin_family = AF_INET;
  209. he = (struct hostent *)gethostbyname (host);
  210. if (he == (struct hostent*)0) {
  211. close (s->fd);
  212. return false;
  213. }
  214. sa.sin_addr = *((struct in_addr *)he->h_addr);
  215. s->port = r_socket_port_by_name (port);
  216. sa.sin_port = htons (s->port);
  217. if (!connect (s->fd, (const struct sockaddr*)&sa, sizeof (struct sockaddr))) {
  218. close (s->fd);
  219. return false;
  220. }
  221. iMode = 0;
  222. iResult = ioctlsocket (s->fd, FIONBIO, &iMode);
  223. if (iResult != NO_ERROR) {
  224. eprintf ("ioctlsocket error: %d\n", iResult);
  225. }
  226. fd_set Write, Err;
  227. FD_ZERO (&Write);
  228. FD_ZERO (&Err);
  229. FD_SET (s->fd, &Write);
  230. FD_SET (s->fd, &Err);
  231. select (0, NULL, &Write, &Err, &Timeout);
  232. if(FD_ISSET (s->fd, &Write)) {
  233. return true;
  234. }
  235. return false;
  236. #elif __UNIX__ || defined(__CYGWIN__)
  237. int gai, ret;
  238. struct addrinfo hints, *res, *rp;
  239. if (!proto) {
  240. proto = R_SOCKET_PROTO_TCP;
  241. }
  242. signal (SIGPIPE, SIG_IGN);
  243. if (proto == R_SOCKET_PROTO_UNIX) {
  244. if (!r_socket_unix_connect (s, host)) {
  245. return false;
  246. }
  247. } else {
  248. memset (&hints, 0, sizeof (struct addrinfo));
  249. hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
  250. hints.ai_protocol = proto;
  251. gai = getaddrinfo (host, port, &hints, &res);
  252. if (gai != 0) {
  253. eprintf ("Error in getaddrinfo: %s\n", gai_strerror (gai));
  254. return false;
  255. }
  256. for (rp = res; rp != NULL; rp = rp->ai_next) {
  257. int flag = 1;
  258. s->fd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  259. if (s->fd == -1) {
  260. perror ("socket");
  261. continue;
  262. }
  263. ret = setsockopt (s->fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof (flag));
  264. if (ret < 0) {
  265. perror ("setsockopt");
  266. close (s->fd);
  267. s->fd = -1;
  268. continue;
  269. }
  270. if (timeout > 0) {
  271. r_socket_block_time (s, 1, timeout);
  272. //fcntl (s->fd, F_SETFL, O_NONBLOCK, 1);
  273. }
  274. ret = connect (s->fd, rp->ai_addr, rp->ai_addrlen);
  275. if (timeout == 0 && ret == 0) {
  276. freeaddrinfo (res);
  277. return true;
  278. }
  279. if (ret == 0 /* || nonblocking */) {
  280. struct timeval tv;
  281. fd_set fdset, errset;
  282. FD_ZERO (&fdset);
  283. FD_SET (s->fd, &fdset);
  284. tv.tv_sec = 1; //timeout;
  285. tv.tv_usec = 0;
  286. if (r_socket_is_connected (s)) {
  287. freeaddrinfo (res);
  288. return true;
  289. }
  290. if (select (s->fd + 1, NULL, NULL, &errset, &tv) == 1) {
  291. int so_error;
  292. socklen_t len = sizeof so_error;
  293. ret = getsockopt (s->fd, SOL_SOCKET,
  294. SO_ERROR, &so_error, &len);
  295. if (ret == 0 && so_error == 0) {
  296. //fcntl (s->fd, F_SETFL, O_NONBLOCK, 0);
  297. //r_socket_block_time (s, 0, 0);
  298. freeaddrinfo (res);
  299. return true;
  300. }
  301. }
  302. }
  303. close (s->fd);
  304. s->fd = -1;
  305. }
  306. freeaddrinfo (res);
  307. if (!rp) {
  308. eprintf ("Could not resolve address '%s' or failed to connect\n", host);
  309. return false;
  310. }
  311. }
  312. #endif
  313. #if HAVE_LIB_SSL
  314. if (s->is_ssl) {
  315. s->ctx = SSL_CTX_new (SSLv23_client_method ());
  316. if (!s->ctx) {
  317. r_socket_free (s);
  318. return false;
  319. }
  320. s->sfd = SSL_new (s->ctx);
  321. SSL_set_fd (s->sfd, s->fd);
  322. if (SSL_connect (s->sfd) != 1) {
  323. r_socket_free (s);
  324. return false;
  325. }
  326. }
  327. #endif
  328. return true;
  329. }
  330. /* close the file descriptor associated with the RSocket s */
  331. R_API int r_socket_close_fd (RSocket *s) {
  332. return s->fd != -1 ? close (s->fd) : false;
  333. }
  334. /* shutdown the socket and close the file descriptor */
  335. R_API int r_socket_close (RSocket *s) {
  336. int ret = false;
  337. if (!s) return false;
  338. if (s->fd != -1) {
  339. #if __UNIX__ || defined(__CYGWIN__)
  340. shutdown (s->fd, SHUT_RDWR);
  341. #endif
  342. #if __WINDOWS__ && !defined(__CYGWIN__) //&& !defined(__MINGW64__)
  343. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms740481(v=vs.85).aspx
  344. shutdown (s->fd, SD_SEND);
  345. do {
  346. char buf = 0;
  347. ret = recv (s->fd, &buf, 1, 0);
  348. } while (ret != 0 && ret != SOCKET_ERROR);
  349. ret = closesocket (s->fd);
  350. #else
  351. ret = close (s->fd);
  352. #endif
  353. }
  354. #if HAVE_LIB_SSL
  355. if (s->is_ssl && s->sfd) {
  356. SSL_free (s->sfd);
  357. s->sfd = NULL;
  358. }
  359. #endif
  360. return ret;
  361. }
  362. /* shutdown the socket, close the file descriptor and free the RSocket */
  363. R_API int r_socket_free (RSocket *s) {
  364. int res = r_socket_close (s);
  365. #if HAVE_LIB_SSL
  366. if (s->is_ssl) {
  367. if (s->sfd)
  368. SSL_free (s->sfd);
  369. if (s->ctx)
  370. SSL_CTX_free (s->ctx);
  371. }
  372. #endif
  373. free (s);
  374. return res;
  375. }
  376. R_API int r_socket_port_by_name(const char *name) {
  377. struct servent *p = getservbyname (name, "tcp");
  378. if (p && p->s_port)
  379. return ntohs (p->s_port);
  380. return r_num_get (NULL, name);
  381. }
  382. R_API bool r_socket_listen (RSocket *s, const char *port, const char *certfile) {
  383. #if __UNIX__ || defined(__CYGWIN__)
  384. int optval = 1;
  385. int ret;
  386. struct linger linger = { 0 };
  387. #endif
  388. if (r_sandbox_enable (0)) {
  389. return false;
  390. }
  391. #if __WINDOWS__ && !defined(__CYGWIN__) //&& !defined(__MINGW64__)
  392. WSADATA wsadata;
  393. if (WSAStartup (MAKEWORD (1, 1), &wsadata) == SOCKET_ERROR) {
  394. eprintf ("Error creating socket.");
  395. return false;
  396. }
  397. #endif
  398. if ((s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
  399. return false;
  400. #if __UNIX__ || defined(__CYGWIN__)
  401. linger.l_onoff = 1;
  402. linger.l_linger = 1;
  403. ret = setsockopt (s->fd, SOL_SOCKET, SO_LINGER, (void*)&linger, sizeof (linger));
  404. if (ret < 0) {
  405. return false;
  406. }
  407. { // fix close after write bug //
  408. int x = 1500; // FORCE MTU
  409. ret = setsockopt (s->fd, SOL_SOCKET, SO_SNDBUF, (void*)&x, sizeof (int));
  410. if (ret < 0)
  411. return false;
  412. }
  413. ret = setsockopt (s->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof optval);
  414. if (ret < 0)
  415. return false;
  416. #endif
  417. memset (&s->sa, 0, sizeof (s->sa));
  418. s->sa.sin_family = AF_INET;
  419. s->sa.sin_addr.s_addr = htonl (s->local? INADDR_LOOPBACK: INADDR_ANY);
  420. s->port = r_socket_port_by_name (port);
  421. if (s->port < 1) {
  422. return false;
  423. }
  424. s->sa.sin_port = htons (s->port); // TODO honor etc/services
  425. if (bind (s->fd, (struct sockaddr *)&s->sa, sizeof (s->sa)) < 0) {
  426. r_sys_perror ("bind");
  427. close (s->fd);
  428. return false;
  429. }
  430. #if __UNIX__ || defined(__CYGWIN__)
  431. signal (SIGPIPE, SIG_IGN);
  432. #endif
  433. if (listen (s->fd, 32) < 0) {
  434. close (s->fd);
  435. return false;
  436. }
  437. #if HAVE_LIB_SSL
  438. if (s->is_ssl) {
  439. s->ctx = SSL_CTX_new (SSLv23_method ());
  440. if (!s->ctx) {
  441. r_socket_free (s);
  442. return false;
  443. }
  444. if (!SSL_CTX_use_certificate_chain_file (s->ctx, certfile)) {
  445. r_socket_free (s);
  446. return false;
  447. }
  448. if (!SSL_CTX_use_PrivateKey_file (s->ctx, certfile, SSL_FILETYPE_PEM)) {
  449. r_socket_free (s);
  450. return false;
  451. }
  452. SSL_CTX_set_verify_depth (s->ctx, 1);
  453. }
  454. #endif
  455. return true;
  456. }
  457. R_API RSocket *r_socket_accept(RSocket *s) {
  458. RSocket *sock;
  459. socklen_t salen = sizeof (s->sa);
  460. if (!s) {
  461. return NULL;
  462. }
  463. sock = R_NEW0 (RSocket);
  464. if (!sock) {
  465. return NULL;
  466. }
  467. //signal (SIGPIPE, SIG_DFL);
  468. sock->fd = accept (s->fd, (struct sockaddr *)&s->sa, &salen);
  469. if (sock->fd == -1) {
  470. r_sys_perror ("accept");
  471. free (sock);
  472. return NULL;
  473. }
  474. #if HAVE_LIB_SSL
  475. sock->is_ssl = s->is_ssl;
  476. if (sock->is_ssl) {
  477. sock->sfd = NULL;
  478. sock->ctx = NULL;
  479. sock->bio = NULL;
  480. BIO *sbio = BIO_new_socket (sock->fd, BIO_NOCLOSE);
  481. sock->sfd = SSL_new (s->ctx);
  482. SSL_set_bio (sock->sfd, sbio, sbio);
  483. if (SSL_accept (sock->sfd) <= 0) {
  484. r_socket_free (sock);
  485. return NULL;
  486. }
  487. sock->bio = BIO_new (BIO_f_buffer ());
  488. sbio = BIO_new (BIO_f_ssl ());
  489. BIO_set_ssl (sbio, sock->sfd, BIO_CLOSE);
  490. BIO_push (sock->bio, sbio);
  491. }
  492. #else
  493. sock->is_ssl = 0;
  494. #endif
  495. return sock;
  496. }
  497. R_API int r_socket_block_time (RSocket *s, int block, int sec) {
  498. #if __UNIX__ || defined(__CYGWIN__)
  499. int ret, flags;
  500. #endif
  501. if (!s) return false;
  502. #if __UNIX__ || defined(__CYGWIN__)
  503. flags = fcntl (s->fd, F_GETFL, 0);
  504. if (flags < 0)
  505. return false;
  506. ret = fcntl (s->fd, F_SETFL, block?
  507. (flags & ~O_NONBLOCK):
  508. (flags | O_NONBLOCK));
  509. if (ret < 0)
  510. return false;
  511. #elif __WINDOWS__ && !defined(__CYGWIN__) //&& !defined(__MINGW64__)
  512. // HACK: nonblocking io on w32 behaves strange
  513. return true;
  514. ioctlsocket (s->fd, FIONBIO, (u_long FAR*)&block);
  515. #endif
  516. if (sec > 0) {
  517. struct timeval tv = {0};
  518. tv.tv_sec = sec;
  519. tv.tv_usec = 0;
  520. if (setsockopt (s->fd, SOL_SOCKET, SO_RCVTIMEO,
  521. (char *)&tv, sizeof (tv)) < 0)
  522. return false;
  523. }
  524. return true;
  525. }
  526. R_API int r_socket_flush(RSocket *s) {
  527. #if HAVE_LIB_SSL
  528. if (s->is_ssl && s->bio)
  529. return BIO_flush(s->bio);
  530. #endif
  531. return true;
  532. }
  533. // XXX: rewrite it to use select //
  534. /* waits secs until new data is received. */
  535. /* returns -1 on error, 0 is false, 1 is true */
  536. R_API int r_socket_ready(RSocket *s, int secs, int usecs) {
  537. #if __UNIX__ || defined(__CYGWIN__)
  538. //int msecs = (1000 * secs) + (usecs / 1000);
  539. int msecs = (usecs / 1000);
  540. struct pollfd fds[1];
  541. fds[0].fd = s->fd;
  542. fds[0].events = POLLIN | POLLPRI;
  543. fds[0].revents = POLLNVAL | POLLHUP | POLLERR;
  544. return poll ((struct pollfd *)&fds, 1, msecs);
  545. #elif __WINDOWS__ && !defined(__CYGWIN__) //&& !defined(__MINGW64__)
  546. return 1;
  547. #if XXX_THIS_IS_NOT_WORKING_WELL
  548. fd_set rfds;
  549. struct timeval tv;
  550. if (s->fd == -1) {
  551. return -1;
  552. }
  553. FD_ZERO (&rfds);
  554. FD_SET (s->fd, &rfds);
  555. tv.tv_sec = secs;
  556. tv.tv_usec = usecs;
  557. if (select (s->fd+1, &rfds, NULL, NULL, &tv) == -1)
  558. return -1;
  559. return FD_ISSET (0, &rfds);
  560. #endif
  561. #else
  562. return true; /* always ready if unknown */
  563. #endif
  564. }
  565. R_API char *r_socket_to_string(RSocket *s) {
  566. #if __WINDOWS__ && !defined(__CYGWIN__) //&& !defined(__MINGW64__)
  567. char *str = malloc (32);
  568. snprintf (str, 31, "fd%d", s->fd);
  569. return str;
  570. #elif __UNIX__ || defined(__CYGWIN__)
  571. char *str = NULL;
  572. struct sockaddr sa;
  573. socklen_t sl = sizeof (sa);
  574. memset (&sa, 0, sizeof (sa));
  575. if (!getpeername (s->fd, &sa, &sl)) {
  576. struct sockaddr_in *sain = (struct sockaddr_in*) &sa;
  577. ut8 *a = (ut8*) &(sain->sin_addr);
  578. if ((str = malloc (32)))
  579. sprintf (str, "%d.%d.%d.%d:%d",
  580. a[0],a[1],a[2],a[3], ntohs (sain->sin_port));
  581. } else eprintf ("getperrname: failed\n"); //r_sys_perror ("getpeername");
  582. return str;
  583. #else
  584. return NULL;
  585. #endif
  586. }
  587. /* Read/Write functions */
  588. R_API int r_socket_write(RSocket *s, void *buf, int len) {
  589. int ret, delta = 0;
  590. #if __UNIX__ || defined(__CYGWIN__)
  591. signal (SIGPIPE, SIG_IGN);
  592. #endif
  593. for (;;) {
  594. int b = 1500; //65536; // Use MTU 1500?
  595. if (b > len) {
  596. b = len;
  597. }
  598. #if HAVE_LIB_SSL
  599. if (s->is_ssl) {
  600. if (s->bio)
  601. ret = BIO_write (s->bio, buf+delta, b);
  602. else
  603. ret = SSL_write (s->sfd, buf+delta, b);
  604. } else
  605. #endif
  606. {
  607. ret = send (s->fd, buf+delta, b, 0);
  608. }
  609. //if (ret == 0) return -1;
  610. if (ret<1) break;
  611. if (ret == len)
  612. return len;
  613. delta += ret;
  614. len -= ret;
  615. }
  616. if (ret == -1)
  617. return -1;
  618. return delta;
  619. }
  620. R_API int r_socket_puts(RSocket *s, char *buf) {
  621. return r_socket_write (s, buf, strlen (buf));
  622. }
  623. R_API void r_socket_printf(RSocket *s, const char *fmt, ...) {
  624. char buf[BUFFER_SIZE];
  625. va_list ap;
  626. if (s->fd >= 0) {
  627. va_start (ap, fmt);
  628. vsnprintf (buf, BUFFER_SIZE, fmt, ap);
  629. r_socket_write (s, buf, strlen (buf));
  630. va_end (ap);
  631. }
  632. }
  633. R_API int r_socket_read(RSocket *s, unsigned char *buf, int len) {
  634. if (!s) {
  635. return -1;
  636. }
  637. #if HAVE_LIB_SSL
  638. if (s->is_ssl) {
  639. if (s->bio) {
  640. return BIO_read (s->bio, buf, len);
  641. }
  642. return SSL_read (s->sfd, buf, len);
  643. }
  644. #endif
  645. #if __WINDOWS__ && !defined(__CYGWIN__) //&& !defined(__MINGW64__)
  646. rep:
  647. {
  648. int ret = recv (s->fd, (void *)buf, len, 0);
  649. //if (ret != len)
  650. // return 0;
  651. //r_sys_perror ("recv");
  652. if (ret == -1) goto rep;
  653. return ret;
  654. }
  655. #else
  656. return read (s->fd, buf, len);
  657. #endif
  658. }
  659. R_API int r_socket_read_block(RSocket *s, unsigned char *buf, int len) {
  660. int r, ret = 0;
  661. for (ret = 0; ret < len; ) {
  662. r = r_socket_read (s, buf+ret, len-ret);
  663. if (r < 1) {
  664. break;
  665. }
  666. ret += r;
  667. }
  668. return ret;
  669. }
  670. R_API int r_socket_gets(RSocket *s, char *buf, int size) {
  671. int i = 0;
  672. int ret = 0;
  673. if (s->fd == -1) {
  674. return -1;
  675. }
  676. while (i < size) {
  677. ret = r_socket_read (s, (ut8 *)buf + i, 1);
  678. if (ret == 0) {
  679. if (i > 0) {
  680. return i;
  681. }
  682. return -1;
  683. }
  684. if (ret < 0) {
  685. r_socket_close (s);
  686. return i == 0? -1: i;
  687. }
  688. if (buf[i] == '\r' || buf[i] == '\n') {
  689. buf[i] = 0;
  690. break;
  691. }
  692. i += ret;
  693. }
  694. buf[i]='\0';
  695. return i;
  696. }
  697. R_API RSocket *r_socket_new_from_fd (int fd) {
  698. RSocket *s = R_NEW0 (RSocket);
  699. if (s) {
  700. s->fd = fd;
  701. }
  702. return s;
  703. }
  704. R_API ut8* r_socket_slurp(RSocket *s, int *len) {
  705. int blockSize = 4096;
  706. ut8 *ptr, *buf = malloc (blockSize);
  707. int copied = 0;
  708. if (len) {
  709. *len = 0;
  710. }
  711. for (;;) {
  712. int rc = r_socket_read (s, buf + copied, blockSize);
  713. if (rc > 0) {
  714. copied += rc;
  715. }
  716. ptr = realloc (buf, copied + blockSize);
  717. if (ptr) {
  718. buf = ptr;
  719. } else {
  720. break;
  721. }
  722. if (rc < 1) {
  723. break;
  724. }
  725. }
  726. if (copied == 0) {
  727. R_FREE (buf);
  728. }
  729. if (len) {
  730. *len = copied;
  731. }
  732. return buf;
  733. }
  734. #endif // EMSCRIPTEN