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

Write to a socket Connection. More...

+ Collaboration diagram for write():

Functions

static int tls_socket_write (struct Connection *conn, const char *buf, size_t count)
 Write data to a TLS socket - Implements Connection::write() -. More...
 
static int ssl_socket_write (struct Connection *conn, const char *buf, size_t count)
 Write data to an SSL socket - Implements Connection::write() -. More...
 
int raw_socket_write (struct Connection *conn, const char *buf, size_t count)
 Write data to a socket - Implements Connection::write() -. More...
 
static int mutt_sasl_conn_write (struct Connection *conn, const char *buf, size_t count)
 Write to an SASL connection - Implements Connection::write() -. More...
 
static int tunnel_socket_write (struct Connection *conn, const char *buf, size_t count)
 Write data to a tunnel socket - Implements Connection::write() -. More...
 
static int zstrm_write (struct Connection *conn, const char *buf, size_t count)
 Write compressed data to a socket - Implements Connection::write() -. More...
 

Variables

int(* SaslSockData::write )(struct Connection *conn, const char *buf, size_t count)
 Write to a socket Connection - Implements Connection::write() -. More...
 

Detailed Description

Write to a socket Connection.

Parameters
connConnection to a server
bufBuffer to read into
countNumber of bytes to read
Return values
>0Success, number of bytes written
-1Error, see errno

Function Documentation

◆ tls_socket_write()

static int tls_socket_write ( struct Connection conn,
const char *  buf,
size_t  count 
)
static

Write data to a TLS socket - Implements Connection::write() -.

Definition at line 1094 of file gnutls.c.

1095 {
1096  struct TlsSockData *data = conn->sockdata;
1097  size_t sent = 0;
1098 
1099  if (!data)
1100  {
1101  mutt_error(_("Error: no TLS socket open"));
1102  return -1;
1103  }
1104 
1105  do
1106  {
1107  int ret;
1108  do
1109  {
1110  ret = gnutls_record_send(data->state, buf + sent, count - sent);
1111  } while ((ret == GNUTLS_E_AGAIN) || (ret == GNUTLS_E_INTERRUPTED));
1112 
1113  if (ret < 0)
1114  {
1115  mutt_error("tls_socket_write (%s)", gnutls_strerror(ret));
1116  return -1;
1117  }
1118 
1119  sent += ret;
1120  } while (sent < count);
1121 
1122  return sent;
1123 }
#define mutt_error(...)
Definition: logging.h:88
void * sockdata
Backend-specific socket data.
Definition: connection.h:46
#define _(a)
Definition: message.h:28
gnutls_session_t state
Definition: gnutls.c:81
TLS socket data -.
Definition: gnutls.c:79
+ Here is the caller graph for this function:

◆ ssl_socket_write()

static int ssl_socket_write ( struct Connection conn,
const char *  buf,
size_t  count 
)
static

Write data to an SSL socket - Implements Connection::write() -.

Definition at line 1369 of file openssl.c.

1370 {
1371  if (!conn || !conn->sockdata || !buf || (count == 0))
1372  return -1;
1373 
1374  int rc = SSL_write(sockdata(conn)->ssl, buf, count);
1375  if ((rc <= 0) || (errno == EINTR))
1376  {
1377  if (errno == EINTR)
1378  {
1379  rc = -1;
1380  }
1381  ssl_err(sockdata(conn), rc);
1382  }
1383 
1384  return rc;
1385 }
void * sockdata
Backend-specific socket data.
Definition: connection.h:46
static void ssl_err(struct SslSockData *data, int err)
Display an SSL error message.
Definition: openssl.c:238
static struct SslSockData * sockdata(struct Connection *conn)
Get a Connection&#39;s socket data.
Definition: openssl.c:1198
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ raw_socket_write()

int raw_socket_write ( struct Connection conn,
const char *  buf,
size_t  count 
)

Write data to a socket - Implements Connection::write() -.

Definition at line 297 of file raw.c.

298 {
299  int rc;
300  size_t sent = 0;
301 
303  do
304  {
305  do
306  {
307  rc = write(conn->fd, buf + sent, count - sent);
308  } while (rc < 0 && (errno == EINTR));
309 
310  if (rc < 0)
311  {
312  mutt_error(_("Error talking to %s (%s)"), conn->account.host, strerror(errno));
314  return -1;
315  }
316 
317  sent += rc;
318  } while ((sent < count) && !SigInt);
319 
321  return sent;
322 }
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:40
#define mutt_error(...)
Definition: logging.h:88
WHERE SIG_ATOMIC_VOLATILE_T SigInt
true after SIGINT is received
Definition: mutt_globals.h:67
#define _(a)
Definition: message.h:28
void mutt_sig_allow_interrupt(bool allow)
Allow/disallow Ctrl-C (SIGINT)
Definition: signal.c:238
char host[128]
Server to login to.
Definition: connaccount.h:53
int fd
Socket file descriptor.
Definition: connection.h:44
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:

◆ mutt_sasl_conn_write()

static int mutt_sasl_conn_write ( struct Connection conn,
const char *  buf,
size_t  count 
)
static

Write to an SASL connection - Implements Connection::write() -.

Definition at line 531 of file sasl.c.

