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