NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches

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() -.
 
static int ssl_socket_write (struct Connection *conn, const char *buf, size_t count)
 Write data to an SSL socket - Implements Connection::write() -.
 
int raw_socket_write (struct Connection *conn, const char *buf, size_t count)
 Write data to a socket - Implements Connection::write() -.
 
static int mutt_sasl_conn_write (struct Connection *conn, const char *buf, size_t count)
 Write to an SASL connection - Implements Connection::write() -.
 
static int tunnel_socket_write (struct Connection *conn, const char *buf, size_t count)
 Write data to a tunnel socket - Implements Connection::write() -.
 
static int zstrm_write (struct Connection *conn, const char *buf, size_t count)
 Write compressed data to a socket - Implements Connection::write() -.
 

Variables

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

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 1093 of file gnutls.c.

1094{
1095 struct TlsSockData *data = conn->sockdata;
1096 size_t sent = 0;
1097
1098 if (!data)
1099 {
1100 mutt_error(_("Error: no TLS socket open"));
1101 return -1;
1102 }
1103
1104 do
1105 {
1106 int rc;
1107 do
1108 {
1109 rc = gnutls_record_send(data->session, buf + sent, count - sent);
1110 if (rc == GNUTLS_E_AGAIN)
1111 {
1112 // Socket would block, wait for data to become available
1114 return -1;
1115 }
1116 } while ((rc == GNUTLS_E_AGAIN) || (rc == GNUTLS_E_INTERRUPTED));
1117
1118 if (rc < 0)
1119 {
1120 mutt_error("tls_socket_write (%s)", gnutls_strerror(rc));
1121 return -1;
1122 }
1123
1124 sent += rc;
1125 } while (sent < count);
1126
1127 return sent;
1128}
static bool tls_socket_poll_with_timeout(struct Connection *conn)
Poll a socket with configured timeout.
Definition gnutls.c:856
#define mutt_error(...)
Definition logging2.h:94
#define _(a)
Definition message.h:28
void * sockdata
Backend-specific socket data.
Definition connection.h:55
TLS socket data -.
Definition gnutls.c:83
gnutls_session_t session
GNUTLS session.
Definition gnutls.c:84
+ Here is the call graph for this function:
+ 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 1362 of file openssl.c.

1363{
1364 if (!conn || !conn->sockdata || !buf || (count == 0))
1365 return -1;
1366
1367 struct SslSockData *data = sockdata(conn);
1368 int rc;
1369
1370retry:
1371 rc = SSL_write(data->ssl, buf, count);
1372 if (rc > 0)
1373 return rc;
1374
1375 // User hit Ctrl-C
1376 if (SigInt && (errno == EINTR))
1377 {
1378 rc = -1;
1379 }
1380 else if (BIO_should_retry(SSL_get_wbio(data->ssl)))
1381 {
1382 // Temporary failure, e.g. signal received
1383 goto retry;
1384 }
1385
1386 ssl_err(data, rc);
1387 return rc;
1388}
static struct SslSockData * sockdata(struct Connection *conn)
Get a Connection's socket data.
Definition openssl.c:1197
static void ssl_err(struct SslSockData *data, int err)
Display an SSL error message.
Definition openssl.c:240
volatile sig_atomic_t SigInt
true after SIGINT is received
Definition signal.c:68
+ 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 325 of file raw.c.

326{
327 ssize_t rc;
328 size_t sent = 0;
329
331 do
332 {
333 do
334 {
335 rc = write(conn->fd, buf + sent, count - sent);
336 } while (rc < 0 && (errno == EINTR));
337
338 if (rc < 0)
339 {
340 mutt_error(_("Error talking to %s (%s)"), conn->account.host, strerror(errno));
342 return -1;
343 }
344
345 sent += rc;
346 } while ((sent < count) && !SigInt);
347
349 return sent;
350}
void mutt_sig_allow_interrupt(bool allow)
Allow/disallow Ctrl-C (SIGINT)
Definition signal.c:315
char host[128]
Server to login to.
Definition connaccount.h:54
struct ConnAccount account
Account details: username, password, etc.
Definition connection.h:49
int fd
Socket file descriptor.
Definition connection.h:53
+ 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 529 of file sasl.c.

