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

Read from a socket Connection. More...

Collaboration diagram for read():

Functions

static int tls_socket_read (struct Connection *conn, char *buf, size_t count)
 Read data from a TLS socket - Implements Connection::read() -.
static int ssl_socket_read (struct Connection *conn, char *buf, size_t count)
 Read data from an SSL socket - Implements Connection::read() -.
int raw_socket_read (struct Connection *conn, char *buf, size_t count)
 Read data from a socket - Implements Connection::read() -.
static int mutt_sasl_conn_read (struct Connection *conn, char *buf, size_t count)
 Read data from an SASL connection - Implements Connection::read() -.
static int tunnel_socket_read (struct Connection *conn, char *buf, size_t count)
 Read data from a tunnel socket - Implements Connection::read() -.
static int zstrm_read (struct Connection *conn, char *buf, size_t len)
 Read compressed data from a socket - Implements Connection::read() -.

Variables

int(* SaslSockData::read )(struct Connection *conn, char *buf, size_t count)
 Read from a socket Connection - Implements Connection::read() -.

Detailed Description

Read from a socket Connection.

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

Function Documentation

◆ tls_socket_read()

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

Read data from a TLS socket - Implements Connection::read() -.

Definition at line 1063 of file gnutls.c.

1064{
1065 struct TlsSockData *data = conn->sockdata;
1066 if (!data)
1067 {
1068 mutt_error(_("Error: no TLS socket open"));
1069 return -1;
1070 }
1071
1072 int rc;
1073 do
1074 {
1075 rc = gnutls_record_recv(data->session, buf, count);
1076 if (rc == GNUTLS_E_AGAIN)
1077 {
1078 // Socket would block, wait for data to become available
1080 return -1;
1081 }
1082 } while ((rc == GNUTLS_E_AGAIN) || (rc == GNUTLS_E_INTERRUPTED));
1083
1084 if (rc < 0)
1085 {
1086 mutt_error("tls_socket_read (%s)", gnutls_strerror(rc));
1087 return -1;
1088 }
1089
1090 return rc;
1091}
static bool tls_socket_poll_with_timeout(struct Connection *conn)
Poll a socket with configured timeout.
Definition gnutls.c:859
#define mutt_error(...)
Definition logging2.h:94
#define _(a)
Definition message.h:28
void * sockdata
Backend-specific socket data.
Definition connection.h:55
This array needs to be large enough to hold all the possible values support by NeoMutt.
Definition gnutls.c:82
gnutls_session_t session
GNUTLS session.
Definition gnutls.c:83
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ssl_socket_read()

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

Read data from an SSL socket - Implements Connection::read() -.

Definition at line 1344 of file openssl.c.

1345{
1346 struct SslSockData *data = sockdata(conn);
1347 int rc;
1348
1349retry:
1350 rc = SSL_read(data->ssl, buf, count);
1351 if (rc > 0)
1352 return rc;
1353
1354 // User hit Ctrl-C
1355 if (SigInt && (errno == EINTR))
1356 {
1357 rc = -1;
1358 }
1359 else if (BIO_should_retry(SSL_get_rbio(data->ssl)))
1360 {
1361 // Temporary failure, e.g. signal received
1362 if (raw_socket_poll(conn, 0) > 0)
1363 {
1364 goto retry;
1365 }
1366 }
1367
1368 data->is_open = false;
1369 ssl_err(data, rc);
1370 return rc;
1371}
int raw_socket_poll(struct Connection *conn, time_t wait_secs)
Check if any data is waiting on a socket - Implements Connection::poll() -.
Definition raw.c:355
static struct SslSockData * sockdata(struct Connection *conn)
Get a Connection's socket data.
Definition openssl.c:1211
static void ssl_err(struct SslSockData *data, int err)
Display an SSL error message.
Definition openssl.c:241
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_read()

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

Read data from a socket - Implements Connection::read() -.

Definition at line 295 of file raw.c.

