Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
conn.h File Reference

Go to the source code of this file.

Macros

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

Typedefs

typedef struct 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)
 

Macro Definition Documentation

◆ conn_register

#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

◆ conn_callback_data_destroyer_t

typedef void conn_callback_data_destroyer_t(void *data)

Definition at line 113 of file conn.h.

◆ conn_callback_t

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

Definition at line 105 of file conn.h.

◆ Connection

typedef struct Connection Connection

Definition at line 95 of file conn.h.

Function Documentation

◆ conn_claim()

void conn_claim ( Connection conn)

Definition at line 665 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().

666 {
667  gw_assert(conn != NULL);
668 
669  if (conn->claimed)
670  panic(0, "Connection is being claimed twice!");
671  conn->claimed = 1;
672 #ifndef NO_GWASSERT
673  conn->claiming_thread = gwthread_self();
674 #endif
675 }
long gwthread_self(void)
gw_assert(wtls_machine->packet_to_send !=NULL)
long claiming_thread
Definition: conn.c:115
volatile sig_atomic_t claimed
Definition: conn.c:113
#define panic
Definition: log.h:87

◆ conn_config_ssl()

void conn_config_ssl ( CfgGroup grp)

Definition at line 1538 of file conn.c.

References info().

Referenced by conn_read_packet(), init_bearerbox(), init_smppbox(), init_smsbox(), init_sqlbox(), and init_wapbox().

1539 {
1540  info(0, "SSL not supported, no SSL initialization done.");
1541 }
void info(int err, const char *fmt,...)
Definition: log.c:672

◆ conn_destroy()

void conn_destroy ( Connection conn)

Definition at line 621 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::registered, and unlocked_try_write().

Referenced by boxc_destroy(), cgw_listener(), cgw_open_send_connection(), cgw_sender(), client_destroy(), close_connection_to_bearerbox_real(), 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_receiver(), open_send_connection(), open_transceiver(), open_transmitter(), send_request(), smasi_thread(), smpp_emu(), smsbox_thread(), and unlocked_register_pollout().

622 {
623  int ret;
624 
625  if (conn == NULL)
626  return;
627 
628  /* No locking done here. conn_destroy should not be called
629  * if any thread might still be interested in the connection. */
630 
631  if (conn->registered) {
632  fdset_unregister(conn->registered, conn->fd);
633  /* call data destroyer if any */
634  if (conn->callback_data != NULL && conn->callback_data_destroyer != NULL)
636  }
637 
638  if (conn->fd >= 0) {
639  /* Try to flush any remaining data */
640  unlocked_try_write(conn);
641 
642 #ifdef HAVE_LIBSSL
643  if (conn->ssl != NULL) {
644  SSL_smart_shutdown(conn->ssl);
645  SSL_free(conn->ssl);
646  if (conn->peer_certificate != NULL)
647  X509_free(conn->peer_certificate);
648  }
649 #endif /* HAVE_LIBSSL */
650 
651  ret = close(conn->fd);
652  if (ret < 0)
653  error(errno, "conn_destroy: error on close");
654  conn->fd = -1;
655  }
656 
657  octstr_destroy(conn->outbuf);
658  octstr_destroy(conn->inbuf);
659  mutex_destroy(conn->inlock);
660  mutex_destroy(conn->outlock);
661 
662  gw_free(conn);
663 }
void error(int err, const char *fmt,...)
Definition: log.c:648
static int unlocked_try_write(Connection *conn)
Definition: conn.c:289
Mutex * outlock
Definition: conn.c:112
FDSet * registered
Definition: conn.c:141
void * callback_data
Definition: conn.c:143
Octstr * outbuf
Definition: conn.c:125
void fdset_unregister(FDSet *set, int fd)
Definition: fdset.c:510
conn_callback_data_destroyer_t * callback_data_destroyer
Definition: conn.c:144
int fd
Definition: conn.c:119
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
void mutex_destroy(Mutex *mutex)
Definition: thread.c:97
Mutex * inlock
Definition: conn.c:111
Octstr * inbuf
Definition: conn.c:133

◆ conn_eof()

◆ conn_error()

int conn_error ( Connection conn)

Definition at line 710 of file conn.c.

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

Referenced by cgw_receiver(), cgw_wait_command(), 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().

711 {
712  int err;
713 
714  lock_out(conn);
715  lock_in(conn);
716  err = conn->io_error;
717  unlock_in(conn);
718  unlock_out(conn);
719 
720  return err;
721 }
int io_error
Definition: conn.c:137
static void lock_out(Connection *conn)
Definition: conn.c:197
#define unlock_in(conn)
Definition: conn.c:168
static void lock_in(Connection *conn)
Definition: conn.c:172
#define unlock_out(conn)
Definition: conn.c:169

◆ conn_flush()

int conn_flush ( Connection conn)

Definition at line 989 of file conn.c.

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

Referenced by boxc_sender().

990 {
991  int ret;
992  int revents;
993  int fd;
994 
995  lock_out(conn);
996  ret = unlocked_write(conn);
997  if (ret < 0) {
998  unlock_out(conn);
999  return -1;
1000  }
1001 
1002  while (unlocked_outbuf_len(conn) != 0) {
1003  fd = conn->fd;
1004 
1005  unlock_out(conn);
1006  revents = gwthread_pollfd(fd, POLLOUT, -1.0);
1007 
1008  /* Note: Make sure we have the "out" lock when
1009  * going through the loop again, because the
1010  * loop condition needs it. */
1011 
1012  if (revents < 0) {
1013  if (errno == EINTR)
1014  return 1;
1015  error(0, "conn_flush: poll failed on fd %d:", fd);
1016  return -1;
1017  }
1018 
1019  if (revents == 0) {
1020  /* We were woken up */
1021  return 1;
1022  }
1023 
1024  if (revents & POLLNVAL) {
1025  error(0, "conn_flush: fd %d not open.", fd);
1026  return -1;
1027  }
1028 
1029  lock_out(conn);
1030 
1031  if (revents & (POLLOUT | POLLERR | POLLHUP)) {
1032  ret = unlocked_write(conn);
1033  if (ret < 0) {
1034  unlock_out(conn);
1035  return -1;
1036  }
1037  }
1038  }
1039 
1040  unlock_out(conn);
1041 
1042  return 0;
1043 }
void error(int err, const char *fmt,...)
Definition: log.c:648
static long unlocked_outbuf_len(Connection *conn)
Definition: conn.c:222
#define POLLNVAL
Definition: gwpoll.h:98
static void lock_out(Connection *conn)
Definition: conn.c:197
int fd
Definition: conn.c:119
#define POLLERR
Definition: gwpoll.h:96
int gwthread_pollfd(int fd, int events, double timeout)
static long unlocked_write(Connection *conn)
Definition: conn.c:235
#define unlock_out(conn)
Definition: conn.c:169
#define POLLHUP
Definition: gwpoll.h:97
#define POLLOUT
Definition: gwpoll.h:93

◆ conn_get_connect_result()

int conn_get_connect_result ( Connection conn)

Definition at line 524 of file conn.c.

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

Referenced by handle_transaction().

525 {
526  int err;
527  socklen_t len;
528 
529  len = sizeof(err);
530  if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
531  return -1;
532  }
533 
534  if (err) {
535  return -1;
536  }
537 
538  conn->connected = yes;
539  return 0;
540 }
int fd
Definition: conn.c:119
enum Connection::@56 connected
int socklen_t
Definition: socket.h:73

◆ conn_get_id()

int conn_get_id ( Connection conn)

Definition at line 1544 of file conn.c.

References Connection::fd.

Referenced by conn_pool_get().

1544  {
1545  if(conn == NULL)
1546  return 0;
1547  else
1548  return conn->fd;
1549 }
int fd
Definition: conn.c:119

◆ conn_inbuf_len()

long conn_inbuf_len ( Connection conn)

Definition at line 688 of file conn.c.

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

689 {
690  long len;
691 
692  lock_in(conn);
693  len = unlocked_inbuf_len(conn);
694  unlock_in(conn);
695 
696  return len;
697 }
#define unlock_in(conn)
Definition: conn.c:168
static long unlocked_inbuf_len(Connection *conn)
Definition: conn.c:228
static void lock_in(Connection *conn)
Definition: conn.c:172

◆ conn_is_connected()

int conn_is_connected ( Connection conn)

Definition at line 519 of file conn.c.

References Connection::connected, and Connection::yes.

