NeoMutt  2022-04-29-215-gc12b98
Teaching an old dog new tricks
DOXYGEN
socket.c File Reference

Low-level socket handling. More...

#include "config.h"
#include <errno.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include "private.h"
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "socket.h"
#include "connaccount.h"
#include "connection.h"
#include "protos.h"
#include "ssl.h"
+ Include dependency graph for socket.c:

Go to the source code of this file.

Functions

static int socket_preconnect (void)
 Execute a command before opening a socket. More...
 
int mutt_socket_open (struct Connection *conn)
 Simple wrapper. More...
 
int mutt_socket_close (struct Connection *conn)
 Close a socket. More...
 
int mutt_socket_read (struct Connection *conn, char *buf, size_t len)
 Read from a Connection. More...
 
int mutt_socket_write (struct Connection *conn, const char *buf, size_t len)
 Write to a Connection. More...
 
int mutt_socket_write_d (struct Connection *conn, const char *buf, int len, int dbg)
 Write data to a socket. More...
 
int mutt_socket_poll (struct Connection *conn, time_t wait_secs)
 Checks whether reads would block. More...
 
int mutt_socket_readchar (struct Connection *conn, char *c)
 Simple read buffering to speed things up. More...
 
int mutt_socket_readln_d (char *buf, size_t buflen, struct Connection *conn, int dbg)
 Read a line from a socket. More...
 
struct Connectionmutt_socket_new (enum ConnectionType type)
 Allocate and initialise a new connection. More...
 
void mutt_socket_empty (struct Connection *conn)
 Clear out any queued data. More...
 
int mutt_socket_buffer_readln_d (struct Buffer *buf, struct Connection *conn, int dbg)
 Read a line from a socket into a Buffer. More...
 

Detailed Description

Low-level socket handling.

Authors
  • Michael R. Elkins
  • Brendan Cully
  • Tommi Komulainen

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file socket.c.

Function Documentation

◆ socket_preconnect()

static int socket_preconnect ( void  )
static

Execute a command before opening a socket.

Return values
0Success
>0An errno, e.g. EPERM

Definition at line 51 of file socket.c.

52{
53 const char *const c_preconnect = cs_subset_string(NeoMutt->sub, "preconnect");
54 if (!c_preconnect)
55 return 0;
56
57 mutt_debug(LL_DEBUG2, "Executing preconnect: %s\n", c_preconnect);
58 const int rc = mutt_system(c_preconnect);
59 mutt_debug(LL_DEBUG2, "Preconnect result: %d\n", rc);
60 if (rc != 0)
61 {
62 const int save_errno = errno;
63 mutt_perror(_("Preconnect command failed"));
64
65 return save_errno;
66 }
67
68 return 0;
69}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
#define mutt_perror(...)
Definition: logging.h:88
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
#define _(a)
Definition: message.h:28
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:51
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_socket_open()

int mutt_socket_open ( struct Connection conn)

Simple wrapper.

Parameters
connConnection to a server
Return values
0Success
-1Error

Definition at line 77 of file socket.c.

78{
79 int rc;
80
82 return -1;
83
84 rc = conn->open(conn);
85
86 mutt_debug(LL_DEBUG2, "Connected to %s:%d on fd=%d\n", conn->account.host,
87 conn->account.port, conn->fd);
88
89 return rc;
90}
static int socket_preconnect(void)
Execute a command before opening a socket.
Definition: socket.c:51
char host[128]
Server to login to.
Definition: connaccount.h:54
unsigned short port
Port to connect to.
Definition: connaccount.h:58
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:50
int(* open)(struct Connection *conn)
Definition: connection.h:67
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_socket_close()

int mutt_socket_close ( struct Connection conn)

Close a socket.

Parameters
connConnection to a server
Return values
0Success
-1Error

Definition at line 98 of file socket.c.

99{
100 if (!conn)
101 return 0;
102
103 int rc = -1;
104
105 if (conn->fd < 0)
106 mutt_debug(LL_DEBUG1, "Attempt to close closed connection\n");
107 else
108 rc = conn->close(conn);
109
110 conn->fd = -1;
111 conn->ssf = 0;
112 conn->bufpos = 0;
113 conn->available = 0;
114
115 return rc;
116}
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
unsigned int ssf
Security strength factor, in bits (see notes)
Definition: connection.h:51
int bufpos
Current position in the buffer.
Definition: connection.h:53
int(* close)(struct Connection *conn)
Definition: connection.h:117
int available
Amount of data waiting to be read.
Definition: connection.h:55
+ Here is the caller graph for this function:

◆ mutt_socket_read()

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

