• lcx HTran 端口转发工具 c++ > delphi


      1 (*
      2   ************************************************************************************
      3   *
      4   * HTran.cpp - HUC Packet Transmit Tool.
      5   *
      6   * Copyright (C) 2000-2004 HUC All Rights Reserved.
      7   *
      8   * Author   : lion
      9   *       : lion#cnhonker.net
     10   *       : [url]http://www.cnhonker.com[/url]
     11   *       :
     12   * Notice: Thx to bkbll (bkbll#cnhonker.net)
     13   *       :
     14   * Date  : 2003-10-20
     15   *       :
     16   * Complie : cl HTran.cpp
     17   *       :
     18   * Usage : E:\>HTran
     19   *       : ======================== HUC Packet Transmit Tool V1.00 =======================
     20   *       : =========== Code by lion & bkbll, Welcome to http://www.cnhonker.com ==========
     21   *       :
     22   *       : [Usage of Packet Transmit:]
     23   *       :   HTran -<listen|tran|slave> <option> [-log logfile]
     24   *       :
     25   *       : [option:]
     26   *       :   -listen <ConnectPort> <TransmitPort>
     27   *       :   -tran   <ConnectPort> <TransmitHost> <TransmitPort>
     28   *       :   -slave <ConnectHost> <ConnectPort> <TransmitHost> <TransmitPort>
     29   *
     30   ************************************************************************************
     31   * Pascal version by adsj 2013.5.4
     32   ************************************************************************************
     33 *)
     34 unit LCXUnit;
     35 
     36 interface
     37 
     38 uses
     39   System.SysUtils, codesitelogging,
     40   Winapi.Windows,
     41   Winapi.Winsock2;
     42 
     43 const
     44   VERSION = '0.01';
     45   TIMEOUT = 300;
     46   MAXSIZE = 20480;
     47   HOSTLEN = 40;
     48   CONNECTNUM = 5;
     49   // ERROR CODE
     50   EINTR = 4;
     51   ENOSPC = 28;
     52 
     53 type
     54   pTransock = ^transock;
     55 
     56   transock = record
     57     fd1: TSocket;
     58     fd2: TSocket;
     59   end;
     60 
     61   //
     62 procedure ver();
     63 
     64 procedure usage(prog: PAnsiChar);
     65 
     66 procedure transmitdata(data: Pointer); stdcall;
     67 
     68 procedure getctrlc(j: Integer);
     69 
     70 procedure closeallfd();
     71 
     72 procedure makelog(buffer: PAnsiChar; bflength: Integer);
     73 
     74 procedure proxy(port: Integer);
     75 
     76 procedure bind2bind(port1, port2: Integer);
     77 
     78 procedure bind2conn(port1: Integer; host: PAnsiChar; port2: Integer);
     79 
     80 procedure conn2conn(host1: PAnsiChar; port1: Integer; host2: PAnsiChar;
     81   port2: Integer);
     82 
     83 function testfisvalue(str: PAnsiChar): Integer;
     84 
     85 function create_socket(): Integer;
     86 
     87 function create_server(sockfd: Integer; port: Integer): Integer;
     88 
     89 function client_connect(sockfd: Integer; server: PAnsiChar;
     90   port: Integer): Integer;
     91 
     92 //
     93 procedure __Main();
     94 
     95 var
     96   error: Integer;
     97   method: Integer;
     98   connects: Integer;
     99 
    100 implementation
    101 
    102 procedure ver();
    103 begin
    104   Writeln(Format
    105     ('==================================my lcx %s===================================',
    106     [VERSION]));
    107   Writeln('=========== Code by lion & bkbll, Welcome to http://www.cnhonker.com ===========');
    108 end;
    109 
    110 procedure usage(prog: PAnsiChar);
    111 begin
    112   // print some sth about this app
    113   Writeln('[Usage of Packet Transmit:]');
    114   Writeln(Format(' %s -<listen|tran|slave> <option> [-log logfile]', [prog]));
    115   Writeln('[option:]');
    116   Writeln(' -listen <ConnectPort> <TransmitPort>');
    117   Writeln(' -tran   <ConnectPort> <TransmitHost> <TransmitPort>');
    118   Writeln(' -slave <ConnectHost> <ConnectPort> <TransmitHost> <TransmitPort>');
    119 end;
    120 
    121 procedure transmitdata(data: Pointer); stdcall;
    122 var
    123   fd1, fd2: TSocket;
    124   psock: pTransock;
    125   timeset: timeval;
    126   readfd, writefd: fd_set;
    127   ret, i: Integer;
    128   read_in1, send_out1: array [0 .. MAXSIZE - 1] of AnsiChar;
    129   read_in2, send_out2: array [0 .. MAXSIZE - 1] of AnsiChar;
    130   read1, totalread1, send1: Integer;
    131   read2, totalread2, send2: Integer;
    132   sendcount1, sendcount2: Integer;
    133   maxfd: Integer;
    134   client1, client2: sockaddr_in;
    135   structsize1, structsize2: Integer;
    136   host1, host2: array [0 .. 19] of AnsiChar;
    137   port1, port2: Integer;
    138   tmpbuf: array [0 .. 99] of AnsiChar;
    139   tbfstr: AnsiString;
    140   //
    141   err, err2: Integer;
    142 begin
    143   psock := pTransock(data);
    144   fd1 := psock^.fd1;
    145   fd2 := psock^.fd2;
    146 
    147   FillChar(host1, 20, 0);
    148   FillChar(host2, 20, 0);
    149   FillChar(tmpbuf, 100, 0);
    150 
    151   structsize1 := SizeOf(sockaddr);
    152   structsize2 := SizeOf(sockaddr);
    153 
    154   if getpeername(fd1, sockaddr(client1), structsize1) < 0 then
    155   begin
    156     StrCopy(@host1[0], 'fd1');
    157   end
    158   else
    159   begin
    160     StrCopy(@host1[0], inet_ntoa(client1.sin_addr));
    161     port1 := ntohs(client1.sin_port);
    162   end;
    163 
    164   if getpeername(fd2, sockaddr(client2), structsize2) < 0 then
    165   begin
    166     StrCopy(@host2[0], 'fd2');
    167   end
    168   else
    169   begin
    170     StrCopy(@host2, inet_ntoa(client2.sin_addr));
    171     port2 := ntohs(client2.sin_port);
    172   end;
    173 
    174   // printf start transmit host1:port1 <-> host2:port2
    175   Writeln(Format('[+] Start Transmit (%s:%d <-> %s:%d) ......',
    176     [host1, port1, host2, port2]));
    177 
    178   if fd1 > fd2 then
    179     maxfd := fd1 + 1
    180   else
    181     maxfd := fd2 + 1;
    182 
    183   FillChar(read_in1, MAXSIZE, 0);
    184   FillChar(read_in2, MAXSIZE, 0);
    185   FillChar(send_out1, MAXSIZE, 0);
    186   FillChar(send_out2, MAXSIZE, 0);
    187 
    188   timeset.tv_sec := TIMEOUT;
    189   timeset.tv_usec := 0;
    190 
    191   while True do
    192   begin
    193     FD_ZERO(readfd);
    194     FD_ZERO(writefd);
    195 
    196     _FD_SET(fd1, readfd);
    197     _FD_SET(fd1, writefd);
    198     _FD_SET(fd2, writefd);
    199     _FD_SET(fd2, readfd);
    200 
    201     ret := select(maxfd, @readfd, @writefd, nil, @timeset);
    202     if (ret < 0) and (h_errno <> WSAEINTR) then
    203     begin
    204       // printf select error
    205       Writeln('[-] Select error.');
    206       Break;
    207     end
    208     else if ret = 0 then
    209     begin
    210       // printf socket time out
    211       Writeln('[-] Socket time out.');
    212       Break;
    213     end;
    214 
    215     if FD_ISSET(fd1, readfd) then
    216     begin
    217       //
    218       if totalread1 < MAXSIZE then
    219       begin
    220         read1 := recv(fd1, read_in1, MAXSIZE - totalread1, 0);
    221         if (read1 = SOCKET_ERROR) or (read1 = 0) then
    222         begin
    223           // printf read fd1 data error,maybe close?
    224           Writeln('[-] Read fd1 data error,maybe close?');
    225           Break;
    226         end;
    227 
    228         CopyMemory(@send_out1[totalread1], @read_in1[0], read1);
    229         // sprintf(tmpbuf,"\r\nRecv %5d bytes from %s:%d\r\n", read1, host1, port1);
    230         tbfstr := Format(' Recv %5d bytes from %s:%d', [read1, host1, port1]);
    231         StrCopy(@tmpbuf, PAnsiChar(tbfstr));
    232         // recv read1 bytes from host1:port1
    233         Writeln(Format(' Recv %5d bytes %16s:%d', [read1, host1, port1]));
    234         totalread1 := totalread1 + read1;
    235         FillChar(read_in1, MAXSIZE, 0);
    236       end;
    237     end;
    238 
    239     if FD_ISSET(fd2, writefd) then
    240     begin
    241       err := 0;
    242       sendcount1 := 0;
    243       while totalread1 > 0 do
    244       begin
    245         send1 := send(fd2, send_out1, totalread1, 0);
    246         if send1 = 0 then
    247           Break;
    248         if (send1 < 0) and (h_errno <> EINTR) then
    249         begin
    250           // printf send to fd2 unknow error
    251           Writeln('[-] Send to fd2 unknow error.');
    252           err := 1;
    253           Break;
    254         end;
    255 
    256         if (send1 < 0) and (h_errno = ENOSPC) then
    257           Break;
    258 
    259         sendcount1 := sendcount1 + send1;
    260         totalread1 := totalread1 - send1;
    261 
    262         // printf send send1 bytes  host2 : port2
    263         Writeln(Format(' Send %5d bytes %16s:%d', [send1, host2, port2]));
    264       end;
    265 
    266       if err = 1 then
    267         Break;
    268 
    269       if (totalread1 > 0) and (sendcount1 > 0) then
    270       begin
    271         // move not sended data to start addr
    272         CopyMemory(@send_out1, @send_out1[sendcount1], totalread1);
    273         FillChar(send_out1[totalread1], MAXSIZE - totalread1, 0);
    274       end
    275       else
    276         FillChar(send_out1, MAXSIZE, 0);
    277     end;
    278 
    279     if FD_ISSET(fd2, readfd) then
    280     begin
    281       if totalread2 < MAXSIZE then
    282       begin
    283         read2 := recv(fd2, read_in2, MAXSIZE - totalread2, 0);
    284         if read2 = 0 then
    285           Break;
    286 
    287         if (read2 < 0) and (h_errno <> EINTR) then
    288         begin
    289           // Read fd2 data error,maybe close?
    290           Writeln('[-] Read fd2 data error,maybe close?');
    291           Break;
    292         end;
    293         CopyMemory(@send_out2[totalread2], @read_in2, read2);
    294 
    295         // Recv read2 bytes host2:port2
    296         tbfstr := Format('Recv %5d bytes from %s:%d', [read2, host2, port2]);
    297         StrCopy(@tmpbuf, PAnsiChar(tbfstr));
    298         Writeln(Format(' Recv %5d bytes %16s:%d', [read2, host2, port2]));
    299         // log
    300         //
    301         totalread2 := totalread2 + read2;
    302         FillChar(read_in2, MAXSIZE, 0);
    303       end;
    304     end;
    305 
    306     if FD_ISSET(fd1, writefd) then
    307     begin
    308       err2 := 0;
    309       sendcount2 := 0;
    310       while totalread2 > 0 do
    311       begin
    312         send2 := send(fd1, send_out2[sendcount2], totalread2, 0);
    313         if send2 = 0 then
    314           Break;
    315         if (send2 < 0) and (h_errno <> EINTR) then
    316         begin
    317           // send to fd1 unknow error.
    318           Writeln('[-] Send to fd1 unknow error.');
    319           err2 := 1;
    320           Break;
    321         end;
    322         if (send2 < 0) and (h_errno = ENOSPC) then
    323           Break;
    324         sendcount2 := sendcount2 + send2;
    325         totalread2 := totalread2 - send2;
    326         // Send send2 bytes host1:port1
    327         Writeln(Format(' Send %5d bytes %16s:%d', [send2, host1, port1]));
    328       end;
    329 
    330       if err2 = 1 then
    331         Break;
    332       if (totalread2 > 0) and (sendcount2 > 0) then
    333       begin
    334         CopyMemory(@send_out2, @send_out2[sendcount2], totalread2);
    335         FillChar(send_out2[totalread2], MAXSIZE - totalread2, 0);
    336       end
    337       else
    338         FillChar(send_out2, MAXSIZE, 0);
    339     end;
    340     Sleep(5);
    341   end;
    342 
    343   closesocket(fd1);
    344   closesocket(fd2);
    345   //
    346   // ok i closed the two socket.
    347   Writeln('[+] OK! I Closed The Two Socket.');
    348 end;
    349 
    350 procedure getctrlc(j: Integer);
    351 begin
    352   // received  ctrl + c
    353   Writeln('[-] Received Ctrl+C');
    354   closeallfd();
    355   Exit;
    356 end;
    357 
    358 procedure closeallfd();
    359 var
    360   i: Integer;
    361 begin
    362   // let me exit......
    363   Writeln('[+] Let me exit ......');
    364   // fflush(stdout)
    365   for i := 3 to 255 do
    366     closesocket(i);
    367 
    368   // if fp<> nil then
    369   // begin
    370   // print exit
    371   // fclose(fp)
    372   // end;
    373   // All Right
    374   Writeln('[+] All Right!');
    375 end;
    376 
    377 procedure makelog(buffer: PAnsiChar; bflength: Integer);
    378 begin
    379 
    380 end;
    381 
    382 procedure proxy(port: Integer);
    383 begin
    384 
    385 end;
    386 
    387 procedure bind2bind(port1, port2: Integer);
    388 var
    389   fd1, fd2, sockfd1, sockfd2: TSocket;
    390   client1, client2: sockaddr_in;
    391   size1, size2: Integer;
    392 
    393   hThread: THandle;
    394   sock: transock;
    395   dwThreadID: DWORD;
    396 begin
    397   fd1 := create_socket();
    398   if (fd1) = 0 then
    399     Exit;
    400   fd2 := create_socket();
    401   if (fd2) = 0 then
    402     Exit;
    403   // printf listening  port1
    404   Writeln(Format('[+] Listen port %d!', [port1]));
    405   if create_server(fd1, port1) = 0 then
    406   begin
    407     closesocket(fd1);
    408     Exit;
    409   end;
    410 
    411   // listen ok
    412   Writeln('[+] Listen OK!');
    413   // printf listening port2
    414   Writeln(Format('[+] Listening port %d ......', [port2]));
    415 
    416   if create_server(fd2, port2) = 0 then
    417   begin
    418     closesocket(fd2);
    419     Exit;
    420   end;
    421 
    422   // listen ok
    423   Writeln('[+] Listen OK!');
    424 
    425   size1 := SizeOf(sockaddr);
    426   size2 := SizeOf(sockaddr);
    427 
    428   while True do
    429   begin
    430     // waiting for Client on port 1
    431     Writeln(Format('[+] Waiting for Client on port:%d ......', [port1]));
    432 
    433     sockfd1 := accept(fd1, @client1, @size1);
    434     if (sockfd1) < 0 then
    435     begin
    436       // accept error
    437       Writeln('[-] Accept1 error.');
    438       Continue;
    439     end;
    440     // printf accept a Client on port1
    441     Writeln(Format('[+] Accept a Client on port %d from %s ......',
    442       [port1, inet_ntoa(client1.sin_addr)]));
    443     // waiting another Client on port2
    444     Writeln(Format('[+] Waiting another Client on port:%d....', [port2]));
    445 
    446     sockfd2 := accept(fd2, @client2, @size2);
    447     if (sockfd2) < 0 then
    448     begin
    449       // accept2 error
    450       Writeln('[-] Accept2 error.');
    451       closesocket(sockfd1);
    452       Continue;
    453     end;
    454     // printf accept a Client on port2 ..
    455     Writeln(Format('[+] Accept a Client on port %d from %s',
    456       [port2, inet_ntoa(client2.sin_addr)]));
    457     // accept connect ok
    458     Writeln('[+] Accept Connect OK!');
    459 
    460     sock.fd1 := sockfd1;
    461     sock.fd2 := sockfd2;
    462 
    463     hThread := CreateThread(nil, 0, @transmitdata, @sock, 0, dwThreadID);
    464     if hThread <= 0 then
    465     begin
    466       TerminateThread(hThread, 0);
    467       Exit;
    468     end;
    469     Sleep(1000);
    470     // printf CreateThread OK
    471     Writeln('[+] CreateThread OK!');
    472   end;
    473 end;
    474 
    475 procedure bind2conn(port1: Integer; host: PAnsiChar; port2: Integer);
    476 var
    477   sockfd, sockfd1, sockfd2: TSocket;
    478   remote: sockaddr_in;
    479   size: Integer;
    480   buffer: array [0 .. 1023] of AnsiChar;
    481   aStr: AnsiString;
    482   hThread: THandle;
    483   sock: transock;
    484   dwThreadID: DWORD;
    485 begin
    486   if (port1 < 1) or (port1 > 65535) then
    487   begin
    488     // ConnectPort invalid.
    489     Writeln('[-] ConnectPort invalid.');
    490     Exit;
    491   end;
    492   if (port2 < 1) or (port2 > 65535) then
    493   begin
    494     // TransmitPort invalid.
    495     Writeln('[-] TransmitPort invalid.');
    496     Exit;
    497   end;
    498 
    499   FillChar(buffer, 1024, 0);
    500 
    501   sockfd := create_socket();
    502   if sockfd = INVALID_SOCKET then
    503     Exit;
    504 
    505   if (create_server(sockfd, port1)) = 0 then
    506   begin
    507     closesocket(sockfd);
    508     Exit;
    509   end;
    510 
    511   size := SizeOf(sockaddr);
    512 
    513   while True do
    514   begin
    515     // Waiting for Client.....
    516     Writeln('[+] Waiting for Client.....');
    517     sockfd1 := accept(sockfd, @remote, @size);
    518     if sockfd1 < 0 then
    519     begin
    520       // Accept error.
    521       Writeln('[-] Accept error.');
    522       Continue;
    523     end;
    524 
    525     // Accept a Client form  inet_ntoa( remote.sin_addr ) : ntohs( remote.sin_port)
    526     Writeln(Format('[+] Accept a Client from %s:%d ......',
    527       [inet_ntoa(remote.sin_addr), ntohs(remote.sin_port)]));
    528 
    529     sockfd2 := create_socket();
    530     if sockfd2 = 0 then
    531     begin
    532       closesocket(sockfd1);
    533       Continue;
    534     end;
    535 
    536     // make a Connection to host : port
    537     // fflush(stdout)
    538     Writeln(Format('[+] Make a Connection to %s:%d ......', [host, port2]));
    539 
    540     if client_connect(sockfd2, host, port2) = 0 then
    541     begin
    542       closesocket(sockfd2);
    543       // sprintf(buffer,'[Server]connection to host:port2')
    544       aStr := Format('[SERVER]connection to %s:%d error', [host, port2]);
    545       StrCopy(@buffer, PAnsiChar(aStr));
    546       send(sockfd1, buffer, StrLen(buffer), 0);
    547       FillChar(buffer, 1024, 0);
    548       closesocket(sockfd1);
    549       Continue;
    550     end;
    551     // printf Connect OK
    552     Writeln('[+] Connect OK!');
    553     sock.fd1 := sockfd1;
    554     sock.fd2 := sockfd2;
    555 
    556     hThread := CreateThread(nil, 0, @transmitdata, @sock, 0, dwThreadID);
    557     if hThread = 0 then
    558     begin
    559       TerminateThread(hThread, 0);
    560       Exit;
    561     end;
    562     Sleep(1000);
    563     // printf CreateThread OK!
    564     Writeln('[+] CreateThread OK!');
    565   end;
    566 
    567 end;
    568 
    569 procedure conn2conn(host1: PAnsiChar; port1: Integer; host2: PAnsiChar;
    570   port2: Integer);
    571 var
    572   sockfd1, sockfd2: TSocket;
    573   hThread: THandle;
    574   sock: transock;
    575   dwThreadID: DWORD;
    576   fds: fd_set;
    577   l: Integer;
    578   buffer: array [0 .. MAXSIZE - 1] of AnsiChar;
    579 begin
    580   while True do
    581   begin
    582     //
    583 
    584     sockfd1 := create_socket();
    585     if sockfd1 = 0 then
    586       Exit;
    587     sockfd2 := create_socket();
    588     if sockfd2 = 0 then
    589       Exit;
    590 
    591     // make a connection to host1:port1
    592     Writeln(Format('[+] Make a Connection to %s:%d....', [host1, port1]));
    593     // ffliush(stdout)
    594     if client_connect(sockfd1, host1, port1) = 0 then
    595     begin
    596       closesocket(sockfd1);
    597       closesocket(sockfd2);
    598       Continue;
    599     end;
    600     // fix by bkbll
    601     // if host1:port1 recved data, then connect to host2:port2
    602     l := 0;
    603     FillChar(buffer, MAXSIZE, 0);
    604     while True do
    605     begin
    606       FD_ZERO(fds);
    607       _FD_SET(sockfd1, fds);
    608 
    609       if select(sockfd1, @fds, nil, nil, nil) = SOCKET_ERROR then
    610       begin
    611         if h_errno = WSAEINTR then
    612           Continue;
    613         Break;
    614       end;
    615 
    616       if FD_ISSET(sockfd1, fds) then
    617       begin
    618         l := recv(sockfd1, buffer, MAXSIZE, 0);
    619         Break;
    620       end;
    621       Sleep(5);
    622     end;
    623 
    624     if (l <= 0) then
    625     begin
    626       // there is a error...Create a new connection.
    627       Writeln('[-] There is a error...Create a new connection.');
    628       Continue;
    629     end;
    630 
    631     while True do
    632     begin
    633       // connect ok!
    634       Writeln('[+] Connect OK!');
    635       // make a connection to host2:port2
    636       Writeln(Format('[+] Make a Connection to %s:%d....', [host2, port2]));
    637       // fflush(stdout)
    638 
    639       if client_connect(sockfd2, host2, port2) = 0 then
    640       begin
    641         closesocket(sockfd1);
    642         closesocket(sockfd2);
    643         Continue;
    644       end;
    645 
    646       if send(sockfd2, buffer, 1, 0) = SOCKET_ERROR then
    647       begin
    648         // send failed.
    649         Writeln('[-] Send failed.');
    650         Continue;
    651       end;
    652       l := 0;
    653       FillChar(buffer, 0, MAXSIZE);
    654       Break;
    655     end;
    656 
    657     // all connect ok!
    658     Writeln('[+] All Connect OK!');
    659     sock.fd1 := sockfd1;
    660     sock.fd2 := sockfd2;
    661 
    662     hThread := CreateThread(nil, 0, @transmitdata, @sock, 0, dwThreadID);
    663     if hThread = 0 then
    664     begin
    665       TerminateThread(hThread, 0);
    666       Exit;
    667     end;
    668 
    669     Sleep(1000);
    670     // printf CreateThread OK!
    671     Writeln('[+] CreateThread OK!');
    672   end;
    673 end;
    674 
    675 function testfisvalue(str: PAnsiChar): Integer;
    676 begin
    677   if str = nil then
    678     Exit(0);
    679   if str^ = '-' then
    680     Exit(0);
    681 
    682   Exit(1);
    683 end;
    684 
    685 function create_socket(): Integer;
    686 var
    687   sockfd: TSocket;
    688 begin
    689   sockfd := socket(AF_INET, SOCK_STREAM, 0);
    690   if sockfd < 0 then
    691   begin
    692     Writeln('[-] Create socket error.');
    693     Exit(0);
    694   end;
    695 
    696   Exit(sockfd);
    697 end;
    698 
    699 function create_server(sockfd: Integer; port: Integer): Integer;
    700 var
    701   srvaddr: sockaddr_in;
    702   ion: Integer;
    703 begin
    704   srvaddr.sin_port := htons(port);
    705   srvaddr.sin_family := AF_INET;
    706   srvaddr.sin_addr.S_addr := htonl(INADDR_ANY);
    707 
    708   setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, '1', 1);
    709 
    710   if bind(sockfd, sockaddr(srvaddr), SizeOf(sockaddr)) < 0 then
    711   begin
    712     Writeln('[-] Socket bind error.');
    713     Exit(0);
    714   end;
    715 
    716   if listen(sockfd, CONNECTNUM) < 0 then
    717   begin
    718     Writeln('[-] Socket Listen error.');
    719     Exit(0);
    720   end;
    721 
    722   Exit(1);
    723 end;
    724 
    725 function client_connect(sockfd: Integer; server: PAnsiChar;
    726   port: Integer): Integer;
    727 var
    728   cliaddr: sockaddr_in;
    729   host: phostent;
    730 begin
    731   host := gethostbyname(server);
    732   if host = nil then
    733   begin
    734     Writeln(Format('[-] Gethostbyname(%s) error:%d', [server, (h_errno)]));
    735     Exit(0);
    736   end;
    737 
    738   cliaddr.sin_family := AF_INET;
    739   cliaddr.sin_port := htons(port);
    740   cliaddr.sin_addr := in_addr(PInAddr(host.h_addr^)^); // ?
    741 
    742   if connect(sockfd, sockaddr(cliaddr), SizeOf(sockaddr)) < 0 then
    743   begin
    744     Writeln(Format('[-] Connect %s error: %d', [server, h_errno]));
    745     Exit(0);
    746   end;
    747 
    748   Exit(1);
    749 end;
    750 
    751 procedure __Main();
    752 var
    753   sConnectHost, sTransmitHost: array [0 .. HOSTLEN - 1] of AnsiChar;
    754   iConnectPort, iTransmitPort: Integer;
    755   wsadata: TWsaData;
    756   i: Integer;
    757 begin
    758   ver();
    759   FillChar(sConnectHost, HOSTLEN, 0);
    760   FillChar(sTransmitHost, HOSTLEN, 0);
    761 
    762   WSAStartup(MakeWord(1, 1), wsadata);
    763   method := 0;
    764   CodeSite.send('ParamCount', ParamCount);
    765   for i := 0 to ParamCount do
    766     CodeSite.send(ParamStr(i));
    767   if ParamCount > 2 then
    768   begin
    769     if (ParamStr(1) = '-listen') and (ParamCount >= 3) then
    770     begin
    771       iConnectPort := StrToInt(ParamStr(2));
    772       iTransmitPort := StrToInt(ParamStr(3));
    773       method := 1;
    774     end
    775     else if (ParamStr(1) = '-tran') and (ParamCount >= 4) then
    776     begin
    777       iConnectPort := StrToInt(ParamStr(2));
    778       StrCopy(@sTransmitHost, PAnsiChar(AnsiString(ParamStr(3))));
    779       iTransmitPort := StrToInt(ParamStr(4));
    780       method := 2;
    781     end
    782     else if (ParamStr(1) = '-slave') and (ParamCount >= 5) then
    783     begin
    784       StrCopy(@sConnectHost, PAnsiChar(AnsiString(ParamStr(2))));
    785       iConnectPort := StrToInt(ParamStr(3));
    786       StrCopy(@sTransmitHost, PAnsiChar(AnsiString(ParamStr(4))));
    787       iTransmitPort := StrToInt(ParamStr(5));
    788       method := 3;
    789     end;
    790   end;
    791 
    792   case method of
    793     1:
    794       bind2bind(iConnectPort, iTransmitPort);
    795     2:
    796       bind2conn(iConnectPort, sTransmitHost, iTransmitPort);
    797     3:
    798       conn2conn(sConnectHost, iConnectPort, sTransmitHost, iTransmitPort);
    799   else
    800     usage(PAnsiChar(AnsiString(ParamStr(0))));
    801   end;
    802 
    803   if method <> 0 then
    804   begin
    805     closeallfd();
    806   end;
    807 
    808   WSACleanup();
    809 end;
    810 
    811 end.
  • 相关阅读:
    linux中apt-get使用
    部署ceph
    cinder存储服务
    ceph简介
    Horizon Web管理界面
    neutron网络服务2
    neutron网络服务
    nova计算服务
    cinder存储服务
    keystone身份认证服务
  • 原文地址:https://www.cnblogs.com/xspace/p/3059573.html
Copyright © 2020-2023  润新知