NeoMutt  2025-12-11-694-ga89709
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()

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

1061{
1062 struct TlsSockData *data = conn->sockdata;
1063 if (!data)
1064 {
1065 mutt_error(_("Error: no TLS socket open"));
1066 return -1;
1067 }
1068
1069 int rc;
1070 do
1071 {
1072 rc = gnutls_record_recv(data->session, buf, count);
1073 if (rc == GNUTLS_E_AGAIN)
1074 {
1075 // Socket would block, wait for data to become available
1077 return -1;
1078 }
1079 } while ((rc == GNUTLS_E_AGAIN) || (rc == GNUTLS_E_INTERRUPTED));
1080
1081 if (rc < 0)
1082 {
1083 mutt_error("tls_socket_read (%s)", gnutls_strerror(rc));
1084 return -1;
1085 }
1086
1087 return rc;
1088}
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_read()

static 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 1330 of file openssl.c.

1331{
1332 struct SslSockData *data = sockdata(conn);
1333 int rc;
1334
1335retry:
1336 rc = SSL_read(data->ssl, buf, count);
1337 if (rc > 0)
1338 return rc;
1339
1340 // User hit Ctrl-C
1341 if (SigInt && (errno == EINTR))
1342 {
1343 rc = -1;
1344 }
1345 else if (BIO_should_retry(SSL_get_rbio(data->ssl)))
1346 {
1347 // Temporary failure, e.g. signal received
1348 if (raw_socket_poll(conn, 0) >= 0)
1349 {
1350 goto retry;
1351 }
1352 }
1353
1354 data->isopen = 0;
1355 ssl_err(data, rc);
1356 return rc;
1357}
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: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_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: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_read()

static 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 463 of file sasl.c.

464{
465 int rc;
466 unsigned int olen;
467
468 struct SaslSockData *sasldata = conn->sockdata;
469
470 /* if we still have data in our read buffer, copy it into buf */
471 if (sasldata->blen > sasldata->bpos)
472 {
473 olen = ((sasldata->blen - sasldata->bpos) > count) ?
474 count :
475 sasldata->blen - sasldata->bpos;
476
477 memcpy(buf, sasldata->buf + sasldata->bpos, olen);
478 sasldata->bpos += olen;
479
480 return olen;
481 }
482
483 conn->sockdata = sasldata->sockdata;
484
485 sasldata->bpos = 0;
486 sasldata->blen = 0;
487
488 /* and decode the result, if necessary */
489 if (*sasldata->ssf != 0)
490 {
491 do
492 {
493 /* call the underlying read function to fill the buffer */
494 rc = sasldata->read(conn, buf, count);
495 if (rc <= 0)
496 goto out;
497
498 rc = sasl_decode(sasldata->saslconn, buf, rc, &sasldata->buf, &sasldata->blen);
499 if (rc != SASL_OK)
500 {
501 mutt_debug(LL_DEBUG1, "SASL decode failed: %s\n", sasl_errstring(rc, NULL, NULL));
502 goto out;
503 }
504 } while (sasldata->blen == 0);
505
506 olen = ((sasldata->blen - sasldata->bpos) > count) ?
507 count :
508 sasldata->blen - sasldata->bpos;
509
510 memcpy(buf, sasldata->buf, olen);
511 sasldata->bpos += olen;
512
513 rc = olen;
514 }
515 else
516 {
517 rc = sasldata->read(conn, buf, count);
518 }
519
520out:
521 conn->sockdata = sasldata;
522
523 return rc;
524}
int(* read)(struct Connection *conn, char *buf, size_t count)
Read from a socket Connection - Implements Connection::read() -.
Definition sasl.c:84
#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
unsigned int blen
Size of the read buffer.
Definition sasl.c:71
unsigned int bpos
Current read position.
Definition sasl.c:72
const sasl_ssf_t * ssf
Security strength factor, in bits.
Definition sasl.c:66
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_read()

static 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 146 of file tunnel.c.

147{
148 struct TunnelSockData *tunnel = conn->sockdata;
149 int rc;
150
151 do
152 {
153 rc = read(tunnel->fd_read, buf, count);
154 } while (rc < 0 && errno == EINTR);
155
156 if (rc < 0)
157 {
158 mutt_error(_("Tunnel error talking to %s: %s"), conn->account.host, strerror(errno));
159 return -1;
160 }
161
162 return rc;
163}
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()

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

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

Definition at line 138 of file zstrm.c.

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