2 #include <sys/socket.h>
4 #include <netinet/in.h>
11 #include "subgetopt.h"
15 #include "remoteinfo.h"
19 void die() { _exit(111); }
21 struct sockaddr_in salocal
;
22 unsigned long localport
;
23 struct ip_address iplocal
;
24 stralloc localname
= {0};
26 struct sockaddr_in saremote
;
27 unsigned long remoteport
;
28 struct ip_address ipremote
;
29 stralloc remotename
= {0};
31 char temp
[IPFMT
+ FMT_ULONG
];
41 unsigned long timeout
;
47 while ((opt
= sgopt(argc
,argv
,"rRt:")) != sgoptdone
)
50 case 'r': flagremoteinfo
= 1; break;
51 case 'R': flagremoteinfo
= 0; break;
52 case 't': scan_ulong(sgoptarg
,&timeout
); break;
59 if (!env_init()) die();
61 proto
= env_get("PROTO");
62 if (!proto
|| str_diff(proto
,"TCP"))
64 if (!env_put("PROTO=TCP")) die();
66 dummy
= sizeof(salocal
);
67 if (getsockname(0,(struct sockaddr
*) &salocal
,&dummy
) == -1) die();
69 localport
= ntohs(salocal
.sin_port
);
70 temp
[fmt_ulong(temp
,localport
)] = 0;
71 if (!env_put2("TCPLOCALPORT",temp
)) die();
73 byte_copy(&iplocal
,4,&salocal
.sin_addr
);
74 temp
[ip_fmt(temp
,&iplocal
)] = 0;
75 if (!env_put2("TCPLOCALIP",temp
)) die();
77 switch(dns_ptr(&localname
,&iplocal
))
81 if (!stralloc_copys(&localname
,"softdnserror")) die();
83 if (!stralloc_0(&localname
)) die();
84 case_lowers(localname
.s
);
85 if (!env_put2("TCPLOCALHOST",localname
.s
)) die();
88 if (!env_unset("TCPLOCALHOST")) die();
91 dummy
= sizeof(saremote
);
92 if (getpeername(0,(struct sockaddr
*) &saremote
,&dummy
) == -1) die();
94 remoteport
= ntohs(saremote
.sin_port
);
95 temp
[fmt_ulong(temp
,remoteport
)] = 0;
96 if (!env_put2("TCPREMOTEPORT",temp
)) die();
98 byte_copy(&ipremote
,4,&saremote
.sin_addr
);
99 temp
[ip_fmt(temp
,&ipremote
)] = 0;
100 if (!env_put2("TCPREMOTEIP",temp
)) die();
102 switch(dns_ptr(&remotename
,&ipremote
))
106 if (!stralloc_copys(&remotename
,"softdnserror")) die();
108 if (!stralloc_0(&remotename
)) die();
109 case_lowers(remotename
.s
);
110 if (!env_put2("TCPREMOTEHOST",remotename
.s
)) die();
113 if (!env_unset("TCPREMOTEHOST")) die();
116 if (!env_unset("TCPREMOTEINFO")) die();
120 rinfo
= remoteinfo_get(&ipremote
,remoteport
,&iplocal
,localport
,(int) timeout
);
122 if (!env_put2("TCPREMOTEINFO",rinfo
)) die();