Referenced by write_request_thread().

520 {
521  return conn->connected == yes ? 0 : -1;
522 }
enum Connection::@56 connected

◆ conn_open_tcp()

Connection* conn_open_tcp ( Octstr host,
int  port,
Octstr our_host 
)

Definition at line 490 of file conn.c.

References conn_open_tcp_with_port().

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

491 {
492  return conn_open_tcp_with_port(host, port, 0, our_host);
493 }
Definition: http.c:1999
Connection * conn_open_tcp_with_port(Octstr *host, int port, int our_port, Octstr *our_host)
Definition: conn.c:542

◆ conn_open_tcp_nb()

Connection* conn_open_tcp_nb ( Octstr host,
int  port,
Octstr our_host 
)

Definition at line 495 of file conn.c.

References conn_open_tcp_nb_with_port().

Referenced by conn_pool_get(), and unlocked_register_pollout().

496 {
497  return conn_open_tcp_nb_with_port(host, port, 0, our_host);
498 }
Definition: http.c:1999
Connection * conn_open_tcp_nb_with_port(Octstr *host, int port, int our_port, Octstr *our_host)
Definition: conn.c:500

◆ conn_open_tcp_nb_with_port()

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

Definition at line 500 of file conn.c.

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

Referenced by conn_open_tcp_nb().

502 {
503  int sockfd;
504  int done = -1;
505  Connection *c;
506 
508  our_port, our_host == NULL ?
509  NULL : octstr_get_cstr(our_host), &done);
510  if (sockfd < 0)
511  return NULL;
512  c = conn_wrap_fd(sockfd, 0);
513  if (done != 0) {
514  c->connected = no;
515  }
516  return c;
517 }
static long our_port
Definition: radius_acct.c:87
Definition: http.c:1999
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
int sockfd
Definition: test_cimd2.c:145
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
enum Connection::@56 connected
Connection * conn_wrap_fd(int fd, int ssl)
Definition: conn.c:560

◆ conn_open_tcp_with_port()

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

Definition at line 542 of file conn.c.

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

Referenced by cgw_open_send_connection(), conn_open_tcp(), open_connection(), open_receiver(), open_send_connection(), open_transceiver(), and open_transmitter().

544 {
545  int sockfd;
546 
548  our_port, our_host == NULL ?
549  NULL : octstr_get_cstr(our_host));
550  if (sockfd < 0)
551  return NULL;
552  return conn_wrap_fd(sockfd, 0);
553 }
static long our_port
Definition: radius_acct.c:87
Definition: http.c:1999
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
int tcpip_connect_to_server_with_port(char *hostname, int port, int our_port, const char *source_addr)
Definition: socket.c:156
int sockfd
Definition: test_cimd2.c:145
Connection * conn_wrap_fd(int fd, int ssl)
Definition: conn.c:560

◆ conn_outbuf_len()

long conn_outbuf_len ( Connection conn)

Definition at line 677 of file conn.c.

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

Referenced by receive_request().

678 {
679  long len;
680 
681  lock_out(conn);
682  len = unlocked_outbuf_len(conn);
683  unlock_out(conn);
684 
685  return len;
686 }
static long unlocked_outbuf_len(Connection *conn)
Definition: conn.c:222
static void lock_out(Connection *conn)
Definition: conn.c:197
#define unlock_out(conn)
Definition: conn.c:169

◆ conn_read_everything()

Octstr* conn_read_everything ( Connection conn)

Definition at line 1084 of file conn.c.

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

Referenced by read_body_until_eof().

1085 {
1086  Octstr *result = NULL;
1087 
1088  lock_in(conn);
1089  if (unlocked_inbuf_len(conn) == 0) {
1090  unlocked_read(conn);
1091  if (unlocked_inbuf_len(conn) == 0) {
1092  unlock_in(conn);
1093  return NULL;
1094  }
1095  }
1096 
1097  result = unlocked_get(conn, unlocked_inbuf_len(conn));
1098  gw_claim_area(result);
1099  unlock_in(conn);
1100 
1101  return result;
1102 }
static Octstr * unlocked_get(Connection *conn, long length)
Definition: conn.c:355
static void unlocked_read(Connection *conn)
Definition: conn.c:310
#define unlock_in(conn)
Definition: conn.c:168
static long unlocked_inbuf_len(Connection *conn)
Definition: conn.c:228
Definition: octstr.c:118
static void lock_in(Connection *conn)
Definition: conn.c:172

