Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

conn.h File Reference

#include <openssl/x509.h>
#include <openssl/ssl.h>

Include dependency graph for conn.h:

Include dependency graph

This graph shows which files directly or indirectly include this file:

Included by dependency graph

Go to the source code of this file.

Defines

#define conn_register(conn, fdset, callback, data)   conn_register_real(conn, fdset, callback, data, NULL)

Typedefs

typedef Connection Connection
typedef void conn_callback_t (Connection *conn, void *data)
typedef void conn_callback_data_destroyer_t (void *data)

Functions

void conn_config_ssl (CfgGroup *grp)
Connectionconn_open_tcp (Octstr *host, int port, Octstr *our_host)
Connectionconn_open_tcp_with_port (Octstr *host, int port, int our_port, Octstr *our_host)
Connectionconn_open_tcp_nb (Octstr *host, int port, Octstr *our_host)
Connectionconn_open_tcp_nb_with_port (Octstr *host, int port, int our_port, Octstr *our_host)
int conn_is_connected (Connection *conn)
int conn_get_connect_result (Connection *conn)
Connectionconn_wrap_fd (int fd, int ssl)
void conn_destroy (Connection *conn)
void conn_claim (Connection *conn)
long conn_outbuf_len (Connection *conn)
long conn_inbuf_len (Connection *conn)
int conn_eof (Connection *conn)
int conn_error (Connection *conn)
void conn_set_output_buffering (Connection *conn, unsigned int size)
int conn_register_real (Connection *conn, FDSet *fdset, conn_callback_t callback, void *data, conn_callback_data_destroyer_t destroyer)
void conn_unregister (Connection *conn)
int conn_wait (Connection *conn, double seconds)
int conn_flush (Connection *conn)
int conn_write (Connection *conn, Octstr *data)
int conn_write_data (Connection *conn, unsigned char *data, long length)
int conn_write_withlen (Connection *conn, Octstr *data)
Octstrconn_read_everything (Connection *conn)
Octstrconn_read_fixed (Connection *conn, long length)
Octstrconn_read_line (Connection *conn)
Octstrconn_read_withlen (Connection *conn)
Octstrconn_read_packet (Connection *conn, int startmark, int endmark)
int conn_get_id (Connection *conn)


Define Documentation

#define conn_register conn,
fdset,
callback,
data   )     conn_register_real(conn, fdset, callback, data, NULL)
 

Definition at line 215 of file conn.h.

Referenced by http_send_reply(), server_thread(), and write_request_thread().


Typedef Documentation

typedef void conn_callback_data_destroyer_t(void *data)
 

Definition at line 113 of file conn.h.

Referenced by conn_unregister().

typedef void conn_callback_t(Connection *conn, void *data)
 

Definition at line 105 of file conn.h.

typedef struct Connection Connection
 

Definition at line 95 of file conn.h.


Function Documentation

void conn_claim Connection conn  ) 
 

Definition at line 658 of file conn.c.

References Connection::claimed, Connection::claiming_thread, gw_assert, gwthread_self(), and panic.

Referenced by cgw_listener(), emi2_listener(), and fake_listener().

00659 {
00660     gw_assert(conn != NULL);
00661 
00662     if (conn->claimed)
00663         panic(0, "Connection is being claimed twice!");
00664     conn->claimed = 1;
00665 #ifndef NO_GWASSERT
00666     conn->claiming_thread = gwthread_self();
00667 #endif
00668 }

Here is the call graph for this function:

void conn_config_ssl CfgGroup grp  ) 
 

Definition at line 1421 of file conn.c.

References cfg_get, octstr_destroy(), octstr_imm(), use_global_client_certkey_file(), use_global_server_certkey_file(), and use_global_trusted_ca_file().

Referenced by init_bearerbox(), init_smsbox(), and init_wapbox().

