您的位置:程序门 -> vb -> 网络编程



如何发出一个ttl=1的icmp包??


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何发出一个ttl=1的icmp包??
发表于:2007-03-27 11:47:11 楼主
应该是ip包头修改吧?怎么做呢
发表于:2007-03-27 11:56:471楼 得分:0
ping一个ip地址(向它发送一个数据包并等待回应)        
     
    本例演示了怎样通过api的调用向一个ip地址发送一个包的数据并等待回音。    
    新建一个工程,添加一个标准模块,写入以下代码:    
     
    option       explicit    
     
    public       const       ip_status_base       =       11000    
    public       const       ip_success       =       0    
    public       const       ip_buf_too_small       =       (11000       +       1)    
    public       const       ip_dest_net_unreachable       =       (11000       +       2)    
    public       const       ip_dest_host_unreachable       =       (11000       +       3)    
    public       const       ip_dest_prot_unreachable       =       (11000       +       4)    
    public       const       ip_dest_port_unreachable       =       (11000       +       5)    
    public       const       ip_no_resources       =       (11000       +       6)    
    public       const       ip_bad_option       =       (11000       +       7)    
    public       const       ip_hw_error       =       (11000       +       8)    
    public       const       ip_packet_too_big       =       (11000       +       9)    
    public       const       ip_req_timed_out       =       (11000       +       10)    
    public       const       ip_bad_req       =       (11000       +       11)    
    public       const       ip_bad_route       =       (11000       +       12)    
    public       const       ip_ttl_expired_transit       =       (11000       +       13)    
    public       const       ip_ttl_expired_reassem       =       (11000       +       14)    
    public       const       ip_param_problem       =       (11000       +       15)    
    public       const       ip_source_quench       =       (11000       +       16)    
    public       const       ip_option_too_big       =       (11000       +       17)    
    public       const       ip_bad_destination       =       (11000       +       18)    
    public       const       ip_addr_deleted       =       (11000       +       19)    
    public       const       ip_spec_mtu_change       =       (11000       +       20)    
    public       const       ip_mtu_change       =       (11000       +       21)    
    public       const       ip_unload       =       (11000       +       22)    
    public       const       ip_addr_added       =       (11000       +       23)    
    public       const       ip_general_failure       =       (11000       +       50)    
    public       const       max_ip_status       =       11000       +       50    
    public       const       ip_pending       =       (11000       +       255)    
    public       const       ping_timeout       =       200    
    public       const       ws_version_reqd       =       &h101    
    public       const       ws_version_major       =       ws_version_reqd       \       &h100       and       &hff&    
    public       const       ws_version_minor       =       ws_version_reqd       and       &hff&    
    public       const       min_sockets_reqd       =       1    
    public       const       socket_error       =       -1    
     
    public       const       max_wsadescription       =       256    
    public       const       max_wsasysstatus       =       128    
     
    public       type       icmp_options    
    ttl       as       byte    
    tos       as       byte    
    flags       as       byte    
    optionssize       as       byte    
    optionsdata       as       long    
    end       type    
     
    dim       icmpopt       as       icmp_options    
     
    public       type       icmp_echo_reply    
    address       as       long    
    status       as       long    
    roundtriptime       as       long    
    datasize       as       integer    
    reserved       as       integer    
    datapointer       as       long    
    options       as       icmp_options    
    data       as       string       *       250    
    end       type    
     
    public       type       hostent    
    hname       as       long    
    haliases       as       long    
    haddrtype       as       integer    
    hlen       as       integer    
    haddrlist       as       long    
    end       type    
     
    public       type       wsadata    
    wversion       as       integer    
    whighversion       as       integer    
    szdescription(0       to       max_wsadescription)       as       byte    
    szsystemstatus(0       to       max_wsasysstatus)       as       byte    
    wmaxsockets       as       integer    
    wmaxudpdg       as       integer    
    dwvendorinfo       as       long    
    end       type    
     
    public       declare       function       icmpcreatefile       lib       "icmp.dll "       ()       as       long    
    public       declare       function       icmpclosehandle       lib       "icmp.dll "       (byval       icmphandle       as       long)       as       long    
    public       declare       function       icmpsendecho       lib       "icmp.dll "       (byval       icmphandle       as       long,       byval       destinationaddress       as       long,       byval       requestdata       as       string,       byval       requestsize       as       integer,       byval       requestoptions       as       long,       replybuffer       as       icmp_echo_reply,       byval       replysize       as       long,       byval       timeout       as       long)       as       long    
    public       declare       function       wsagetlasterror       lib       "wsock32.dll "       ()       as       long    
    public       declare       function       wsastartup       lib       "wsock32.dll "       (byval       wversionrequired       as       long,       lpwsadata       as       wsadata)       as       long    
    public       declare       function       wsacleanup       lib       "wsock32.dll "       ()       as       long    
    public       declare       function       gethostname       lib       "wsock32.dll "       (byval       szhost       as       string,       byval       dwhostlen       as       long)       as       long    
    public       declare       function       gethostbyname       lib       "wsock32.dll "       (byval       szhost       as       string)       as       long    
    public       declare       sub       rtlmovememory       lib       "kernel32 "       (hpvdest       as       any,       byval       hpvsource       as       long,       byval       cbcopy       as       long)