◆ conn_read_fixed()

Octstr* conn_read_fixed ( Connection conn,
long  length 
)

Definition at line 1104 of file conn.c.

References lock_in(), 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().

1105 {
1106  Octstr *result = NULL;
1107 
1108  if (length < 1)
1109  return NULL;
1110 
1111  /* See if the data is already available. If not, try a read(),
1112  * then see if we have enough data after that. If not, give up. */
1113  lock_in(conn);
1114  if (unlocked_inbuf_len(conn) < length) {
1115  unlocked_read(conn);
1116  if (unlocked_inbuf_len(conn) < length) {
1117  unlock_in(conn);
1118  return NULL;
1119  }
1120  }
1121  result = unlocked_get(conn, length);
1122  gw_claim_area(result);
1123  unlock_in(conn);
1124 
1125  return result;
1126 }
static Octstr * unlocked_get(Connection *conn, long length)
Definition: conn.c:355
static void unlocked_read(Connection *conn)
Definition: conn.c:310
#define unlock_in(conn)
Definition: conn.c:168
static long unlocked_inbuf_len(Connection *conn)
Definition: conn.c:228
Definition: octstr.c:118
static void lock_in(Connection *conn)
Definition: conn.c:172

◆ conn_read_line()

Octstr* conn_read_line ( Connection conn)

Definition at line 1128 of file conn.c.

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

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

1129 {
1130  Octstr *result = NULL;
1131  long pos;
1132 
1133  lock_in(conn);
1134  /* 10 is the code for linefeed. We don't rely on \n because that
1135  * might be a different value on some (strange) systems, and
1136  * we are reading from a network connection. */
1137  pos = octstr_search_char(conn->inbuf, 10, conn->inbufpos);
1138  if (pos < 0) {
1139  unlocked_read(conn);
1140  pos = octstr_search_char(conn->inbuf, 10, conn->inbufpos);
1141  if (pos < 0) {
1142  unlock_in(conn);
1143  return NULL;
1144  }
1145  }
1146 
1147  result = unlocked_get(conn, pos - conn->inbufpos);
1148  gw_claim_area(result);
1149 
1150  /* Skip the LF, which we left in the buffer */
1151  conn->inbufpos++;
1152 
1153  /* If the line was terminated with CR LF, we have to remove
1154  * the CR from the result. */
1155  if (octstr_len(result) > 0 &&
1156  octstr_get_char(result, octstr_len(result) - 1) == 13)
1157  octstr_delete(result, octstr_len(result) - 1, 1);
1158 
1159  unlock_in(conn);
1160  return result;
1161 }
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1010
static Octstr * unlocked_get(Connection *conn, long length)
Definition: conn.c:355
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1525
static void unlocked_read(Connection *conn)
Definition: conn.c:310
#define unlock_in(conn)
Definition: conn.c:168
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
static void lock_in(Connection *conn)
Definition: conn.c:172
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:404
Octstr * inbuf
Definition: conn.c:133
long inbufpos
Definition: conn.c:134

◆ conn_read_packet()

Octstr* conn_read_packet ( Connection conn,
int  startmark,
int  endmark 
)

Definition at line 1206 of file conn.c.

References cfg_get, conn_config_ssl(), error(), file, gw_assert(), gwthread_self(), Connection::inbuf, Connection::inbufpos, info(), lock_in(), lock_out(), mutex_create, mutex_destroy(), mutex_lock, mutex_unlock, octstr_destroy(), octstr_get_cstr, octstr_imm(), octstr_len(), octstr_search_char(), panic, ssl_client_certkey_file, unlock_in, unlock_out, unlocked_get(), and unlocked_read().

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

