summaryrefslogtreecommitdiff
path: root/src/rexmpp_tcp.rs
blob: 56c204c92b05a0408139a5bea463de2cd8518e4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use std::os::raw::{c_int, c_char};
use libc::*;
use std::ptr::{null_mut,null};
use std::mem;
use errno::{errno};

use super::{rexmpp_dns, rexmpp};


#[link(name = "libc")]
extern {
    fn inet_pton (af: c_int, src: *const c_char, dst: *mut c_void) -> c_int;
}


const REXMPP_TCP_MAX_CONNECTION_ATTEMPTS: usize = 20;
const REXMPP_TCP_IPV6_DELAY_MS: i64 = 50;
const REXMPP_TCP_CONN_DELAY_MS: i64 = 250;

#[derive(PartialEq, Copy, Clone)]
#[repr(C)]
pub enum ResolutionStatus {
    Inactive,
    Waiting,
    Success,
    Failure
}

#[derive(PartialEq, Copy, Clone)]
#[repr(C)]
pub enum ConnectionError {
    Done,
    ResolverError,
    InProgress,
    Failure,
    Error
}

#[repr(C)]
pub struct RexmppTCPConnection {
    pub host: *const c_char,
    pub port: u16,
    pub resolution_v4: ResolutionStatus,
    pub resolver_status_v4: c_int,
    pub resolved_v4: *mut rexmpp_dns::RexmppDNSResult,
    pub addr_cur_v4: c_int,
    pub resolution_v6: ResolutionStatus,
    pub resolver_status_v6: c_int,
    pub resolved_v6: *mut rexmpp_dns::RexmppDNSResult,
    pub addr_cur_v6: c_int,
    pub sockets: [c_int; REXMPP_TCP_MAX_CONNECTION_ATTEMPTS],
    pub connection_attempts: c_int,
    pub next_connection_time: timespec,
    pub fd: c_int,
    pub dns_secure: bool
}

#[no_mangle]
unsafe extern "C"
fn rexmpp_tcp_dns_aaaa_cb (_s: *mut rexmpp::Rexmpp,
                           ptr: *mut c_void,
                           result: *mut rexmpp_dns::RexmppDNSResult)
                           -> () {
    let conn = ptr as *mut RexmppTCPConnection;
    (*conn).resolved_v6 = result;
    if result != null_mut() {
        (*conn).resolution_v6 = ResolutionStatus::Success;
        (*conn).addr_cur_v6 = -1;
    } else {
        (*conn).resolution_v6 = ResolutionStatus::Failure;
    }
}

#[no_mangle]
unsafe extern "C"
fn rexmpp_tcp_dns_a_cb (_s: *mut rexmpp::Rexmpp,
                        ptr: *mut c_void,
                        result: *mut rexmpp_dns::RexmppDNSResult)
                        -> () {
    let conn = ptr as *mut RexmppTCPConnection;
    (*conn).resolved_v4 = result;
    if result != null_mut() {
        (*conn).resolution_v4 = ResolutionStatus::Success;
        (*conn).addr_cur_v4 = -1;
        if (*conn).resolution_v6 == ResolutionStatus::Waiting {
            // Wait a bit (usually 50 ms) for IPv6
            clock_gettime(CLOCK_MONOTONIC, &mut (*conn).next_connection_time);
            (*conn).next_connection_time.tv_nsec += REXMPP_TCP_IPV6_DELAY_MS * 1000000;
            if (*conn).next_connection_time.tv_nsec >= 1000000000 {
                (*conn).next_connection_time.tv_nsec -= 1000000000;
                (*conn).next_connection_time.tv_sec += 1;
            }
        }
    } else {
        (*conn).resolution_v4 = ResolutionStatus::Failure;
    }
}


#[no_mangle]
unsafe extern "C"
fn rexmpp_tcp_cleanup (conn: *mut RexmppTCPConnection) -> () {
    for i in 0..=(REXMPP_TCP_MAX_CONNECTION_ATTEMPTS - 1) {
        if (*conn).sockets[i] != -1 && (*conn).sockets[i] != (*conn).fd {
            close((*conn).sockets[i]);
            (*conn).sockets[i] = -1;
        }
    }
    if (*conn).resolution_v4 != ResolutionStatus::Inactive {
        (*conn).resolution_v4 = ResolutionStatus::Inactive;
        (*conn).resolution_v6 = ResolutionStatus::Inactive;
    }
    if (*conn).resolved_v4 != null_mut() {
        rexmpp_dns::rexmpp_dns_result_free((*conn).resolved_v4);
        (*conn).resolved_v4 = null_mut();
    }
    if (*conn).resolved_v6 != null_mut() {
        rexmpp_dns::rexmpp_dns_result_free((*conn).resolved_v6);
        (*conn).resolved_v6 = null_mut();
    }
}