01422 {
01423     Octstr *ssl_client_certkey_file = NULL;
01424     Octstr *ssl_server_cert_file    = NULL;
01425     Octstr *ssl_server_key_file     = NULL;
01426     Octstr *ssl_trusted_ca_file     = NULL;
01427 
01428     /*
01429      * check if SSL is desired for HTTP servers and then
01430      * load SSL client and SSL server public certificates 
01431      * and private keys
01432      */    
01433     ssl_client_certkey_file = cfg_get(grp, octstr_imm("ssl-client-certkey-file"));
01434     if (ssl_client_certkey_file != NULL) 
01435         use_global_client_certkey_file(ssl_client_certkey_file);
01436     
01437     ssl_server_cert_file = cfg_get(grp, octstr_imm("ssl-server-cert-file"));
01438     ssl_server_key_file = cfg_get(grp, octstr_imm("ssl-server-key-file"));
01439     
01440     if (ssl_server_cert_file != NULL && ssl_server_key_file != NULL) {
01441         use_global_server_certkey_file(ssl_server_cert_file, 
01442                        ssl_server_key_file);
01443     }
01444 
01445     ssl_trusted_ca_file = cfg_get(grp, octstr_imm("ssl-trusted-ca-file"));
01446     
01447     use_global_trusted_ca_file(ssl_trusted_ca_file);
01448 
01449     octstr_destroy(ssl_client_certkey_file);
01450     octstr_destroy(ssl_server_cert_file);
01451     octstr_destroy(ssl_server_key_file);
01452     octstr_destroy(ssl_trusted_ca_file);
01453 }

Here is the call graph for this function:

void conn_destroy Connection conn  ) 
 

Definition at line 614 of file conn.c.

References Connection::callback_data, Connection::callback_data_destroyer, error(), Connection::fd, fdset_unregister(), Connection::inbuf, Connection::inlock, mutex_destroy(), octstr_destroy(), Connection::outbuf, Connection::outlock, Connection::peer_certificate, Connection::registered, Connection::ssl, SSL_smart_shutdown(), and unlocked_try_write().

Referenced by boxc_destroy(), cgw_listener(), cgw_open_send_connection(), cgw_sender(), check_pool_conn(), client_destroy(), close_connection_to_bearerbox_real(), conn_open_ssl(), conn_open_ssl_nb(), conn_pool_get(), conn_pool_item_destroy(), conn_wrap_fd(), emi2_idleprocessing(), emi2_idletimeout_handling(), emi2_listener(), emi2_sender(), esme_destroy(), get_connection(), handle_transaction(), io_thread(), main(), main_connection_loop(), open_send_connection(), send_request(), smasi_thread(), smpp_emu(), and smsbox_thread().

00615 {
00616     int ret;
00617 
00618     if (conn == NULL)
00619         return;
00620 
00621     /* No locking done here.  conn_destroy should not be called
00622      * if any thread might still be interested in the connection. */
00623 
00624     if (conn->registered) {
00625         fdset_unregister(conn->registered, conn->fd);
00626         /* call data destroyer if any */
00627         if (conn->callback_data != NULL && conn->callback_data_destroyer != NULL)
00628             conn->callback_data_destroyer(conn->callback_data);
00629     }
00630 
00631     if (conn->fd >= 0) {
00632         /* Try to flush any remaining data */
00633         unlocked_try_write(conn);
00634 
00635 #ifdef HAVE_LIBSSL
00636         if (conn->ssl != NULL) {
00637             SSL_smart_shutdown(conn->ssl);
00638             SSL_free(conn->ssl);
00639             if (conn->peer_certificate != NULL)
00640                 X509_free(conn->peer_certificate);
00641     }
00642 #endif /* HAVE_LIBSSL */
00643 
00644         ret = close(conn->fd);
00645         if (ret < 0)
00646             error(errno, "conn_destroy: error on close");
00647         conn->fd = -1;
00648     }
00649 
00650     octstr_destroy(conn->outbuf);
00651     octstr_destroy(conn->inbuf);
00652     mutex_destroy(conn->inlock);
00653     mutex_destroy(conn->outlock);
00654 
00655     gw_free(conn);
00656 }

Here is the call graph for this function:

int conn_eof Connection conn  ) 
 

Definition at line 692 of file conn.c.

References lock_in(), Connection::read_eof, and unlock_in.

Referenced by cgw_receiver(), cgw_wait_command(), check_pool_conn(), client_read_status(), conn_pool_get(), emi2_handle_smscreq(), emi2_receiver(), emi2_send_loop(), http_accept_request(), main(), main_connection_loop(), read_body_until_eof(), read_body_with_length(), read_chunked_body_crlf(), read_chunked_body_data(), read_chunked_body_len(), read_from_bearerbox_real(), read_from_box(), read_pdu(), read_some_headers(), receive_request(), receive_smpp_thread(), run_requests(), smpp_emu_reader(), smsbox_thread(), and wait_for_ack().

