NeoMutt  2024-03-23-147-g885fbc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches

Open a socket Connection. More...

+ Collaboration diagram for open():

Functions

static int tls_socket_open (struct Connection *conn)
 Open a TLS socket - Implements Connection::open() -.
 
static int ssl_socket_open_err (struct Connection *conn)
 Error callback for opening an SSL connection - Implements Connection::open() -.
 
static int ssl_socket_open (struct Connection *conn)
 Open an SSL socket - Implements Connection::open() -.
 
int raw_socket_open (struct Connection *conn)
 Open a socket - Implements Connection::open() -.
 
static int mutt_sasl_conn_open (struct Connection *conn)
 Empty wrapper for underlying open function - Implements Connection::open() -.
 
static int tunnel_socket_open (struct Connection *conn)
 Open a tunnel socket - Implements Connection::open() -.
 
static int zstrm_open (struct Connection *conn)
 Open a socket - Implements Connection::open() -.
 

Variables

int(* SaslSockData::open )(struct Connection *conn)
 Open a socket Connection - Implements Connection::open() -.
 

Detailed Description

Open a socket Connection.

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

Function Documentation

◆ tls_socket_open()

static int tls_socket_open ( struct Connection conn)
static

Open a TLS socket - Implements Connection::open() -.

Definition at line 1034 of file gnutls.c.

1035{
1036 if (raw_socket_open(conn) < 0)
1037 return -1;
1038
1039 if (tls_negotiate(conn) < 0)
1040 {
1041 tls_socket_close(conn);
1042 return -1;
1043 }
1044
1045 return 0;
1046}
static int tls_negotiate(struct Connection *conn)
Negotiate TLS connection.
Definition: gnutls.c:878
static int tls_socket_close(struct Connection *conn)
Close a TLS socket - Implements Connection::close() -.
Definition: gnutls.c:1009
int raw_socket_open(struct Connection *conn)
Open a socket - Implements Connection::open() -.
Definition: raw.c:146
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ssl_socket_open_err()

static int ssl_socket_open_err ( struct Connection conn)
static

Error callback for opening an SSL connection - Implements Connection::open() -.

Return values
-1Always

Definition at line 356 of file openssl.c.

357{
358 mutt_error(_("SSL disabled due to the lack of entropy"));
359 return -1;
360}
#define mutt_error(...)
Definition: logging2.h:92
#define _(a)
Definition: message.h:28
+ Here is the caller graph for this function:

◆ ssl_socket_open()

static int ssl_socket_open ( struct Connection conn)
static

Open an SSL socket - Implements Connection::open() -.

Definition at line 1327 of file openssl.c.

1328{
1329 if (raw_socket_open(conn) < 0)
1330 return -1;
1331
1332 int rc = ssl_setup(conn);
1333 if (rc)
1334 raw_socket_close(conn);
1335
1336 return rc;
1337}
int raw_socket_close(struct Connection *conn)
Close a socket - Implements Connection::close() -.
Definition: raw.c:391
static int ssl_setup(struct Connection *conn)
Set up SSL on the Connection.
Definition: openssl.c:1210
+ Here is the call graph for this function:
+ 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 146 of file raw.c.

