You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

508 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: nanohttp-socket.c,v 1.61 2006/07/09 16:24:19 snowdrop Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C
  5. * Copyright (C) 2003 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ferhatayaz@yahoo.com
  23. ******************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #ifdef HAVE_SYS_TYPES_H
  28. #include <sys/types.h>
  29. #endif
  30. #ifdef HAVE_SYS_SOCKET_H
  31. #include <sys/socket.h>
  32. #endif
  33. #ifdef HAVE_NETINET_IN_H
  34. #include <netinet/in.h>
  35. #endif
  36. #ifdef HAVE_ARPA_INET_H
  37. #include <arpa/inet.h>
  38. #endif
  39. #ifdef HAVE_FCNTL_H
  40. #include <fcntl.h>
  41. #endif
  42. #ifdef HAVE_NETDB_H
  43. #include <netdb.h>
  44. #endif
  45. #ifdef HAVE_UNISTD_H
  46. #include <unistd.h>
  47. #endif
  48. #ifdef HAVE_STDIO_H
  49. #include <stdio.h>
  50. #endif
  51. #ifdef HAVE_ERRNO_H
  52. #include <errno.h>
  53. #endif
  54. #ifdef HAVE_STRING_H
  55. #include <string.h>
  56. #endif
  57. #ifdef WIN32
  58. #include "wsockcompat.h"
  59. #include <winsock2.h>
  60. #include <process.h>
  61. #define inline
  62. #ifndef __MINGW32__
  63. typedef int ssize_t;
  64. #endif
  65. #undef errno
  66. #define errno WSAGetLastError()
  67. #endif
  68. #ifdef MEM_DEBUG
  69. #include <utils/alloc.h>
  70. #endif
  71. #include "nanohttp-logging.h"
  72. #include "nanohttp-socket.h"
  73. #include "nanohttp-common.h"
  74. #include "nanohttp-ssl.h"
  75. #ifdef WIN32
  76. static inline void
  77. _hsocket_module_sys_init(int argc, char **argv)
  78. {
  79. struct WSAData info;
  80. WSAStartup(MAKEWORD(2, 2), &info);
  81. return;
  82. }
  83. static inline void
  84. _hsocket_module_sys_destroy(void)
  85. {
  86. WSACleanup();
  87. return;
  88. }
  89. #else
  90. static inline void
  91. _hsocket_module_sys_init(int argc, char **argv)
  92. {
  93. return;
  94. }
  95. static inline void
  96. _hsocket_module_sys_destroy(void)
  97. {
  98. return;
  99. }
  100. #endif
  101. /*--------------------------------------------------
  102. FUNCTION: hsocket_module_init
  103. NOTE: This will be called from httpd_init()
  104. for server and from httpc_init() for client
  105. ----------------------------------------------------*/
  106. herror_t
  107. hsocket_module_init(int argc, char **argv)
  108. {
  109. _hsocket_module_sys_init(argc, argv);
  110. return hssl_module_init(argc, argv);
  111. }
  112. /*--------------------------------------------------
  113. FUNCTION: hsocket_module_destroy
  114. ----------------------------------------------------*/
  115. void
  116. hsocket_module_destroy(void)
  117. {
  118. _hsocket_module_sys_destroy();
  119. return;
  120. }
  121. /*--------------------------------------------------
  122. FUNCTION: hsocket_init
  123. ----------------------------------------------------*/
  124. herror_t
  125. hsocket_init(hsocket_t * sock)
  126. {
  127. memset(sock, 0, sizeof(hsocket_t));
  128. sock->sock = HSOCKET_FREE;
  129. return H_OK;
  130. }
  131. /*--------------------------------------------------
  132. FUNCTION: hsocket_free
  133. ----------------------------------------------------*/
  134. void
  135. hsocket_free(hsocket_t * sock)
  136. {
  137. /* nop */
  138. return;
  139. }
  140. /*--------------------------------------------------
  141. FUNCTION: hsocket_open
  142. ----------------------------------------------------*/
  143. herror_t
  144. hsocket_open(hsocket_t * dsock, const char *hostname, int port, int ssl)
  145. {
  146. struct sockaddr_in address;
  147. struct hostent *host;
  148. char *ip;
  149. if ((dsock->sock = socket(AF_INET, SOCK_STREAM, 0)) <= 0)
  150. return herror_new("hsocket_open", HSOCKET_ERROR_CREATE,
  151. "Socket error (%s)", strerror(errno));
  152. /* Get host data */
  153. if (!(host = gethostbyname(hostname)))
  154. return herror_new("hsocket_open", HSOCKET_ERROR_GET_HOSTNAME,
  155. "Socket error (%s)", strerror(errno));
  156. ip = inet_ntoa(*(struct in_addr *) *host->h_addr_list);
  157. address.sin_addr.s_addr = inet_addr(ip);
  158. /* set server addresss */
  159. address.sin_family = host->h_addrtype;
  160. address.sin_port = htons((unsigned short) port);
  161. log_verbose4("Opening %s://%s:%i", ssl ? "https" : "http", hostname, port);
  162. /* connect to the server */
  163. if (connect(dsock->sock, (struct sockaddr *) &address, sizeof(address)) !=
  164. 0)
  165. return herror_new("hsocket_open", HSOCKET_ERROR_CONNECT,
  166. "Socket error (%s)", strerror(errno));
  167. if (ssl)
  168. {
  169. herror_t status;
  170. if ((status = hssl_client_ssl(dsock)) != H_OK)
  171. {
  172. log_error2("hssl_client_ssl failed (%s)", herror_message(status));
  173. return status;
  174. }
  175. }
  176. return H_OK;
  177. }
  178. /*--------------------------------------------------
  179. FUNCTION: hsocket_bind
  180. ----------------------------------------------------*/
  181. herror_t
  182. hsocket_bind(hsocket_t * dsock, int port)
  183. {
  184. hsocket_t sock;
  185. struct sockaddr_in addr;
  186. int opt = 1;
  187. /* create socket */
  188. if ((sock.sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  189. {
  190. log_error2("Cannot create socket (%s)", strerror(errno));
  191. return herror_new("hsocket_bind", HSOCKET_ERROR_CREATE,
  192. "Socket error (%s)", strerror(errno));
  193. }
  194. setsockopt(sock.sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  195. /* bind socket */
  196. addr.sin_family = AF_INET;
  197. addr.sin_port = htons((unsigned short) port); /* short, network byte order */
  198. addr.sin_addr.s_addr = INADDR_ANY;
  199. memset(&(addr.sin_zero), '\0', 8); /* zero the rest of the struct */
  200. if (bind(sock.sock, (struct sockaddr *) &addr, sizeof(struct sockaddr)) ==
  201. -1)
  202. {
  203. log_error2("Cannot bind socket (%s)", strerror(errno));
  204. return herror_new("hsocket_bind", HSOCKET_ERROR_BIND, "Socket error (%s)",
  205. strerror(errno));
  206. }
  207. dsock->sock = sock.sock;
  208. return H_OK;
  209. }
  210. #ifdef WIN32
  211. static herror_t
  212. _hsocket_sys_accept(hsocket_t * sock, hsocket_t * dest)
  213. {
  214. int asize;
  215. hsocket_t sockfd;
  216. asize = sizeof(struct sockaddr_in);
  217. while (1)
  218. {
  219. sockfd.sock =
  220. accept(sock->sock, (struct sockaddr *) &(dest->addr), &asize);
  221. if (sockfd.sock == INVALID_SOCKET)
  222. {
  223. if (WSAGetLastError() != WSAEWOULDBLOCK)
  224. return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT,
  225. "Socket error (%s)", strerror(errno));
  226. }
  227. else
  228. {
  229. break;
  230. }
  231. }
  232. dest->sock = sockfd.sock;
  233. return H_OK;
  234. }
  235. #else
  236. static herror_t
  237. _hsocket_sys_accept(hsocket_t * sock, hsocket_t * dest)
  238. {
  239. socklen_t len;
  240. len = sizeof(struct sockaddr_in);
  241. if ((dest->sock =
  242. accept(sock->sock, (struct sockaddr *) &(dest->addr), &len)) == -1)
  243. {
  244. log_warn2("accept failed (%s)", strerror(errno));
  245. return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT,
  246. "Cannot accept network connection (%s)",
  247. strerror(errno));
  248. }
  249. return H_OK;
  250. }
  251. #endif
  252. /*----------------------------------------------------------
  253. FUNCTION: hsocket_accept
  254. ----------------------------------------------------------*/
  255. herror_t
  256. hsocket_accept(hsocket_t * sock, hsocket_t * dest)
  257. {
  258. herror_t status;
  259. if (sock->sock < 0)
  260. return herror_new("hsocket_accept", HSOCKET_ERROR_NOT_INITIALIZED,
  261. "hsocket_t not initialized");
  262. if ((status = _hsocket_sys_accept(sock, dest)) != H_OK)
  263. return status;
  264. if ((status = hssl_server_ssl(dest)) != H_OK)
  265. {
  266. log_warn2("SSL startup failed (%s)", herror_message(status));
  267. return status;
  268. }
  269. log_verbose3("accepting connection from '%s' socket=%d",
  270. SAVE_STR(((char *) inet_ntoa(dest->addr.sin_addr))),
  271. dest->sock);
  272. return H_OK;
  273. }
  274. /*--------------------------------------------------
  275. FUNCTION: hsocket_listen
  276. ----------------------------------------------------*/
  277. herror_t
  278. hsocket_listen(hsocket_t * sock)
  279. {
  280. if (sock->sock < 0)
  281. return herror_new("hsocket_listen", HSOCKET_ERROR_NOT_INITIALIZED,
  282. "Called hsocket_listen before initializing!");
  283. if (listen(sock->sock, 15) == -1)
  284. {
  285. log_error2("listen failed (%s)", strerror(errno));
  286. return herror_new("hsocket_listen", HSOCKET_ERROR_LISTEN,
  287. "Cannot listen on this socket (%s)", strerror(errno));
  288. }
  289. return H_OK;
  290. }
  291. #ifdef WIN32
  292. static inline void
  293. _hsocket_sys_close(hsocket_t * sock)
  294. {
  295. char junk[10];
  296. /* shutdown(sock,SD_RECEIVE); */
  297. shutdown(sock->sock, SD_SEND);
  298. while (recv(sock->sock, junk, sizeof(junk), 0) > 0);
  299. /* nothing */
  300. closesocket(sock->sock);
  301. return;
  302. }
  303. #else
  304. static inline void
  305. _hsocket_sys_close(hsocket_t * sock)
  306. {
  307. shutdown(sock->sock, SHUT_RDWR);
  308. close(sock->sock);
  309. return;
  310. }
  311. #endif
  312. /*--------------------------------------------------
  313. FUNCTION: hsocket_close
  314. ----------------------------------------------------*/
  315. void
  316. hsocket_close(hsocket_t * sock)
  317. {
  318. log_verbose3("closing socket %p (%d)...", sock, sock->sock);
  319. hssl_cleanup(sock);
  320. _hsocket_sys_close(sock);
  321. log_verbose1("socket closed");
  322. return;
  323. }
  324. /*--------------------------------------------------
  325. FUNCTION: hsocket_send
  326. ----------------------------------------------------*/
  327. herror_t
  328. hsocket_nsend(hsocket_t * sock, const byte_t * bytes, int n)
  329. {
  330. herror_t status;
  331. size_t total = 0;
  332. size_t size;
  333. log_verbose2("Starting to send on sock=%p", &sock);
  334. if (sock->sock < 0)
  335. return herror_new("hsocket_nsend", HSOCKET_ERROR_NOT_INITIALIZED,
  336. "hsocket not initialized");
  337. /* log_verbose2( "SENDING %s", bytes ); */
  338. while (1)
  339. {
  340. if ((status = hssl_write(sock, bytes + total, n, &size)) != H_OK)
  341. {
  342. log_warn2("hssl_write failed (%s)", herror_message(status));
  343. return status;
  344. }
  345. n -= size;
  346. total += size;
  347. if (n <= 0)
  348. break;
  349. }
  350. return H_OK;
  351. }
  352. /*--------------------------------------------------
  353. FUNCTION: hsocket_send
  354. ----------------------------------------------------*/
  355. herror_t
  356. hsocket_send(hsocket_t * sock, const char *str)
  357. {
  358. return hsocket_nsend(sock, str, strlen(str));
  359. }
  360. int
  361. hsocket_select_read(int sock, char *buf, size_t len)
  362. {
  363. struct timeval timeout;
  364. fd_set fds;
  365. int ret;
  366. FD_ZERO(&fds);
  367. FD_SET(sock, &fds);
  368. timeout.tv_sec = httpd_get_timeout();
  369. timeout.tv_usec = 0;
  370. ret = select(sock + 1, &fds, NULL, NULL, &timeout);
  371. if (ret == 0)
  372. {
  373. errno = ETIMEDOUT;
  374. log_verbose2("Socket %d timeout", sock);
  375. return -1;
  376. }
  377. #ifdef WIN32
  378. return recv(sock, buf, len, 0);
  379. #else
  380. return read(sock, buf, len);
  381. #endif
  382. }
  383. herror_t
  384. hsocket_read(hsocket_t * sock, byte_t * buffer, int total, int force,
  385. int *received)
  386. {
  387. herror_t status;
  388. size_t totalRead;
  389. size_t count;
  390. /* log_verbose3("Entering hsocket_read(total=%d,force=%d)", total, force); */
  391. totalRead = 0;
  392. do
  393. {
  394. if ((status =
  395. hssl_read(sock, &buffer[totalRead], (size_t) total - totalRead,
  396. &count)) != H_OK)
  397. {
  398. log_warn2("hssl_read failed (%s)", herror_message(status));
  399. return status;
  400. }
  401. if (!force)
  402. {
  403. /* log_verbose3("Leaving !force (received=%d)(status=%d)", *received,
  404. status); */
  405. *received = count;
  406. return H_OK;
  407. }
  408. totalRead += count;
  409. if (totalRead == total)
  410. {
  411. *received = totalRead;
  412. /*
  413. log_verbose4("Leaving totalRead == total
  414. (received=%d)(status=%d)(totalRead=%d)", *received, status,
  415. totalRead); */
  416. return H_OK;
  417. }
  418. }
  419. while (1);
  420. }