00693 {
00694     int eof;
00695 
00696     lock_in(conn);
00697     eof = conn->read_eof;
00698     unlock_in(conn);
00699 
00700     return eof;
00701 }

Here is the call graph for this function:

int conn_error Connection conn  ) 
 

Definition at line 703 of file conn.c.

References Connection::io_error, lock_in(), lock_out(), unlock_in, and unlock_out.

Referenced by cgw_receiver(), cgw_wait_command(), check_pool_conn(), client_read_status(), conn_pool_get(), emi2_handle_smscreq(), emi2_receiver(), emi2_send_loop(), http_accept_request(), main(), main_connection_loop(), read_body_until_eof(), read_body_with_length(), read_chunked_body_crlf(), read_chunked_body_data(), read_chunked_body_len(), read_from_bearerbox_real(), read_from_box(), read_pdu(), read_some_headers(), receive_request(), receive_smpp_thread(), smpp_emu_reader(), smsbox_thread(), and wait_for_ack().

00704 {
00705     int err;
00706 
00707     lock_out(conn);
00708     lock_in(conn);
00709     err = conn->io_error;
00710     unlock_in(conn);
00711     unlock_out(conn);
00712 
00713     return err;
00714 }

Here is the call graph for this function:

int conn_flush Connection conn  ) 
 

Definition at line 982 of file conn.c.

References error(), Connection::fd, gwthread_pollfd(), lock_out(), POLLERR, POLLOUT, unlock_out, unlocked_outbuf_len(), and unlocked_write().

Referenced by boxc_sender().

00983 {
00984     int ret;
00985     int revents;
00986     int fd;
00987 
00988     lock_out(conn);
00989     ret = unlocked_write(conn);
00990     if (ret < 0) {
00991         unlock_out(conn);
00992         return -1;
00993     }
00994 
00995     while (unlocked_outbuf_len(conn) != 0) {
00996         fd = conn->fd;
00997 
00998         unlock_out(conn);
00999         revents = gwthread_pollfd(fd, POLLOUT, -1.0);
01000 
01001         /* Note: Make sure we have the "out" lock when
01002          * going through the loop again, because the 
01003          * loop condition needs it. */
01004 
01005         if (revents < 0) {
01006             if (errno == EINTR)
01007                 return 1;
01008             error(0, "conn_flush: poll failed on fd %d:", fd);
01009             return -1;
01010         }
01011 
01012         if (revents == 0) {
01013             /* We were woken up */
01014             return 1;
01015         }
01016 
01017         if (revents & POLLNVAL) {
01018             error(0, "conn_flush: fd %d not open.", fd);
01019             return -1;
01020         }
01021 
01022         lock_out(conn);
01023 
01024         if (revents & (POLLOUT | POLLERR | POLLHUP)) {
01025             ret = unlocked_write(conn);
01026             if (ret < 0) {
01027                 unlock_out(conn);
01028                 return -1;
01029             }
01030         }
01031     }
01032 
01033     unlock_out(conn);
01034 
01035     return 0;
01036 }

Here is the call graph for this function:

int conn_get_connect_result Connection conn  ) 
 

Definition at line 517 of file conn.c.

References Connection::connected, and Connection::fd.

Referenced by handle_transaction().

00518 {
00519   int err;
00520   socklen_t len;
00521 
00522   len = sizeof(err);
00523   if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
00524     return -1;
00525   }
00526   
00527   if (err) {
00528     return -1;
00529   }
00530   
00531   conn->connected = yes;
00532   return 0;
00533 }

int conn_get_id Connection conn  ) 
 

Definition at line 1471 of file conn.c.

References Connection::fd.

Referenced by check_pool_conn(), and conn_pool_get().

01471                                   {
01472     if(conn == NULL)
01473     return 0;
01474     else
01475     return conn->fd;
01476 }

long conn_inbuf_len Connection conn  ) 
 

Definition at line 681 of file conn.c.

References lock_in(), unlock_in, and unlocked_inbuf_len().

00682 {
00683     long len;
00684 
00685     lock_in(conn);
00686     len = unlocked_inbuf_len(conn);
00687     unlock_in(conn);
00688 
00689     return len;
00690 }

