NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN

Close a socket Connection. More...

+ Collaboration diagram for close():

Functions

static int tls_socket_close (struct Connection *conn)
 Close a TLS socket - Implements Connection::close() -. More...
 
static int tls_starttls_close (struct Connection *conn)
 Close a TLS connection - Implements Connection::close() -. More...
 
static int ssl_socket_close_and_restore (struct Connection *conn)
 Close an SSL Connection and restore Connection callbacks - Implements Connection::close() -. More...
 
static int ssl_socket_close (struct Connection *conn)
 Close an SSL connection - Implements Connection::close() -. More...
 
int raw_socket_close (struct Connection *conn)
 Close a socket - Implements Connection::close() -. More...
 
static int mutt_sasl_conn_close (struct Connection *conn)
 close SASL connection - Implements Connection::close() -Calls underlying close function and disposes of the sasl_conn_t object, then restores connection to pre-sasl state More...
 
static int tunnel_socket_close (struct Connection *conn)
 Close a tunnel socket - Implements Connection::close() -. More...
 
static int zstrm_close (struct Connection *conn)
 Close a socket - Implements Connection::close() -. More...
 

Variables

int(* SaslSockData::close )(struct Connection *conn)
 Close a socket Connection - Implements Connection::close() -. More...
 

Detailed Description

Close a socket Connection.

Parameters
connConnection to a server
Return values
0Success
-1Error, see errno

Function Documentation

◆ tls_socket_close()

static int tls_socket_close ( struct Connection conn)
static

Close a TLS socket - Implements Connection::close() -.

Definition at line 1025 of file gnutls.c.

1026 {
1027  struct TlsSockData *data = conn->sockdata;
1028  if (data)
1029  {
1030  /* shut down only the write half to avoid hanging waiting for the remote to respond.
1031  *
1032  * RFC5246 7.2.1. "Closure Alerts"
1033  *
1034  * It is not required for the initiator of the close to wait for the
1035  * responding close_notify alert before closing the read side of the
1036  * connection. */
1037  gnutls_bye(data->state, GNUTLS_SHUT_WR);
1038 
1039  gnutls_certificate_free_credentials(data->xcred);
1040  gnutls_deinit(data->state);
1041  FREE(&conn->sockdata);
1042  }
1043 
1044  return raw_socket_close(conn);
1045 }
void * sockdata
Backend-specific socket data.
Definition: connection.h:46
gnutls_session_t state
Definition: gnutls.c:81
gnutls_certificate_credentials_t xcred
Definition: gnutls.c:82
TLS socket data -.
Definition: gnutls.c:79
int raw_socket_close(struct Connection *conn)
Close a socket - Implements Connection::close() -.
Definition: raw.c:365
#define FREE(x)
Definition: memory.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ tls_starttls_close()

static int tls_starttls_close ( struct Connection conn)
static

Close a TLS connection - Implements Connection::close() -.

Definition at line 1128 of file gnutls.c.

1129 {
1130  int rc;
1131 
1132  rc = tls_socket_close(conn);
1133  conn->read = raw_socket_read;
1134  conn->write = raw_socket_write;
1135  conn->close = raw_socket_close;
1136  conn->poll = raw_socket_poll;
1137 
1138  return rc;
1139 }
int(* read)(struct Connection *conn, char *buf, size_t count)
Definition: connection.h:82
int raw_socket_write(struct Connection *conn, const char *buf, size_t count)
Write data to a socket - Implements Connection::write() -.
Definition: raw.c:297
static int tls_socket_close(struct Connection *conn)
Close a TLS socket - Implements Connection::close() -.
Definition: gnutls.c:1025
int(* close)(struct Connection *conn)
Definition: connection.h:119
int raw_socket_poll(struct Connection *conn, time_t wait_secs)
Checks whether reads would block - Implements Connection::poll() -.
Definition: raw.c:327
int raw_socket_close(struct Connection *conn)
Close a socket - Implements Connection::close() -.
Definition: raw.c:365
int(* poll)(struct Connection *conn, time_t wait_secs)
Definition: connection.h:108
int raw_socket_read(struct Connection *conn, char *buf, size_t len)
Read data from a socket - Implements Connection::read() -.
Definition: raw.c:267
int(* write)(struct Connection *conn, const char *buf, size_t count)
Definition: connection.h:95
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ssl_socket_close_and_restore()

static int ssl_socket_close_and_restore ( struct Connection conn)
static

Close an SSL Connection and restore Connection callbacks - Implements Connection::close() -.

Definition at line 627 of file openssl.c.