Read from a Connection.

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

Definition at line 126 of file socket.c.

127{
128 return conn->read(conn, buf, len);
129}
int(* read)(struct Connection *conn, char *buf, size_t count)
Definition: connection.h:80
+ Here is the caller graph for this function:

◆ mutt_socket_write()

int mutt_socket_write ( struct Connection conn,
const char *  buf,
size_t  len 
)

Write to a Connection.

Parameters
connConnection to a server
bufBuffer with data to write
lenLength of data to write
Return values
>0Number of bytes written
-1Error

Definition at line 139 of file socket.c.

140{
141 return conn->write(conn, buf, len);
142}
int(* write)(struct Connection *conn, const char *buf, size_t count)
Definition: connection.h:93

◆ mutt_socket_write_d()

int mutt_socket_write_d ( struct Connection conn,
const char *  buf,
int  len,
int  dbg 
)

Write data to a socket.

Parameters
connConnection to a server
bufBuffer with data to write
lenLength of data to write
dbgDebug level for logging
Return values
>0Number of bytes written
-1Error

Definition at line 153 of file socket.c.

154{
155 int sent = 0;
156
157 mutt_debug(dbg, "%d> %s", conn->fd, buf);
158
159 if (conn->fd < 0)
160 {
161 mutt_debug(LL_DEBUG1, "attempt to write to closed connection\n");
162 return -1;
163 }
164
165 while (sent < len)
166 {
167 const int rc = conn->write(conn, buf + sent, len - sent);
168 if (rc < 0)
169 {
170 mutt_debug(LL_DEBUG1, "error writing (%s), closing socket\n", strerror(errno));
171 mutt_socket_close(conn);
172
173 return -1;
174 }
175
176 if (rc < len - sent)
177 mutt_debug(LL_DEBUG3, "short write (%d of %d bytes)\n", rc, len - sent);
178
179 sent += rc;
180 }
181
182 return sent;
183}
@ LL_DEBUG3
Log at debug level 3.
Definition: logging.h:42
int mutt_socket_close(struct Connection *conn)
Close a socket.
Definition: socket.c:98
+ Here is the call graph for this function:

◆ mutt_socket_poll()

int mutt_socket_poll ( struct Connection conn,
time_t  wait_secs 
)

Checks whether reads would block.

Parameters
connConnection to a server
wait_secsHow long to wait for a response
Return values
>0There is data to read
0Read would block
-1Connection doesn't support polling

Definition at line 193 of file socket.c.

194{
195 if (conn->bufpos < conn->available)
196 return conn->available - conn->bufpos;
197
198 if (conn->poll)
199 return conn->poll(conn, wait_secs);
200
201 return -1;
202}
int(* poll)(struct Connection *conn, time_t wait_secs)
Definition: connection.h:106
+ Here is the caller graph for this function:

◆ mutt_socket_readchar()

int mutt_socket_readchar ( struct Connection conn,
char *  c 
)

Simple read buffering to speed things up.

Parameters
[in]connConnection to a server
[out]cCharacter that was read
Return values
1Success
-1Error

Definition at line 211 of file socket.c.

212{
213 if (conn->bufpos >= conn->available)
214 {
215 if (conn->fd >= 0)
216 conn->available = conn->read(conn, conn->inbuf, sizeof(conn->inbuf));
217 else
218 {
219 mutt_debug(LL_DEBUG1, "attempt to read from closed connection\n");
220 return -1;
221 }
222 conn->bufpos = 0;
223 if (conn->available == 0)
224 {
225 mutt_error(_("Connection to %s closed"), conn->account.host);
226 }
227 if (conn->available <= 0)
228 {
229 mutt_socket_close(conn);
230 return -1;
231 }
232 }
233 *c = conn->inbuf[conn->bufpos];
234 conn->bufpos++;
235 return 1;
236}
#define mutt_error(...)
Definition: logging.h:87
char inbuf[1024]
Buffer for incoming traffic.
Definition: connection.h:52
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_socket_readln_d()

int mutt_socket_readln_d ( char *  buf,
size_t  buflen,
struct Connection conn,
int  dbg 
)

Read a line from a socket.

Parameters
bufBuffer to store the line
buflenLength of data to write
connConnection to a server
dbgDebug level for logging
Return values
>0Success, number of bytes read
-1Error

Definition at line 247 of file socket.c.

