fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
timeoutconn.c
Go to the documentation of this file.
1#include "ndelay.h"
2#include "socket_if.h"
3#include "iopause.h"
4#include "error.h"
5#include "timeoutconn.h"
6#include "ip.h"
7
15int timeoutconn4(int s,char ip[4],uint16 port,unsigned int timeout)
16{
17 struct taia now;
18 struct taia deadline;
19 iopause_fd x;
20 unsigned int p = 0;
21
22 if (socket_connect4(s,ip,port) == -1) {
23 if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) return -1;
24 x.fd = s;
25 x.events = IOPAUSE_WRITE;
26 taia_now(&now);
27 taia_uint(&deadline,timeout);
28 taia_add(&deadline,&now,&deadline);
29 for (;;) {
30 taia_now(&now);
31 iopause(&x,1,&deadline,&now);
32 if (x.revents) break; /* 's' available */
33 if (taia_less(&deadline,&now)) {
34 errno = ETIMEDOUT; /* note that connect attempt is continuing */
35 return -1;
36 }
37 p++;
38 }
39 if (!socket_connected(s)) return -1;
40 }
41
42 if (ndelay_off(s) == -1) return -1;
43 return 0;
44}
45
46int timeoutconn6(int s,char ip[16],uint16 port,unsigned int timeout,uint32 netif)
47{
48 struct taia now;
49 struct taia deadline;
50 iopause_fd x;
51 unsigned int p = 0;
52
53 if (socket_connect6(s,ip,port,netif) == -1) {
54 if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) return -1;
55 x.fd = s;
56 x.events = IOPAUSE_WRITE;
57 taia_now(&now);
58 taia_uint(&deadline,timeout);
59 taia_add(&deadline,&now,&deadline);
60 for (;;) {
61 taia_now(&now);
62 iopause(&x,1,&deadline,&now);
63 if (x.revents) break; /* 's' available */
64 if (taia_less(&deadline,&now)) {
65 errno = ETIMEDOUT; /* note that connect attempt is continuing */
66 return -1;
67 }
68 p++;
69 }
70 if (!socket_connected(s)) return -1;
71 }
72
73 if (ndelay_off(s) == -1) return -1;
74 return 0;
75}
76
77int timeoutconn(int s,char ip[16],uint16 port,unsigned int timeout,uint32 netif)
78{
79 struct taia now;
80 struct taia deadline;
81 iopause_fd x;
82 unsigned int p = 0;
83 int r;
84
85 if (ip6_isv4mapped(ip))
86 r = socket_connect4(s,ip + 12,port);
87 else
88 r = socket_connect6(s,ip,port,netif);
89
90 if (r == -1) {
91 if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) return -1;
92 x.fd = s;
93 x.events = IOPAUSE_WRITE;
94 taia_now(&now);
95 taia_uint(&deadline,timeout);
96 taia_add(&deadline,&now,&deadline);
97 for (;;) {
98 taia_now(&now);
99 iopause(&x,1,&deadline,&now);
100 if (x.revents) break; /* 's' available */
101 if (taia_less(&deadline,&now)) {
102 errno = ETIMEDOUT; /* note that connect attempt is continuing */
103 return -1;
104 }
105 p++;
106 }
107 if (!socket_connected(s)) return -1;
108 }
109
110 if (ndelay_off(s) == -1) return -1;
111 return 0;
112}
int timeoutconn(int s, char ip[16], uint16 port, unsigned int timeout, uint32 netif)
Definition: timeoutconn.c:77
int timeoutconn4(int s, char ip[4], uint16 port, unsigned int timeout)
Definition: timeoutconn.c:15
int timeoutconn6(int s, char ip[16], uint16 port, unsigned int timeout, uint32 netif)
Definition: timeoutconn.c:46
int ndelay_off(int)
Definition: ndelay.c:21
#define IOPAUSE_WRITE
Definition: iopause.h:20
int iopause(iopause_fd *, unsigned int, struct taia *, struct taia *)
Definition: iopause.c:14
uint16_t uint16
Definition: uint_t.h:29
uint32_t uint32
Definition: uint_t.h:40
#define ip6_isv4mapped(ip)
Definition: ip.h:105
int socket_connect6(int, const char[16], uint16, uint32)
int socket_connected(int)
int socket_connect4(int, const char[4], uint16)
int taia_less(struct taia *, struct taia *)
Definition: taia.c:39
void taia_add(struct taia *, struct taia *, struct taia *)
Definition: taia.c:14
void taia_uint(struct taia *, unsigned int)
Definition: taia.c:99
int taia_now(struct taia *)
Definition: taia.c:48
int fd
Definition: iopause.h:15
Definition: taia.h:13