Here is the call graph for this function:

int conn_is_connected Connection conn  ) 
 

Definition at line 512 of file conn.c.

References Connection::connected.

Referenced by write_request_thread().

00513 {
00514   return conn->connected == yes ? 0 : -1;
00515 }

Connection* conn_open_tcp Octstr host,
int  port,
Octstr our_host
 

Definition at line 483 of file conn.c.

References conn_open_tcp_with_port(), and port.

Referenced by conn_open_ssl(), connect_to_bearerbox_real(), main(), open_receiver(), open_transceiver(), open_transmitter(), and smsbox_thread().

00484 {
00485     return conn_open_tcp_with_port(host, port, 0, our_host);
00486 }

Here is the call graph for this function:

Connection* conn_open_tcp_nb Octstr host,
int  port,
Octstr our_host
 

Definition at line 488 of file conn.c.

References conn_open_tcp_nb_with_port(), and port.

Referenced by conn_open_ssl_nb(), and conn_pool_get().

00489 {
00490   return conn_open_tcp_nb_with_port(host, port, 0, our_host);
00491 }

Here is the call graph for this function:

Connection* conn_open_tcp_nb_with_port Octstr host,
int  port,
int  our_port,
Octstr our_host
 

Definition at line 493 of file conn.c.

References conn_wrap_fd(), Connection::connected, octstr_get_cstr, port, sockfd, and tcpip_connect_nb_to_server_with_port().

Referenced by conn_open_tcp_nb().

00495 {
00496   int sockfd;
00497   int done = -1;
00498   Connection *c;
00499 
00500   sockfd = tcpip_connect_nb_to_server_with_port(octstr_get_cstr(host), port,
00501                         our_port, our_host == NULL ?
00502                         NULL : octstr_get_cstr(our_host), &done);
00503   if (sockfd < 0)
00504     return NULL;
00505   c = conn_wrap_fd(sockfd, 0);
00506   if (done != 0) {
00507     c->connected = no;
00508   }
00509   return c;
00510 }

Here is the call graph for this function:

Connection* conn_open_tcp_with_port Octstr host,
int  port,
int  our_port,
Octstr our_host
 

Definition at line 535 of file conn.c.

References conn_wrap_fd(), octstr_get_cstr, port, sockfd, and tcpip_connect_to_server_with_port().

Referenced by cgw_open_send_connection(), conn_open_tcp(), open_connection(), and open_send_connection().

00537 {
00538     int sockfd;
00539 
00540     sockfd = tcpip_connect_to_server_with_port(octstr_get_cstr(host), port,
00541                            our_port, our_host == NULL ?
00542                            NULL : octstr_get_cstr(our_host));
00543     if (sockfd < 0)
00544     return NULL;
00545     return conn_wrap_fd(sockfd, 0);
00546 }

Here is the call graph for this function:

long conn_outbuf_len Connection conn  ) 
 

Definition at line 670 of file conn.c.

References lock_out(), unlock_out, and unlocked_outbuf_len().

Referenced by receive_request().

00671 {
00672     long len;
00673 
00674     lock_out(conn);
00675     len = unlocked_outbuf_len(conn);
00676     unlock_out(conn);
00677 
00678     return len;
00679 }

Here is the call graph for this function:

Octstr* conn_read_everything Connection conn  ) 
 

Definition at line 1077 of file conn.c.

References lock_in(), result, unlock_in, unlocked_get(), unlocked_inbuf_len(), and unlocked_read().

Referenced by read_body_until_eof().

01078 {
01079     Octstr *result = NULL;
01080 
01081     lock_in(conn);
01082     if (unlocked_inbuf_len(conn) == 0) {
01083         unlocked_read(conn);
01084         if (unlocked_inbuf_len(conn) == 0) {
01085             unlock_in(conn);
01086             return NULL;
01087         }
01088     }
01089 
01090     result = unlocked_get(conn, unlocked_inbuf_len(conn));
01091     gw_claim_area(result);
01092     unlock_in(conn);
01093 
01094     return result;
01095 }

Here is the call graph for this function:

Octstr* conn_read_fixed Connection conn,
long  length
 

Definition at line 1097 of file conn.c.

References lock_in(), result, unlock_in, unlocked_get(), unlocked_inbuf_len(), and unlocked_read().

