ucspi-tcp6 1.13.02
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,int 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,int 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,char iplocal[16],uint16 portlocal,unsigned int timeout,uint32 netif)
58{
59 buffer b;
60 char bspace[128];
61 char strnum[FMT_ULONG];
62 int numcolons;
63 char ch;
64
65 if (ip6_isv4mapped(ipremote)) {
66 if (socket_bind4(s,iplocal,0) == -1) return -1;
67 if (timeoutconn4(s,ipremote,TIMEOUT,timeout) == -1) return -1;
68 } else {
69 if (socket_bind(s,iplocal,0,netif) == -1) return -1;
70 if (timeoutconn(s,ipremote,TIMEOUT,timeout,netif) == -1) return -1;
71 }
72
73
74 buffer_init(&b,mywrite,s,bspace,sizeof(bspace));
75 buffer_put(&b,strnum,fmt_ulong(strnum,portremote));
76 buffer_put(&b," , ",3);
77 buffer_put(&b,strnum,fmt_ulong(strnum,portlocal));
78 buffer_put(&b,"\r\n",2);
79 if (buffer_flush(&b) == -1) return -1;
80
81 buffer_init(&b,myread,s,bspace,sizeof(bspace));
82 numcolons = 0;
83 for (;;) {
84 if (buffer_get(&b,&ch,1) != 1) return -1;
85 if ((ch == ' ') || (ch == '\t') || (ch == '\r')) continue;
86 if (ch == '\n') return 0;
87 if (numcolons < 3) {
88 if (ch == ':') ++numcolons;
89 }
90 else {
91 if (!stralloc_append(out,&ch)) return -1;
92 if (out->len > 256) return 0;
93 }
94 }
95}
96
97int remoteinfo(stralloc *out,char ipremote[16],uint16 portremote,char iplocal[16],uint16 portlocal,unsigned int timeout,uint32 netif)
98{
99 int s;
100 int r;
101
102 if (!stralloc_copys(out,"")) return -1;
103
104 taia_now(&now);
105 taia_uint(&deadline,timeout);
106 taia_add(&deadline,&now,&deadline);
107
108 s = socket_tcp();
109 if (s == -1) return -1;
111 close(s);
112 return r;
113}
int remoteinfo(stralloc *out, char ipremote[16], uint16 portremote, char iplocal[16], uint16 portlocal, unsigned int timeout, uint32 netif)
Definition: remoteinfo.c:97
#define TIMEOUT
Definition: remoteinfo.c:12
ssize_t myread(int fd, char *buf, int len)
Definition: mconnect-io.c:18
buffer out
Definition: rblsmtpd.c:156
unsigned long timeout
Definition: rblsmtpd.c:94
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 b
Definition: auto-str.c:5
char bspace[BUFFER_SMALL]
Definition: auto-str.c:4
int read(int, char *, int)
void doit(int fdleft, int fdright)
Definition: fixcrio.c:32
int close(int)
int write(int, char *, int)