NeoMutt  2023-05-17-56-ga67199
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 1067 of file gnutls.c.

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

1348{
1349 if (!conn || !conn->sockdata || !buf || (count == 0))
1350 return -1;
1351
1352 int rc = SSL_write(sockdata(conn)->ssl, buf, count);
1353 if ((rc <= 0) || (errno == EINTR))
1354 {
1355 if (errno == EINTR)
1356 {
1357 rc = -1;
1358 }
1359 ssl_err(sockdata(conn), rc);
1360 }
1361
1362 return rc;
1363}
static struct SslSockData * sockdata(struct Connection *conn)
Get a Connection's socket data.
Definition: openssl.c:1182
static void ssl_err(struct SslSockData *data, int err)
Display an SSL error message.
Definition: openssl.c:234
+ 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 310 of file raw.c.

311{
312 int rc;
313 size_t sent = 0;
314
316 do
317 {
318 do
319 {
320 rc = write(conn->fd, buf + sent, count - sent);
321 } while (rc < 0 && (errno == EINTR));
322
323 if (rc < 0)
324 {
325 mutt_error(_("Error talking to %s (%s)"), conn->account.host, strerror(errno));
327 return -1;
328 }
329
330 sent += rc;
331 } while ((sent < count) && !SigInt);
332
334 return sent;
335}
SIG_ATOMIC_VOLATILE_T SigInt
true after SIGINT is received
Definition: globals.c:59
void mutt_sig_allow_interrupt(bool allow)
Allow/disallow Ctrl-C (SIGINT)
Definition: signal.c:252
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 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
573fail:
574 conn->sockdata = sasldata;
575 return -1;
576}
int(* write)(struct Connection *conn, const char *buf, size_t count)
Write to a socket Connection - Implements Connection::write() -.
Definition: sasl.c:87
#define mutt_debug(LEVEL,...)
Definition: logging2.h:87
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
SASL authentication API -.
Definition: sasl.c:62
void * sockdata
Underlying socket data.
Definition: sasl.c:72
const sasl_ssf_t * ssf
Definition: sasl.c:64
const unsigned int * pbufsize
Definition: sasl.c:65
const char * buf
Buffer for data read from the connection.
Definition: sasl.c:68
sasl_conn_t * saslconn
Definition: sasl.c:63
+ 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 166 of file tunnel.c.

167{
168 struct TunnelSockData *tunnel = conn->sockdata;
169 int rc;
170 size_t sent = 0;
171
172 do
173 {
174 do
175 {
176 rc = write(tunnel->fd_write, buf + sent, count - sent);
177 } while (rc < 0 && errno == EINTR);
178
179 if (rc < 0)
180 {
181 mutt_error(_("Tunnel error talking to %s: %s"), conn->account.host, strerror(errno));
182 return -1;
183 }
184
185 sent += rc;
186 } while (sent < count);
187
188 return sent;
189}
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 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 %lu/%lu 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 87 of file sasl.c.