628 {
629  int rc = ssl_socket_close(conn);
630  conn->read = raw_socket_read;
631  conn->write = raw_socket_write;
632  conn->close = raw_socket_close;
633  conn->poll = raw_socket_poll;
634 
635  return rc;
636 }
int(* read)(struct Connection *conn, char *buf, size_t count)
Definition: connection.h:82
int raw_socket_write(struct Connection *conn, const char *buf, size_t count)
Write data to a socket - Implements Connection::write() -.
Definition: raw.c:297
static int ssl_socket_close(struct Connection *conn)
Close an SSL connection - Implements Connection::close() -.
Definition: openssl.c:1390
int(* close)(struct Connection *conn)
Definition: connection.h:119
int raw_socket_poll(struct Connection *conn, time_t wait_secs)
Checks whether reads would block - Implements Connection::poll() -.
Definition: raw.c:327
int raw_socket_close(struct Connection *conn)
Close a socket - Implements Connection::close() -.
Definition: raw.c:365
int(* poll)(struct Connection *conn, time_t wait_secs)
Definition: connection.h:108
int raw_socket_read(struct Connection *conn, char *buf, size_t len)
Read data from a socket - Implements Connection::read() -.
Definition: raw.c:267
int(* write)(struct Connection *conn, const char *buf, size_t count)
Definition: connection.h:95
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ssl_socket_close()

static int ssl_socket_close ( struct Connection conn)
static

Close an SSL connection - Implements Connection::close() -.

Definition at line 1390 of file openssl.c.

1391 {
1392  struct SslSockData *data = sockdata(conn);
1393 
1394  if (data)
1395  {
1396  if (data->isopen && (raw_socket_poll(conn, 0) >= 0))
1397  SSL_shutdown(data->ssl);
1398 
1399  SSL_free(data->ssl);
1400  data->ssl = NULL;
1401  SSL_CTX_free(data->sctx);
1402  data->sctx = NULL;
1403  FREE(&conn->sockdata);
1404  }
1405 
1406  return raw_socket_close(conn);
1407 }
void * sockdata
Backend-specific socket data.
Definition: connection.h:46
static struct SslSockData * sockdata(struct Connection *conn)
Get a Connection's socket data.
Definition: openssl.c:1198
int raw_socket_poll(struct Connection *conn, time_t wait_secs)
Checks whether reads would block - Implements Connection::poll() -.
Definition: raw.c:327
int raw_socket_close(struct Connection *conn)
Close a socket - Implements Connection::close() -.
Definition: raw.c:365
#define FREE(x)
Definition: memory.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ raw_socket_close()

int raw_socket_close ( struct Connection conn)

Close a socket - Implements Connection::close() -.

Definition at line 365 of file raw.c.

366 {
367  return close(conn->fd);
368 }
int fd
Socket file descriptor.
Definition: connection.h:44
+ Here is the caller graph for this function:

◆ mutt_sasl_conn_close()

static int mutt_sasl_conn_close ( struct Connection conn)
static

close SASL connection - Implements Connection::close() -Calls underlying close function and disposes of the sasl_conn_t object, then restores connection to pre-sasl state

Definition at line 442 of file sasl.c.

443 {
444  struct SaslSockData *sasldata = conn->sockdata;
445 
446  /* restore connection's underlying methods */
447  conn->sockdata = sasldata->sockdata;
448  conn->open = sasldata->open;
449  conn->read = sasldata->read;
450  conn->write = sasldata->write;
451  conn->poll = sasldata->poll;
452  conn->close = sasldata->close;
453 
454  /* release sasl resources */
455  sasl_dispose(&sasldata->saslconn);
456  FREE(&sasldata);
457 
458  /* call underlying close */
459  int rc = conn->close(conn);
460 
461  return rc;
462 }
int(* read)(struct Connection *conn, char *buf, size_t count)
Definition: connection.h:82
SASL authentication API -.
Definition: sasl.c:60
void * sockdata
Backend-specific socket data.
Definition: connection.h:46
sasl_conn_t * saslconn
Definition: sasl.c:62
void * sockdata
Underlying socket data.
Definition: sasl.c:71
int(* open)(struct Connection *conn)
Definition: connection.h:69
int(* write)(struct Connection *conn, const char *buf, size_t count)
Write to a socket Connection - Implements Connection::write() -.
Definition: sasl.c:86
int(* open)(struct Connection *conn)
Open a socket Connection - Implements Connection::open() -.
Definition: sasl.c:76
int(* poll)(struct Connection *conn, time_t wait_secs)
Check whether a socket read would block - Implements Connection::poll() -.
Definition: sasl.c:91
int(* close)(struct Connection *conn)
Definition: connection.h:119
int(* poll)(struct Connection *conn, time_t wait_secs)
Definition: connection.h:108
int(* write)(struct Connection *conn, const char *buf, size_t count)
Definition: connection.h:95
int(* read)(struct Connection *conn, char *buf, size_t count)
Read from a socket Connection - Implements Connection::read() -.
Definition: sasl.c:81
#define FREE(x)
Definition: memory.h:40
int(* close)(struct Connection *conn)
Close a socket Connection - Implements Connection::close() -.
Definition: sasl.c:96
+ Here is the caller graph for this function:

◆ tunnel_socket_close()

static int tunnel_socket_close ( struct Connection conn)
static

Close a tunnel socket - Implements Connection::close() -.

Definition at line 204 of file tunnel.c.

205 {
206  struct TunnelSockData *tunnel = conn->sockdata;
207  if (!tunnel)
208  {
209  return 0;
210  }
211 
212  int status;
213 
214  close(tunnel->fd_read);
215  close(tunnel->fd_write);
216  waitpid(tunnel->pid, &status, 0);
217  if (!WIFEXITED(status) || WEXITSTATUS(status))
218  {
219  mutt_error(_("Tunnel to %s returned error %d (%s)"), conn->account.host,
220  WEXITSTATUS(status), NONULL(mutt_str_sysexit(WEXITSTATUS(status))));
221  }
222  FREE(&conn->sockdata);
223 
224  return 0;
225 }
pid_t pid
Process ID of tunnel program.
Definition: tunnel.c:49
#define NONULL(x)
Definition: string2.h:37
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:40
#define mutt_error(...)
Definition: logging.h:88
int fd_read
File descriptor to read from.
Definition: tunnel.c:50
void * sockdata
Backend-specific socket data.
Definition: connection.h:46
#define _(a)
Definition: message.h:28
A network tunnel (pair of sockets)
Definition: tunnel.c:47
char host[128]
Server to login to.
Definition: connaccount.h:53
const char * mutt_str_sysexit(int err_num)
Return a string matching an error code.
Definition: string.c:112
int fd_write
File descriptor to write to.
Definition: tunnel.c:51
#define FREE(x)
Definition: memory.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ zstrm_close()

static int zstrm_close ( struct Connection conn)
static

Close a socket - Implements Connection::close() -.

Definition at line 97 of file zstrm.c.

98 {
99  struct ZstrmContext *zctx = conn->sockdata;
100 
101  int rc = zctx->next_conn.close(&zctx->next_conn);
102 
103  mutt_debug(LL_DEBUG5, "read %llu->%llu (%.1fx) wrote %llu<-%llu (%.1fx)\n",
104  zctx->read.z.total_in, zctx->read.z.total_out,
105  (float) zctx->read.z.total_out / (float) zctx->read.z.total_in,
106  zctx->write.z.total_in, zctx->write.z.total_out,
107  (float) zctx->write.z.total_in / (float) zctx->write.z.total_out);
108 
109  // Restore the Connection's original functions
110  conn->sockdata = zctx->next_conn.sockdata;
111  conn->open = zctx->next_conn.open;
112  conn->close = zctx->next_conn.close;
113  conn->read = zctx->next_conn.read;
114  conn->write = zctx->next_conn.write;
115  conn->poll = zctx->next_conn.poll;
116 
117  inflateEnd(&zctx->read.z);
118  deflateEnd(&zctx->write.z);
119  FREE(&zctx->read.buf);
120  FREE(&zctx->write.buf);
121  FREE(&zctx);
122 
123  return rc;
124 }
Data compression layer.
Definition: zstrm.c:54
struct ZstrmDirection write
Data being compressed and written.
Definition: zstrm.c:57
int(* read)(struct Connection *conn, char *buf, size_t count)
Definition: connection.h:82
void * sockdata
Backend-specific socket data.
Definition: connection.h:46
struct Connection next_conn
Underlying stream.
Definition: zstrm.c:58
int(* open)(struct Connection *conn)
Definition: connection.h:69
struct ZstrmDirection read
Data being read and de-compressed.
Definition: zstrm.c:56
int(* close)(struct Connection *conn)
Definition: connection.h:119
char * buf
Buffer for data being (de-)compressed.
Definition: zstrm.c:44
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
int(* poll)(struct Connection *conn, time_t wait_secs)
Definition: connection.h:108
int(* write)(struct Connection *conn, const char *buf, size_t count)
Definition: connection.h:95
#define FREE(x)
Definition: memory.h:40
z_stream z
zlib compression handle
Definition: zstrm.c:43
Log at debug level 5.
Definition: logging.h:44
+ Here is the caller graph for this function:

Variable Documentation

◆ close

int(* SaslSockData::close) (struct Connection *conn)

Close a socket Connection - Implements Connection::close() -.

Definition at line 96 of file sasl.c.