Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
conn.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /* conn.c - implement Connection type
58  *
59  * This file implements the interface defined in conn.h.
60  *
61  * Richard Braakman
62  *
63  * SSL client implementation contributed by
64  * Jarkko Kovala <jarkko.kovala@iki.fi>
65  *
66  * SSL server implementation contributed by
67  * Stipe Tolj <stolj@wapme.de> for Wapme Systems AG
68  */
69 
70 /* TODO: unlocked_close() on error */
71 /* TODO: have I/O functions check if connection is open */
72 /* TODO: have conn_open_tcp do a non-blocking connect() */
73 
74 #include <signal.h>
75 #include <unistd.h>
76 #include <errno.h>
77 
78 #include <sys/types.h>
79 #include <sys/socket.h>
80 #include <string.h>
81 
82 #include "gwlib/gwlib.h"
83 
84 #ifdef HAVE_LIBSSL
85 #include <openssl/ssl.h>
86 #include <openssl/err.h>
87 
88 static SSL_CTX *global_ssl_context = NULL;
89 static SSL_CTX *global_server_ssl_context = NULL;
90 #endif /* HAVE_LIBSSL */
91 
92 typedef unsigned long (*CRYPTO_CALLBACK_PTR)(void);
93 
94 /*
95  * This used to be 4096. It is now 0 so that callers don't have to
96  * deal with the complexities of buffering (i.e. deciding when to
97  * flush) unless they want to.
98  * FIXME: Figure out how to combine buffering sensibly with use of
99  * conn_register.
100  */
101 #define DEFAULT_OUTPUT_BUFFERING 0
102 #define SSL_CONN_TIMEOUT 30
103 
105 {
106  /* We use separate locks for input and ouput fields, so that
107  * read and write activities don't have to get in each other's
108  * way. If you need both, then acquire the outlock first. */
111  volatile sig_atomic_t claimed;
112 #ifndef NO_GWASSERT
114 #endif
115 
116  /* fd value is read-only and is not locked */
117  int fd;
118 
119  /* socket state */
120  enum {yes,no} connected;
121 
122  /* Protected by outlock */
124  long outbufpos; /* start of unwritten data in outbuf */
125 
126  /* Try to buffer writes until there are this many octets to send.
127  * Set it to 0 to get an unbuffered connection. */
128  unsigned int output_buffering;
129 
130  /* Protected by inlock */
132  long inbufpos; /* start of unread data in inbuf */
133 
134  int read_eof; /* we encountered eof on read */
135  int io_error; /* we encountered error on IO operation */
136 
137  /* Protected by both locks when updating, so you need only one
138  * of the locks when reading. */
143  /* Protected by inlock */
145  /* Protected by outlock */
147 
148 #ifdef HAVE_LIBSSL
149  SSL *ssl;
150  X509 *peer_certificate;
151 #endif /* HAVE_LIBSSL */
152 };
153 
154 static void unlocked_register_pollin(Connection *conn, int onoff);
155 static void unlocked_register_pollout(Connection *conn, int onoff);
156 
157 /* There are a number of functions that play with POLLIN and POLLOUT flags.
158  * The general rule is that we always want to poll for POLLIN except when
159  * we have detected eof (which may be reported as eternal POLLIN), and
160  * we want to poll for POLLOUT only if there's data waiting in the
161  * output buffer. If output buffering is set, we may not want to poll for
162  * POLLOUT if there's not enough data waiting, which is why we have
163  * unlocked_try_write. */
164 
165 /* Macros to get more information for debugging purposes */
166 #define unlock_in(conn) unlock_in_real(conn, __FILE__, __LINE__, __func__)
167 #define unlock_out(conn) unlock_out_real(conn, __FILE__, __LINE__, __func__)
168 
169 /* Lock a Connection's read direction, if the Connection is unclaimed */
170 static void inline lock_in(Connection *conn)
171 {
172  gw_assert(conn != NULL);
173 
174  if (conn->claimed)
176  else
177  mutex_lock(conn->inlock);
178 }
179 
180 /* Unlock a Connection's read direction, if the Connection is unclaimed */
181 static void inline unlock_in_real(Connection *conn, char *file, int line, const char *func)
182 {
183  int ret;
184  gw_assert(conn != NULL);
185 
186  if (!conn->claimed && (ret = mutex_unlock(conn->inlock)) != 0) {
187  panic(0, "%s:%ld: %s: Mutex unlock failed. "
188  "(Called from %s:%ld:%s.)",
189  __FILE__, (long) __LINE__, __func__,
190  file, (long) line, func);
191  }
192 }
193 
194 /* Lock a Connection's write direction, if the Connection is unclaimed */
195 static void inline lock_out(Connection *conn)
196 {
197  gw_assert(conn != NULL);
198 
199  if (conn->claimed)
201  else
202  mutex_lock(conn->outlock);
203 }
204 
205 /* Unlock a Connection's write direction, if the Connection is unclaimed */
206 static void inline unlock_out_real(Connection *conn, char *file, int line, const char *func)
207 {
208  int ret;
209  gw_assert(conn != NULL);
210 
211  if (!conn->claimed && (ret = mutex_unlock(conn->outlock)) != 0) {
212  panic(0, "%s:%ld: %s: Mutex unlock failed. "
213  "(Called from %s:%ld:%s.)",
214  __FILE__, (long) __LINE__, __func__,
215  file, (long) line, func);
216  }
217 }
218 
219 /* Return the number of bytes in the Connection's output buffer */
220 static long inline unlocked_outbuf_len(Connection *conn)
221 {
222  return octstr_len(conn->outbuf) - conn->outbufpos;
223 }
224 
225 /* Return the number of bytes in the Connection's input buffer */
226 static long inline unlocked_inbuf_len(Connection *conn)
227 {
228  return octstr_len(conn->inbuf) - conn->inbufpos;
229 }
230 
231 /* Send as much data as can be sent without blocking. Return the number
232  * of bytes written, or -1 in case of error. */
233 static long unlocked_write(Connection *conn)
234 {
235  long ret = 0;
236 
237 #ifdef HAVE_LIBSSL
238  if (conn->ssl != NULL) {
239  if (octstr_len(conn->outbuf) - conn->outbufpos > 0)
240  ret = SSL_write(conn->ssl,
241  octstr_get_cstr(conn->outbuf) + conn->outbufpos,
242  octstr_len(conn->outbuf) - conn->outbufpos);
243 
244  if (ret < 0) {
245  int SSL_error = SSL_get_error(conn->ssl, ret);
246 
247  if (SSL_error == SSL_ERROR_WANT_READ || SSL_error == SSL_ERROR_WANT_WRITE) {
248  ret = 0; /* no error */
249  } else {
250  error(errno, "SSL write failed: OpenSSL error %d: %s",
251  SSL_error, ERR_error_string(SSL_error, NULL));
252  if (SSL_error == SSL_ERROR_SSL) { /* trace library errors */
253  long err;
254  while ((err = ERR_get_error()) != 0)
255  error(0, "SSL %s", ERR_error_string(err, NULL));
256  }
257  return -1;
258  }
259  }
260  } else
261 #endif /* HAVE_LIBSSL */
262  ret = octstr_write_data(conn->outbuf, conn->fd, conn->outbufpos);
263 
264  if (ret < 0) {
265  conn->io_error = 1;
266  return -1;
267  }
268 
269  conn->outbufpos += ret;
270 
271  /* Heuristic: Discard the already-written data if it's more than
272  * half of the total. This should keep the buffer size small
273  * without wasting too many cycles on moving data around. */
274  if (conn->outbufpos > octstr_len(conn->outbuf) / 2) {
275  octstr_delete(conn->outbuf, 0, conn->outbufpos);
276  conn->outbufpos = 0;
277  }
278 
279  if (conn->registered)
281 
282  return ret;
283 }
284 
285 /* Try to empty the output buffer without blocking. Return 0 for success,
286  * 1 if there is still data left in the buffer, and -1 for errors. */
288 {
289  long len;
290 
291  len = unlocked_outbuf_len(conn);
292  if (len == 0)
293  return 0;
294 
295  if (len < (long) conn->output_buffering)
296  return 1;
297 
298  if (unlocked_write(conn) < 0)
299  return -1;
300 
301  if (unlocked_outbuf_len(conn) > 0)
302  return 1;
303 
304  return 0;
305 }
306 
307 /* Read whatever data is currently available, up to an internal maximum. */
308 static void unlocked_read(Connection *conn)
309 {
310  unsigned char buf[4096];
311  long len;
312 
313  if (conn->inbufpos > 0) {
314  octstr_delete(conn->inbuf, 0, conn->inbufpos);
315  conn->inbufpos = 0;
316  }
317 
318 #ifdef HAVE_LIBSSL
319  if (conn->ssl != NULL) {
320  len = SSL_read(conn->ssl, buf, sizeof(buf));
321  } else
322 #endif /* HAVE_LIBSSL */
323  len = read(conn->fd, buf, sizeof(buf));
324 
325  if (len < 0) {
326  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
327  return;
328 #ifdef HAVE_LIBSSL
329  if (conn->ssl) {
330  int SSL_error = SSL_get_error(conn->ssl, len);
331  if (SSL_error == SSL_ERROR_WANT_WRITE || SSL_error == SSL_ERROR_WANT_READ)
332  return; /* no error */
333  error(errno, "SSL read failed: OpenSSL error %d: %s",
334  SSL_error, ERR_error_string(SSL_error, NULL));
335  }
336  else
337 #endif /* HAVE_LIBSSL */
338  error(errno, "Error reading from fd %d:", conn->fd);
339  conn->io_error = 1;
340  if (conn->registered)
341  unlocked_register_pollin(conn, 0);
342  return;
343  } else if (len == 0) {
344  conn->read_eof = 1;
345  if (conn->registered)
346  unlocked_register_pollin(conn, 0);
347  } else {
348  octstr_append_data(conn->inbuf, buf, len);
349  }
350 }
351 
352 /* Cut "length" octets from the input buffer and return them as an Octstr */
353 static Octstr *unlocked_get(Connection *conn, long length)
354 {
355  Octstr *result = NULL;
356 
357  gw_assert(unlocked_inbuf_len(conn) >= length);
358  result = octstr_copy(conn->inbuf, conn->inbufpos, length);
359  conn->inbufpos += length;
360 
361  return result;
362 }
363 
364 /* Tell the fdset whether we are interested in POLLIN events, but only
365  * if the status changed. (Calling fdset_listen can be expensive if
366  * it requires synchronization with the polling thread.)
367  * We must already have the inlock.
368  */
369 static void unlocked_register_pollin(Connection *conn, int onoff)
370 {
371  gw_assert(conn->registered);
372 
373  if (onoff == 1 && !conn->listening_pollin) {
374  /* Turn it on */
375  conn->listening_pollin = 1;
376  fdset_listen(conn->registered, conn->fd, POLLIN, POLLIN);
377  } else if (onoff == 0 && conn->listening_pollin) {
378  /* Turn it off */
379  conn->listening_pollin = 0;
380  fdset_listen(conn->registered, conn->fd, POLLIN, 0);
381  }
382 }
383 
384 /* Tell the fdset whether we are interested in POLLOUT events, but only
385  * if the status changed. (Calling fdset_listen can be expensive if
386  * it requires synchronization with the polling thread.)
387  * We must already have the outlock.
388  */
389 static void unlocked_register_pollout(Connection *conn, int onoff)
390 {
391  gw_assert(conn->registered);
392 
393  if (onoff == 1 && !conn->listening_pollout) {
394  /* Turn it on */
395  conn->listening_pollout = 1;
396  fdset_listen(conn->registered, conn->fd, POLLOUT, POLLOUT);
397  } else if (onoff == 0 && conn->listening_pollout) {
398  /* Turn it off */
399  conn->listening_pollout = 0;
400  fdset_listen(conn->registered, conn->fd, POLLOUT, 0);
401  }
402 }
403 
404 #ifdef HAVE_LIBSSL
405 static int conn_init_client_ssl(Connection *ret, Octstr *certkeyfile)
406 {
407  ret->ssl = SSL_new(global_ssl_context);
408 
409  /*
410  * The current thread's error queue must be empty before
411  * the TLS/SSL I/O operation is attempted, or SSL_get_error()
412  * will not work reliably.
413  */
414  ERR_clear_error();
415 
416  if (certkeyfile != NULL) {
417  SSL_use_certificate_file(ret->ssl, octstr_get_cstr(certkeyfile),
418  SSL_FILETYPE_PEM);
419  SSL_use_PrivateKey_file(ret->ssl, octstr_get_cstr(certkeyfile),
420  SSL_FILETYPE_PEM);
421  if (SSL_check_private_key(ret->ssl) != 1) {
422  error(0, "conn_open_ssl: private key isn't consistent with the "
423  "certificate from file %s (or failed reading the file)",
424  octstr_get_cstr(certkeyfile));
425  return -1;
426  }
427  }
428 
429  /* SSL_set_fd can fail, so check it */
430  if (SSL_set_fd(ret->ssl, ret->fd) == 0) {
431  /* SSL_set_fd failed, log error */
432  error(errno, "SSL: OpenSSL: %.256s", ERR_error_string(ERR_get_error(), NULL));
433  return -1;
434  }
435 
436  /*
437  * make sure the socket is non-blocking while we do SSL_connect
438  */
439  if (socket_set_blocking(ret->fd, 0) < 0) {
440  return -1;
441  }
442  BIO_set_nbio(SSL_get_rbio(ret->ssl), 1);
443  BIO_set_nbio(SSL_get_wbio(ret->ssl), 1);
444 
445  SSL_set_connect_state(ret->ssl);
446 
447  return 0;
448 }
449 
450 Connection *conn_open_ssl_nb(Octstr *host, int port, Octstr *certkeyfile,
451  Octstr *our_host)
452 {
453  Connection *ret;
454 
455  /* open the TCP connection */
456  if (!(ret = conn_open_tcp_nb(host, port, our_host))) {
457  return NULL;
458  }
459 
460  if (conn_init_client_ssl(ret, certkeyfile) == -1) {
461  conn_destroy(ret);
462  return NULL;
463  }
464 
465  return ret;
466 }
467 
468 Connection *conn_open_ssl(Octstr *host, int port, Octstr *certkeyfile,
469  Octstr *our_host)
470 {
471  Connection *ret;
472 
473  /* open the TCP connection */
474  if (!(ret = conn_open_tcp(host, port, our_host))) {
475  return NULL;
476  }
477 
478  if (conn_init_client_ssl(ret, certkeyfile) == -1) {
479  conn_destroy(ret);
480  return NULL;
481  }
482 
483  return ret;
484 }
485 
486 #endif /* HAVE_LIBSSL */
487 
488 Connection *conn_open_tcp(Octstr *host, int port, Octstr *our_host)
489 {
490  return conn_open_tcp_with_port(host, port, 0, our_host);
491 }
492 
493 Connection *conn_open_tcp_nb(Octstr *host, int port, Octstr *our_host)
494 {
495  return conn_open_tcp_nb_with_port(host, port, 0, our_host);
496 }
497 
499  Octstr *our_host)
500 {
501  int sockfd;
502  int done = -1;
503  Connection *c;
504 
506  our_port, our_host == NULL ?
507  NULL : octstr_get_cstr(our_host), &done);
508  if (sockfd < 0)
509  return NULL;
510  c = conn_wrap_fd(sockfd, 0);
511  if (done != 0) {
512  c->connected = no;
513  }
514  return c;
515 }
516 
518 {
519  return conn->connected == yes ? 0 : -1;
520 }
521 
523 {
524  int err;
525  socklen_t len;
526 
527  len = sizeof(err);
528  if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
529  return -1;
530  }
531 
532  if (err) {
533  return -1;
534  }
535 
536  conn->connected = yes;
537  return 0;
538 }
539 
541  Octstr *our_host)
542 {
543  int sockfd;
544 
546  our_port, our_host == NULL ?
547  NULL : octstr_get_cstr(our_host));
548  if (sockfd < 0)
549  return NULL;
550  return conn_wrap_fd(sockfd, 0);
551 }
552 
553 
554 /*
555  * XXX bad assumption here that conn_wrap_fd for SSL can only happens
556  * for the server side!!!! FIXME !!!!
557  */
559 {
560  Connection *conn;
561 
562  if (socket_set_blocking(fd, 0) < 0)
563  return NULL;
564 
565  conn = gw_malloc(sizeof(*conn));
566  conn->inlock = mutex_create();
567  conn->outlock = mutex_create();
568  conn->claimed = 0;
569 
570  conn->outbuf = octstr_create("");
571  conn->outbufpos = 0;
572  conn->inbuf = octstr_create("");
573  conn->inbufpos = 0;
574 
575  conn->fd = fd;
576  conn->connected = yes;
577  conn->read_eof = 0;
578  conn->io_error = 0;
580 
581  conn->registered = NULL;
582  conn->callback = NULL;
583  conn->callback_data = NULL;
584  conn->callback_data_destroyer = NULL;
585  conn->listening_pollin = 0;
586  conn->listening_pollout = 0;
587 #ifdef HAVE_LIBSSL
588  /*
589  * do all the SSL magic for this connection
590  */
591  if (ssl) {
592  conn->ssl = SSL_new(global_server_ssl_context);
593  conn->peer_certificate = NULL;
594 
595  /* SSL_set_fd can fail, so check it */
596  if (SSL_set_fd(conn->ssl, conn->fd) == 0) {
597  /* SSL_set_fd failed, log error and return NULL */
598  error(errno, "SSL: OpenSSL: %.256s", ERR_error_string(ERR_get_error(), NULL));
599  conn_destroy(conn);
600  return NULL;
601  }
602  /* SSL_set_verify(conn->ssl, 0, NULL); */
603 
604  /* set read/write BIO layer to non-blocking mode */
605  BIO_set_nbio(SSL_get_rbio(conn->ssl), 1);
606  BIO_set_nbio(SSL_get_wbio(conn->ssl), 1);
607 
608  /* set accept state , SSL-Handshake will be handled transparent while SSL_[read|write] */
609  SSL_set_accept_state(conn->ssl);
610  } else {
611  conn->ssl = NULL;
612  conn->peer_certificate = NULL;
613  }
614 #endif /* HAVE_LIBSSL */
615 
616  return conn;
617 }
618 
620 {
621  int ret;
622 
623  if (conn == NULL)
624  return;
625 
626  /* No locking done here. conn_destroy should not be called
627  * if any thread might still be interested in the connection. */
628 
629  if (conn->registered) {
630  fdset_unregister(conn->registered, conn->fd);
631  /* call data destroyer if any */
632  if (conn->callback_data != NULL && conn->callback_data_destroyer != NULL)
634  }
635 
636  if (conn->fd >= 0) {
637  /* Try to flush any remaining data */
638  unlocked_try_write(conn);
639 
640 #ifdef HAVE_LIBSSL
641  if (conn->ssl != NULL) {
642  SSL_smart_shutdown(conn->ssl);
643  SSL_free(conn->ssl);
644  if (conn->peer_certificate != NULL)
645  X509_free(conn->peer_certificate);
646  }
647 #endif /* HAVE_LIBSSL */
648 
649  ret = close(conn->fd);
650  if (ret < 0)
651  error(errno, "conn_destroy: error on close");
652  conn->fd = -1;
653  }
654 
655  octstr_destroy(conn->outbuf);
656  octstr_destroy(conn->inbuf);
657  mutex_destroy(conn->inlock);
658  mutex_destroy(conn->outlock);
659 
660  gw_free(conn);
661 }
662 
664 {
665  gw_assert(conn != NULL);
666 
667  if (conn->claimed)
668  panic(0, "Connection is being claimed twice!");
669  conn->claimed = 1;
670 #ifndef NO_GWASSERT
671  conn->claiming_thread = gwthread_self();
672 #endif
673 }
674 
676 {
677  long len;
678 
679  lock_out(conn);
680  len = unlocked_outbuf_len(conn);
681  unlock_out(conn);
682 
683  return len;
684 }
685 
687 {
688  long len;
689 
690  lock_in(conn);
691  len = unlocked_inbuf_len(conn);
692  unlock_in(conn);
693 
694  return len;
695 }
696 
698 {
699  int eof;
700 
701  lock_in(conn);
702  eof = conn->read_eof;
703  unlock_in(conn);
704 
705  return eof;
706 }
707 
709 {
710  int err;
711 
712  lock_out(conn);
713  lock_in(conn);
714  err = conn->io_error;
715  unlock_in(conn);
716  unlock_out(conn);
717 
718  return err;
719 }
720 
721 void conn_set_output_buffering(Connection *conn, unsigned int size)
722 {
723  lock_out(conn);
724  conn->output_buffering = size;
725  /* If the buffer size is smaller, we may have to write immediately. */
726  unlocked_try_write(conn);
727  unlock_out(conn);
728 }
729 
730 static void poll_callback(int fd, int revents, void *data)
731 {
732  Connection *conn;
733  int do_callback = 0;
734 
735  conn = data;
736 
737  if (conn == NULL) {
738  error(0, "poll_callback called with NULL connection.");
739  return;
740  }
741 
742  if (conn->fd != fd) {
743  error(0, "poll_callback called on wrong connection.");
744  return;
745  }
746 
747  /* Get result of nonblocking connect, before any reads and writes
748  * we must check result (it must be handled in initial callback) */
749  if (conn->connected == no) {
750  if (conn->callback)
751  conn->callback(conn, conn->callback_data);
752  return;
753  }
754 
755  /* If got POLLERR or POLHUP, then unregister the descriptor from the
756  * fdset and set the error condition variable to let the upper layer
757  * close and destroy the connection. */
758  if (revents & (POLLERR|POLLHUP)) {
759  lock_out(conn);
760  lock_in(conn);
761  if (conn->listening_pollin)
762  unlocked_register_pollin(conn, 0);
763  if (conn->listening_pollout)
764  unlocked_register_pollout(conn, 0);
765  conn->io_error = 1;
766  unlock_in(conn);
767  unlock_out(conn);
768  do_callback = 1;
769  }
770 
771  /* If unlocked_write manages to write all pending data, it will
772  * tell the fdset to stop listening for POLLOUT. */
773  if (revents & POLLOUT) {
774  lock_out(conn);
775  unlocked_write(conn);
776  if (unlocked_outbuf_len(conn) == 0)
777  do_callback = 1;
778  unlock_out(conn);
779  }
780 
781  /* We read only in unlocked_read in we received POLLIN, cause the
782  * descriptor is already broken and of no use anymore. */
783  if (revents & POLLIN) {
784  lock_in(conn);
785  unlocked_read(conn);
786  unlock_in(conn);
787  do_callback = 1;
788  }
789 
790  if (do_callback && conn->callback)
791  conn->callback(conn, conn->callback_data);
792 }
793 
795  conn_callback_t callback, void *data, conn_callback_data_destroyer_t *data_destroyer)
796 {
797  int events;
798  int result = 0;
799 
800  gw_assert(conn != NULL);
801 
802  if (conn->fd < 0)
803  return -1;
804 
805  /* We need both locks if we want to update the registration
806  * information. */
807  lock_out(conn);
808  lock_in(conn);
809 
810  if (conn->registered == fdset) {
811  /* Re-registering. Change only the callback info. */
812  conn->callback = callback;
813  /* call data destroyer if new data supplied */
814  if (conn->callback_data != NULL && conn->callback_data != data && conn->callback_data_destroyer != NULL)
816  conn->callback_data = data;
817  conn->callback_data_destroyer = data_destroyer;
818  result = 0;
819  } else if (conn->registered) {
820  /* Already registered to a different fdset. */
821  result = -1;
822  } else {
823  events = 0;
824  /* For nonconnected socket we must lesten both directions */
825  if (conn->connected == yes) {
826  if (conn->read_eof == 0 && conn->io_error == 0)
827  events |= POLLIN;
828  if (unlocked_outbuf_len(conn) > 0)
829  events |= POLLOUT;
830  } else {
831  events |= POLLIN | POLLOUT;
832  }
833 
834  conn->registered = fdset;
835  conn->callback = callback;
836  conn->callback_data = data;
837  conn->callback_data_destroyer = data_destroyer;
838  conn->listening_pollin = (events & POLLIN) != 0;
839  conn->listening_pollout = (events & POLLOUT) != 0;
840  fdset_register(fdset, conn->fd, events, poll_callback, conn);
841  result = 0;
842  }
843 
844  unlock_in(conn);
845  unlock_out(conn);
846 
847  return result;
848 }
849 
851 {
852  FDSet *set = NULL;
853  int fd = -1;
854  void *data = NULL;
855  conn_callback_data_destroyer_t *destroyer = NULL;
856 
857  gw_assert(conn != NULL);
858 
859  if (conn == NULL || conn->fd < 0)
860  return;
861 
862  /* We need both locks to update the registration information */
863  lock_out(conn);
864  lock_in(conn);
865 
866  if (conn->registered) {
867  set = conn->registered;
868  fd = conn->fd;
869  conn->registered = NULL;
870  conn->callback = NULL;
871  /*
872  * remember and don't destroy data and data_destroyer because we
873  * may be in callback right now. So destroy only after fdset_unregister
874  * call which guarantee us we are not in callback anymore.
875  */
876  data = conn->callback_data;
877  conn->callback_data = NULL;
878  destroyer = conn->callback_data_destroyer;
879  conn->callback_data_destroyer = NULL;
880  conn->listening_pollin = 0;
881  conn->listening_pollout = 0;
882  }
883 
884  unlock_in(conn);
885  unlock_out(conn);
886 
887  /* now unregister from FDSet */
888  if (set != NULL)
889  fdset_unregister(set, fd);
890 
891  /* ok we are not in callback anymore, destroy data if any */
892  if (data != NULL && destroyer != NULL)
893  destroyer(data);
894 }
895 
896 int conn_wait(Connection *conn, double seconds)
897 {
898  int events;
899  int ret;
900  int fd;
901 
902  lock_out(conn);
903 
904  /* Try to write any data that might still be waiting to be sent */
905  ret = unlocked_write(conn);
906  if (ret < 0) {
907  unlock_out(conn);
908  return -1;
909  }
910  if (ret > 0) {
911  /* We did something useful. No need to poll or wait now. */
912  unlock_out(conn);
913  return 0;
914  }
915 
916  fd = conn->fd;
917 
918  /* Normally, we block until there is more data available. But
919  * if any data still needs to be sent, we block until we can
920  * send it (or there is more data available). We always block
921  * for reading, unless we know there is no more data coming.
922  * (Because in that case, poll will keep reporting POLLIN to
923  * signal the end of the file). If the caller explicitly wants
924  * to wait even though there is no data to write and we're at
925  * end of file, then poll for new data anyway because the caller
926  * apparently doesn't trust eof. */
927  events = 0;
928  if (unlocked_outbuf_len(conn) > 0)
929  events |= POLLOUT;
930  /* Don't keep the connection locked while we wait */
931  unlock_out(conn);
932 
933  /* We need the in lock to query read_eof */
934  lock_in(conn);
935  if ((conn->read_eof == 0 && conn->io_error == 0) || events == 0)
936  events |= POLLIN;
937  unlock_in(conn);
938 
939  ret = gwthread_pollfd(fd, events, seconds);
940  if (ret < 0) {
941  if (errno == EINTR)
942  return 0;
943  error(0, "conn_wait: poll failed on fd %d:", fd);
944  return -1;
945  }
946 
947  if (ret == 0)
948  return 1;
949 
950  if (ret & POLLNVAL) {
951  error(0, "conn_wait: fd %d not open.", fd);
952  return -1;
953  }
954 
955  if (ret & (POLLERR | POLLHUP)) {
956  /* Call unlocked_read to report the specific error,
957  * and handle the results of the error. We can't be
958  * certain that the error still exists, because we
959  * released the lock for a while. */
960  lock_in(conn);
961  unlocked_read(conn);
962  unlock_in(conn);
963  return -1;
964  }
965 
966  /* If POLLOUT is on, then we must have wanted
967  * to write something. */
968  if (ret & POLLOUT) {
969  lock_out(conn);
970  unlocked_write(conn);
971  unlock_out(conn);
972  }
973 
974  /* Since we normally select for reading, we must
975  * try to read here. Otherwise, if the caller loops
976  * around conn_wait without making conn_read* calls
977  * in between, we will keep polling this same data. */
978  if (ret & POLLIN) {
979  lock_in(conn);
980  unlocked_read(conn);
981  unlock_in(conn);
982  }
983 
984  return 0;
985 }
986 
988 {
989  int ret;
990  int revents;
991  int fd;
992 
993  lock_out(conn);
994  ret = unlocked_write(conn);
995  if (ret < 0) {
996  unlock_out(conn);
997  return -1;
998  }
999 
1000  while (unlocked_outbuf_len(conn) != 0) {
1001  fd = conn->fd;
1002 
1003  unlock_out(conn);
1004  revents = gwthread_pollfd(fd, POLLOUT, -1.0);
1005 
1006  /* Note: Make sure we have the "out" lock when
1007  * going through the loop again, because the
1008  * loop condition needs it. */
1009 
1010  if (revents < 0) {
1011  if (errno == EINTR)
1012  return 1;
1013  error(0, "conn_flush: poll failed on fd %d:", fd);
1014  return -1;
1015  }
1016 
1017  if (revents == 0) {
1018  /* We were woken up */
1019  return 1;
1020  }
1021 
1022  if (revents & POLLNVAL) {
1023  error(0, "conn_flush: fd %d not open.", fd);
1024  return -1;
1025  }
1026 
1027  lock_out(conn);
1028 
1029  if (revents & (POLLOUT | POLLERR | POLLHUP)) {
1030  ret = unlocked_write(conn);
1031  if (ret < 0) {
1032  unlock_out(conn);
1033  return -1;
1034  }
1035  }
1036  }
1037 
1038  unlock_out(conn);
1039 
1040  return 0;
1041 }
1042 
1043 int conn_write(Connection *conn, Octstr *data)
1044 {
1045  int ret;
1046 
1047  lock_out(conn);
1048  octstr_append(conn->outbuf, data);
1049  ret = unlocked_try_write(conn);
1050  unlock_out(conn);
1051 
1052  return ret;
1053 }
1054 
1055 int conn_write_data(Connection *conn, unsigned char *data, long length)
1056 {
1057  int ret;
1058 
1059  lock_out(conn);
1060  octstr_append_data(conn->outbuf, data, length);
1061  ret = unlocked_try_write(conn);
1062  unlock_out(conn);
1063 
1064  return ret;
1065 }
1066 
1068 {
1069  int ret;
1070  unsigned char lengthbuf[4];
1071 
1072  encode_network_long(lengthbuf, octstr_len(data));
1073  lock_out(conn);
1074  octstr_append_data(conn->outbuf, lengthbuf, 4);
1075  octstr_append(conn->outbuf, data);
1076  ret = unlocked_try_write(conn);
1077  unlock_out(conn);
1078 
1079  return ret;
1080 }
1081 
1083 {
1084  Octstr *result = NULL;
1085 
1086  lock_in(conn);
1087  if (unlocked_inbuf_len(conn) == 0) {
1088  unlocked_read(conn);
1089  if (unlocked_inbuf_len(conn) == 0) {
1090  unlock_in(conn);
1091  return NULL;
1092  }
1093  }
1094 
1095  result = unlocked_get(conn, unlocked_inbuf_len(conn));
1096  gw_claim_area(result);
1097  unlock_in(conn);
1098 
1099  return result;
1100 }
1101 
1102 Octstr *conn_read_fixed(Connection *conn, long length)
1103 {
1104  Octstr *result = NULL;
1105 
1106  if (length < 1)
1107  return NULL;
1108 
1109  /* See if the data is already available. If not, try a read(),
1110  * then see if we have enough data after that. If not, give up. */
1111  lock_in(conn);
1112  if (unlocked_inbuf_len(conn) < length) {
1113  unlocked_read(conn);
1114  if (unlocked_inbuf_len(conn) < length) {
1115  unlock_in(conn);
1116  return NULL;
1117  }
1118  }
1119  result = unlocked_get(conn, length);
1120  gw_claim_area(result);
1121  unlock_in(conn);
1122 
1123  return result;
1124 }
1125 
1127 {
1128  Octstr *result = NULL;
1129  long pos;
1130 
1131  lock_in(conn);
1132  /* 10 is the code for linefeed. We don't rely on \n because that
1133  * might be a different value on some (strange) systems, and
1134  * we are reading from a network connection. */
1135  pos = octstr_search_char(conn->inbuf, 10, conn->inbufpos);
1136  if (pos < 0) {
1137  unlocked_read(conn);
1138  pos = octstr_search_char(conn->inbuf, 10, conn->inbufpos);
1139  if (pos < 0) {
1140  unlock_in(conn);
1141  return NULL;
1142  }
1143  }
1144 
1145  result = unlocked_get(conn, pos - conn->inbufpos);
1146  gw_claim_area(result);
1147 
1148  /* Skip the LF, which we left in the buffer */
1149  conn->inbufpos++;
1150 
1151  /* If the line was terminated with CR LF, we have to remove
1152  * the CR from the result. */
1153  if (octstr_len(result) > 0 &&
1154  octstr_get_char(result, octstr_len(result) - 1) == 13)
1155  octstr_delete(result, octstr_len(result) - 1, 1);
1156 
1157  unlock_in(conn);
1158  return result;
1159 }
1160 
1162 {
1163  Octstr *result = NULL;
1164  unsigned char lengthbuf[4];
1165  long length = 0; /* for compiler please */
1166  int try, retry;
1167 
1168  lock_in(conn);
1169 
1170  for (try = 1; try <= 2; try++) {
1171  if (try > 1)
1172  unlocked_read(conn);
1173 
1174  do {
1175  retry = 0;
1176  /* First get the length. */
1177  if (unlocked_inbuf_len(conn) < 4)
1178  continue;
1179 
1180  octstr_get_many_chars(lengthbuf, conn->inbuf, conn->inbufpos, 4);
1181  length = decode_network_long(lengthbuf);
1182 
1183  if (length < 0) {
1184  warning(0, "conn_read_withlen: got negative length, skipping");
1185  conn->inbufpos += 4;
1186  retry = 1;
1187  }
1188  } while(retry == 1);
1189 
1190  /* Then get the data. */
1191  if (unlocked_inbuf_len(conn) - 4 < length)
1192  continue;
1193 
1194  conn->inbufpos += 4;
1195  result = unlocked_get(conn, length);
1196  gw_claim_area(result);
1197  break;
1198  }
1199 
1200  unlock_in(conn);
1201  return result;
1202 }
1203 
1205 {
1206  int startpos, endpos;
1207  Octstr *result = NULL;
1208  int try;
1209 
1210  lock_in(conn);
1211 
1212  for (try = 1; try <= 2; try++) {
1213  if (try > 1)
1214  unlocked_read(conn);
1215 
1216  /* Find startmark, and discard everything up to it */
1217  if (startmark >= 0) {
1218  startpos = octstr_search_char(conn->inbuf, startmark, conn->inbufpos);
1219  if (startpos < 0) {
1220  conn->inbufpos = octstr_len(conn->inbuf);
1221  continue;
1222  } else {
1223  conn->inbufpos = startpos;
1224  }
1225  } else {
1226  startpos = conn->inbufpos;
1227  }
1228 
1229  /* Find first endmark after startmark */
1230  endpos = octstr_search_char(conn->inbuf, endmark, conn->inbufpos);
1231  if (endpos < 0)
1232  continue;
1233 
1234  result = unlocked_get(conn, endpos - startpos + 1);
1235  gw_claim_area(result);
1236  break;
1237  }
1238 
1239  unlock_in(conn);
1240  return result;
1241 }
1242 
1243 #ifdef HAVE_LIBSSL
1244 X509 *conn_get_peer_certificate(Connection *conn)
1245 {
1246  /* Don't know if it needed to be locked , but better safe as crash */
1247  lock_out(conn);
1248  lock_in(conn);
1249  if (conn->peer_certificate == NULL && conn->ssl != NULL)
1250  conn->peer_certificate = SSL_get_peer_certificate(conn->ssl);
1251  unlock_in(conn);
1252  unlock_out(conn);
1253 
1254  return conn->peer_certificate;
1255 }
1256 
1257 /*
1258  * XXX Alex decalred the RSA callback routine static and now we're getting
1259  * warning messages for our automatic compilation tests. So we are commenting
1260  * the function out to avoid the warnings.
1261  *
1262 
1263 static RSA *tmp_rsa_callback(SSL *ssl, int export, int key_len)
1264 {
1265  static RSA *rsa = NULL;
1266  debug("gwlib.http", 0, "SSL: Generating new RSA key (export=%d, keylen=%d)", export, key_len);
1267  if (export) {
1268  rsa = RSA_generate_key(key_len, RSA_F4, NULL, NULL);
1269  } else {
1270  debug("gwlib.http", 0, "SSL: Export not set");
1271  }
1272  return rsa;
1273 }
1274 */
1275 
1276 static Mutex **ssl_static_locks = NULL;
1277 
1278 /* the call-back function for the openssl crypto thread locking */
1279 static void openssl_locking_function(int mode, int n, const char *file, int line)
1280 {
1281  if (mode & CRYPTO_LOCK)
1282  mutex_lock(ssl_static_locks[n-1]);
1283  else
1284  mutex_unlock(ssl_static_locks[n-1]);
1285 }
1286 
1287 void openssl_init_locks(void)
1288 {
1289  int c, maxlocks = CRYPTO_num_locks();
1290 
1291  gw_assert(ssl_static_locks == NULL);
1292 
1293  ssl_static_locks = gw_malloc(sizeof(Mutex *) * maxlocks);
1294  for (c = 0; c < maxlocks; c++)
1295  ssl_static_locks[c] = mutex_create();
1296 
1297  /* after the mutexes have been created, apply the call-back to it */
1298  CRYPTO_set_locking_callback(openssl_locking_function);
1299  CRYPTO_set_id_callback((CRYPTO_CALLBACK_PTR)gwthread_self);
1300 }
1301 
1302 void openssl_shutdown_locks(void)
1303 {
1304  int c, maxlocks = CRYPTO_num_locks();
1305 
1306  gw_assert(ssl_static_locks != NULL);
1307 
1308  /* remove call-back from the locks */
1309  CRYPTO_set_locking_callback(NULL);
1310 
1311  for (c = 0; c < maxlocks; c++)
1312  mutex_destroy(ssl_static_locks[c]);
1313 
1314  gw_free(ssl_static_locks);
1315  ssl_static_locks = NULL;
1316 }
1317 
1318 void conn_init_ssl(void)
1319 {
1320  SSL_library_init();
1321  SSL_load_error_strings();
1322  global_ssl_context = SSL_CTX_new(SSLv23_client_method());
1323  SSL_CTX_set_mode(global_ssl_context,
1324  SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
1325 }
1326 
1327 void server_ssl_init(void)
1328 {
1329  SSLeay_add_ssl_algorithms();
1330  SSL_load_error_strings();
1331  global_server_ssl_context = SSL_CTX_new(SSLv23_server_method());
1332  SSL_CTX_set_mode(global_server_ssl_context,
1333  SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
1334  if (!SSL_CTX_set_default_verify_paths(global_server_ssl_context)) {
1335  panic(0, "can not set default path for server");
1336  }
1337 }
1338 
1339 void conn_shutdown_ssl(void)
1340 {
1341  if (global_ssl_context)
1342  SSL_CTX_free(global_ssl_context);
1343 
1344  ERR_free_strings();
1345  EVP_cleanup();
1346 }
1347 
1348 void server_shutdown_ssl(void)
1349 {
1350  if (global_server_ssl_context)
1351  SSL_CTX_free(global_server_ssl_context);
1352 
1353  ERR_free_strings();
1354  EVP_cleanup();
1355 }
1356 
1357 void conn_use_global_client_certkey_file(Octstr *certkeyfile)
1358 {
1359  SSL_CTX_use_certificate_chain_file(global_ssl_context,
1360  octstr_get_cstr(certkeyfile));
1361  SSL_CTX_use_PrivateKey_file(global_ssl_context,
1362  octstr_get_cstr(certkeyfile),
1363  SSL_FILETYPE_PEM);
1364  if (SSL_CTX_check_private_key(global_ssl_context) != 1)
1365  panic(0, "reading global client certificate file `%s', the certificate "
1366  "isn't consistent with the private key (or failed reading the file)",
1367  octstr_get_cstr(certkeyfile));
1368  info(0, "Using global SSL certificate and key from file `%s'",
1369  octstr_get_cstr(certkeyfile));
1370 }
1371 
1372 void conn_use_global_server_certkey_file(Octstr *certfile, Octstr *keyfile)
1373 {
1374  SSL_CTX_use_certificate_chain_file(global_server_ssl_context,
1375  octstr_get_cstr(certfile));
1376  SSL_CTX_use_PrivateKey_file(global_server_ssl_context,
1377  octstr_get_cstr(keyfile),
1378  SSL_FILETYPE_PEM);
1379  if (SSL_CTX_check_private_key(global_server_ssl_context) != 1) {
1380  error(0, "SSL: %s", ERR_error_string(ERR_get_error(), NULL));
1381  panic(0, "reading global server certificate file %s, the certificate \
1382  isn't consistent with the private key in file %s \
1383  (or failed reading the file)",
1384  octstr_get_cstr(certfile), octstr_get_cstr(keyfile));
1385  }
1386  info(0, "Using global server SSL certificate from file `%s'", octstr_get_cstr(certfile));
1387  info(0, "Using global server SSL key from file `%s'", octstr_get_cstr(keyfile));
1388 }
1389 
1390 static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
1391 {
1392  char subject[256];
1393  char issuer [256];
1394  char *status;
1395 
1396  X509_NAME_oneline(X509_get_subject_name(ctx->current_cert), subject, sizeof(subject));
1397  X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), issuer, sizeof (issuer));
1398 
1399  status = preverify_ok ? "Accepting" : "Rejecting";
1400 
1401  info(0, "%s certificate for \"%s\" signed by \"%s\"", status, subject, issuer);
1402 
1403  return preverify_ok;
1404 }
1405 
1406 void conn_use_global_trusted_ca_file(Octstr *ssl_trusted_ca_file)
1407 {
1408  if (ssl_trusted_ca_file != NULL) {
1409  if (!SSL_CTX_load_verify_locations(global_ssl_context,
1410  octstr_get_cstr(ssl_trusted_ca_file),
1411  NULL)) {
1412  panic(0, "Failed to load SSL CA file: %s", octstr_get_cstr(ssl_trusted_ca_file));
1413  } else {
1414  info(0, "Using CA root certificates from file %s",
1415  octstr_get_cstr(ssl_trusted_ca_file));
1416  SSL_CTX_set_verify(global_ssl_context,
1417  SSL_VERIFY_PEER,
1418  verify_callback);
1419  }
1420 
1421  } else {
1422  SSL_CTX_set_verify(global_ssl_context,
1423  SSL_VERIFY_NONE,
1424  NULL);
1425  }
1426 }
1427 
1428 void conn_config_ssl (CfgGroup *grp)
1429 {
1431  Octstr *ssl_server_cert_file = NULL;
1432  Octstr *ssl_server_key_file = NULL;
1433  Octstr *ssl_trusted_ca_file = NULL;
1434 
1435  /*
1436  * check if SSL is desired for HTTP servers and then
1437  * load SSL client and SSL server public certificates
1438  * and private keys
1439  */
1440  ssl_client_certkey_file = cfg_get(grp, octstr_imm("ssl-client-certkey-file"));
1441  if (ssl_client_certkey_file != NULL)
1442  conn_use_global_client_certkey_file(ssl_client_certkey_file);
1443 
1444  ssl_server_cert_file = cfg_get(grp, octstr_imm("ssl-server-cert-file"));
1445  ssl_server_key_file = cfg_get(grp, octstr_imm("ssl-server-key-file"));
1446 
1447  if (ssl_server_cert_file != NULL && ssl_server_key_file != NULL) {
1448  conn_use_global_server_certkey_file(ssl_server_cert_file,
1449  ssl_server_key_file);
1450  }
1451 
1452  ssl_trusted_ca_file = cfg_get(grp, octstr_imm("ssl-trusted-ca-file"));
1453 
1454  conn_use_global_trusted_ca_file(ssl_trusted_ca_file);
1455 
1456  octstr_destroy(ssl_client_certkey_file);
1457  octstr_destroy(ssl_server_cert_file);
1458  octstr_destroy(ssl_server_key_file);
1459  octstr_destroy(ssl_trusted_ca_file);
1460 }
1461 
1462 SSL *conn_get_ssl(Connection *conn)
1463 {
1464  if (conn != NULL)
1465  return conn->ssl;
1466  else
1467  return NULL;
1468 }
1469 
1470 #else
1471 
1473 {
1474  info(0, "SSL not supported, no SSL initialization done.");
1475 }
1476 #endif /* HAVE_LIBSSL */
1477 
1479  if(conn == NULL)
1480  return 0;
1481  else
1482  return conn->fd;
1483 }
Octstr * conn_read_line(Connection *conn)
Definition: conn.c:1126
int listening_pollout
Definition: conn.c:146
void error(int err, const char *fmt,...)
Definition: log.c:612
void info(int err, const char *fmt,...)
Definition: log.c:636
static long our_port
Definition: radius_acct.c:87
int socket_set_blocking(int fd, int blocking)
Definition: socket.c:368
Definition: http.c:1998
int size
Definition: wsasm.c:84
Connection * conn_open_tcp(Octstr *host, int port, Octstr *our_host)
Definition: conn.c:488
long gwthread_self(void)
void octstr_append_data(Octstr *ostr, const char *data, long len)
Definition: octstr.c:1495
static int unlocked_try_write(Connection *conn)
Definition: conn.c:287
void fdset_listen(FDSet *set, int fd, int mask, int events)
Definition: fdset.c:470
conn_callback_t * callback
Definition: conn.c:140
int ssl
Mutex * outlock
Definition: conn.c:110
static void startmark(unsigned char *p, long number)
Definition: gwmem-check.c:263
#define mutex_unlock(m)
Definition: thread.h:136
void encode_network_long(unsigned char *data, unsigned long value)
Definition: utils.c:940
FDSet * registered
Definition: conn.c:139
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1502
unsigned int output_buffering
Definition: conn.c:128
int read_eof
Definition: conn.c:134
int conn_is_connected(Connection *conn)
Definition: conn.c:517
static void unlocked_register_pollin(Connection *conn, int onoff)
Definition: conn.c:369
#define mutex_create()
Definition: thread.h:96
static void unlocked_register_pollout(Connection *conn, int onoff)
Definition: conn.c:389
Connection * conn_open_tcp_nb(Octstr *host, int port, Octstr *our_host)
Definition: conn.c:493
int listening_pollin
Definition: conn.c:144
static void endmark(unsigned char *p, size_t size)
Definition: gwmem-check.c:255
static Octstr * host
Definition: fakesmsc.c:121
#define cfg_get(grp, varname)
Definition: cfg.h:86
static long unlocked_outbuf_len(Connection *conn)
Definition: conn.c:220
int conn_write_data(Connection *conn, unsigned char *data, long length)
Definition: conn.c:1055
#define POLLNVAL
Definition: gwpoll.h:98
long outbufpos
Definition: conn.c:124
void * callback_data
Definition: conn.c:141
long conn_inbuf_len(Connection *conn)
Definition: conn.c:686
Octstr * outbuf
Definition: conn.c:123
void fdset_unregister(FDSet *set, int fd)
Definition: fdset.c:510
int io_error
Definition: conn.c:135
long claiming_thread
Definition: conn.c:113
int conn_eof(Connection *conn)
Definition: conn.c:697
static void lock_out(Connection *conn)
Definition: conn.c:195
volatile sig_atomic_t claimed
Definition: conn.c:111
conn_callback_data_destroyer_t * callback_data_destroyer
Definition: conn.c:142
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1010
void fdset_register(FDSet *set, int fd, int events, fdset_callback_t callback, void *data)
Definition: fdset.c:425
static Octstr * unlocked_get(Connection *conn, long length)
Definition: conn.c:353
#define DEFAULT_OUTPUT_BUFFERING
Definition: conn.c:101
FILE * file
Definition: log.c:133
static Octstr * our_host
Definition: radius_acct.c:86
Connection * conn_open_tcp_nb_with_port(Octstr *host, int port, int our_port, Octstr *our_host)
Definition: conn.c:498
void conn_callback_data_destroyer_t(void *data)
Definition: conn.h:113
static Octstr * ssl_client_certkey_file
Definition: test_http.c:83
void conn_claim(Connection *conn)
Definition: conn.c:663
#define POLLIN
Definition: gwpoll.h:91
void conn_set_output_buffering(Connection *conn, unsigned int size)
Definition: conn.c:721
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
int conn_write(Connection *conn, Octstr *data)
Definition: conn.c:1043
int conn_get_connect_result(Connection *conn)
Definition: conn.c:522
Octstr * conn_read_packet(Connection *conn, int startmark, int endmark)
Definition: conn.c:1204
int conn_get_id(Connection *conn)
Definition: conn.c:1478
void conn_config_ssl(CfgGroup *grp)
Definition: conn.c:1472
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1525
static void unlocked_read(Connection *conn)
Definition: conn.c:308
void conn_destroy(Connection *conn)
Definition: conn.c:619
#define unlock_in(conn)
Definition: conn.c:166
int conn_register_real(Connection *conn, FDSet *fdset, conn_callback_t callback, void *data, conn_callback_data_destroyer_t *data_destroyer)
Definition: conn.c:794
int fd
Definition: conn.c:117
Connection * conn_open_tcp_with_port(Octstr *host, int port, int our_port, Octstr *our_host)
Definition: conn.c:540
unsigned long(* CRYPTO_CALLBACK_PTR)(void)
Definition: conn.c:92
void warning(int err, const char *fmt,...)
Definition: log.c:624
long conn_outbuf_len(Connection *conn)
Definition: conn.c:675
int tcpip_connect_to_server_with_port(char *hostname, int port, int our_port, const char *source_addr)
Definition: socket.c:156
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define POLLERR
Definition: gwpoll.h:96
void conn_callback_t(Connection *conn, void *data)
Definition: conn.h:105
#define octstr_create(cstr)
Definition: octstr.h:125
gw_assert(wtls_machine->packet_to_send!=NULL)
void mutex_destroy(Mutex *mutex)
Definition: thread.c:97
Octstr * conn_read_withlen(Connection *conn)
Definition: conn.c:1161
int conn_write_withlen(Connection *conn, Octstr *data)
Definition: conn.c:1067
Mutex * inlock
Definition: conn.c:109
long octstr_write_data(Octstr *ostr, int fd, long from)
Definition: octstr.c:1253
int gwthread_pollfd(int fd, int events, double timeout)
int sockfd
Definition: test_cimd2.c:145
static void poll_callback(int fd, int revents, void *data)
Definition: conn.c:730
static void unlock_in_real(Connection *conn, char *file, int line, const char *func)
Definition: conn.c:181
int tcpip_connect_nb_to_server_with_port(char *hostname, int port, int our_port, const char *source_addr, int *done)
Definition: socket.c:246
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
void conn_unregister(Connection *conn)
Definition: conn.c:850
long decode_network_long(unsigned char *data)
Definition: utils.c:935
enum Connection::@56 connected
static long unlocked_inbuf_len(Connection *conn)
Definition: conn.c:226
Definition: octstr.c:118
int conn_wait(Connection *conn, double seconds)
Definition: conn.c:896
static void unlock_out_real(Connection *conn, char *file, int line, const char *func)
Definition: conn.c:206
#define panic
Definition: log.h:87
Definition: cfg.c:73
static void lock_in(Connection *conn)
Definition: conn.c:170
static long unlocked_write(Connection *conn)
Definition: conn.c:233
int socklen_t
Definition: socket.h:73
Definition: fdset.c:70
Definition: thread.h:76
Octstr * conn_read_fixed(Connection *conn, long length)
Definition: conn.c:1102
int conn_error(Connection *conn)
Definition: conn.c:708
void octstr_get_many_chars(char *buf, Octstr *ostr, long pos, long len)
Definition: octstr.c:423
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:404
#define mutex_lock(m)
Definition: thread.h:130
#define unlock_out(conn)
Definition: conn.c:167
Octstr * inbuf
Definition: conn.c:131
#define POLLHUP
Definition: gwpoll.h:97
Octstr * conn_read_everything(Connection *conn)
Definition: conn.c:1082
#define POLLOUT
Definition: gwpoll.h:93
Connection * conn_wrap_fd(int fd, int ssl)
Definition: conn.c:558
long inbufpos
Definition: conn.c:132
int conn_flush(Connection *conn)
Definition: conn.c:987
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.