#[no_mangle]
unsafe extern "C"
fn rexmpp_tcp_connected (conn: *mut RexmppTCPConnection, fd: c_int)
                         -> ConnectionError {
    let mut sa_ptr = mem::MaybeUninit::<sockaddr>::uninit();
    let mut sa_len : socklen_t = mem::size_of::<sockaddr>() as u32;
    getsockname(fd, sa_ptr.as_mut_ptr(), &mut sa_len);
    let sa = sa_ptr.assume_init();
    if sa.sa_family == (AF_INET as u16)
        && (*conn).resolved_v4 != null_mut() {
            (*conn).dns_secure = (*(*conn).resolved_v4).secure;
        }
    else if sa.sa_family == (AF_INET6 as u16)
        && (*conn).resolved_v6 != null_mut() {
            (*conn).dns_secure = (*(*conn).resolved_v6).secure;
        }
    (*conn).fd = fd;
    rexmpp_tcp_cleanup(conn);
    return ConnectionError::Done;
}

#[no_mangle]
unsafe extern "C"
fn rexmpp_tcp_socket (s: *mut rexmpp::Rexmpp, domain: c_int) -> c_int {
    let sock: c_int = socket(domain, SOCK_STREAM, 0);

    // Make it non-blocking
    let flags: c_int = fcntl(sock, F_GETFL, 0);
    fcntl(sock, F_SETFL, flags | O_NONBLOCK);

    // Call the socket creation callback, if provided
    ((*s).socket_cb).map(|cb| cb(s, sock));

    return sock;
}

#[no_mangle]
unsafe extern "C"
fn rexmpp_tcp_conn_init (s: *mut rexmpp::Rexmpp,
                         conn: *mut RexmppTCPConnection,
                         host: *const c_char,
                         port: u16)
                         -> ConnectionError {
    for i in 0..=(REXMPP_TCP_MAX_CONNECTION_ATTEMPTS - 1) {
        (*conn).sockets[i] = -1;
    }
    (*conn).connection_attempts = 0;
    (*conn).port = port;
    (*conn).resolved_v4 = null_mut();
    (*conn).resolved_v6 = null_mut();
    (*conn).fd = -1;
    (*conn).dns_secure = false;
    (*conn).next_connection_time.tv_sec = 0;
    (*conn).next_connection_time.tv_nsec = 0;

    (*conn).resolution_v4 = ResolutionStatus::Inactive;
    (*conn).resolution_v6 = ResolutionStatus::Inactive;

    let mut addr_v4 = mem::MaybeUninit::<sockaddr_in>::uninit();
    if inet_pton(AF_INET, host,
                 &mut ((*addr_v4.as_mut_ptr()).sin_addr)
                 as *mut in_addr as *mut c_void) == 1 {
        (*addr_v4.as_mut_ptr()).sin_family = AF_INET as u16;
        (*addr_v4.as_mut_ptr()).sin_port = port.to_be();
        (*conn).sockets[(*conn).connection_attempts as usize] =
            rexmpp_tcp_socket(s, AF_INET);
        if connect((*conn).sockets[(*conn).connection_attempts as usize],
                   addr_v4.as_mut_ptr() as *mut sockaddr,
                   mem::size_of::<sockaddr_in>() as u32) != 0 {
            if errno().0 != EINPROGRESS {
                return ConnectionError::Error;
            }
        } else {
            return rexmpp_tcp_connected(conn,
                                        (*conn).sockets[(*conn).connection_attempts as usize]);
        }
        (*conn).connection_attempts += 1;
        return ConnectionError::InProgress;
    }

    let mut addr_v6 = mem::MaybeUninit::<sockaddr_in6>::uninit();
    if inet_pton(AF_INET6, host,
                 &mut ((*addr_v6.as_mut_ptr()).sin6_addr)
                 as *mut in6_addr as *mut c_void) == 1 {
        (*addr_v6.as_mut_ptr()).sin6_family = AF_INET as u16;
        (*addr_v6.as_mut_ptr()).sin6_port = port.to_be();
        (*addr_v6.as_mut_ptr()).sin6_flowinfo = 0;
        (*addr_v6.as_mut_ptr()).sin6_scope_id = 0;
        (*conn).sockets[(*conn).connection_attempts as usize] =
            rexmpp_tcp_socket(s, AF_INET6);
        if connect((*conn).sockets[(*conn).connection_attempts as usize],
                   addr_v6.as_mut_ptr() as *mut sockaddr,
                   mem::size_of::<sockaddr_in6>() as u32) != 0 {
            if errno().0 != EINPROGRESS {
                return ConnectionError::Error;
            }
        } else {
            return rexmpp_tcp_connected(conn,
                                        (*conn).sockets[(*conn).connection_attempts as usize]);
        }
        (*conn).connection_attempts += 1;
        return ConnectionError::InProgress;
    }
    (*conn).resolution_v4 = ResolutionStatus::Waiting;
    (*conn).resolution_v6 = ResolutionStatus::Waiting;

    rexmpp_dns::rexmpp_dns_resolve(s, host, 28, 1,
                                   conn as *mut c_void,
                                   rexmpp_tcp_dns_aaaa_cb);
    rexmpp_dns::rexmpp_dns_resolve(s, host, 1, 1,
                                   conn as *mut c_void,
                                   rexmpp_tcp_dns_a_cb);
    return ConnectionError::InProgress;
}

#[no_mangle]
unsafe extern "C"
fn rexmpp_tcp_conn_finish (conn: *mut RexmppTCPConnection) -> c_int {
  rexmpp_tcp_cleanup(conn);
  return (*conn).fd;
}

#[no_mangle]
unsafe extern "C"
fn rexmpp_tcp_conn_ipv4_available (conn: *mut RexmppTCPConnection) -> bool {
    (*conn).resolution_v4 == ResolutionStatus::Success
        && (*conn).resolved_v4 != null_mut()
        && *(*(*conn).resolved_v4).data
        .offset(((*conn).addr_cur_v4 + 1) as isize) != null_mut()
}

#[no_mangle]
unsafe extern "C"
fn rexmpp_tcp_conn_ipv6_available (conn: *mut RexmppTCPConnection) -> bool {
    (*conn).resolution_v6 == ResolutionStatus::Success
        && (*conn).resolved_v6 != null_mut()
        && *(*(*conn).resolved_v6).data
        .offset(((*conn).addr_cur_v6 + 1) as isize) != null_mut()
}

#[no_mangle]
unsafe extern "C"
fn rexmpp_tcp_conn_proceed (s: *mut rexmpp::Rexmpp,
                            conn: *mut RexmppTCPConnection,
                            read_fds: *mut fd_set,
                            write_fds: *mut fd_set) -> ConnectionError {
    // Check for successful connections.
    for i in 0..=(REXMPP_TCP_MAX_CONNECTION_ATTEMPTS - 1) {
        if (*conn).sockets[i] != -1 && FD_ISSET((*conn).sockets[i], write_fds) {
            let mut err: c_int = 0;
            let mut err_len: socklen_t = mem::size_of::<c_int>() as u32;
            if getsockopt((*conn).sockets[i], SOL_SOCKET, SO_ERROR,
                          &mut err as *mut c_int as *mut c_void,
                          &mut err_len) < 0 {
                return ConnectionError::Error;
            } else {
                if err == 0 {
                    return rexmpp_tcp_connected(conn, (*conn).sockets[i]);
                } else if err != EINPROGRESS {
                    close((*conn).sockets[i]);
                    (*conn).sockets[i] = -1;
                }
            }
        }
    }

    // Name resolution
    if (*conn).resolution_v4 == ResolutionStatus::Waiting
        || (*conn).resolution_v6 == ResolutionStatus::Waiting {
            rexmpp_dns::rexmpp_dns_process(s, read_fds, write_fds);
        }
    if (*conn).resolution_v4 == ResolutionStatus::Failure
        && (*conn).resolution_v6 == ResolutionStatus::Failure {
            // Failed to resolve anything
            return ConnectionError::Failure;
        }

    // New connections
    let mut repeat: bool;
    let mut now = mem::MaybeUninit::<timespec>::uninit();
    let now_ptr = now.as_mut_ptr();
    loop {
        repeat = false;
        if (*conn).connection_attempts < REXMPP_TCP_MAX_CONNECTION_ATTEMPTS as i32
            && (rexmpp_tcp_conn_ipv4_available(conn)
                || rexmpp_tcp_conn_ipv6_available(conn)) {
                clock_gettime(CLOCK_MONOTONIC, now_ptr);
                if (*now_ptr).tv_sec > (*conn).next_connection_time.tv_sec
                    || ((*now_ptr).tv_sec == (*conn).next_connection_time.tv_sec
                        && (*now_ptr).tv_nsec >= (*conn).next_connection_time.tv_nsec) {
                        // Time to attempt a new connection
                        let mut use_ipv6 = false;
                        if rexmpp_tcp_conn_ipv4_available(conn) &&
                            rexmpp_tcp_conn_ipv6_available(conn) {
                                if (*conn).addr_cur_v4 >= (*conn).addr_cur_v6 {
                                    use_ipv6 = true;
                                }
                            } else if rexmpp_tcp_conn_ipv6_available(conn) {
                                use_ipv6 = true;
                            }

                        let addr: *mut sockaddr;
                        let addrlen: socklen_t;
                        let domain: c_int;
                        if use_ipv6 {
                            let mut addr_v6: sockaddr_in6 = mem::zeroed();
                            (*conn).addr_cur_v6 += 1;
                            let len = (mem::size_of::<in6_addr>() as i32)
                                .min(*(*(*conn).resolved_v6).len.offset((*conn).addr_cur_v6 as isize));
                            memcpy(&mut addr_v6.sin6_addr as *mut in6_addr as *mut c_void,
                                   *(*(*conn).resolved_v6).data.offset((*conn).addr_cur_v6 as isize),
                                   len as usize);
                            addr_v6.sin6_family = AF_INET6 as u16;
                            addr_v6.sin6_port = (*conn).port.to_be();
                            addr_v6.sin6_flowinfo = 0;
                            addr_v6.sin6_scope_id = 0;
                            domain = AF_INET6;
                            addr = &mut addr_v6 as *mut sockaddr_in6 as *mut sockaddr;
                            addrlen = mem::size_of::<sockaddr_in6>() as u32;
                        } else {
                            let mut addr_v4: sockaddr_in = mem::zeroed();
                            (*conn).addr_cur_v4 += 1;
                            let len = (mem::size_of::<in_addr>() as i32)
                                .min(*(*(*conn).resolved_v4).len.offset((*conn).addr_cur_v4 as isize));
                            memcpy(&mut addr_v4.sin_addr as *mut in_addr as *mut c_void,
                                   *(*(*conn).resolved_v4).data.offset((*conn).addr_cur_v4 as isize),
                                   len as usize);
                            addr_v4.sin_family = AF_INET as u16;
                            addr_v4.sin_port = (*conn).port.to_be();
                            domain = AF_INET;
                            addr = &mut addr_v4 as *mut sockaddr_in as *mut sockaddr;
                            addrlen = mem::size_of::<sockaddr_in>() as u32;
                        }
                        (*conn).sockets[(*conn).connection_attempts as usize] =
                            rexmpp_tcp_socket(s, domain);
                        if connect((*conn).sockets[(*conn).connection_attempts as usize],
                                   addr, addrlen) != 0 {
                            if errno().0 == EINPROGRESS {
                                clock_gettime(CLOCK_MONOTONIC, &mut (*conn).next_connection_time);
                                (*conn).next_connection_time.tv_nsec +=
                                    REXMPP_TCP_CONN_DELAY_MS * 1000000;
                                if (*conn).next_connection_time.tv_nsec >= 1000000000 {
                                    (*conn).next_connection_time.tv_nsec -= 1000000000;
                                    (*conn).next_connection_time.tv_sec += 1;
                                }
                                (*conn).connection_attempts += 1;
                            } else {
                                close((*conn).sockets[(*conn).connection_attempts as usize]);
                                (*conn).sockets[(*conn).connection_attempts as usize] = -1;
                                if (*conn).connection_attempts < REXMPP_TCP_MAX_CONNECTION_ATTEMPTS as i32
                                    && (rexmpp_tcp_conn_ipv4_available(conn) ||
                                        rexmpp_tcp_conn_ipv6_available(conn)) {
                                    repeat = true;
                                }
                            }
                        } else {
                            return rexmpp_tcp_connected(conn,
                                                        (*conn).sockets[(*conn).connection_attempts as usize]);
                        }
                    }
            }
        if ! repeat {
            break;
        }
    }

    let mut active_connections = false;
    for i in 0..=(REXMPP_TCP_MAX_CONNECTION_ATTEMPTS - 1) {
        if (*conn).sockets[i] != -1 {
            active_connections = true;
            break;
        }
    }

    clock_gettime(CLOCK_MONOTONIC, now_ptr);

    if active_connections
        || (*conn).resolution_v4 == ResolutionStatus::Waiting
        || (*conn).resolution_v6 == ResolutionStatus::Waiting
        || ((*conn).next_connection_time.tv_sec > (*now_ptr).tv_sec
            || ((*conn).next_connection_time.tv_sec == (*now_ptr).tv_sec
                && (*conn).next_connection_time.tv_nsec > (*now_ptr).tv_nsec)) {
            ConnectionError::InProgress
        } else {
            ConnectionError::Failure
        }
}

