35#include <netinet/in.h>
40#include <sys/select.h>
41#include <sys/socket.h>
56#ifdef HAVE_GETADDRINFO
73 struct sigaction oldalrm = { 0 };
74 struct sigaction act = { 0 };
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);
84 mutt_debug(LL_DEBUG1,
"Unknown address family!\n");
91 if (c_socket_timeout > 0)
93 sigemptyset(&act.sa_mask);
96 act.sa_flags = SA_INTERRUPT;
100 sigaction(SIGALRM, &act, &oldalrm);
101 alarm(c_socket_timeout);
109 sigaddset(&set, SIGWINCH);
110 sigprocmask(SIG_BLOCK, &set, NULL);
114 if (c_socket_timeout > 0)
116 const struct timeval tv = { c_socket_timeout, 0 };
117 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(tv)) < 0)
121 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv,
sizeof(tv)) < 0)
127 if (connect(fd, sa, sa_size) < 0)
134 if (c_socket_timeout > 0)
137 sigaction(SIGALRM, &oldalrm, NULL);
140 sigprocmask(SIG_UNBLOCK, &set, NULL);
152 char *host_idna = NULL;
154#ifdef HAVE_GETADDRINFO
158 char port[6] = { 0 };
159 struct addrinfo hints = { 0 };
160 struct addrinfo *res = NULL;
161 struct addrinfo *cur = NULL;
166 hints.ai_family = AF_UNSPEC;
168 hints.ai_family = AF_INET;
170 hints.ai_socktype = SOCK_STREAM;
172 snprintf(port,
sizeof(port),
"%d", conn->
account.
port);
187 rc = getaddrinfo(host_idna, port, &hints, &res);
203 for (cur = res; cur; cur = cur->ai_next)
205 int fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
211 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
226 struct hostent *he = NULL;
227 struct sockaddr_in sin = { 0 };
229 sin.sin_family = AF_INET;
244 he = gethostbyname(host_idna);
261 for (
int i = 0; he->h_addr_list[i]; i++)
263 memcpy(&sin.sin_addr, he->h_addr_list[i], he->h_length);
264 int fd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
271 fcntl(fd, F_SETFD, FD_CLOEXEC);
285 (rc > 0) ? strerror(rc) :
_(
"unknown error"));
302 rc = read(conn->
fd, buf, count);
303 }
while (rc < 0 && (errno == EINTR));
335 rc = write(conn->
fd, buf + sent, count - sent);
336 }
while (rc < 0 && (errno == EINTR));
346 }
while ((sent < count) && !
SigInt);
361 struct timeval tv = { 0 };
363 uint64_t wait_millis = wait_secs * 1000UL;
367 tv.tv_sec = wait_millis / 1000;
368 tv.tv_usec = (wait_millis % 1000) * 1000;
371 FD_SET(conn->
fd, &rfds);
374 const int rc = select(conn->
fd + 1, &rfds, NULL, NULL, &tv);
377 if ((rc > 0) || ((rc < 0) && (errno != EINTR)))
383 wait_millis += pre_t;
384 if (wait_millis <= post_t)
386 wait_millis -= post_t;
395 return close(conn->
fd);
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Convenience wrapper for the config headers.
Shared functions that are private to Connections.
An open network connection (socket)
Convenience wrapper for the core headers.
void mutt_query_exit(void)
Ask the user if they want to leave NeoMutt.
bool OptGui
(pseudo) when the gui (and curses) are started
int raw_socket_close(struct Connection *conn)
Close a socket - Implements Connection::close() -.
int raw_socket_open(struct Connection *conn)
Open a socket - Implements Connection::open() -.
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_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() -.
#define mutt_message(...)
#define mutt_debug(LEVEL,...)
Convenience wrapper for the gui headers.
int mutt_idna_to_ascii_lz(const char *input, char **output, uint8_t flags)
Convert a domain to Punycode.
@ LL_DEBUG2
Log at debug level 2.
uint64_t mutt_date_now_ms(void)
Return the number of milliseconds since the Unix epoch.
Convenience wrapper for the library headers.
static int socket_connect(int fd, struct sockaddr *sa)
Set up to connect to a socket fd.
void mutt_sig_empty_handler(int sig)
Dummy signal handler.
volatile sig_atomic_t SigInt
true after SIGINT is received
void mutt_sig_allow_interrupt(bool allow)
Allow/disallow Ctrl-C (SIGINT)
char host[128]
Server to login to.
unsigned short port
Port to connect to.
struct ConnAccount account
Account details: username, password, etc.
int fd
Socket file descriptor.
Container for Accounts, Notifications.
struct ConfigSubset * sub
Inherited config items.