Referenced by read_body_with_length(), read_chunked_body_data(), smpp_pdu_read_data(), and smpp_pdu_read_len().

01098 {
01099     Octstr *result = NULL;
01100 
01101     if (length < 1)
01102         return NULL;
01103 
01104     /* See if the data is already available.  If not, try a read(),
01105      * then see if we have enough data after that.  If not, give up. */
01106     lock_in(conn);
01107     if (unlocked_inbuf_len(conn) < length) {
01108         unlocked_read(conn);
01109         if (unlocked_inbuf_len(conn) < length) {
01110             unlock_in(conn);
01111             return NULL;
01112         }
01113     }
01114     result = unlocked_get(conn, length);
01115     gw_claim_area(result);
01116     unlock_in(conn);
01117 
01118     return result;
01119 }

Here is the call graph for this function:

Octstr* conn_read_line Connection conn  ) 
 

Definition at line 1121 of file conn.c.

References Connection::inbuf, Connection::inbufpos, lock_in(), octstr_delete(), octstr_get_char(), octstr_len(), octstr_search_char(), result, unlock_in, unlocked_get(), and unlocked_read().

Referenced by client_read_status(), main(), main_connection_loop(), read_chunked_body_crlf(), read_chunked_body_len(), read_some_headers(), receive_request(), and smasi_pdu_read().

01122 {
01123     Octstr *result = NULL;
01124     long pos;
01125 
01126     lock_in(conn);
01127     /* 10 is the code for linefeed.  We don't rely on \n because that
01128      * might be a different value on some (strange) systems, and
01129      * we are reading from a network connection. */
01130     pos = octstr_search_char(conn->inbuf, 10, conn->inbufpos);
01131     if (pos < 0) {
01132         unlocked_read(conn);
01133         pos = octstr_search_char(conn->inbuf, 10, conn->inbufpos);
01134         if (pos < 0) {
01135             unlock_in(conn);
01136             return NULL;
01137         }
01138     }
01139 
01140     result = unlocked_get(conn, pos - conn->inbufpos);
01141     gw_claim_area(result);
01142 
01143     /* Skip the LF, which we left in the buffer */
01144     conn->inbufpos++;
01145 
01146     /* If the line was terminated with CR LF, we have to remove
01147      * the CR from the result. */
01148     if (octstr_len(result) > 0 &&
01149         octstr_get_char(result, octstr_len(result) - 1) == 13)
01150         octstr_delete(result, octstr_len(result) - 1, 1);
01151 
01152     unlock_in(conn);
01153     return result;
01154 }

Here is the call graph for this function:

Octstr* conn_read_packet Connection conn,
int  startmark,
int  endmark
 

Definition at line 1199 of file conn.c.

References Connection::inbuf, Connection::inbufpos, lock_in(), octstr_len(), octstr_search_char(), result, unlock_in, unlocked_get(), and unlocked_read().

Referenced by emi2_handle_smscreq(), emi2_receiver(), and wait_for_ack().

01200 {
01201     int startpos, endpos;
01202     Octstr *result = NULL;
01203     int try;
01204 
01205     lock_in(conn);
01206 
01207     for (try = 1; try <= 2; try++) {
01208         if (try > 1)
01209             unlocked_read(conn);
01210 
01211         /* Find startmark, and discard everything up to it */
01212         startpos = octstr_search_char(conn->inbuf, startmark, conn->inbufpos);
01213         if (startpos < 0) {
01214             conn->inbufpos = octstr_len(conn->inbuf);
01215             continue;
01216         } else {
01217             conn->inbufpos = startpos;
01218         }
01219 
01220         /* Find first endmark after startmark */
01221         endpos = octstr_search_char(conn->inbuf, endmark, conn->inbufpos);
01222         if (endpos < 0)
01223             continue;
01224 
01225         result = unlocked_get(conn, endpos - startpos + 1);
01226         gw_claim_area(result);
01227         break;
01228     }
01229 
01230     unlock_in(conn);
01231     return result;
01232 }

Here is the call graph for this function:

Octstr* conn_read_withlen Connection conn  ) 
 

Definition at line 1156 of file conn.c.

References decode_network_long(), Connection::inbuf, Connection::inbufpos, lock_in(), octstr_get_many_chars(), result, unlock_in, unlocked_get(), unlocked_inbuf_len(), unlocked_read(), and warning().