532 {
533  int rc;
534  const char *pbuf = NULL;
535  unsigned int olen, plen;
536 
537  struct SaslSockData *sasldata = conn->sockdata;
538  conn->sockdata = sasldata->sockdata;
539 
540  /* encode data, if necessary */
541  if (*sasldata->ssf != 0)
542  {
543  /* handle data larger than MAXOUTBUF */
544  do
545  {
546  olen = (count > *sasldata->pbufsize) ? *sasldata->pbufsize : count;
547 
548  rc = sasl_encode(sasldata->saslconn, buf, olen, &pbuf, &plen);
549  if (rc != SASL_OK)
550  {
551  mutt_debug(LL_DEBUG1, "SASL encoding failed: %s\n", sasl_errstring(rc, NULL, NULL));
552  goto fail;
553  }
554 
555  rc = sasldata->write(conn, pbuf, plen);
556  if (rc != plen)
557  goto fail;
558 
559  count -= olen;
560  buf += olen;
561  } while (count > *sasldata->pbufsize);
562  }
563  else
564  {
565  /* just write using the underlying socket function */
566  rc = sasldata->write(conn, buf, count);
567  }
568 
569  conn->sockdata = sasldata;
570 
571  return rc;
572 
573 fail:
574  conn->sockdata = sasldata;
575  return -1;
576 }
static size_t plen
Length of cached packet.
Definition: pgppacket.c:39
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(* write)(struct Connection *conn, const char *buf, size_t count)
Write to a socket Connection - Implements Connection::write() -.
Definition: sasl.c:86
const sasl_ssf_t * ssf
Definition: sasl.c:63
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Log at debug level 1.
Definition: logging.h:40
const char * buf
Definition: sasl.c:67
const unsigned int * pbufsize
Definition: sasl.c:64
static unsigned char * pbuf
Cache PGP data packet.
Definition: pgppacket.c:38
+ Here is the caller graph for this function:

◆ tunnel_socket_write()

static int tunnel_socket_write ( struct Connection conn,
const char *  buf,
size_t  count 
)
static

Write data to a tunnel socket - Implements Connection::write() -.

Definition at line 159 of file tunnel.c.

160 {
161  struct TunnelSockData *tunnel = conn->sockdata;
162  int rc;
163  size_t sent = 0;
164 
165  do
166  {
167  do
168  {
169  rc = write(tunnel->fd_write, buf + sent, count - sent);
170  } while (rc < 0 && errno == EINTR);
171 
172  if (rc < 0)
173  {
174  mutt_error(_("Tunnel error talking to %s: %s"), conn->account.host, strerror(errno));
175  return -1;
176  }
177 
178  sent += rc;
179  } while (sent < count);
180 
181  return sent;
182 }
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:40
#define mutt_error(...)
Definition: logging.h:88
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
int fd_write
File descriptor to write to.
Definition: tunnel.c:51
+ Here is the caller graph for this function:

◆ zstrm_write()

static int zstrm_write ( struct Connection conn,
const char *  buf,
size_t  count 
)
static

Write compressed data to a socket - Implements Connection::write() -.

Definition at line 228 of file zstrm.c.

229 {
230  struct ZstrmContext *zctx = conn->sockdata;
231  int rc;
232 
233  zctx->write.z.avail_in = (uInt) count;
234  zctx->write.z.next_in = (Bytef *) buf;
235  zctx->write.z.avail_out = (uInt) zctx->write.len;
236  zctx->write.z.next_out = (Bytef *) zctx->write.buf;
237 
238  do
239  {
240  int zrc = deflate(&zctx->write.z, Z_PARTIAL_FLUSH);
241  if (zrc == Z_OK)
242  {
243  /* push out produced data to the underlying stream */
244  zctx->write.pos = zctx->write.len - zctx->write.z.avail_out;
245  char *wbufp = zctx->write.buf;
246  mutt_debug(LL_DEBUG5, "deflate consumed %d/%d bytes\n",
247  count - zctx->write.z.avail_in, count);
248  while (zctx->write.pos > 0)
249  {
250  rc = zctx->next_conn.write(&zctx->next_conn, wbufp, zctx->write.pos);
251  mutt_debug(LL_DEBUG5, "next stream wrote: %d bytes\n", rc);
252  if (rc < 0)
253  return -1; /* we can't recover from write failure */
254 
255  wbufp += rc;
256  zctx->write.pos -= rc;
257  }
258 
259  /* see if there's more for us to do, retry if the output buffer
260  * was full (there may be something in zlib buffers), and retry
261  * when there is still available input data */
262  if ((zctx->write.z.avail_out != 0) && (zctx->write.z.avail_in == 0))
263  break;
264 
265  zctx->write.z.avail_out = (uInt) zctx->write.len;
266  zctx->write.z.next_out = (Bytef *) zctx->write.buf;
267  }
268  else
269  {
270  /* compression went wrong, but this is basically impossible
271  * according to the docs */
272  return -1;
273  }
274  } while (true);
275 
276  rc = (int) count;
277  return (rc <= 0) ? 1 : rc; /* avoid wrong behaviour due to overflow */
278 }
Data compression layer.
Definition: zstrm.c:54
struct ZstrmDirection write
Data being compressed and written.
Definition: zstrm.c:57
void * sockdata
Backend-specific socket data.
Definition: connection.h:46
struct Connection next_conn
Underlying stream.
Definition: zstrm.c:58
unsigned int len
Length of data.
Definition: zstrm.c:45
unsigned int pos
Current position.
Definition: zstrm.c:46
char * buf
Buffer for data being (de-)compressed.
Definition: zstrm.c:44
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
int(* write)(struct Connection *conn, const char *buf, size_t count)
Definition: connection.h:95
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

◆ write

int(* SaslSockData::write) (struct Connection *conn, const char *buf, size_t count)

Write to a socket Connection - Implements Connection::write() -.

Definition at line 86 of file sasl.c.