NeoMutt  2024-10-02-7-gd3e66a
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 1045 of file gnutls.c.

1046{
1047 struct TlsSockData *data = conn->sockdata;
1048 if (!data)
1049 {
1050 mutt_error(_("Error: no TLS socket open"));
1051 return -1;
1052 }
1053
1054 int rc;
1055 do
1056 {
1057 rc = gnutls_record_recv(data->session, buf, count);
1058 } while ((rc == GNUTLS_E_AGAIN) || (rc == GNUTLS_E_INTERRUPTED));
1059
1060 if (rc < 0)
1061 {
1062 mutt_error("tls_socket_read (%s)", gnutls_strerror(rc));
1063 return -1;
1064 }
1065
1066 return rc;
1067}
#define mutt_error(...)
Definition: logging2.h:92
#define _(a)
Definition: message.h:28
void * sockdata
Backend-specific socket data.
Definition: connection.h:55
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_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 1337 of file openssl.c.

1338{
1339 struct SslSockData *data = sockdata(conn);
1340 int rc;
1341
1342retry:
1343 rc = SSL_read(data->ssl, buf, count);
1344 if (rc > 0)
1345 return rc;
1346
1347 // User hit Ctrl-C
1348 if (SigInt && (errno == EINTR))
1349 {
1350 rc = -1;
1351 }
1352 else if (BIO_should_retry(SSL_get_rbio(data->ssl)))
1353 {
1354 // Temporary failure, e.g. signal received
1355 goto retry;
1356 }
1357
1358 data->isopen = 0;
1359 ssl_err(data, rc);
1360 return rc;
1361}
static struct SslSockData * sockdata(struct Connection *conn)
Get a Connection's socket data.
Definition: openssl.c:1194
static void ssl_err(struct SslSockData *data, int err)
Display an SSL error message.
Definition: openssl.c:239
volatile sig_atomic_t SigInt
true after SIGINT is received
Definition: signal.c:66
+ 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:300
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 468 of file sasl.c.

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

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