socket.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2008,2010,2011, 2013 Oswald Buddenhagen <ossi@users.sf.net>
  5. * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program 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
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * As a special exception, mbsync may be linked with the OpenSSL library,
  21. * despite that library's more restrictive license.
  22. */
  23. #include "socket.h"
  24. #include <assert.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <stddef.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <fcntl.h>
  31. #include <sys/socket.h>
  32. #include <sys/ioctl.h>
  33. #include <netinet/in.h>
  34. #include <netinet/tcp.h>
  35. #include <arpa/inet.h>
  36. #include <netdb.h>
  37. #ifdef HAVE_LIBSSL
  38. # include <openssl/ssl.h>
  39. # include <openssl/err.h>
  40. # include <openssl/hmac.h>
  41. # include <openssl/x509v3.h>
  42. #endif
  43. enum {
  44. SCK_CONNECTING,
  45. #ifdef HAVE_LIBSSL
  46. SCK_STARTTLS,
  47. #endif
  48. SCK_READY
  49. };
  50. static void
  51. socket_fail( conn_t *conn )
  52. {
  53. conn->bad_callback( conn->callback_aux );
  54. }
  55. #ifdef HAVE_LIBSSL
  56. static int
  57. ssl_return( const char *func, conn_t *conn, int ret )
  58. {
  59. int err;
  60. switch ((err = SSL_get_error( conn->ssl, ret ))) {
  61. case SSL_ERROR_NONE:
  62. return ret;
  63. case SSL_ERROR_WANT_WRITE:
  64. conf_notifier( &conn->notify, POLLIN, POLLOUT );
  65. /* fallthrough */
  66. case SSL_ERROR_WANT_READ:
  67. return 0;
  68. case SSL_ERROR_SYSCALL:
  69. case SSL_ERROR_SSL:
  70. if (!(err = ERR_get_error())) {
  71. if (ret == 0)
  72. error( "Socket error: secure %s %s: unexpected EOF\n", func, conn->name );
  73. else
  74. sys_error( "Socket error: secure %s %s", func, conn->name );
  75. } else {
  76. error( "Socket error: secure %s %s: %s\n", func, conn->name, ERR_error_string( err, 0 ) );
  77. }
  78. break;
  79. default:
  80. error( "Socket error: secure %s %s: unhandled SSL error %d\n", func, conn->name, err );
  81. break;
  82. }
  83. if (conn->state == SCK_STARTTLS)
  84. conn->callbacks.starttls( 0, conn->callback_aux );
  85. else
  86. socket_fail( conn );
  87. return -1;
  88. }
  89. /* Some of this code is inspired by / lifted from mutt. */
  90. static int
  91. host_matches( const char *host, const char *pattern )
  92. {
  93. if (pattern[0] == '*' && pattern[1] == '.') {
  94. pattern += 2;
  95. if (!(host = strchr( host, '.' )))
  96. return 0;
  97. host++;
  98. }
  99. return *host && *pattern && !strcasecmp( host, pattern );
  100. }
  101. static int
  102. verify_hostname( X509 *cert, const char *hostname )
  103. {
  104. int i, len, found;
  105. X509_NAME *subj;
  106. STACK_OF(GENERAL_NAME) *subj_alt_names;
  107. char cname[1000];
  108. /* try the DNS subjectAltNames */
  109. found = 0;
  110. if ((subj_alt_names = X509_get_ext_d2i( cert, NID_subject_alt_name, NULL, NULL ))) {
  111. int num_subj_alt_names = sk_GENERAL_NAME_num( subj_alt_names );
  112. for (i = 0; i < num_subj_alt_names; i++) {
  113. GENERAL_NAME *subj_alt_name = sk_GENERAL_NAME_value( subj_alt_names, i );
  114. if (subj_alt_name->type == GEN_DNS &&
  115. strlen( (const char *)subj_alt_name->d.ia5->data ) == (size_t)subj_alt_name->d.ia5->length &&
  116. host_matches( hostname, (const char *)(subj_alt_name->d.ia5->data) ))
  117. {
  118. found = 1;
  119. break;
  120. }
  121. }
  122. sk_GENERAL_NAME_pop_free( subj_alt_names, GENERAL_NAME_free );
  123. }
  124. if (found)
  125. return 0;
  126. /* try the common name */
  127. if (!(subj = X509_get_subject_name( cert ))) {
  128. error( "Error, cannot get certificate subject\n" );
  129. return -1;
  130. }
  131. if ((len = X509_NAME_get_text_by_NID( subj, NID_commonName, cname, sizeof(cname) )) < 0) {
  132. error( "Error, cannot get certificate common name\n" );
  133. return -1;
  134. }
  135. if (strlen( cname ) == (size_t)len && host_matches( hostname, cname ))
  136. return 0;
  137. error( "Error, certificate owner does not match hostname %s\n", hostname );
  138. return -1;
  139. }
  140. static int
  141. verify_cert_host( const server_conf_t *conf, conn_t *sock )
  142. {
  143. int i;
  144. long err;
  145. X509 *cert;
  146. STACK_OF(X509_OBJECT) *trusted;
  147. cert = SSL_get_peer_certificate( sock->ssl );
  148. if (!cert) {
  149. error( "Error, no server certificate\n" );
  150. return -1;
  151. }
  152. trusted = (STACK_OF(X509_OBJECT) *)sock->conf->trusted_certs;
  153. for (i = 0; i < sk_X509_OBJECT_num( trusted ); i++) {
  154. if (!X509_cmp( cert, sk_X509_OBJECT_value( trusted, i )->data.x509 ))
  155. return 0;
  156. }
  157. err = SSL_get_verify_result( sock->ssl );
  158. if (err != X509_V_OK) {
  159. error( "SSL error connecting %s: %s\n", sock->name, ERR_error_string( err, NULL ) );
  160. return -1;
  161. }
  162. if (!conf->host) {
  163. error( "SSL error connecting %s: Neither host nor matching certificate specified\n", sock->name );
  164. return -1;
  165. }
  166. return verify_hostname( cert, conf->host );
  167. }
  168. static int
  169. init_ssl_ctx( const server_conf_t *conf )
  170. {
  171. server_conf_t *mconf = (server_conf_t *)conf;
  172. int options = 0;
  173. if (conf->SSLContext)
  174. return conf->ssl_ctx_valid;
  175. mconf->SSLContext = SSL_CTX_new( SSLv23_client_method() );
  176. if (!(conf->ssl_versions & SSLv2))
  177. options |= SSL_OP_NO_SSLv2;
  178. if (!(conf->ssl_versions & SSLv3))
  179. options |= SSL_OP_NO_SSLv3;
  180. if (!(conf->ssl_versions & TLSv1))
  181. options |= SSL_OP_NO_TLSv1;
  182. #ifdef SSL_OP_NO_TLSv1_1
  183. if (!(conf->ssl_versions & TLSv1_1))
  184. options |= SSL_OP_NO_TLSv1_1;
  185. #endif
  186. #ifdef SSL_OP_NO_TLSv1_2
  187. if (!(conf->ssl_versions & TLSv1_2))
  188. options |= SSL_OP_NO_TLSv1_2;
  189. #endif
  190. SSL_CTX_set_options( mconf->SSLContext, options );
  191. if (conf->cert_file && !SSL_CTX_load_verify_locations( mconf->SSLContext, conf->cert_file, 0 )) {
  192. error( "Error while loading certificate file '%s': %s\n",
  193. conf->cert_file, ERR_error_string( ERR_get_error(), 0 ) );
  194. return 0;
  195. }
  196. mconf->trusted_certs = (_STACK *)sk_X509_OBJECT_dup( SSL_CTX_get_cert_store( mconf->SSLContext )->objs );
  197. if (mconf->system_certs && !SSL_CTX_set_default_verify_paths( mconf->SSLContext ))
  198. warn( "Warning: Unable to load default certificate files: %s\n",
  199. ERR_error_string( ERR_get_error(), 0 ) );
  200. SSL_CTX_set_verify( mconf->SSLContext, SSL_VERIFY_NONE, NULL );
  201. mconf->ssl_ctx_valid = 1;
  202. return 1;
  203. }
  204. static void start_tls_p2( conn_t * );
  205. static void start_tls_p3( conn_t *, int );
  206. static void ssl_fake_cb( void * );
  207. void
  208. socket_start_tls( conn_t *conn, void (*cb)( int ok, void *aux ) )
  209. {
  210. static int ssl_inited;
  211. conn->callbacks.starttls = cb;
  212. if (!ssl_inited) {
  213. SSL_library_init();
  214. SSL_load_error_strings();
  215. ssl_inited = 1;
  216. }
  217. if (!init_ssl_ctx( conn->conf )) {
  218. start_tls_p3( conn, 0 );
  219. return;
  220. }
  221. init_wakeup( &conn->ssl_fake, ssl_fake_cb, conn );
  222. conn->ssl = SSL_new( ((server_conf_t *)conn->conf)->SSLContext );
  223. SSL_set_fd( conn->ssl, conn->fd );
  224. SSL_set_mode( conn->ssl, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER );
  225. conn->state = SCK_STARTTLS;
  226. start_tls_p2( conn );
  227. }
  228. static void
  229. start_tls_p2( conn_t *conn )
  230. {
  231. if (ssl_return( "connect to", conn, SSL_connect( conn->ssl ) ) > 0) {
  232. if (verify_cert_host( conn->conf, conn )) {
  233. start_tls_p3( conn, 0 );
  234. } else {
  235. info( "Connection is now encrypted\n" );
  236. start_tls_p3( conn, 1 );
  237. }
  238. }
  239. }
  240. static void start_tls_p3( conn_t *conn, int ok )
  241. {
  242. conn->state = SCK_READY;
  243. conn->callbacks.starttls( ok, conn->callback_aux );
  244. }
  245. #endif /* HAVE_LIBSSL */
  246. static void socket_fd_cb( int, void * );
  247. static void socket_connect_one( conn_t * );
  248. static void socket_connect_failed( conn_t * );
  249. static void socket_connected( conn_t * );
  250. static void socket_connect_bail( conn_t * );
  251. static void
  252. socket_open_internal( conn_t *sock, int fd )
  253. {
  254. sock->fd = fd;
  255. fcntl( fd, F_SETFL, O_NONBLOCK );
  256. init_notifier( &sock->notify, fd, socket_fd_cb, sock );
  257. }
  258. static void
  259. socket_close_internal( conn_t *sock )
  260. {
  261. wipe_notifier( &sock->notify );
  262. close( sock->fd );
  263. sock->fd = -1;
  264. }
  265. void
  266. socket_connect( conn_t *sock, void (*cb)( int ok, void *aux ) )
  267. {
  268. const server_conf_t *conf = sock->conf;
  269. sock->callbacks.connect = cb;
  270. /* open connection to server */
  271. if (conf->tunnel) {
  272. int a[2];
  273. nfasprintf( &sock->name, "tunnel '%s'", conf->tunnel );
  274. infon( "Starting %s... ", sock->name );
  275. if (socketpair( PF_UNIX, SOCK_STREAM, 0, a )) {
  276. perror( "socketpair" );
  277. exit( 1 );
  278. }
  279. if (fork() == 0) {
  280. if (dup2( a[0], 0 ) == -1 || dup2( a[0], 1 ) == -1)
  281. _exit( 127 );
  282. close( a[0] );
  283. close( a[1] );
  284. execl( "/bin/sh", "sh", "-c", conf->tunnel, (char *)0 );
  285. _exit( 127 );
  286. }
  287. close( a[0] );
  288. socket_open_internal( sock, a[1] );
  289. info( "\vok\n" );
  290. socket_connected( sock );
  291. } else {
  292. #ifdef HAVE_IPV6
  293. int gaierr;
  294. struct addrinfo hints;
  295. memset( &hints, 0, sizeof(hints) );
  296. hints.ai_family = AF_UNSPEC;
  297. hints.ai_socktype = SOCK_STREAM;
  298. hints.ai_flags = AI_ADDRCONFIG;
  299. infon( "Resolving %s... ", conf->host );
  300. if ((gaierr = getaddrinfo( conf->host, NULL, &hints, &sock->addrs ))) {
  301. error( "Error: Cannot resolve server '%s': %s\n", conf->host, gai_strerror( gaierr ) );
  302. socket_connect_bail( sock );
  303. return;
  304. }
  305. info( "\vok\n" );
  306. sock->curr_addr = sock->addrs;
  307. #else
  308. struct hostent *he;
  309. infon( "Resolving %s... ", conf->host );
  310. he = gethostbyname( conf->host );
  311. if (!he) {
  312. error( "Error: Cannot resolve server '%s': %s\n", conf->host, hstrerror( h_errno ) );
  313. socket_connect_bail( sock );
  314. return;
  315. }
  316. info( "\vok\n" );
  317. sock->curr_addr = he->h_addr_list;
  318. #endif
  319. socket_connect_one( sock );
  320. }
  321. }
  322. static void
  323. socket_connect_one( conn_t *sock )
  324. {
  325. int s;
  326. #ifdef HAVE_IPV6
  327. struct addrinfo *ai;
  328. #else
  329. struct {
  330. struct sockaddr_in ai_addr[1];
  331. } ai[1];
  332. #endif
  333. #ifdef HAVE_IPV6
  334. if (!(ai = sock->curr_addr)) {
  335. #else
  336. if (!*sock->curr_addr) {
  337. #endif
  338. error( "No working address found for %s\n", sock->conf->host );
  339. socket_connect_bail( sock );
  340. return;
  341. }
  342. #ifdef HAVE_IPV6
  343. if (ai->ai_family == AF_INET6) {
  344. struct sockaddr_in6 *in6 = ((struct sockaddr_in6 *)ai->ai_addr);
  345. char sockname[64];
  346. in6->sin6_port = htons( sock->conf->port );
  347. nfasprintf( &sock->name, "%s ([%s]:%hu)",
  348. sock->conf->host, inet_ntop( AF_INET6, &in6->sin6_addr, sockname, sizeof(sockname) ), sock->conf->port );
  349. } else
  350. #endif
  351. {
  352. struct sockaddr_in *in = ((struct sockaddr_in *)ai->ai_addr);
  353. #ifndef HAVE_IPV6
  354. memset( in, 0, sizeof(*in) );
  355. in->sin_family = AF_INET;
  356. in->sin_addr.s_addr = *((int *)*sock->curr_addr);
  357. #endif
  358. in->sin_port = htons( sock->conf->port );
  359. nfasprintf( &sock->name, "%s (%s:%hu)",
  360. sock->conf->host, inet_ntoa( in->sin_addr ), sock->conf->port );
  361. }
  362. #ifdef HAVE_IPV6
  363. s = socket( ai->ai_family, SOCK_STREAM, 0 );
  364. #else
  365. s = socket( PF_INET, SOCK_STREAM, 0 );
  366. #endif
  367. if (s < 0) {
  368. perror( "socket" );
  369. exit( 1 );
  370. }
  371. socket_open_internal( sock, s );
  372. infon( "Connecting to %s... ", sock->name );
  373. #ifdef HAVE_IPV6
  374. if (connect( s, ai->ai_addr, ai->ai_addrlen )) {
  375. #else
  376. if (connect( s, ai->ai_addr, sizeof(*ai->ai_addr) )) {
  377. #endif
  378. if (errno != EINPROGRESS) {
  379. socket_connect_failed( sock );
  380. return;
  381. }
  382. conf_notifier( &sock->notify, 0, POLLOUT );
  383. sock->state = SCK_CONNECTING;
  384. info( "\v\n" );
  385. return;
  386. }
  387. info( "\vok\n" );
  388. socket_connected( sock );
  389. }
  390. static void
  391. socket_connect_failed( conn_t *conn )
  392. {
  393. sys_error( "Cannot connect to %s", conn->name );
  394. socket_close_internal( conn );
  395. free( conn->name );
  396. conn->name = 0;
  397. #ifdef HAVE_IPV6
  398. conn->curr_addr = conn->curr_addr->ai_next;
  399. #else
  400. conn->curr_addr++;
  401. #endif
  402. socket_connect_one( conn );
  403. }
  404. static void
  405. socket_connected( conn_t *conn )
  406. {
  407. #ifdef HAVE_IPV6
  408. freeaddrinfo( conn->addrs );
  409. #endif
  410. conf_notifier( &conn->notify, 0, POLLIN );
  411. conn->state = SCK_READY;
  412. conn->callbacks.connect( 1, conn->callback_aux );
  413. }
  414. static void
  415. socket_connect_bail( conn_t *conn )
  416. {
  417. #ifdef HAVE_IPV6
  418. freeaddrinfo( conn->addrs );
  419. #endif
  420. free( conn->name );
  421. conn->name = 0;
  422. conn->callbacks.connect( 0, conn->callback_aux );
  423. }
  424. static void dispose_chunk( conn_t *conn );
  425. void
  426. socket_close( conn_t *sock )
  427. {
  428. if (sock->fd >= 0)
  429. socket_close_internal( sock );
  430. free( sock->name );
  431. sock->name = 0;
  432. #ifdef HAVE_LIBSSL
  433. if (sock->ssl) {
  434. SSL_free( sock->ssl );
  435. sock->ssl = 0;
  436. wipe_wakeup( &sock->ssl_fake );
  437. }
  438. #endif
  439. while (sock->write_buf)
  440. dispose_chunk( sock );
  441. }
  442. static void
  443. socket_fill( conn_t *sock )
  444. {
  445. char *buf;
  446. int n = sock->offset + sock->bytes;
  447. int len = sizeof(sock->buf) - n;
  448. if (!len) {
  449. error( "Socket error: receive buffer full. Probably protocol error.\n" );
  450. socket_fail( sock );
  451. return;
  452. }
  453. assert( sock->fd >= 0 );
  454. buf = sock->buf + n;
  455. #ifdef HAVE_LIBSSL
  456. if (sock->ssl) {
  457. if ((n = ssl_return( "read from", sock, SSL_read( sock->ssl, buf, len ) )) <= 0)
  458. return;
  459. if (n == len && SSL_pending( sock->ssl ))
  460. conf_wakeup( &sock->ssl_fake, 0 );
  461. } else
  462. #endif
  463. {
  464. if ((n = read( sock->fd, buf, len )) < 0) {
  465. sys_error( "Socket error: read from %s", sock->name );
  466. socket_fail( sock );
  467. return;
  468. } else if (!n) {
  469. error( "Socket error: read from %s: unexpected EOF\n", sock->name );
  470. socket_fail( sock );
  471. return;
  472. }
  473. }
  474. sock->bytes += n;
  475. sock->read_callback( sock->callback_aux );
  476. }
  477. int
  478. socket_read( conn_t *conn, char *buf, int len )
  479. {
  480. int n = conn->bytes;
  481. if (n > len)
  482. n = len;
  483. memcpy( buf, conn->buf + conn->offset, n );
  484. if (!(conn->bytes -= n))
  485. conn->offset = 0;
  486. else
  487. conn->offset += n;
  488. return n;
  489. }
  490. char *
  491. socket_read_line( conn_t *b )
  492. {
  493. char *p, *s;
  494. int n;
  495. s = b->buf + b->offset;
  496. p = memchr( s + b->scanoff, '\n', b->bytes - b->scanoff );
  497. if (!p) {
  498. b->scanoff = b->bytes;
  499. if (b->offset + b->bytes == sizeof(b->buf)) {
  500. memmove( b->buf, b->buf + b->offset, b->bytes );
  501. b->offset = 0;
  502. }
  503. return 0;
  504. }
  505. n = p + 1 - s;
  506. b->offset += n;
  507. b->bytes -= n;
  508. b->scanoff = 0;
  509. if (p != s && p[-1] == '\r')
  510. p--;
  511. *p = 0;
  512. return s;
  513. }
  514. static int
  515. do_write( conn_t *sock, char *buf, int len )
  516. {
  517. int n;
  518. assert( sock->fd >= 0 );
  519. #ifdef HAVE_LIBSSL
  520. if (sock->ssl)
  521. return ssl_return( "write to", sock, SSL_write( sock->ssl, buf, len ) );
  522. #endif
  523. n = write( sock->fd, buf, len );
  524. if (n < 0) {
  525. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  526. sys_error( "Socket error: write to %s", sock->name );
  527. socket_fail( sock );
  528. } else {
  529. n = 0;
  530. conf_notifier( &sock->notify, POLLIN, POLLOUT );
  531. }
  532. } else if (n != len) {
  533. conf_notifier( &sock->notify, POLLIN, POLLOUT );
  534. }
  535. return n;
  536. }
  537. static void
  538. dispose_chunk( conn_t *conn )
  539. {
  540. buff_chunk_t *bc = conn->write_buf;
  541. if (!(conn->write_buf = bc->next))
  542. conn->write_buf_append = &conn->write_buf;
  543. if (bc->data != bc->buf)
  544. free( bc->data );
  545. free( bc );
  546. }
  547. static int
  548. do_queued_write( conn_t *conn )
  549. {
  550. buff_chunk_t *bc;
  551. if (!conn->write_buf)
  552. return 0;
  553. while ((bc = conn->write_buf)) {
  554. int n, len = bc->len - conn->write_offset;
  555. if ((n = do_write( conn, bc->data + conn->write_offset, len )) < 0)
  556. return -1;
  557. if (n != len) {
  558. conn->write_offset += n;
  559. return 0;
  560. }
  561. conn->write_offset = 0;
  562. dispose_chunk( conn );
  563. }
  564. #ifdef HAVE_LIBSSL
  565. if (conn->ssl && SSL_pending( conn->ssl ))
  566. conf_wakeup( &conn->ssl_fake, 0 );
  567. #endif
  568. return conn->write_callback( conn->callback_aux );
  569. }
  570. static void
  571. do_append( conn_t *conn, char *buf, int len, ownership_t takeOwn )
  572. {
  573. buff_chunk_t *bc;
  574. if (takeOwn == GiveOwn) {
  575. bc = nfmalloc( offsetof(buff_chunk_t, buf) );
  576. bc->data = buf;
  577. } else {
  578. bc = nfmalloc( offsetof(buff_chunk_t, buf) + len );
  579. bc->data = bc->buf;
  580. memcpy( bc->data, buf, len );
  581. }
  582. bc->len = len;
  583. bc->next = 0;
  584. *conn->write_buf_append = bc;
  585. conn->write_buf_append = &bc->next;
  586. }
  587. int
  588. socket_write( conn_t *conn, conn_iovec_t *iov, int iovcnt )
  589. {
  590. for (; iovcnt; iovcnt--, iov++) {
  591. if (conn->write_buf) {
  592. do_append( conn, iov->buf, iov->len, iov->takeOwn );
  593. } else {
  594. int n = do_write( conn, iov->buf, iov->len );
  595. if (n < 0) {
  596. do {
  597. if (iov->takeOwn == GiveOwn)
  598. free( iov->buf );
  599. iovcnt--, iov++;
  600. } while (iovcnt);
  601. return -1;
  602. }
  603. if (n != iov->len) {
  604. conn->write_offset = n;
  605. do_append( conn, iov->buf, iov->len, iov->takeOwn );
  606. } else if (iov->takeOwn == GiveOwn) {
  607. free( iov->buf );
  608. }
  609. }
  610. }
  611. return 0;
  612. }
  613. static void
  614. socket_fd_cb( int events, void *aux )
  615. {
  616. conn_t *conn = (conn_t *)aux;
  617. if ((events & POLLERR) || conn->state == SCK_CONNECTING) {
  618. int soerr;
  619. socklen_t selen = sizeof(soerr);
  620. if (getsockopt( conn->fd, SOL_SOCKET, SO_ERROR, &soerr, &selen )) {
  621. perror( "getsockopt" );
  622. exit( 1 );
  623. }
  624. errno = soerr;
  625. if (conn->state == SCK_CONNECTING) {
  626. if (errno)
  627. socket_connect_failed( conn );
  628. else
  629. socket_connected( conn );
  630. return;
  631. }
  632. sys_error( "Socket error from %s", conn->name );
  633. socket_fail( conn );
  634. return;
  635. }
  636. if (events & POLLOUT)
  637. conf_notifier( &conn->notify, POLLIN, 0 );
  638. #ifdef HAVE_LIBSSL
  639. if (conn->state == SCK_STARTTLS) {
  640. start_tls_p2( conn );
  641. return;
  642. }
  643. if (conn->ssl) {
  644. if (do_queued_write( conn ) < 0)
  645. return;
  646. socket_fill( conn );
  647. return;
  648. }
  649. #endif
  650. if ((events & POLLOUT) && do_queued_write( conn ) < 0)
  651. return;
  652. if (events & POLLIN)
  653. socket_fill( conn );
  654. }
  655. #ifdef HAVE_LIBSSL
  656. static void
  657. ssl_fake_cb( void *aux )
  658. {
  659. conn_t *conn = (conn_t *)aux;
  660. socket_fill( conn );
  661. }
  662. #endif