296{
297 int rc;
298
300 do
301 {
302 rc = read(conn->fd, buf, count);
303 } while (rc < 0 && (errno == EINTR));
304
305 if (rc < 0)
306 {
307 mutt_error(_("Error talking to %s (%s)"), conn->account.host, strerror(errno));
308 SigInt = false;
309 }
311
312 if (SigInt)
313 {
314 mutt_error(_("Connection to %s has been aborted"), conn->account.host);
315 SigInt = false;
316 rc = -1;
317 }
318
319 return rc;
320}
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:60
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_read()

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

Read data from an SASL connection - Implements Connection::read() -.

Definition at line 473 of file sasl.c.

474{
475 int rc;
476 unsigned int olen;
477
478 struct SaslSockData *sasldata = conn->sockdata;
479
480 /* if we still have data in our read buffer, copy it into buf */
481 if (sasldata->blen > sasldata->bpos)
482 {
483 olen = ((sasldata->blen - sasldata->bpos) > count) ?
484 count :
485 sasldata->blen - sasldata->bpos;
486
487 memcpy(buf, sasldata->buf + sasldata->bpos, olen);
488 sasldata->bpos += olen;
489
490 return olen;
491 }
492
493 conn->sockdata = sasldata->sockdata;
494
495 sasldata->bpos = 0;
496 sasldata->blen = 0;
497
498 /* and decode the result, if necessary */
499 if (*sasldata->ssf != 0)
500 {
501 do
502 {
503 /* call the underlying read function to fill the buffer */
504 rc = sasldata->read(conn, buf, count);
505 if (rc <= 0)
506 goto out;
507
508 rc = sasl_decode(sasldata->saslconn, buf, rc, &sasldata->buf, &sasldata->blen);
509 if (rc != SASL_OK)
510 {
511 mutt_debug(LL_DEBUG1, "SASL decode failed: %s\n", sasl_errstring(rc, NULL, NULL));
512 goto out;
513 }
514 } while (sasldata->blen == 0);
515
516 olen = ((sasldata->blen - sasldata->bpos) > count) ?
517 count :
518 sasldata->blen - sasldata->bpos;
519
520 memcpy(buf, sasldata->buf, olen);
521 sasldata->bpos += olen;
522
523 rc = olen;
524 }
525 else
526 {
527 rc = sasldata->read(conn, buf, count);
528 }
529
530out:
531 conn->sockdata = sasldata;
532
533 return rc;
534}
int(* read)(struct Connection *conn, char *buf, size_t count)
Read from a socket Connection - Implements Connection::read() -.
Definition sasl.c:86
#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:66
void * sockdata
Underlying socket data.
Definition sasl.c:76
unsigned int blen
Size of the read buffer.
Definition sasl.c:73
unsigned int bpos
Current read position.
Definition sasl.c:74
const sasl_ssf_t * ssf
Security strength factor, in bits.
Definition sasl.c:68
const char * buf
Buffer for data read from the connection.
Definition sasl.c:72
sasl_conn_t * saslconn
Raw SASL connection.
Definition sasl.c:67
Here is the caller graph for this function:

◆ tunnel_socket_read()

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

Read data from a tunnel socket - Implements Connection::read() -.

Definition at line 147 of file tunnel.c.

148{
149 struct TunnelSockData *tunnel = conn->sockdata;
150 int rc;
151
152 do
153 {
154 rc = read(tunnel->fd_read, buf, count);
155 } while (rc < 0 && errno == EINTR);
156
157 if (rc < 0)
158 {
159 mutt_error(_("Tunnel error talking to %s: %s"), conn->account.host, strerror(errno));
160 return -1;
161 }
162
163 return rc;
164}
A network tunnel (pair of sockets).
Definition tunnel.c:49
int fd_read
File descriptor to read from.
Definition tunnel.c:51
Here is the caller graph for this function:

◆ zstrm_read()

int zstrm_read ( struct Connection * conn,
char * buf,
size_t len )
static

Read compressed data from a socket - Implements Connection::read() -.

Definition at line 139 of file zstrm.c.

140{
141 struct ZstrmContext *zctx = conn->sockdata;
142 int rc = 0;
143 int zrc = 0;
144
145retry:
146 if (zctx->read.stream_eof)
147 return 0;
148
149 /* when avail_out was 0 on last call, we need to call inflate again, because
150 * more data might be available using the current input, so avoid calling
151 * read on the underlying stream in that case (for it might block) */
152 if ((zctx->read.pos == 0) && !zctx->read.conn_eof)
153 {
154 rc = zctx->next_conn.read(&zctx->next_conn, zctx->read.buf, zctx->read.len);
155 mutt_debug(LL_DEBUG5, "consuming data from next stream: %d bytes\n", rc);
156 if (rc < 0)
157 return rc;
158 else if (rc == 0)
159 zctx->read.conn_eof = true;
160 else
161 zctx->read.pos += rc;
162 }
163
164 zctx->read.z.avail_in = (uInt) zctx->read.pos;
165 zctx->read.z.next_in = (Bytef *) zctx->read.buf;
166 zctx->read.z.avail_out = (uInt) len;
167 zctx->read.z.next_out = (Bytef *) buf;
168
169 zrc = inflate(&zctx->read.z, Z_SYNC_FLUSH);
170 mutt_debug(LL_DEBUG5, "rc=%d, consumed %u/%u bytes, produced %zu/%zu bytes\n",
171 zrc, zctx->read.pos - zctx->read.z.avail_in, zctx->read.pos,
172 len - zctx->read.z.avail_out, len);
173
174 /* shift any remaining input data to the front of the buffer */
175 if ((Bytef *) zctx->read.buf != zctx->read.z.next_in)
176 {
177 memmove(zctx->read.buf, zctx->read.z.next_in, zctx->read.z.avail_in);
178 zctx->read.pos = zctx->read.z.avail_in;
179 }
180
181 switch (zrc)
182 {
183 case Z_OK: /* progress has been made */
184 zrc = len - zctx->read.z.avail_out; /* "returned" bytes */
185 if (zrc == 0)
186 {
187 /* there was progress, so must have been reading input */
188 mutt_debug(LL_DEBUG5, "inflate just consumed\n");
189 goto retry;
190 }
191 break;
192
193 case Z_STREAM_END: /* everything flushed, nothing remaining */
194 mutt_debug(LL_DEBUG5, "inflate returned Z_STREAM_END\n");
195 zrc = len - zctx->read.z.avail_out; /* "returned" bytes */
196 zctx->read.stream_eof = true;
197 break;
198
199 case Z_BUF_ERROR: /* no progress was possible */
200 if (!zctx->read.conn_eof)
201 {
202 mutt_debug(LL_DEBUG5, "inflate returned Z_BUF_ERROR. retrying\n");
203 goto retry;
204 }
205 zrc = 0;
206 break;
207
208 default:
209 /* bail on other rcs, such as Z_DATA_ERROR, or Z_MEM_ERROR */
210 mutt_debug(LL_DEBUG5, "inflate returned %d. aborting\n", zrc);
211 zrc = -1;
212 break;
213 }
214
215 return zrc;
216}
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
int(* read)(struct Connection *conn, char *buf, size_t count)
Definition connection.h:79
Data compression layer.
Definition zstrm.c:59
struct ZstrmDirection read
Data being read and de-compressed.
Definition zstrm.c:60
struct Connection next_conn
Underlying stream.
Definition zstrm.c:62
unsigned int pos
Current position.
Definition zstrm.c:50
bool conn_eof
Connection end-of-file reached.
Definition zstrm.c:51
unsigned int len
Length of data.
Definition zstrm.c:49
z_stream z
zlib compression handle
Definition zstrm.c:47
char * buf
Buffer for data being (de-)compressed.
Definition zstrm.c:48
bool stream_eof
Stream end-of-file reached.
Definition zstrm.c:52
Here is the caller graph for this function:

Variable Documentation

◆ read

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

Read from a socket Connection - Implements Connection::read() -.

Definition at line 86 of file sasl.c.