NeoMutt  2023-11-03-107-g582dc1
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 1071 of file gnutls.c.

1072{
1073 struct TlsSockData *data = conn->sockdata;
1074 size_t sent = 0;
1075
1076 if (!data)
1077 {
1078 mutt_error(_("Error: no TLS socket open"));
1079 return -1;
1080 }
1081
1082 do
1083 {
1084 int rc;
1085 do
1086 {
1087 rc = gnutls_record_send(data->session, buf + sent, count - sent);
1088 } while ((rc == GNUTLS_E_AGAIN) || (rc == GNUTLS_E_INTERRUPTED));
1089
1090 if (rc < 0)
1091 {
1092 mutt_error("tls_socket_write (%s)", gnutls_strerror(rc));
1093 return -1;
1094 }
1095
1096 sent += rc;
1097 } while (sent < count);
1098
1099 return sent;
1100}
#define mutt_error(...)
Definition: logging2.h:92
#define _(a)
Definition: message.h:28
void * sockdata
Backend-specific socket data.
Definition: connection.h:56
TLS socket data -.
Definition: gnutls.c:82
gnutls_session_t session
Definition: gnutls.c:83
+ 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 1361 of file openssl.c.

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

308{
309 int rc;
310 size_t sent = 0;
311
313 do
314 {
315 do
316 {
317 rc = write(conn->fd, buf + sent, count - sent);
318 } while (rc < 0 && (errno == EINTR));
319
320 if (rc < 0)
321 {
322 mutt_error(_("Error talking to %s (%s)"), conn->account.host, strerror(errno));
324 return -1;
325 }
326
327 sent += rc;
328 } while ((sent < count) && !SigInt);
329
331 return sent;
332}
void mutt_sig_allow_interrupt(bool allow)
Allow/disallow Ctrl-C (SIGINT)
Definition: signal.c:251
char host[128]
Server to login to.
Definition: connaccount.h:54
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:50
int fd
Socket file descriptor.
Definition: connection.h:54
+ 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 533 of file sasl.c.

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

168{
169 struct TunnelSockData *tunnel = conn->sockdata;
170 int rc;
171 size_t sent = 0;
172
173 do
174 {
175 do
176 {
177 rc = write(tunnel->fd_write, buf + sent, count - sent);
178 } while (rc < 0 && errno == EINTR);
179
180 if (rc < 0)
181 {
182 mutt_error(_("Tunnel error talking to %s: %s"), conn->account.host, strerror(errno));
183 return -1;
184 }
185
186 sent += rc;
187 } while (sent < count);
188
189 return sent;
190}
A network tunnel (pair of sockets)
Definition: tunnel.c:50
int fd_write
File descriptor to write to.
Definition: tunnel.c:53
+ 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 %zu/%zu 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}
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
int(* write)(struct Connection *conn, const char *buf, size_t count)
Definition: connection.h:93
Data compression layer.
Definition: zstrm.c:55
struct ZstrmDirection write
Data being compressed and written.
Definition: zstrm.c:57
struct Connection next_conn
Underlying stream.
Definition: zstrm.c:58
unsigned int pos
Current position.
Definition: zstrm.c:46
unsigned int len
Length of data.
Definition: zstrm.c:45
z_stream z
zlib compression handle
Definition: zstrm.c:43
char * buf
Buffer for data being (de-)compressed.
Definition: zstrm.c: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 89 of file sasl.c.