summaryrefslogtreecommitdiff
path: root/src/timeoutconn.c
blob: 402e9d34b89626b202e4665bdd7cdbd7091548ed (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
#include "timeoutconn.h"

#include "error.h"
#include "iopause.h"
#include "ip.h"
#include "ndelay.h"
#include "socket_if.h"

/**
  @file timeoutconn.c
  @authors djb, fefe, feh
  @ref qmail
  @brief socket read/write timeout handling; return code of iopause considered
*/

int timeoutconn4(int s, char ip[4], uint16 port, unsigned int timeout)
{
  struct taia now;
  struct taia deadline;
  iopause_fd x;
  unsigned int p = 0;

  if (socket_connect4(s, ip, port) == -1) {
    if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) return -1;
    x.fd = s;
    x.events = IOPAUSE_WRITE;
    taia_now(&now);
    taia_uint(&deadline, timeout);
    taia_add(&deadline, &now, &deadline);
    for (;;) {
      taia_now(&now);
      iopause(&x, 1, &deadline, &now);
      if (x.revents) break;  // 's' available
      if (taia_less(&deadline, &now)) {
        errno = ETIMEDOUT;  // note that connect attempt is continuing
        return -1;
      }
      p++;
    }
    if (!socket_connected(s)) return -1;
  }

  if (ndelay_off(s) == -1) return -1;
  return 0;
}

int timeoutconn6(int s, char ip[16], uint16 port, unsigned int timeout, uint32 netif)
{
  struct taia now;
  struct taia deadline;
  iopause_fd x;
  unsigned int p = 0;

  if (socket_connect6(s, ip, port, netif) == -1) {
    if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) return -1;
    x.fd = s;
    x.events = IOPAUSE_WRITE;
    taia_now(&now);
    taia_uint(&deadline, timeout);
    taia_add(&deadline, &now, &deadline);
    for (;;) {
      taia_now(&now);
      iopause(&x, 1, &deadline, &now);
      if (x.revents) break;  // 's' available
      if (taia_less(&deadline, &now)) {
        errno = ETIMEDOUT;  // note that connect attempt is continuing
        return -1;
      }
      p++;
    }
    if (!socket_connected(s)) return -1;
  }

  if (ndelay_off(s) == -1) return -1;
  return 0;
}

int timeoutconn(int s, char ip[16], uint16 port, unsigned int timeout, uint32 netif)
{
  struct taia now;
  struct taia deadline;
  iopause_fd x;
  unsigned int p = 0;
  int r;

  if (ip6_isv4mapped(ip))
    r = socket_connect4(s, ip + 12, port);
  else
    r = socket_connect6(s, ip, port, netif);

  if (r == -1) {
    if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) return -1;
    x.fd = s;
    x.events = IOPAUSE_WRITE;
    taia_now(&now);
    taia_uint(&deadline, timeout);
    taia_add(&deadline, &now, &deadline);
    for (;;) {
      taia_now(&now);
      iopause(&x, 1, &deadline, &now);
      if (x.revents) break;  // 's' available
      if (taia_less(&deadline, &now)) {
        errno = ETIMEDOUT;  // note that connect attempt is continuing
        return -1;
      }
      p++;
    }
    if (!socket_connected(s)) return -1;
  }

  if (ndelay_off(s) == -1) return -1;
  return 0;
}