1207 {
1208  int startpos, endpos;
1209  Octstr *result = NULL;
1210  int try;
1211 
1212  lock_in(conn);
1213 
1214  for (try = 1; try <= 2; try++) {
1215  if (try > 1)
1216  unlocked_read(conn);
1217 
1218  /* Find startmark, and discard everything up to it */
1219  if (startmark >= 0) {
1220  startpos = octstr_search_char(conn->inbuf, startmark, conn->inbufpos);
1221  if (startpos < 0) {
1222  conn->inbufpos = octstr_len(conn->inbuf);
1223  continue;
1224  } else {
1225  conn->inbufpos = startpos;
1226  }
1227  } else {
1228  startpos = conn->inbufpos;
1229  }
1230 
1231  /* Find first endmark after startmark */
1232  endpos = octstr_search_char(conn->inbuf, endmark, conn->inbufpos);
1233  if (endpos < 0)
1234  continue;
1235 
1236  result = unlocked_get(conn, endpos - startpos + 1);
1237  gw_claim_area(result);
1238  break;
1239  }
1240 
1241  unlock_in(conn);
1242  return result;
1243 }
static void startmark(unsigned char *p, long number)
Definition: gwmem-check.c:263
static void endmark(unsigned char *p, size_t size)
Definition: gwmem-check.c:255
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1010
static Octstr * unlocked_get(Connection *conn, long length)
Definition: conn.c:355
static void unlocked_read(Connection *conn)
Definition: conn.c:310
#define unlock_in(conn)
Definition: conn.c:168
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
static void lock_in(Connection *conn)
Definition: conn.c:172
Octstr * inbuf
Definition: conn.c:133
long inbufpos
Definition: conn.c:134

◆ conn_read_withlen()

Octstr* conn_read_withlen ( Connection conn)

Definition at line 1163 of file conn.c.

References decode_network_long(), Connection::inbuf, Connection::inbufpos, lock_in(), octstr_get_many_chars(), 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().

1164 {
1165  Octstr *result = NULL;
1166  unsigned char lengthbuf[4];
1167  long length = 0; /* for compiler please */
1168  int try, retry;
1169 
1170  lock_in(conn);
1171 
1172  for (try = 1; try <= 2; try++) {
1173  if (try > 1)
1174  unlocked_read(conn);
1175 
1176  do {
1177  retry = 0;
1178  /* First get the length. */
1179  if (unlocked_inbuf_len(conn) < 4)
1180  continue;
1181 
1182  octstr_get_many_chars(lengthbuf, conn->inbuf, conn->inbufpos, 4);
1183  length = decode_network_long(lengthbuf);
1184 
1185  if (length < 0) {
1186  warning(0, "conn_read_withlen: got negative length, skipping");
1187  conn->inbufpos += 4;
1188  retry = 1;
1189  }
1190  } while(retry == 1);
1191 
1192  /* Then get the data. */
1193  if (unlocked_inbuf_len(conn) - 4 < length)
1194  continue;
1195 
1196  conn->inbufpos += 4;
1197  result = unlocked_get(conn, length);
1198  gw_claim_area(result);
1199  break;
1200  }
1201 
1202  unlock_in(conn);
1203  return result;
1204 }
static Octstr * unlocked_get(Connection *conn, long length)
Definition: conn.c:355
static void unlocked_read(Connection *conn)
Definition: conn.c:310
#define unlock_in(conn)
Definition: conn.c:168
void warning(int err, const char *fmt,...)
Definition: log.c:660
long decode_network_long(unsigned char *data)
Definition: utils.c:935
static long unlocked_inbuf_len(Connection *conn)
Definition: conn.c:228
Definition: octstr.c:118
static void lock_in(Connection *conn)
Definition: conn.c:172
void octstr_get_many_chars(char *buf, Octstr *ostr, long pos, long len)
Definition: octstr.c:423
Octstr * inbuf
Definition: conn.c:133
long inbufpos
Definition: conn.c:134

◆ conn_register_real()

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

◆ conn_set_output_buffering()

void conn_set_output_buffering ( Connection conn,
unsigned int  size 
)

Definition at line 723 of file conn.c.

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

724 {
725  lock_out(conn);
726  conn->output_buffering = size;
727  /* If the buffer size is smaller, we may have to write immediately. */
728  unlocked_try_write(conn);
729  unlock_out(conn);
730 }
int size
Definition: wsasm.c:84
static int unlocked_try_write(Connection *conn)
Definition: conn.c:289
unsigned int output_buffering
Definition: conn.c:130
static void lock_out(Connection *conn)
Definition: conn.c:197
#define unlock_out(conn)
Definition: conn.c:169

◆ conn_unregister()