248{
249 char ch;
250 int i;
251
252 for (i = 0; i < buflen - 1; i++)
253 {
254 if (mutt_socket_readchar(conn, &ch) != 1)
255 {
256 buf[i] = '\0';
257 return -1;
258 }
259
260 if (ch == '\n')
261 break;
262 buf[i] = ch;
263 }
264
265 /* strip \r from \r\n termination */
266 if (i && (buf[i - 1] == '\r'))
267 i--;
268 buf[i] = '\0';
269
270 mutt_debug(dbg, "%d< %s\n", conn->fd, buf);
271
272 /* number of bytes read, not strlen */
273 return i + 1;
274}
int mutt_socket_readchar(struct Connection *conn, char *c)
Simple read buffering to speed things up.
Definition: socket.c:211
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_socket_new()

struct Connection * mutt_socket_new ( enum ConnectionType  type)

Allocate and initialise a new connection.

Parameters
typeType of the new Connection
Return values
ptrNew Connection

Definition at line 281 of file socket.c.

282{
283 struct Connection *conn = mutt_mem_calloc(1, sizeof(struct Connection));
284 conn->fd = -1;
285
286 if (type == MUTT_CONNECTION_TUNNEL)
287 {
289 }
290 else if (type == MUTT_CONNECTION_SSL)
291 {
292 int rc = mutt_ssl_socket_setup(conn);
293 if (rc < 0)
294 FREE(&conn);
295 }
296 else
297 {
298 conn->read = raw_socket_read;
299 conn->write = raw_socket_write;
300 conn->open = raw_socket_open;
301 conn->close = raw_socket_close;
302 conn->poll = raw_socket_poll;
303 }
304
305 return conn;
306}
void mutt_tunnel_socket_setup(struct Connection *conn)
Sets up tunnel connection functions.
Definition: tunnel.c:233
int mutt_ssl_socket_setup(struct Connection *conn)
Set up SSL socket mulitplexor.
Definition: gnutls.c:1123
int raw_socket_close(struct Connection *conn)
Close a socket - Implements Connection::close() -.
Definition: raw.c:362
int raw_socket_open(struct Connection *conn)
Open a socket - Implements Connection::open() -.
Definition: raw.c:117
int raw_socket_poll(struct Connection *conn, time_t wait_secs)
Checks whether reads would block - Implements Connection::poll() -.
Definition: raw.c:324
int raw_socket_read(struct Connection *conn, char *buf, size_t len)
Read data from a socket - Implements Connection::read() -.
Definition: raw.c:264
int raw_socket_write(struct Connection *conn, const char *buf, size_t count)
Write data to a socket - Implements Connection::write() -.
Definition: raw.c:294
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:43
@ MUTT_CONNECTION_SSL
SSL/TLS-encrypted connection.
Definition: socket.h:39
@ MUTT_CONNECTION_TUNNEL
Tunnelled connection.
Definition: socket.h:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_socket_empty()

void mutt_socket_empty ( struct Connection conn)

Clear out any queued data.

The internal buffer is emptied and any data that has already arrived at this machine (in kernel buffers) is read and dropped.

Definition at line 314 of file socket.c.

315{
316 if (!conn)
317 return;
318
319 char buf[1024] = { 0 };
320 int bytes;
321
322 while ((bytes = mutt_socket_poll(conn, 0)) > 0)
323 {
324 mutt_socket_read(conn, buf, MIN(bytes, sizeof(buf)));
325 }
326}
#define MIN(a, b)
Definition: memory.h:31
int mutt_socket_poll(struct Connection *conn, time_t wait_secs)
Checks whether reads would block.
Definition: socket.c:193
int mutt_socket_read(struct Connection *conn, char *buf, size_t len)
Read from a Connection.
Definition: socket.c:126
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_socket_buffer_readln_d()

int mutt_socket_buffer_readln_d ( struct Buffer buf,
struct Connection conn,
int  dbg 
)

Read a line from a socket into a Buffer.

Parameters
bufBuffer to store the line
connConnection to a server
dbgDebug level for logging
Return values
>0Success, number of bytes read
-1Error

Definition at line 336 of file socket.c.

337{
338 char ch;
339 bool has_cr = false;
340
342
343 while (true)
344 {
345 if (mutt_socket_readchar(conn, &ch) != 1)
346 return -1;
347
348 if (ch == '\n')
349 break;
350
351 if (has_cr)
352 {
353 mutt_buffer_addch(buf, '\r');
354 has_cr = false;
355 }
356
357 if (ch == '\r')
358 has_cr = true;
359 else
360 mutt_buffer_addch(buf, ch);
361 }
362
363 mutt_debug(dbg, "%d< %s\n", conn->fd, mutt_buffer_string(buf));
364 return 0;
365}
size_t mutt_buffer_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:248
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:85
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
+ Here is the call graph for this function: