NeoMutt  2020-06-26-250-g349c94
Teaching an old dog new tricks
DOXYGEN
private.h File Reference

Shared functions that are private to Connections. More...

#include <stddef.h>
#include <stdbool.h>
#include <time.h>
+ Include dependency graph for private.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int raw_socket_close (struct Connection *conn)
 Close a socket - Implements Connection::close() More...
 
int raw_socket_open (struct Connection *conn)
 Open a socket - Implements Connection::open() More...
 
int raw_socket_poll (struct Connection *conn, time_t wait_secs)
 Checks whether reads would block - Implements Connection::poll() More...
 
int raw_socket_read (struct Connection *conn, char *buf, size_t len)
 Read data from a socket - Implements Connection::read() More...
 
int raw_socket_write (struct Connection *conn, const char *buf, size_t count)
 Write data to a socket - Implements Connection::write() More...
 
void mutt_tunnel_socket_setup (struct Connection *conn)
 sets up tunnel connection functions More...
 

Variables

const char * C_CertificateFile
 Config: (ssl) File containing trusted certificates. More...
 
short C_ConnectTimeout
 Config: Timeout for making network connections (-1 to wait indefinitely) More...
 
const char * C_EntropyFile
 Config: (ssl) File/device containing random data to initialise SSL. More...
 
const char * C_Preconnect
 Config: External command to run prior to opening a socket. More...
 
const char * C_SslCaCertificatesFile
 Config: File containing trusted CA certificates. More...
 
const char * C_SslCiphers
 Config: (ssl) Ciphers to use when using SSL. More...
 
const char * C_SslClientCert
 Config: (ssl) File containing client certificates. More...
 
bool C_SslForceTls
 Config: (ssl) Require TLS encryption for all connections. More...
 
short C_SslMinDhPrimeBits
 Config: Minimum keysize for Diffie-Hellman key exchange. More...
 
unsigned char C_SslStarttls
 Config: (ssl) Use STARTTLS on servers advertising the capability. More...
 
bool C_SslUseSslv2
 
bool C_SslUseSslv3
 Config: (ssl) INSECURE: Use SSLv3 for authentication. More...
 
bool C_SslUseTlsv11
 Config: (ssl) Use TLSv1.1 for authentication. More...
 
bool C_SslUseTlsv12
 Config: (ssl) Use TLSv1.2 for authentication. More...
 
bool C_SslUseTlsv13
 Config: (ssl) Use TLSv1.3 for authentication. More...
 
bool C_SslUseTlsv1
 Config: (ssl) Use TLSv1 for authentication. More...
 
bool C_SslUsesystemcerts
 
bool C_SslVerifyDates
 Config: (ssl) Verify the dates on the server certificate. More...
 
bool C_SslVerifyHost
 Config: (ssl) Verify the server's hostname against the certificate. More...
 
bool C_SslVerifyPartialChains
 Config: (ssl) Allow verification using partial certificate chains. More...
 
const char * C_Tunnel
 Config: Shell command to establish a tunnel. More...
 
bool C_TunnelIsSecure
 Config: Assume a tunneled connection is secure. More...
 
bool C_UseIpv6
 Config: Lookup IPv6 addresses when making connections. More...
 

Detailed Description

Shared functions that are private to Connections.

Authors
  • Richard Russon

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 private.h.

Function Documentation

◆ raw_socket_close()

int raw_socket_close ( struct Connection conn)

Close a socket - Implements Connection::close()

Definition at line 360 of file raw.c.

361 {
362  return close(conn->fd);
363 }
int fd
Socket file descriptor.
Definition: connection.h:40
void(* close)(void **cctx)
Close a compression context.
Definition: lib.h:103
+ Here is the caller graph for this function:

◆ raw_socket_open()

int raw_socket_open ( struct Connection conn)

Open a socket - Implements Connection::open()

Definition at line 116 of file raw.c.

117 {
118  int rc;
119 
120  char *host_idna = NULL;
121 
122 #ifdef HAVE_GETADDRINFO
123  /* --- IPv4/6 --- */
124 
125  /* "65536\0" */
126  char port[6];
127  struct addrinfo hints;
128  struct addrinfo *res = NULL;
129  struct addrinfo *cur = NULL;
130 
131  /* we accept v4 or v6 STREAM sockets */
132  memset(&hints, 0, sizeof(hints));
133 
134  if (C_UseIpv6)
135  hints.ai_family = AF_UNSPEC;
136  else
137  hints.ai_family = AF_INET;
138 
139  hints.ai_socktype = SOCK_STREAM;
140 
141  snprintf(port, sizeof(port), "%d", conn->account.port);
142 
143 #ifdef HAVE_LIBIDN
144  if (mutt_idna_to_ascii_lz(conn->account.host, &host_idna, 1) != 0)
145  {
146  mutt_error(_("Bad IDN: '%s'"), conn->account.host);
147  return -1;
148  }
149 #else
150  host_idna = conn->account.host;
151 #endif
152 
153  if (!OptNoCurses)
154  mutt_message(_("Looking up %s..."), conn->account.host);
155 
156  rc = getaddrinfo(host_idna, port, &hints, &res);
157 
158 #ifdef HAVE_LIBIDN
159  FREE(&host_idna);
160 #endif
161 
162  if (rc)
163  {
164  mutt_error(_("Could not find the host \"%s\""), conn->account.host);
165  return -1;
166  }
167 
168  if (!OptNoCurses)
169  mutt_message(_("Connecting to %s..."), conn->account.host);
170 
171  rc = -1;
172  for (cur = res; cur; cur = cur->ai_next)
173  {
174  int fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
175  if (fd >= 0)
176  {
177  rc = socket_connect(fd, cur->ai_addr);
178  if (rc == 0)
179  {
180  fcntl(fd, F_SETFD, FD_CLOEXEC);
181  conn->fd = fd;
182  break;
183  }
184  else
185  close(fd);
186  }
187  }
188 
189  freeaddrinfo(res);
190 #else
191  /* --- IPv4 only --- */
192 
193  struct sockaddr_in sin;
194  struct hostent *he = NULL;
195 
196  memset(&sin, 0, sizeof(sin));
197  sin.sin_port = htons(conn->account.port);
198  sin.sin_family = AF_INET;
199 
200 #ifdef HAVE_LIBIDN
201  if (mutt_idna_to_ascii_lz(conn->account.host, &host_idna, 1) != 0)
202  {
203  mutt_error(_("Bad IDN: '%s'"), conn->account.host);
204  return -1;
205  }
206 #else
207  host_idna = conn->account.host;
208 #endif
209 
210  if (!OptNoCurses)
211  mutt_message(_("Looking up %s..."), conn->account.host);
212 
213  he = gethostbyname(host_idna);
214 
215 #ifdef HAVE_LIBIDN
216  FREE(&host_idna);
217 #endif
218 
219  if (!he)
220  {
221  mutt_error(_("Could not find the host \"%s\""), conn->account.host);
222 
223  return -1;
224  }
225 
226  if (!OptNoCurses)
227  mutt_message(_("Connecting to %s..."), conn->account.host);
228 
229  rc = -1;
230  for (int i = 0; he->h_addr_list[i]; i++)
231  {
232  memcpy(&sin.sin_addr, he->h_addr_list[i], he->h_length);
233  int fd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
234 
235  if (fd >= 0)
236  {
237  rc = socket_connect(fd, (struct sockaddr *) &sin);
238  if (rc == 0)
239  {
240  fcntl(fd, F_SETFD, FD_CLOEXEC);
241  conn->fd = fd;
242  break;
243  }
244  else
245  close(fd);
246  }
247  }
248 #endif
249  if (rc)
250  {
251  mutt_error(_("Could not connect to %s (%s)"), conn->account.host,
252  (rc > 0) ? strerror(rc) : _("unknown error"));
253  return -1;
254  }
255 
256  return 0;
257 }
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:36
#define mutt_message(...)
Definition: logging.h:83
#define _(a)
Definition: message.h:28
WHERE bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:48
char host[128]
Server to login to.
Definition: connaccount.h:53
static int socket_connect(int fd, struct sockaddr *sa)
set up to connect to a socket fd
Definition: raw.c:67
unsigned short port
Port to connect to.
Definition: connaccount.h:57
bool C_UseIpv6
Config: Lookup IPv6 addresses when making connections.
Definition: config.c:67
int fd
Socket file descriptor.
Definition: connection.h:40
int mutt_idna_to_ascii_lz(const char *input, char **output, int flags)
#define mutt_error(...)
Definition: logging.h:84
#define FREE(x)
Definition: memory.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ raw_socket_poll()

int raw_socket_poll ( struct Connection conn,
time_t  wait_secs 
)

Checks whether reads would block - Implements Connection::poll()

Definition at line 322 of file raw.c.

323 {
324  if (conn->fd < 0)
325  return -1;
326 
327  fd_set rfds;
328  struct timeval tv;
329 
330  uint64_t wait_millis = wait_secs * 1000UL;
331 
332  while (true)
333  {
334  tv.tv_sec = wait_millis / 1000;
335  tv.tv_usec = (wait_millis % 1000) * 1000;
336 
337  FD_ZERO(&rfds);
338  FD_SET(conn->fd, &rfds);
339 
340  uint64_t pre_t = mutt_date_epoch_ms();
341  const int rc = select(conn->fd + 1, &rfds, NULL, NULL, &tv);
342  uint64_t post_t = mutt_date_epoch_ms();
343 
344  if ((rc > 0) || ((rc < 0) && (errno != EINTR)))
345  return rc;
346 
347  if (SigInt)
348  mutt_query_exit();
349 
350  wait_millis += pre_t;
351  if (wait_millis <= post_t)
352  return 0;
353  wait_millis -= post_t;
354  }
355 }
uint64_t mutt_date_epoch_ms(void)
Return the number of milliseconds since the Unix epoch.
Definition: date.c:425
WHERE SIG_ATOMIC_VOLATILE_T SigInt
true after SIGINT is received
Definition: mutt_globals.h:74
void mutt_query_exit(void)
Ask the user if they want to leave NeoMutt.
Definition: curs_lib.c:538
int fd
Socket file descriptor.
Definition: connection.h:40
+ 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  len 
)

Read data from a socket - Implements Connection::read()

Definition at line 262 of file raw.c.

263 {
264  int rc;
265 
267  do
268  {
269  rc = read(conn->fd, buf, count);
270  } while (rc < 0 && (errno == EINTR));
271 
272  if (rc < 0)
273  {
274  mutt_error(_("Error talking to %s (%s)"), conn->account.host, strerror(errno));
275  SigInt = 0;
276  }
278 
279  if (SigInt)
280  {
281  mutt_error(_("Connection to %s has been aborted"), conn->account.host);
282  SigInt = 0;
283  rc = -1;
284  }
285 
286  return rc;
287 }
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:36
WHERE SIG_ATOMIC_VOLATILE_T SigInt
true after SIGINT is received
Definition: mutt_globals.h:74
#define _(a)
Definition: message.h:28
void mutt_sig_allow_interrupt(bool allow)
Allow/disallow Ctrl-C (SIGINT)
Definition: signal.c:238
char host[128]
Server to login to.
Definition: connaccount.h:53
int fd
Socket file descriptor.
Definition: connection.h:40
#define mutt_error(...)
Definition: logging.h:84
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ raw_socket_write()

int raw_socket_write ( struct Connection conn,
const char *  buf,
size_t  count 
)

Write data to a socket - Implements Connection::write()

Definition at line 292 of file raw.c.

293 {
294  int rc;
295  size_t sent = 0;
296 
298  do
299  {
300  do
301  {
302  rc = write(conn->fd, buf + sent, count - sent);
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));
309  return -1;
310  }
311 
312  sent += rc;
313  } while ((sent < count) && (SigInt == 0));
314 
316  return sent;
317 }
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:36
WHERE SIG_ATOMIC_VOLATILE_T SigInt
true after SIGINT is received
Definition: mutt_globals.h:74
#define _(a)
Definition: message.h:28
void mutt_sig_allow_interrupt(bool allow)
Allow/disallow Ctrl-C (SIGINT)
Definition: signal.c:238
char host[128]
Server to login to.
Definition: connaccount.h:53
int fd
Socket file descriptor.
Definition: connection.h:40
#define mutt_error(...)
Definition: logging.h:84
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_tunnel_socket_setup()

void mutt_tunnel_socket_setup ( struct Connection conn)

sets up tunnel connection functions

Parameters
connConnection to assign functions to

Assign tunnel socket functions to the Connection conn.

Definition at line 233 of file tunnel.c.

234 {
235  conn->open = tunnel_socket_open;
236  conn->close = tunnel_socket_close;
237  conn->read = tunnel_socket_read;
238  conn->write = tunnel_socket_write;
239  conn->poll = tunnel_socket_poll;
240  /* Note we are using ssf as a boolean in this case. See the notes in
241  * conn/connection.h */
242  if (C_TunnelIsSecure)
243  conn->ssf = 1;
244 }
static int tunnel_socket_poll(struct Connection *conn, time_t wait_secs)
Checks whether tunnel reads would block - Implements Connection::poll()
Definition: tunnel.c:187
unsigned int ssf
Security strength factor, in bits (see below)
Definition: connection.h:37
int(* read)(struct Connection *conn, char *buf, size_t count)
Read from a socket Connection.
Definition: connection.h:72
bool C_TunnelIsSecure
Config: Assume a tunneled connection is secure.
Definition: config.c:39
int(* open)(struct Connection *conn)
Note about ssf: in actuality, NeoMutt uses this as a boolean to determine if the connection is "secur...
Definition: connection.h:62
static int tunnel_socket_close(struct Connection *conn)
Close a tunnel socket - Implements Connection::close()
Definition: tunnel.c:204
int(* close)(struct Connection *conn)
Close a socket Connection.
Definition: connection.h:100
static int tunnel_socket_open(struct Connection *conn)
Open a tunnel socket - Implements Connection::open()
Definition: tunnel.c:58
static int tunnel_socket_write(struct Connection *conn, const char *buf, size_t count)
Write data to a tunnel socket - Implements Connection::write()
Definition: tunnel.c:159
int(* poll)(struct Connection *conn, time_t wait_secs)
Check whether a socket read would block.
Definition: connection.h:92
int(* write)(struct Connection *conn, const char *buf, size_t count)
Write to a socket Connection.
Definition: connection.h:82
static int tunnel_socket_read(struct Connection *conn, char *buf, size_t count)
Read data from a tunnel socket - Implements Connection::read()
Definition: tunnel.c:137
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ C_CertificateFile

const char* C_CertificateFile

Config: (ssl) File containing trusted certificates.

Definition at line 41 of file config.c.

◆ C_ConnectTimeout

short C_ConnectTimeout

Config: Timeout for making network connections (-1 to wait indefinitely)

Definition at line 36 of file config.c.

◆ C_EntropyFile

const char* C_EntropyFile

Config: (ssl) File/device containing random data to initialise SSL.

Definition at line 42 of file config.c.

◆ C_Preconnect

const char* C_Preconnect

Config: External command to run prior to opening a socket.

Definition at line 37 of file config.c.

◆ C_SslCaCertificatesFile

const char* C_SslCaCertificatesFile

Config: File containing trusted CA certificates.

Definition at line 63 of file config.c.

◆ C_SslCiphers

const char* C_SslCiphers

Config: (ssl) Ciphers to use when using SSL.

Definition at line 43 of file config.c.

◆ C_SslClientCert

const char* C_SslClientCert

Config: (ssl) File containing client certificates.

Definition at line 44 of file config.c.

◆ C_SslForceTls

bool C_SslForceTls

Config: (ssl) Require TLS encryption for all connections.

Definition at line 45 of file config.c.

◆ C_SslMinDhPrimeBits

short C_SslMinDhPrimeBits

Config: Minimum keysize for Diffie-Hellman key exchange.

Definition at line 64 of file config.c.

◆ C_SslStarttls

unsigned char C_SslStarttls

Config: (ssl) Use STARTTLS on servers advertising the capability.

Definition at line 46 of file config.c.

◆ C_SslUseSslv2

bool C_SslUseSslv2

◆ C_SslUseSslv3

bool C_SslUseSslv3

Config: (ssl) INSECURE: Use SSLv3 for authentication.

Definition at line 51 of file config.c.

◆ C_SslUseTlsv11

bool C_SslUseTlsv11

Config: (ssl) Use TLSv1.1 for authentication.

Definition at line 53 of file config.c.

◆ C_SslUseTlsv12

bool C_SslUseTlsv12

Config: (ssl) Use TLSv1.2 for authentication.

Definition at line 54 of file config.c.

◆ C_SslUseTlsv13

bool C_SslUseTlsv13

Config: (ssl) Use TLSv1.3 for authentication.

Definition at line 55 of file config.c.

◆ C_SslUseTlsv1

bool C_SslUseTlsv1

Config: (ssl) Use TLSv1 for authentication.

Definition at line 52 of file config.c.

◆ C_SslUsesystemcerts

bool C_SslUsesystemcerts

◆ C_SslVerifyDates

bool C_SslVerifyDates

Config: (ssl) Verify the dates on the server certificate.

Definition at line 56 of file config.c.

◆ C_SslVerifyHost

bool C_SslVerifyHost

Config: (ssl) Verify the server's hostname against the certificate.

Definition at line 57 of file config.c.

◆ C_SslVerifyPartialChains

bool C_SslVerifyPartialChains

Config: (ssl) Allow verification using partial certificate chains.

Definition at line 59 of file config.c.

◆ C_Tunnel

const char* C_Tunnel

Config: Shell command to establish a tunnel.

Definition at line 38 of file config.c.

◆ C_TunnelIsSecure

bool C_TunnelIsSecure

Config: Assume a tunneled connection is secure.

Definition at line 39 of file config.c.

◆ C_UseIpv6

bool C_UseIpv6

Config: Lookup IPv6 addresses when making connections.

Definition at line 67 of file config.c.