void conn_unregister ( Connection conn)

Definition at line 852 of file conn.c.

References Connection::callback, Connection::callback_data, Connection::callback_data_destroyer, 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 conn_pool_get(), handle_transaction(), port_remove(), and receive_request().

853 {
854  FDSet *set = NULL;
855  int fd = -1;
856  void *data = NULL;
857  conn_callback_data_destroyer_t *destroyer = NULL;
858 
859  gw_assert(conn != NULL);
860 
861  if (conn == NULL || conn->fd < 0)
862  return;
863 
864  /* We need both locks to update the registration information */
865  lock_out(conn);
866  lock_in(conn);
867 
868  if (conn->registered) {
869  set = conn->registered;
870  fd = conn->fd;
871  conn->registered = NULL;
872  conn->callback = NULL;
873  /*
874  * remember and don't destroy data and data_destroyer because we
875  * may be in callback right now. So destroy only after fdset_unregister
876  * call which guarantee us we are not in callback anymore.
877  */
878  data = conn->callback_data;
879  conn->callback_data = NULL;
880  destroyer = conn->callback_data_destroyer;
881  conn->callback_data_destroyer = NULL;
882  conn->listening_pollin = 0;
883  conn->listening_pollout = 0;
884  }
885 
886  unlock_in(conn);
887  unlock_out(conn);
888 
889  /* now unregister from FDSet */
890  if (set != NULL)
891  fdset_unregister(set, fd);
892 
893  /* ok we are not in callback anymore, destroy data if any */
894  if (data != NULL && destroyer != NULL)
895  destroyer(data);
896 }
int listening_pollout
Definition: conn.c:148
conn_callback_t * callback
Definition: conn.c:142
gw_assert(wtls_machine->packet_to_send !=NULL)
FDSet * registered
Definition: conn.c:141
int listening_pollin
Definition: conn.c:146
void * callback_data
Definition: conn.c:143
void fdset_unregister(FDSet *set, int fd)
Definition: fdset.c:510
static void lock_out(Connection *conn)
Definition: conn.c:197
conn_callback_data_destroyer_t * callback_data_destroyer
Definition: conn.c:144
void conn_callback_data_destroyer_t(void *data)
Definition: conn.h:113
#define unlock_in(conn)
Definition: conn.c:168
int fd
Definition: conn.c:119
static void lock_in(Connection *conn)
Definition: conn.c:172
Definition: fdset.c:70
#define unlock_out(conn)
Definition: conn.c:169

◆ conn_wait()

int conn_wait ( Connection conn,
double  seconds 
)

Definition at line 898 of file conn.c.

References error(), Connection::fd, gwthread_pollfd(), Connection::io_error, lock_in(), lock_out(), POLLERR, POLLHUP, POLLIN, POLLNVAL, POLLOUT, 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().

899 {
900  int events;
901  int ret;
902  int fd;
903 
904  lock_out(conn);
905 
906  /* Try to write any data that might still be waiting to be sent */
907  ret = unlocked_write(conn);
908  if (ret < 0) {
909  unlock_out(conn);
910  return -1;
911  }
912  if (ret > 0) {
913  /* We did something useful. No need to poll or wait now. */
914  unlock_out(conn);
915  return 0;
916  }
917 
918  fd = conn->fd;
919 
920  /* Normally, we block until there is more data available. But
921  * if any data still needs to be sent, we block until we can
922  * send it (or there is more data available). We always block
923  * for reading, unless we know there is no more data coming.
924  * (Because in that case, poll will keep reporting POLLIN to
925  * signal the end of the file). If the caller explicitly wants
926  * to wait even though there is no data to write and we're at
927  * end of file, then poll for new data anyway because the caller
928  * apparently doesn't trust eof. */
929  events = 0;
930  if (unlocked_outbuf_len(conn) > 0)
931  events |= POLLOUT;
932  /* Don't keep the connection locked while we wait */
933  unlock_out(conn);
934 
935  /* We need the in lock to query read_eof */
936  lock_in(conn);
937  if ((conn->read_eof == 0 && conn->io_error == 0) || events == 0)
938  events |= POLLIN;
939  unlock_in(conn);
940 
941  ret = gwthread_pollfd(fd, events, seconds);
942  if (ret < 0) {
943  if (errno == EINTR)
944  return 0;
945  error(0, "conn_wait: poll failed on fd %d:", fd);
946  return -1;
947  }
948 
949  if (ret == 0)
950  return 1;
951 
952  if (ret & POLLNVAL) {
953  error(0, "conn_wait: fd %d not open.", fd);
954  return -1;
955  }
956 
957  if (ret & (POLLERR | POLLHUP)) {
958  /* Call unlocked_read to report the specific error,
959  * and handle the results of the error. We can't be
960  * certain that the error still exists, because we
961  * released the lock for a while. */
962  lock_in(conn);
963  unlocked_read(conn);
964  unlock_in(conn);
965  return -1;
966  }
967 
968  /* If POLLOUT is on, then we must have wanted
969  * to write something. */
970  if (ret & POLLOUT) {
971  lock_out(conn);
972  unlocked_write(conn);
973  unlock_out(conn);
974  }
975 
976  /* Since we normally select for reading, we must
977  * try to read here. Otherwise, if the caller loops
978  * around conn_wait without making conn_read* calls
979  * in between, we will keep polling this same data. */
980  if (ret & POLLIN) {
981  lock_in(conn);
982  unlocked_read(conn);
983  unlock_in(conn);
984  }
985 
986  return 0;
987 }
void error(int err, const char *fmt,...)
Definition: log.c:648
int read_eof
Definition: conn.c:136
static long unlocked_outbuf_len(Connection *conn)
Definition: conn.c:222
#define POLLNVAL
Definition: gwpoll.h:98
int io_error
Definition: conn.c:137
static void lock_out(Connection *conn)
Definition: conn.c:197
#define POLLIN
Definition: gwpoll.h:91
static void unlocked_read(Connection *conn)
Definition: conn.c:310
#define unlock_in(conn)
Definition: conn.c:168
int fd
Definition: conn.c:119
#define POLLERR
Definition: gwpoll.h:96
int gwthread_pollfd(int fd, int events, double timeout)
static void lock_in(Connection *conn)
Definition: conn.c:172
static long unlocked_write(Connection *conn)
Definition: conn.c:235
#define unlock_out(conn)
Definition: conn.c:169
#define POLLHUP
Definition: gwpoll.h:97
#define POLLOUT
Definition: gwpoll.h:93

◆ conn_wrap_fd()

Connection* conn_wrap_fd ( int  fd,
int  ssl 
)

Definition at line 560 of file conn.c.

References Connection::callback, Connection::callback_data, Connection::callback_data_destroyer, Connection::claimed, conn_destroy(), Connection::connected, DEFAULT_OUTPUT_BUFFERING, error(), Connection::fd, Connection::inbuf, Connection::inbufpos, Connection::inlock, Connection::io_error, Connection::listening_pollin, Connection::listening_pollout, mutex_create, octstr_create, Connection::outbuf, Connection::outbufpos, Connection::outlock, Connection::output_buffering, Connection::read_eof, Connection::registered, socket_set_blocking(), and Connection::yes.

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().

561 {
562  Connection *conn;
563 
564  if (socket_set_blocking(fd, 0) < 0)
565  return NULL;
566 
567  conn = gw_malloc(sizeof(*conn));
568  conn->inlock = mutex_create();
569  conn->outlock = mutex_create();
570  conn->claimed = 0;
571 
572  conn->outbuf = octstr_create("");
573  conn->outbufpos = 0;
574  conn->inbuf = octstr_create("");
575  conn->inbufpos = 0;
576 
577  conn->fd = fd;
578  conn->connected = yes;
579  conn->read_eof = 0;
580  conn->io_error = 0;
582 
583  conn->registered = NULL;
584  conn->callback = NULL;
585  conn->callback_data = NULL;
586  conn->callback_data_destroyer = NULL;
587  conn->listening_pollin = 0;
588  conn->listening_pollout = 0;
589 #ifdef HAVE_LIBSSL
590  /*
591  * do all the SSL magic for this connection
592  */
593  if (ssl) {
594  conn->ssl = SSL_new(global_server_ssl_context);
595  conn->peer_certificate = NULL;
596 
597  /* SSL_set_fd can fail, so check it */
598  if (SSL_set_fd(conn->ssl, conn->fd) == 0) {
599  /* SSL_set_fd failed, log error and return NULL */
600  error(errno, "SSL: OpenSSL: %.256s", ERR_error_string(ERR_get_error(), NULL));
601  conn_destroy(conn);
602  return NULL;
603  }
604  /* SSL_set_verify(conn->ssl, 0, NULL); */
605 
606  /* set read/write BIO layer to non-blocking mode */
607  BIO_set_nbio(SSL_get_rbio(conn->ssl), 1);
608  BIO_set_nbio(SSL_get_wbio(conn->ssl), 1);
609 
610  /* set accept state , SSL-Handshake will be handled transparent while SSL_[read|write] */
611  SSL_set_accept_state(conn->ssl);
612  } else {
613  conn->ssl = NULL;
614  conn->peer_certificate = NULL;
615  }
616 #endif /* HAVE_LIBSSL */
617 
618  return conn;
619 }
int listening_pollout
Definition: conn.c:148
void error(int err, const char *fmt,...)
Definition: log.c:648
int socket_set_blocking(int fd, int blocking)
Definition: socket.c:368
conn_callback_t * callback
Definition: conn.c:142
int ssl
Mutex * outlock
Definition: conn.c:112
FDSet * registered
Definition: conn.c:141
unsigned int output_buffering
Definition: conn.c:130
int read_eof
Definition: conn.c:136
#define mutex_create()
Definition: thread.h:96
int listening_pollin
Definition: conn.c:146
long outbufpos
Definition: conn.c:126
void * callback_data
Definition: conn.c:143
Octstr * outbuf
Definition: conn.c:125
int io_error
Definition: conn.c:137
volatile sig_atomic_t claimed
Definition: conn.c:113
conn_callback_data_destroyer_t * callback_data_destroyer
Definition: conn.c:144
#define DEFAULT_OUTPUT_BUFFERING
Definition: conn.c:103
void conn_destroy(Connection *conn)
Definition: conn.c:621
int fd
Definition: conn.c:119
#define octstr_create(cstr)
Definition: octstr.h:125
Mutex * inlock
Definition: conn.c:111
enum Connection::@56 connected
Octstr * inbuf
Definition: conn.c:133
long inbufpos
Definition: conn.c:134

◆ conn_write()

int conn_write ( Connection conn,
Octstr data 
)

Definition at line 1045 of file conn.c.

References 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_gnack(), 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().

1046 {
1047  int ret;
1048 
1049  lock_out(conn);
1050  octstr_append(conn->outbuf, data);
1051  ret = unlocked_try_write(conn);
1052  unlock_out(conn);
1053 
1054  return ret;
1055 }
static int unlocked_try_write(Connection *conn)
Definition: conn.c:289
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1502
Octstr * outbuf
Definition: conn.c:125
static void lock_out(Connection *conn)
Definition: conn.c:197
#define unlock_out(conn)
Definition: conn.c:169

◆ conn_write_data()

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

Definition at line 1057 of file conn.c.

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

1058 {
1059  int ret;
1060 
1061  lock_out(conn);
1062  octstr_append_data(conn->outbuf, data, length);
1063  ret = unlocked_try_write(conn);
1064  unlock_out(conn);
1065 
1066  return ret;
1067 }
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:289
Octstr * outbuf
Definition: conn.c:125
static void lock_out(Connection *conn)
Definition: conn.c:197
#define unlock_out(conn)
Definition: conn.c:169

◆ conn_write_withlen()

int conn_write_withlen ( Connection conn,
Octstr data 
)

Definition at line 1069 of file conn.c.

References 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().

1070 {
1071  int ret;
1072  unsigned char lengthbuf[4];
1073 
1074  encode_network_long(lengthbuf, octstr_len(data));
1075  lock_out(conn);
1076  octstr_append_data(conn->outbuf, lengthbuf, 4);
1077  octstr_append(conn->outbuf, data);
1078  ret = unlocked_try_write(conn);
1079  unlock_out(conn);
1080 
1081  return ret;
1082 }
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:289
void encode_network_long(unsigned char *data, unsigned long value)
Definition: utils.c:940
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1502
Octstr * outbuf
Definition: conn.c:125
static void lock_out(Connection *conn)
Definition: conn.c:197
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
#define unlock_out(conn)
Definition: conn.c:169
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.