530{
531 int rc;
532 const char *pbuf = NULL;
533 unsigned int olen, plen;
534
535 struct SaslSockData *sasldata = conn->sockdata;
536 conn->sockdata = sasldata->sockdata;
537
538 /* encode data, if necessary */
539 if (*sasldata->ssf != 0)
540 {
541 /* handle data larger than MAXOUTBUF */
542 do
543 {
544 olen = (count > *sasldata->pbufsize) ? *sasldata->pbufsize : count;
545
546 rc = sasl_encode(sasldata->saslconn, buf, olen, &pbuf, &plen);
547 if (rc != SASL_OK)
548 {
549 mutt_debug(LL_DEBUG1, "SASL encoding failed: %s\n", sasl_errstring(rc, NULL, NULL));
550 goto fail;
551 }
552
553 rc = sasldata->write(conn, pbuf, plen);
554 if (rc != plen)
555 goto fail;
556
557 count -= olen;
558 buf += olen;
559 } while (count > *sasldata->pbufsize);
560 }
561 else
562 {
563 /* just write using the underlying socket function */
564 rc = sasldata->write(conn, buf, count);
565 }
566
567 conn->sockdata = sasldata;
568
569 return rc;
570
571fail:
572 conn->sockdata = sasldata;
573 return -1;
574}
int(* write)(struct Connection *conn, const char *buf, size_t count)
Write to a socket Connection - Implements Connection::write() -.
Definition sasl.c:89
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
SASL authentication API -.
Definition sasl.c:64
void * sockdata
Underlying socket data.
Definition sasl.c:74
const sasl_ssf_t * ssf
Security strength factor, in bits.
Definition sasl.c:66
const unsigned int * pbufsize
Buffer size.
Definition sasl.c:67
const char * buf
Buffer for data read from the connection.
Definition sasl.c:70
sasl_conn_t * saslconn
Raw SASL connection.
Definition sasl.c:65
+ 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 168 of file tunnel.c.

169{
170 struct TunnelSockData *tunnel = conn->sockdata;
171 ssize_t rc;
172 size_t sent = 0;
173
174 do
175 {
176 do
177 {
178 rc = write(tunnel->fd_write, buf + sent, count - sent);
179 } while (rc < 0 && errno == EINTR);
180
181 if (rc < 0)
182 {
183 mutt_error(_("Tunnel error talking to %s: %s"), conn->account.host, strerror(errno));
184 return -1;
185 }
186
187 sent += rc;
188 } while (sent < count);
189
190 return sent;
191}
A network tunnel (pair of sockets)
Definition tunnel.c:49
int fd_write
File descriptor to write to.
Definition tunnel.c:52
+ 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 237 of file zstrm.c.

238{
239 struct ZstrmContext *zctx = conn->sockdata;
240 int rc;
241
242 zctx->write.z.avail_in = (uInt) count;
243 zctx->write.z.next_in = (Bytef *) buf;
244 zctx->write.z.avail_out = (uInt) zctx->write.len;
245 zctx->write.z.next_out = (Bytef *) zctx->write.buf;
246
247 do
248 {
249 int zrc = deflate(&zctx->write.z, Z_PARTIAL_FLUSH);
250 if (zrc == Z_OK)
251 {
252 /* push out produced data to the underlying stream */
253 zctx->write.pos = zctx->write.len - zctx->write.z.avail_out;
254 char *wbufp = zctx->write.buf;
255 mutt_debug(LL_DEBUG5, "deflate consumed %zu/%zu bytes\n",
256 count - zctx->write.z.avail_in, count);
257 while (zctx->write.pos > 0)
258 {
259 rc = zctx->next_conn.write(&zctx->next_conn, wbufp, zctx->write.pos);
260 mutt_debug(LL_DEBUG5, "next stream wrote: %d bytes\n", rc);
261 if (rc < 0)
262 return -1; /* we can't recover from write failure */
263
264 wbufp += rc;
265 zctx->write.pos -= rc;
266 }
267
268 /* see if there's more for us to do, retry if the output buffer
269 * was full (there may be something in zlib buffers), and retry
270 * when there is still available input data */
271 if ((zctx->write.z.avail_out != 0) && (zctx->write.z.avail_in == 0))
272 break;
273
274 zctx->write.z.avail_out = (uInt) zctx->write.len;
275 zctx->write.z.next_out = (Bytef *) zctx->write.buf;
276 }
277 else
278 {
279 /* compression went wrong, but this is basically impossible
280 * according to the docs */
281 return -1;
282 }
283 } while (true);
284
285 rc = (int) count;
286 return (rc <= 0) ? 1 : rc; /* avoid wrong behaviour due to overflow */
287}
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
Data compression layer.
Definition zstrm.c:58
struct ZstrmDirection write
Data being compressed and written.
Definition zstrm.c:60
unsigned int len
Length of data.
Definition zstrm.c:48
z_stream z
zlib compression handle
Definition zstrm.c:46
+ 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 89 of file sasl.c.