Referenced by read_from_bearerbox_real(), read_from_box(), run_requests(), and smsbox_thread().

01157 {
01158     Octstr *result = NULL;
01159     unsigned char lengthbuf[4];
01160     long length = 0; /* for compiler please */
01161     int try, retry;
01162 
01163     lock_in(conn);
01164 
01165     for (try = 1; try <= 2; try++) {
01166         if (try > 1)
01167             unlocked_read(conn);
01168 
01169         do {
01170             retry = 0;
01171             /* First get the length. */
01172             if (unlocked_inbuf_len(conn) < 4)
01173                 continue;
01174 
01175             octstr_get_many_chars(lengthbuf, conn->inbuf, conn->inbufpos, 4);
01176             length = decode_network_long(lengthbuf);
01177 
01178             if (length < 0) {
01179                 warning(0, "conn_read_withlen: got negative length, skipping");
01180                 conn->inbufpos += 4;
01181                 retry = 1;
01182              }
01183         } while(retry == 1);
01184 
01185         /* Then get the data. */
01186         if (unlocked_inbuf_len(conn) - 4 < length)
01187             continue;
01188 
01189         conn->inbufpos += 4;
01190         result = unlocked_get(conn, length);
01191         gw_claim_area(result);
01192         break;
01193     }
01194 
01195     unlock_in(conn);
01196     return result;
01197 }

Here is the call graph for this function:

int conn_register_real Connection conn,
FDSet fdset,
conn_callback_t  callback,
void *  data,
conn_callback_data_destroyer_t  destroyer
 

Referenced by conn_pool_put().

void conn_set_output_buffering Connection conn,
unsigned int  size
 

Definition at line 716 of file conn.c.

References lock_out(), Connection::output_buffering, unlock_out, and unlocked_try_write().

00717 {
00718     lock_out(conn);
00719     conn->output_buffering = size;
00720     /* If the buffer size is smaller, we may have to write immediately. */
00721     unlocked_try_write(conn);
00722     unlock_out(conn);
00723 }

Here is the call graph for this function:

void conn_unregister Connection conn  ) 
 

Definition at line 845 of file conn.c.

References Connection::callback, Connection::callback_data, Connection::callback_data_destroyer, conn_callback_data_destroyer_t, data, Connection::fd, fdset_unregister(), gw_assert, Connection::listening_pollin, Connection::listening_pollout, lock_in(), lock_out(), Connection::registered, unlock_in, and unlock_out.

Referenced by check_pool_conn(), conn_pool_get(), handle_transaction(), port_remove(), and receive_request().

00846 {
00847     FDSet *set = NULL;
00848     int fd = -1;
00849     void *data = NULL;
00850     conn_callback_data_destroyer_t *destroyer = NULL;
00851     
00852     gw_assert(conn != NULL);
00853 
00854     if (conn == NULL || conn->fd < 0)
00855         return;
00856 
00857     /* We need both locks to update the registration information */
00858     lock_out(conn);
00859     lock_in(conn);
00860 
00861     if (conn->registered) {
00862         set = conn->registered;
00863         fd = conn->fd;
00864         conn->registered = NULL;
00865         conn->callback = NULL;
00866         /*
00867          * remember and don't destroy data and data_destroyer because we
00868          * may be in callback right now. So destroy only after fdset_unregister
00869          * call which guarantee us we are not in callback anymore.
00870          */
00871         data = conn->callback_data;
00872         conn->callback_data = NULL;
00873         destroyer = conn->callback_data_destroyer;
00874         conn->callback_data_destroyer = NULL;
00875         conn->listening_pollin = 0;
00876         conn->listening_pollout = 0;
00877     }
00878 
00879     unlock_in(conn);
00880     unlock_out(conn);
00881 
00882     /* now unregister from FDSet */
00883     if (set != NULL)
00884         fdset_unregister(set, fd);
00885 
00886     /* ok we are not in callback anymore, destroy data if any */
00887     if (data != NULL && destroyer != NULL)
00888         destroyer(data);
00889 }

Here is the call graph for this function:

int conn_wait Connection conn,
double  seconds
 

Definition at line 891 of file conn.c.