147{
148 int rc;
149
150 char *host_idna = NULL;
151
152#ifdef HAVE_GETADDRINFO
153 /* --- IPv4/6 --- */
154
155 /* "65536\0" */
156 char port[6] = { 0 };
157 struct addrinfo hints = { 0 };
158 struct addrinfo *res = NULL;
159 struct addrinfo *cur = NULL;
160
161 /* we accept v4 or v6 STREAM sockets */
162 const bool c_use_ipv6 = cs_subset_bool(NeoMutt->sub, "use_ipv6");
163 if (c_use_ipv6)
164 hints.ai_family = AF_UNSPEC;
165 else
166 hints.ai_family = AF_INET;
167
168 hints.ai_socktype = SOCK_STREAM;
169
170 snprintf(port, sizeof(port), "%d", conn->account.port);
171
172#ifdef HAVE_LIBIDN
173 if (mutt_idna_to_ascii_lz(conn->account.host, &host_idna, 1) != 0)
174 {
175 mutt_error(_("Bad IDN: '%s'"), conn->account.host);
176 return -1;
177 }
178#else
179 host_idna = conn->account.host;
180#endif
181
182 if (!OptNoCurses)
183 mutt_message(_("Looking up %s..."), conn->account.host);
184
185 rc = getaddrinfo(host_idna, port, &hints, &res);
186
187#ifdef HAVE_LIBIDN
188 FREE(&host_idna);
189#endif
190
191 if (rc)
192 {
193 mutt_error(_("Could not find the host \"%s\""), conn->account.host);
194 return -1;
195 }
196
197 if (!OptNoCurses)
198 mutt_message(_("Connecting to %s..."), conn->account.host);
199
200 rc = -1;
201 for (cur = res; cur; cur = cur->ai_next)
202 {
203 int fd = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
204 if (fd >= 0)
205 {
206 rc = socket_connect(fd, cur->ai_addr);
207 if (rc == 0)
208 {
209 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
210 conn->fd = fd;
211 break;
212 }
213 else
214 {
215 close(fd);
216 }
217 }
218 }
219
220 freeaddrinfo(res);
221#else
222 /* --- IPv4 only --- */
223
224 struct hostent *he = NULL;
225 struct sockaddr_in sin = { 0 };
226 sin.sin_port = htons(conn->account.port);
227 sin.sin_family = AF_INET;
228
229#ifdef HAVE_LIBIDN
230 if (mutt_idna_to_ascii_lz(conn->account.host, &host_idna, 1) != 0)
231 {
232 mutt_error(_("Bad IDN: '%s'"), conn->account.host);
233 return -1;
234 }
235#else
236 host_idna = conn->account.host;
237#endif
238
239 if (!OptNoCurses)
240 mutt_message(_("Looking up %s..."), conn->account.host);
241
242 he = gethostbyname(host_idna);
243
244#ifdef HAVE_LIBIDN
245 FREE(&host_idna);
246#endif
247
248 if (!he)
249 {
250 mutt_error(_("Could not find the host \"%s\""), conn->account.host);
251
252 return -1;
253 }
254
255 if (!OptNoCurses)
256 mutt_message(_("Connecting to %s..."), conn->account.host);
257
258 rc = -1;
259 for (int i = 0; he->h_addr_list[i]; i++)
260 {
261 memcpy(&sin.sin_addr, he->h_addr_list[i], he->h_length);
262 int fd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
263
264 if (fd >= 0)
265 {
266 rc = socket_connect(fd, (struct sockaddr *) &sin);
267 if (rc == 0)
268 {
269 fcntl(fd, F_SETFD, FD_CLOEXEC);
270 conn->fd = fd;
271 break;
272 }
273 else
274 {
275 close(fd);
276 }
277 }
278 }
279#endif
280 if (rc)
281 {
282 mutt_error(_("Could not connect to %s (%s)"), conn->account.host,
283 (rc > 0) ? strerror(rc) : _("unknown error"));
284 return -1;
285 }
286
287 return 0;
288}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: globals.c:72
#define mutt_message(...)
Definition: logging2.h:91
int mutt_idna_to_ascii_lz(const char *input, char **output, uint8_t flags)
Convert a domain to Punycode.
Definition: idna.c:90
#define FREE(x)
Definition: memory.h:45
static int socket_connect(int fd, struct sockaddr *sa)
Set up to connect to a socket fd.
Definition: raw.c:66
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:49
int fd
Socket file descriptor.
Definition: connection.h:53
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_sasl_conn_open()

static int mutt_sasl_conn_open ( struct Connection conn)
static

Empty wrapper for underlying open function - Implements Connection::open() -.

We don't know in advance that a connection will use SASL, so we replace conn's methods with sasl methods when authentication is successful, using mutt_sasl_setup_conn

Definition at line 427 of file sasl.c.

428{
429 struct SaslSockData *sasldata = conn->sockdata;
430 conn->sockdata = sasldata->sockdata;
431 int rc = sasldata->open(conn);
432 conn->sockdata = sasldata;
433
434 return rc;
435}
int(* open)(struct Connection *conn)
Open a socket Connection - Implements Connection::open() -.
Definition: sasl.c:80
void * sockdata
Backend-specific socket data.
Definition: connection.h:55
SASL authentication API -.
Definition: sasl.c:65
void * sockdata
Underlying socket data.
Definition: sasl.c:75
+ Here is the caller graph for this function:

