NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
raw.c File Reference

Low-level socket handling. More...

#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "private.h"
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "connaccount.h"
#include "connection.h"
#include "globals.h"
#include "address/lib.h"
#include <stdbool.h>
+ Include dependency graph for raw.c:

Go to the source code of this file.

Functions

static int socket_connect (int fd, struct sockaddr *sa)
 Set up to connect to a socket fd.
 
int raw_socket_open (struct Connection *conn)
 Open a socket - Implements Connection::open() -.
 
int raw_socket_read (struct Connection *conn, char *buf, size_t count)
 Read data from a socket - Implements Connection::read() -.
 
int raw_socket_write (struct Connection *conn, const char *buf, size_t count)
 Write data to a socket - Implements Connection::write() -.
 
int raw_socket_poll (struct Connection *conn, time_t wait_secs)
 Check if any data is waiting on a socket - Implements Connection::poll() -.
 
int raw_socket_close (struct Connection *conn)
 Close a socket - Implements Connection::close() -.
 

Detailed Description

Low-level socket handling.

Authors
  • Damien Riegel
  • Richard Russon
  • Pietro Cerutti

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 raw.c.

Function Documentation

◆ socket_connect()

static int socket_connect ( int  fd,
struct sockaddr *  sa 
)
static

Set up to connect to a socket fd.

Parameters
fdFile descriptor to connect with
saAddress info
Return values
0Success
>0An errno, e.g. EPERM
-1Error

Definition at line 68 of file raw.c.

69{
70 int sa_size;
71 int save_errno;
72 sigset_t set;
73 struct sigaction oldalrm = { 0 };
74 struct sigaction act = { 0 };
75
76 if (sa->sa_family == AF_INET)
77 sa_size = sizeof(struct sockaddr_in);
78#ifdef HAVE_GETADDRINFO
79 else if (sa->sa_family == AF_INET6)
80 sa_size = sizeof(struct sockaddr_in6);
81#endif
82 else
83 {
84 mutt_debug(LL_DEBUG1, "Unknown address family!\n");
85 return -1;
86 }
87
88 /* Batch mode does not call mutt_signal_init(), so ensure the alarm
89 * interrupts the connect call */
90 const short c_socket_timeout = cs_subset_number(NeoMutt->sub, "socket_timeout");
91 if (c_socket_timeout > 0)
92 {
93 sigemptyset(&act.sa_mask);
94 act.sa_handler = mutt_sig_empty_handler;
95#ifdef SA_INTERRUPT
96 act.sa_flags = SA_INTERRUPT;
97#else
98 act.sa_flags = 0;
99#endif
100 sigaction(SIGALRM, &act, &oldalrm);
101 alarm(c_socket_timeout);
102 }
103
105
106 /* FreeBSD's connect() does not respect SA_RESTART, meaning
107 * a SIGWINCH will cause the connect to fail. */
108 sigemptyset(&set);
109 sigaddset(&set, SIGWINCH);
110 sigprocmask(SIG_BLOCK, &set, NULL);
111
112 save_errno = 0;
113
114 if (c_socket_timeout > 0)
115 {
116 const struct timeval tv = { c_socket_timeout, 0 };
117 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
118 {
119 mutt_debug(LL_DEBUG2, "Cannot set socket receive timeout. errno: %d\n", errno);
120 }
121 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
122 {
123 mutt_debug(LL_DEBUG2, "Cannot set socket send timeout. errno: %d\n", errno);
124 }
125 }
126
127 if (connect(fd, sa, sa_size) < 0)
128 {
129 save_errno = errno;
130 mutt_debug(LL_DEBUG2, "Connection failed. errno: %d\n", errno);
131 SigInt = false; /* reset in case we caught SIGINTR while in connect() */
132 }
133
134 if (c_socket_timeout > 0)
135 {
136 alarm(0);
137 sigaction(SIGALRM, &oldalrm, NULL);
138 }
140 sigprocmask(SIG_UNBLOCK, &set, NULL);
141
142 return save_errno;
143}
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:143
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
void mutt_sig_empty_handler(int sig)
Dummy signal handler.
Definition: signal.c:117
volatile sig_atomic_t SigInt
true after SIGINT is received
Definition: signal.c:66
void mutt_sig_allow_interrupt(bool allow)
Allow/disallow Ctrl-C (SIGINT)
Definition: signal.c:300
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
+ Here is the call graph for this function:
+ Here is the caller graph for this function: