tcp-env.c (3031B)
1 #include <sys/types.h> 2 #include <sys/socket.h> 3 #include <sys/param.h> 4 #include <netinet/in.h> 5 #include "noreturn.h" 6 #include "sig.h" 7 #include "stralloc.h" 8 #include "str.h" 9 #include "env.h" 10 #include "fmt.h" 11 #include "scan.h" 12 #include "subgetopt.h" 13 #include "ip.h" 14 #include "dns.h" 15 #include "byte.h" 16 #include "remoteinfo.h" 17 #include "exit.h" 18 #include "case.h" 19 20 void _noreturn_ die() { _exit(111); } 21 22 struct sockaddr_in salocal; 23 unsigned long localport; 24 struct ip_address iplocal; 25 stralloc localname = {0}; 26 27 struct sockaddr_in saremote; 28 unsigned long remoteport; 29 struct ip_address ipremote; 30 stralloc remotename = {0}; 31 32 char temp[IPFMT + FMT_ULONG]; 33 34 int main(int argc, char **argv) 35 { 36 unsigned int dummy; 37 char *proto; 38 int opt; 39 int flagremoteinfo; 40 unsigned long timeout; 41 42 sig_pipeignore(); 43 44 flagremoteinfo = 1; 45 timeout = 30; 46 while ((opt = sgopt(argc,argv,"rRt:")) != sgoptdone) 47 switch(opt) 48 { 49 case 'r': flagremoteinfo = 1; break; 50 case 'R': flagremoteinfo = 0; break; 51 case 't': scan_ulong(sgoptarg,&timeout); break; 52 } 53 54 argv += sgoptind; 55 argc -= sgoptind; 56 57 if (argc < 1) die(); 58 if (!env_init()) die(); 59 60 proto = env_get("PROTO"); 61 if (!proto || str_diff(proto,"TCP")) 62 { 63 if (!env_put("PROTO=TCP")) die(); 64 65 dummy = sizeof(salocal); 66 if (getsockname(0,(struct sockaddr *) &salocal,&dummy) == -1) die(); 67 68 localport = ntohs(salocal.sin_port); 69 temp[fmt_ulong(temp,localport)] = 0; 70 if (!env_put2("TCPLOCALPORT",temp)) die(); 71 72 byte_copy(&iplocal,4,&salocal.sin_addr); 73 temp[ip_fmt(temp,&iplocal)] = 0; 74 if (!env_put2("TCPLOCALIP",temp)) die(); 75 76 switch(dns_ptr(&localname,&iplocal)) 77 { 78 case DNS_MEM: die(); 79 case DNS_SOFT: 80 if (!stralloc_copys(&localname,"softdnserror")) die(); 81 case 0: 82 if (!stralloc_0(&localname)) die(); 83 case_lowers(localname.s); 84 if (!env_put2("TCPLOCALHOST",localname.s)) die(); 85 break; 86 default: 87 if (!env_unset("TCPLOCALHOST")) die(); 88 } 89 90 dummy = sizeof(saremote); 91 if (getpeername(0,(struct sockaddr *) &saremote,&dummy) == -1) die(); 92 93 remoteport = ntohs(saremote.sin_port); 94 temp[fmt_ulong(temp,remoteport)] = 0; 95 if (!env_put2("TCPREMOTEPORT",temp)) die(); 96 97 byte_copy(&ipremote,4,&saremote.sin_addr); 98 temp[ip_fmt(temp,&ipremote)] = 0; 99 if (!env_put2("TCPREMOTEIP",temp)) die(); 100 101 switch(dns_ptr(&remotename,&ipremote)) 102 { 103 case DNS_MEM: die(); 104 case DNS_SOFT: 105 if (!stralloc_copys(&remotename,"softdnserror")) die(); 106 case 0: 107 if (!stralloc_0(&remotename)) die(); 108 case_lowers(remotename.s); 109 if (!env_put2("TCPREMOTEHOST",remotename.s)) die(); 110 break; 111 default: 112 if (!env_unset("TCPREMOTEHOST")) die(); 113 } 114 115 if (!env_unset("TCPREMOTEINFO")) die(); 116 if (flagremoteinfo) 117 { 118 char *rinfo; 119 rinfo = remoteinfo_get(&ipremote,remoteport,&iplocal,localport,(int) timeout); 120 if (rinfo) 121 if (!env_put2("TCPREMOTEINFO",rinfo)) die(); 122 } 123 } 124 125 sig_pipedefault(); 126 execvp(*argv,argv); 127 die(); 128 }