◆ tunnel_socket_open()

static int tunnel_socket_open ( struct Connection conn)
static

Open a tunnel socket - Implements Connection::open() -.

Definition at line 59 of file tunnel.c.

60{
61 int pin[2], pout[2];
62
63 struct TunnelSockData *tunnel = mutt_mem_malloc(sizeof(struct TunnelSockData));
64 conn->sockdata = tunnel;
65
66 const char *const c_tunnel = cs_subset_string(NeoMutt->sub, "tunnel");
67 mutt_message(_("Connecting with \"%s\"..."), c_tunnel);
68
69 int rc = pipe(pin);
70 if (rc == -1)
71 {
72 mutt_perror("pipe");
73 FREE(&conn->sockdata);
74 return -1;
75 }
76 rc = pipe(pout);
77 if (rc == -1)
78 {
79 mutt_perror("pipe");
80 close(pin[0]);
81 close(pin[1]);
82 FREE(&conn->sockdata);
83 return -1;
84 }
85
87 int pid = fork();
88 if (pid == 0)
89 {
92 const int fd_null = open("/dev/null", O_RDWR);
93 if ((fd_null < 0) || (dup2(pout[0], STDIN_FILENO) < 0) ||
94 (dup2(pin[1], STDOUT_FILENO) < 0) || (dup2(fd_null, STDERR_FILENO) < 0))
95 {
96 _exit(127);
97 }
98 close(pin[0]);
99 close(pin[1]);
100 close(pout[0]);
101 close(pout[1]);
102 close(fd_null);
103
104 /* Don't let the subprocess think it can use the controlling tty */
105 setsid();
106
107 execle(EXEC_SHELL, "sh", "-c", c_tunnel, NULL, EnvList);
108 _exit(127);
109 }
111
112 if (pid == -1)
113 {
114 mutt_perror("fork");
115 close(pin[0]);
116 close(pin[1]);
117 close(pout[0]);
118 close(pout[1]);
119 FREE(&conn->sockdata);
120 return -1;
121 }
122 if ((close(pin[1]) < 0) || (close(pout[0]) < 0))
123 mutt_perror("close");
124
125 fcntl(pin[0], F_SETFD, FD_CLOEXEC);
126 fcntl(pout[1], F_SETFD, FD_CLOEXEC);
127
128 tunnel->fd_read = pin[0];
129 tunnel->fd_write = pout[1];
130 tunnel->pid = pid;
131
132 conn->fd = 42; /* stupid hack */
133
134 /* Note we are using ssf as a boolean in this case. See the notes in
135 * conn/connection.h */
136 const bool c_tunnel_is_secure = cs_subset_bool(NeoMutt->sub, "tunnel_is_secure");
137 if (c_tunnel_is_secure)
138 conn->ssf = 1;
139
140 return 0;
141}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:292
#define EXEC_SHELL
Definition: filter.h:28
char ** EnvList
Private copy of the environment variables.
Definition: globals.c:78
#define mutt_perror(...)
Definition: logging2.h:93
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
void mutt_sig_reset_child_signals(void)
Reset ignored signals back to the default.
Definition: signal.c:275
void mutt_sig_block_system(void)
Block signals before calling exec()
Definition: signal.c:199
void mutt_sig_unblock_system(bool restore)
Restore previously blocked signals.
Definition: signal.c:223
unsigned int ssf
Security strength factor, in bits (see notes)
Definition: connection.h:50
A network tunnel (pair of sockets)
Definition: tunnel.c:50
int fd_read
File descriptor to read from.
Definition: tunnel.c:52
pid_t pid
Process ID of tunnel program.
Definition: tunnel.c:51
int fd_write
File descriptor to write to.
Definition: tunnel.c:53
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ zstrm_open()

static int zstrm_open ( struct Connection conn)
static

Open a socket - Implements Connection::open() -.

Return values
-1Always

Cannot open a zlib connection, must wrap an existing one

Definition at line 92 of file zstrm.c.

93{
94 return -1;
95}
+ Here is the caller graph for this function:

Variable Documentation

◆ open

int(* SaslSockData::open) (struct Connection *conn)

Open a socket Connection - Implements Connection::open() -.

Definition at line 80 of file sasl.c.