NeoMutt  2025-12-11-860-g80c9cc
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
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()

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 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->isopen = 0;
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: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 472 of file sasl.c.

473{
474 int rc;
475 unsigned int olen;
476
477 struct SaslSockData *sasldata = conn->sockdata;
478
479 /* if we still have data in our read buffer, copy it into buf */
480 if (sasldata->blen > sasldata->bpos)
481 {
482 olen = ((sasldata->blen - sasldata->bpos) > count) ?
483 count :
484 sasldata->blen - sasldata->bpos;
485
486 memcpy(buf, sasldata->buf + sasldata->bpos, olen);
487 sasldata->bpos += olen;
488
489 return olen;
490 }
491
492 conn->sockdata = sasldata->sockdata;
493
494 sasldata->bpos = 0;
495 sasldata->blen = 0;
496
497 /* and decode the result, if necessary */
498 if (*sasldata->ssf != 0)
499 {
500 do
501 {
502 /* call the underlying read function to fill the buffer */
503 rc = sasldata->read(conn, buf, count);
504 if (rc <= 0)
505 goto out;
506
507 rc = sasl_decode(sasldata->saslconn, buf, rc, &sasldata->buf, &sasldata->blen);
508 if (rc != SASL_OK)
509 {
510 mutt_debug(LL_DEBUG1, "SASL decode failed: %s\n", sasl_errstring(rc, NULL, NULL));
511 goto out;
512 }
513 } while (sasldata->blen == 0);
514
515 olen = ((sasldata->blen - sasldata->bpos) > count) ?
516 count :
517 sasldata->blen - sasldata->bpos;
518
519 memcpy(buf, sasldata->buf, olen);
520 sasldata->bpos += olen;
521
522 rc = olen;
523 }
524 else
525 {
526 rc = sasldata->read(conn, buf, count);
527 }
528
529out:
530 conn->sockdata = sasldata;
531
532 return rc;
533}
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()

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