发表于:2007-03-27 11:57:032楼 得分:0
public       function       getstatuscode(status       as       long)       as       string    
     
    dim       msg       as       string    
     
    select       case       status    
    case       ip_success:       msg       =       "ip       success "    
    case       ip_buf_too_small:       msg       =       "ip       buf       too_small "    
    case       ip_dest_net_unreachable:       msg       =       "ip       dest       net       unreachable "    
    case       ip_dest_host_unreachable:       msg       =       "ip       dest       host       unreachable "    
    case       ip_dest_prot_unreachable:       msg       =       "ip       dest       prot       unreachable "    
    case       ip_dest_port_unreachable:       msg       =       "ip       dest       port       unreachable "    
    case       ip_no_resources:       msg       =       "ip       no       resources "    
    case       ip_bad_option:       msg       =       "ip       bad       option "    
    case       ip_hw_error:       msg       =       "ip       hw_error "    
    case       ip_packet_too_big:       msg       =       "ip       packet       too_big "    
    case       ip_req_timed_out:       msg       =       "ip       req       timed       out "    
    case       ip_bad_req:       msg       =       "ip       bad       req "    
    case       ip_bad_route:       msg       =       "ip       bad       route "    
    case       ip_ttl_expired_transit:       msg       =       "ip       ttl       expired       transit "    
    case       ip_ttl_expired_reassem:       msg       =       "ip       ttl       expired       reassem "    
    case       ip_param_problem:       msg       =       "ip       param_problem "    
    case       ip_source_quench:       msg       =       "ip       source       quench "    
    case       ip_option_too_big:       msg       =       "ip       option       too_big "    
    case       ip_bad_destination:       msg       =       "ip       bad       destination "    
    case       ip_addr_deleted:       msg       =       "ip       addr       deleted "    
    case       ip_spec_mtu_change:       msg       =       "ip       spec       mtu       change "    
    case       ip_mtu_change:       msg       =       "ip       mtu_change "    
    case       ip_unload:       msg       =       "ip       unload "    
    case       ip_addr_added:       msg       =       "ip       addr       added "    
    case       ip_general_failure:       msg       =       "ip       general       failure "    
    case       ip_pending:       msg       =       "ip       pending "    
    case       ping_timeout:       msg       =       "ping       timeout "    
    case       else:       msg       =       "unknown       msg       returned "    
    end       select    
    getstatuscode       =       cstr(status)       &       "       [       "       &       msg       &       "       ] "    
    end       function    
     
    public       function       hibyte(byval       wparam       as       integer)    
    hibyte       =       wparam       \       &h1       and       &hff&        
    end       function    
     
    public       function       lobyte(byval       wparam       as       integer)    
    lobyte       =       wparam       and       &hff&        
    end       function    
     
    public       function       ping(szaddress       as       string,       echo       as       icmp_echo_reply)       as       long    
    dim       hport       as       long    
    dim       dwaddress       as       long    
    dim       sdatatosend       as       string    
    dim       iopt       as       long    
    sdatatosend       =       "echo       this "    
    dwaddress       =       addressstringtolong(szaddress)    
    hport       =       icmpcreatefile()    
    if       icmpsendecho(hport,       dwaddress,       sdatatosend,       len(sdatatosend),       0,       echo,       len(echo),       ping_timeout)       then    
    'the       ping       succeeded,    
    '.status       will       be       0    
    '.roundtriptime       is       the       time       in       ms       for       the       ping       to       complete,    
    '.data       is       the       data       returned       (null       terminated)    
    '.address       is       the       ip       address       that       actually       replied    
    '.datasize       is       the       size       of       the       string       in       .data    
    ping       =       echo.roundtriptime    
    else    
    ping       =       echo.status       *       -1    
    end       if    
    call       icmpclosehandle(hport)    
    end       function    
     
    function       addressstringtolong(byval       tmp       as       string)       as       long    
    dim       i       as       integer    
    dim       parts(1       to       4)       as       string    
    i       =       0    
    'we       have       to       extract       each       part       of       the    
    '123.456.789.123       string,       delimited       by    
    'a       period    
    while       instr(tmp,       ". ")       >       0    
    i       =       i       +       1    
    parts(i)       =       mid(tmp,       1,       instr(tmp,       ". ")       -       1)    
    tmp       =       mid(tmp,       instr(tmp,       ". ")       +       1)    
    wend    
    i       =       i       +       1    
    parts(i)       =       tmp    
    if       i       <>       4       then    
    addressstringtolong       =       0    
    exit       function    
    end       if    
    'build       the       long       value       out       of       the    
    'hex       of       the       extracted       strings    
    addressstringtolong       =       val( "&h "       &       right( "00 "       &       hex(parts(4)),       2)       &       _    
    right( "00 "       &       hex(parts(3)),       2)       &       _    
    right( "00 "       &       hex(parts(2)),       2)       &       _    
    right( "00 "       &       hex(parts(1)),       2))    
    end       function    
     
    public       function       socketscleanup()       as       boolean    
    dim       x       as       long    
    x       =       wsacleanup()    
    if       x       <>       0       then    
    msgbox       "windows       sockets       error       "       &       trim$(str$(x))       &       "       occurred       in       cleanup. ",       vbexclamation    
    socketscleanup       =       false    
    else    
    socketscleanup       =       true    
    end       if    
    end       function    
     
    public       function       socketsinitialize()       as       boolean    
    dim       wsad       as       wsadata    
    dim       x       as       integer    
    dim       szlobyte       as       string,       szhibyte       as       string,       szbuf       as       string    
    x       =       wsastartup(ws_version_reqd,       wsad)    
    if       x       <>       0       then    
    msgbox       "windows       sockets       for       32       bit       windows       "       &       "environments       is       not       successfully       responding. "    
    socketsinitialize       =       false    
    exit       function    
    end       if    
    if       lobyte(wsad.wversion)       <       ws_version_major       or       (lobyte(wsad.wversion)       =       ws_version_major       and       hibyte(wsad.wversion)       <       ws_version_minor)       then    
    szhibyte       =       trim$(str$(hibyte(wsad.wversion)))    
    szlobyte       =       trim$(str$(lobyte(wsad.wversion)))    
    szbuf       =       "windows       sockets       version       "       &       szlobyte       &       ". "       &       szhibyte    
    szbuf       =       szbuf       &       "       is       not       supported       by       windows       "       &       "sockets       for       32       bit       windows       environments. "    
    msgbox       szbuf,       vbexclamation    
    socketsinitialize       =       false    
    exit       function    
    end       if    
    if       wsad.wmaxsockets       <       min_sockets_reqd       then    
    szbuf       =       "this       application       requires       a       minimum       of       "       &       trim$(str$(min_sockets_reqd))       &       "       supported       sockets. "    
    msgbox       szbuf,       vbexclamation    
    socketsinitialize       =       false    
    exit       function    
    end       if    
    socketsinitialize       =       true    
    end       function    
     
    在form中添加一个命令按钮command1,一个文本框text2,创建一个textbox数组(text1(0)到text1(5))。在窗体中写入如下代码:    
    private       sub       command1_click()        
    dim       echo       as       icmp_echo_reply        
    dim       pos       as       integer        
    call       ping(text2.text,       echo)        
    text1(0)       =       getstatuscode(echo.status)        
    text1(1)       =       echo.address        
    text1(2)       =       echo.roundtriptime       &       "       ms "        
    text1(3)       =       echo.datasize       &       "       bytes "        
    if       left$(echo.data,       1)       <>       chr$(0)       then        
    pos       =       instr(echo.data,       chr$(0))        
    text1(4)       =       left$(echo.data,       pos       -       1)        
    end       if        
    text1(5)       =       echo.datapointer        
    end       sub
发表于:2007-03-27 12:02:483楼 得分:0
请问在何处设置ttl


快速检索

最新资讯
热门点击