#[no_mangle]
unsafe extern "C"
fn rexmpp_tcp_conn_fds (s: *mut rexmpp::Rexmpp,
                        conn: *mut RexmppTCPConnection,
                        read_fds: *mut fd_set,
                        write_fds: *mut fd_set) -> c_int {
    let mut max_fd: c_int = 0;
    if (*conn).resolution_v4 == ResolutionStatus::Waiting
        || (*conn).resolution_v6 == ResolutionStatus::Waiting {
            max_fd = rexmpp_dns::rexmpp_dns_fds(s, read_fds, write_fds);
        }
    for i in 0..=(REXMPP_TCP_MAX_CONNECTION_ATTEMPTS - 1) {
        if (*conn).sockets[i] != -1 {
            FD_SET((*conn).sockets[i], write_fds);
            if max_fd < (*conn).sockets[i] + 1 {
                max_fd = (*conn).sockets[i] + 1;
            }
        }
    }
    max_fd
}

#[no_mangle]
unsafe extern "C"
fn rexmpp_tcp_conn_timeout (s: *mut rexmpp::Rexmpp,
                            conn: *mut RexmppTCPConnection,
                            max_tv: *mut timespec,
                            tv: *mut timespec) -> *mut timespec {
    let mut now: timespec = mem::zeroed();
    let mut ret: *mut timespec = max_tv;
    if (*conn).resolution_v4 == ResolutionStatus::Waiting
        || (*conn).resolution_v6 == ResolutionStatus::Waiting {
            ret = rexmpp_dns::rexmpp_dns_timeout(s, max_tv, tv);
        }
    if (*conn).resolution_v4 == ResolutionStatus::Success
        || (*conn).resolution_v6 == ResolutionStatus::Success
        || ((*conn).resolution_v4 == ResolutionStatus::Inactive
            && (*conn).resolution_v4 == ResolutionStatus::Inactive) {
            clock_gettime(CLOCK_MONOTONIC, &mut now);
            if now.tv_sec < (*conn).next_connection_time.tv_sec
                || (now.tv_sec == (*conn).next_connection_time.tv_sec
                    && now.tv_nsec <= (*conn).next_connection_time.tv_nsec) {
                    if ret == null_mut()
                        || (*ret).tv_sec > (*conn).next_connection_time.tv_sec - now.tv_sec
                        || ((*ret).tv_sec == (*conn).next_connection_time.tv_sec - now.tv_sec
                            && (*ret).tv_nsec > (*conn).next_connection_time.tv_nsec - now.tv_sec) {
                            ret = tv;
                            (*tv).tv_sec = (*conn).next_connection_time.tv_sec - now.tv_sec;
                            if (*conn).next_connection_time.tv_nsec > now.tv_nsec {
                                (*tv).tv_nsec = (*conn).next_connection_time.tv_nsec - now.tv_nsec;
                            } else {
                                (*tv).tv_nsec = (*conn).next_connection_time.tv_nsec + 1000000000 - now.tv_nsec;
                                (*tv).tv_sec -= 1;
                            }
                        }
                }
        }
    ret
}