References error(), Connection::fd, gwthread_pollfd(), Connection::io_error, lock_in(), lock_out(), POLLERR, Connection::read_eof, unlock_in, unlock_out, unlocked_outbuf_len(), unlocked_read(), and unlocked_write().

Referenced by cgw_receiver(), cgw_wait_command(), conn_pool_get(), emi2_receiver(), emi2_wait(), http_accept_request(), io_thread(), main(), main_connection_loop(), read_from_bearerbox_real(), read_from_box(), receive_smpp_thread(), run_requests(), smasi_thread(), smpp_emu_reader(), smsbox_thread(), and wait_for_ack().

00892 {
00893     int events;
00894     int ret;
00895     int fd;
00896 
00897     lock_out(conn);
00898 
00899     /* Try to write any data that might still be waiting to be sent */
00900     ret = unlocked_write(conn);
00901     if (ret < 0) {
00902         unlock_out(conn);
00903         return -1;
00904     }
00905     if (ret > 0) {
00906         /* We did something useful.  No need to poll or wait now. */
00907         unlock_out(conn);
00908         return 0;
00909     }
00910 
00911     fd = conn->fd;
00912 
00913     /* Normally, we block until there is more data available.  But
00914      * if any data still needs to be sent, we block until we can
00915      * send it (or there is more data available).  We always block
00916      * for reading, unless we know there is no more data coming.
00917      * (Because in that case, poll will keep reporting POLLIN to
00918      * signal the end of the file).  If the caller explicitly wants
00919      * to wait even though there is no data to write and we're at
00920      * end of file, then poll for new data anyway because the caller
00921      * apparently doesn't trust eof. */
00922     events = 0;
00923     if (unlocked_outbuf_len(conn) > 0)
00924         events |= POLLOUT;
00925     /* Don't keep the connection locked while we wait */
00926     unlock_out(conn);
00927 
00928     /* We need the in lock to query read_eof */
00929     lock_in(conn);
00930     if ((conn->read_eof == 0 && conn->io_error == 0) || events == 0)
00931         events |= POLLIN;
00932     unlock_in(conn);
00933 
00934     ret = gwthread_pollfd(fd, events, seconds);
00935     if (ret < 0) {
00936         if (errno == EINTR)
00937             return 0;
00938         error(0, "conn_wait: poll failed on fd %d:", fd);
00939         return -1;
00940     }
00941 
00942     if (ret == 0)
00943         return 1;
00944 
00945     if (ret & POLLNVAL) {
00946         error(0, "conn_wait: fd %d not open.", fd);
00947         return -1;
00948     }
00949 
00950     if (ret & (POLLERR | POLLHUP)) {
00951         /* Call unlocked_read to report the specific error,
00952          * and handle the results of the error.  We can't be
00953          * certain that the error still exists, because we
00954          * released the lock for a while. */
00955         lock_in(conn);
00956         unlocked_read(conn);
00957         unlock_in(conn);
00958         return -1;
00959     }
00960 
00961     /* If POLLOUT is on, then we must have wanted
00962      * to write something. */
00963     if (ret & POLLOUT) {
00964         lock_out(conn);
00965         unlocked_write(conn);
00966         unlock_out(conn);
00967     }
00968 
00969     /* Since we normally select for reading, we must
00970      * try to read here.  Otherwise, if the caller loops
00971      * around conn_wait without making conn_read* calls
00972      * in between, we will keep polling this same data. */
00973     if (ret & POLLIN) {
00974         lock_in(conn);
00975         unlocked_read(conn);
00976         unlock_in(conn);
00977     }
00978 
00979     return 0;
00980 }

Here is the call graph for this function:

Connection* conn_wrap_fd int  fd,
int  ssl
 

Definition at line 553 of file conn.c.

References conn_destroy(), error(), global_server_ssl_context, Connection::inlock, mutex_create, octstr_create, and socket_set_blocking().

Referenced by accept_thread(), boxc_create(), cgw_listener(), conn_open_tcp_nb_with_port(), conn_open_tcp_with_port(), emi2_listener(), fake_listener(), server_thread(), smpp_emu(), and start_wapbox().

