ucspi-tcp6 1.13.06
ucspi-tcp6
Loading...
Searching...
No Matches
remoteinfo.c
Go to the documentation of this file.
1#include <unistd.h>
2#include "fmt.h"
3#include "buffer.h"
4#include "ip.h"
5#include "socket_if.h"
6#include "error.h"
7#include "iopause.h"
8#include "timeoutconn.h"
9#include "dnsresolv.h"
10#include "remoteinfo.h"
11
12#define TIMEOUT 113
13
14static struct taia now;
15static struct taia deadline;
16
17static ssize_t mywrite(int fd,char *buf,size_t len)
18{
19 iopause_fd x;
20 int r;
21
22 x.fd = fd;
23 x.events = IOPAUSE_WRITE;
24 for (;;) {
25 taia_now(&now);
26 r = iopause(&x,1,&deadline,&now);
27 if (r <= 0) break;
28 if (x.revents) break;
29 if (taia_less(&deadline,&now)) {
30 errno = ETIMEDOUT;
31 return -1;
32 }
33 }
34 return write(fd,buf,len);
35}
36
37static ssize_t myread(int fd,char *buf,size_t len)
38{
39 iopause_fd x;
40 int r;
41
42 x.fd = fd;
43 x.events = IOPAUSE_READ;
44 for (;;) {
45 taia_now(&now);
46 r = iopause(&x,1,&deadline,&now);
47 if (r <= 0) break;
48 if (x.revents) break;
49 if (taia_less(&deadline,&now)) {
50 errno = ETIMEDOUT;
51 return -1;
52 }
53 }
54 return read(fd,buf,len);
55}
56
57static int doit(stralloc *out,int s,char ipremote[16],uint16 portremote,
58 char iplocal[16],uint16 portlocal,unsigned int timeout,uint32 netif)
59{
60 buffer b;
61 char bspace[128];
62 char strnum[FMT_ULONG];
63 int numcolons;
64 char ch;
65
66 if (ip6_isv4mapped(ipremote)) {
67 if (socket_bind4(s,iplocal,0) == -1) return -1;
68 if (timeoutconn4(s,ipremote,TIMEOUT,timeout) == -1) return -1;
69 } else {
70 if (socket_bind(s,iplocal,0,netif) == -1) return -1;
71 if (timeoutconn(s,ipremote,TIMEOUT,timeout,netif) == -1) return -1;
72 }
73
74
75 buffer_init(&b,mywrite,s,bspace,sizeof(bspace));
76 buffer_put(&b,strnum,fmt_ulong(strnum,portremote));
77 buffer_put(&b," , ",3);
78 buffer_put(&b,strnum,fmt_ulong(strnum,portlocal));
79 buffer_put(&b,"\r\n",2);
80 if (buffer_flush(&b) == -1) return -1;
81
82 buffer_init(&b,myread,s,bspace,sizeof(bspace));
83 numcolons = 0;
84 for (;;) {
85 if (buffer_get(&b,&ch,1) != 1) return -1;
86 if ((ch == ' ') || (ch == '\t') || (ch == '\r')) continue;
87 if (ch == '\n') return 0;
88 if (numcolons < 3) {
89 if (ch == ':') ++numcolons;
90 }
91 else {
92 if (!stralloc_append(out,&ch)) return -1;
93 if (out->len > 256) return 0;
94 }
95 }
96}
97
98int remoteinfo(stralloc *out,char ipremote[16],uint16 portremote,
99 char iplocal[16],uint16 portlocal,unsigned int timeout,uint32 netif)
100{
101 int s;
102 int r;
103
104 if (!stralloc_copys(out,"")) return -1;
105
106 taia_now(&now);
107 taia_uint(&deadline,timeout);
108 taia_add(&deadline,&now,&deadline);
109
110 s = socket_tcp();
111 if (s == -1) return -1;
113 close(s);
114 return r;
115}
buffer b
Definition auto-str.c:5
char bspace[BUFFER_SMALL]
Definition auto-str.c:4
uint16 portremote
Definition tcpclient.c:58
uint16 portlocal
Definition tcpclient.c:54
char iplocal[16]
Definition tcpclient.c:53
char ipremote[16]
Definition tcpclient.c:57
uint32 netif
Definition tcpclient.c:51
buffer out
Definition rblsmtpd.c:158
unsigned long timeout
Definition rblsmtpd.c:94
int remoteinfo(stralloc *out, char ipremote[16], uint16 portremote, char iplocal[16], uint16 portlocal, unsigned int timeout, uint32 netif)
Definition remoteinfo.c:98
#define TIMEOUT
Definition remoteinfo.c:12
ssize_t myread(int fd, char *buf, size_t len)
Definition mconnect-io.c:18
ssize_t write(int, char *, size_t)
ssize_t read(int, char *, size_t)
void doit(int fdleft, int fdright)
Definition fixcrio.c:32
int close(int)