00554 {
00555     Connection *conn;
00556 
00557     if (socket_set_blocking(fd, 0) < 0)
00558         return NULL;
00559 
00560     conn = gw_malloc(sizeof(*conn));
00561     conn->inlock = mutex_create();
00562     conn->outlock = mutex_create();
00563     conn->claimed = 0;
00564 
00565     conn->outbuf = octstr_create("");
00566     conn->outbufpos = 0;
00567     conn->inbuf = octstr_create("");
00568     conn->inbufpos = 0;
00569 
00570     conn->fd = fd;
00571     conn->connected = yes;
00572     conn->read_eof = 0;
00573     conn->io_error = 0;
00574     conn->output_buffering = DEFAULT_OUTPUT_BUFFERING;
00575 
00576     conn->registered = NULL;
00577     conn->callback = NULL;
00578     conn->callback_data = NULL;
00579     conn->callback_data_destroyer = NULL;
00580     conn->listening_pollin = 0;
00581     conn->listening_pollout = 0;
00582 #ifdef HAVE_LIBSSL
00583     /*
00584      * do all the SSL magic for this connection
00585      */
00586     if (ssl) {
00587         conn->ssl = SSL_new(global_server_ssl_context);
00588         conn->peer_certificate = NULL;
00589 
00590         /* SSL_set_fd can fail, so check it */
00591         if (SSL_set_fd(conn->ssl, conn->fd) == 0) {
00592             /* SSL_set_fd failed, log error and return NULL */
00593             error(errno, "SSL: OpenSSL: %.256s", ERR_error_string(ERR_get_error(), NULL));
00594             conn_destroy(conn);
00595             return NULL;
00596         }
00597         /* SSL_set_verify(conn->ssl, 0, NULL); */
00598 
00599         /* set read/write BIO layer to non-blocking mode */
00600         BIO_set_nbio(SSL_get_rbio(conn->ssl), 1);
00601         BIO_set_nbio(SSL_get_wbio(conn->ssl), 1);
00602 
00603         /* set accept state , SSL-Handshake will be handled transparent while SSL_[read|write] */
00604          SSL_set_accept_state(conn->ssl);
00605     } else {
00606         conn->ssl = NULL;
00607         conn->peer_certificate = NULL;
00608     }
00609 #endif /* HAVE_LIBSSL */
00610 
00611     return conn;
00612 }

Here is the call graph for this function:

int conn_write Connection conn,
Octstr data
 

Definition at line 1038 of file conn.c.

References data, lock_out(), octstr_append(), Connection::outbuf, unlock_out, and unlocked_try_write().

Referenced by cgwop_send(), emimsg_send(), handle_pdu(), http_send_reply(), main(), send_enquire_link(), send_logoff(), send_pdu(), send_request(), send_smpp_thread(), send_unbind(), smpp_emu_handle_pdu(), smpp_emu_writer(), sms_to_client(), and smsc_emu_submit_ack().

01039 {
01040     int ret;
01041 
01042     lock_out(conn);
01043     octstr_append(conn->outbuf, data);
01044     ret = unlocked_try_write(conn);
01045     unlock_out(conn);
01046 
01047     return ret;
01048 }

Here is the call graph for this function:

int conn_write_data Connection conn,
unsigned char *  data,
long  length
 

Definition at line 1050 of file conn.c.

References data, lock_out(), octstr_append_data(), Connection::outbuf, unlock_out, and unlocked_try_write().

01051 {
01052     int ret;
01053 
01054     lock_out(conn);
01055     octstr_append_data(conn->outbuf, data, length);
01056     ret = unlocked_try_write(conn);
01057     unlock_out(conn);
01058 
01059     return ret;
01060 }

Here is the call graph for this function:

int conn_write_withlen Connection conn,
Octstr data
 

Definition at line 1062 of file conn.c.

References data, encode_network_long(), lock_out(), octstr_append(), octstr_append_data(), octstr_len(), Connection::outbuf, unlock_out, and unlocked_try_write().

Referenced by deliver_to_bearerbox_real(), send_msg(), send_pdu(), smsbox_thread(), and write_to_bearerbox_real().

01063 {
01064     int ret;
01065     unsigned char lengthbuf[4];
01066 
01067     encode_network_long(lengthbuf, octstr_len(data));
01068     lock_out(conn);
01069     octstr_append_data(conn->outbuf, lengthbuf, 4);
01070     octstr_append(conn->outbuf, data);
01071     ret = unlocked_try_write(conn);
01072     unlock_out(conn);
01073 
01074     return ret;
01075 }

Here is the call graph for this function:

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.