home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / PKTDRVR / PDTST217.ZIP / PDCLKSRC / PING.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-04-07  |  22.7 KB  |  1,120 lines

  1. ;        ping.asm
  2. ;========================================================================
  3.  
  4. ; Copyright (C) 1991-94 by Jan.Engvald@ldc.lu.se, see file COPYING.
  5.  
  6.  
  7. ;************************************************************************
  8. ;*        EchoAwhile
  9. ;************************************************************************
  10.  
  11. EchoAwhile    proc    near
  12.         assume    ds:code_s
  13.         call    HwTicksNoHi
  14.         mov    EchoStart,si        ; note start ticks
  15.         mov    EchoStart+2,dx
  16.         mov    EchoLoadTime,dx
  17.  
  18.         mov    di,offset EchoSizeVec    ; clear error size table
  19.         mov    cx,(EchoSizeEnd-EchoSizeVec)/2
  20.         xor    ax,ax
  21.         rep    stosw
  22.  
  23.         cmp    EchoTarget,ax        ; want to ping someone?
  24.         jne    EchoYes
  25.  
  26.         test    ArgFlags,TERM_WAIT    ; ping serving?
  27.         jz    EchoNo
  28.         mov    dx,offset MsgEchoStats
  29.         call    DosPr$
  30.                 or      GenFlags,ECHO_DISPL
  31.   EchoNo:
  32.         ret
  33.  
  34.   EchoYes:
  35.         call    InitTimer        ; prepare millisecond timing
  36.  
  37.         mov    ax,ArgFlags
  38.         and    ax,STOP_ON_ERR
  39.         xor    ax,NO_ERR_YET+STOP_ON_ERR+NO_DROP_YET+PING_DELAY
  40.         or    GenFlags,ax
  41.  
  42.         call    HardwareTicks
  43.         mov    EchoStart,si        ; note start ticks
  44.         mov    EchoStart+2,dx
  45.         mov    EchoStart+4,ax
  46.         mov    EchoLoadTime,dx
  47.         mov    EchoLoadTime+2,ax
  48.         mov    si,dx
  49.  
  50.         call    DblShrX
  51.         mov    EchoNext,ax
  52.  
  53.         cmp    EchoTarget,127        ; dns lookup to do?
  54.         jne    EchoIpAddr
  55.  
  56.         mov    dx,offset MsgCalling
  57.         call    DosPr$
  58.  
  59.         mov    dx,2345h        ; ping udp src port
  60.         xor    ax,ax            ; NsId
  61.         mov    si,offset EchoNameBuf    ; Ns question string
  62.  
  63.         call    NsResolve        ; call nameserver(s)
  64.  
  65.         call    HardwareTicks        ; response time
  66.         mov    si,dx
  67.         sub    ax,EchoStart+4
  68.         sbb    dx,EchoStart+2
  69.         div    kTimerScale
  70.         mov    di,offset MsgNameRepTim
  71.         call    PutNumD4Fb
  72.  
  73.         mov    dx,offset MsgNameRep    ; display it.
  74.         call    DosPr$
  75.  
  76.         cmp    EchoTarget,127        ; name resolved?
  77.         jne    EchoIpAddr
  78.   EchoErrTerm:
  79.         mov    dx,offset MsgBadName
  80.         mov    al,'e'-'0'
  81.         call    PrTerminate
  82.   
  83.   EchoIpAddr:
  84.         mov    EchoTxTime,si        ; intialize timers
  85.         mov    EchoRxTime,si
  86. if TBLBUILD
  87.         call    TableReady        ; allow tablebuilding now
  88. endif ; TBLBUILD
  89.         mov    ax,EchoInterval        ; check interval
  90.         test    ArgFlags,NOT_SAFE
  91.         jnz    EchoAsIs
  92.  
  93.         cmp    ax,110
  94.         jge    EchoAsItIs
  95.         mov    ax,110
  96.   EchoAsItIs:
  97.         test    ArgFlags,MICRO_100
  98.         jz    EchoAsIs
  99.         cmp    ax,128*10
  100.         jge    EchoAsIs
  101.         mov    ax,128*10
  102.   EchoAsIs:
  103.         mov    EchoInterval,ax
  104. if HOPCHK
  105.         test    MoreFlags,HOP_CHK    ; hop check going on?
  106.         jz    EchoNoHopBuf
  107.  
  108.         mov    cx,(MAXHOP+1)*80/2
  109.         mov    di,offset HopTabDsp-80
  110.         mov    ax,'  '            ; blank display buff
  111.         rep    stosw
  112.  
  113.         mov    byte ptr MsgEchoHead+6,'e'
  114.         mov    byte ptr MsgEchoStats,'$'
  115.  
  116.         mov    dx,MyIpNr
  117.         mov    ax,MyIpNr+2
  118.         mov    word ptr HopTabIp,dx
  119.         mov    word ptr HopTabIp+2,ax
  120.         call    BufAlloc
  121.         call    MakeSendDescr
  122.         mov    di,[bx].dPtrIp
  123.         mov    [di].iIpSrc,dx        ; my IP # to IP src
  124.         mov    [di].iIpSrc+2,ax
  125.         call    PutPhysSrc
  126.         mov    si,offset TblToDo
  127.         call    AddToList        ; find my name
  128.   EchoNoHopBuf:
  129. endif ; HOPCHK
  130.         call    BufAlloc        ; get a send buf
  131.         assume    ds:nothing
  132.         call    MakeSendDescr
  133.  
  134.         mov    [di].uIcmpTypeCode,8    ; type = echo request
  135.         test    cs:GenFlags,UDP_ECHO    ; ping or udpecho?
  136.         jz    EchoUseIcmp        ; print "Ping  to"
  137.  
  138.         mov    dx,'cE'            ; print "Echo  to"
  139.         mov    ax,'oh'
  140. if HOPCHK
  141.         test    cs:MoreFlags,HOP_CHK    ; hop checking?
  142.         jz    EchoFillEcho
  143.         mov    dx,'rT'            ; print "Trace to"
  144.         mov    ax,'ca'
  145.   EchoFillEcho:
  146. endif ; HOPCHK
  147.         mov    word ptr cs:MsgEchoHead+2,dx
  148.         mov    word ptr cs:MsgEchoHead+4,ax
  149.  
  150.         mov    word ptr [di].uUdpSrc,0703h    ; my udp echo port
  151.         mov    word ptr [di].uUdpDst,0900h    ; dst discard port
  152.         test    cs:ArgFlags,UDP_DISCARD
  153.         jnz    EchoUseIcmp
  154.         mov    word ptr [di].uUdpDst,0700h    ; dst echo port
  155.   EchoUseIcmp:
  156.         mov    cx,cs:MyGiant        ; fill buffer with data
  157.                 sub     cx,HWHDRLEN+IPHDRLEN+ICMPHDRLEN+6
  158.         shr    cx,1
  159.         add    di,ICMPHDRLEN+6
  160.         mov    ax,cs:EchoData
  161.   EchoFillLoop:
  162.         stosw
  163.         add    ax,cs:EchoDataIncW
  164.         loop    EchoFillLoop
  165.   EchoFillEnd:
  166.         mov    dx,cs:EchoTarget    ; set IP destination
  167.         mov    ax,cs:EchoTarget+2
  168.         call    SetIpDst
  169.  
  170.         mov    ds,cs:MySegm
  171.         assume    ds:code_s
  172.         mov    EchoBufSeg,es
  173.  
  174.         mov    si,offset EchoTarget
  175.         mov    di,offset MsgEchoTarget
  176.         call    PutIpNum        ; tell who we are pinging
  177.  
  178.         mov    dx,EchoMinSize
  179.         sub    dx,IPHDRLEN
  180.         cmp    dx,ICMPHDRLEN+6
  181.         jns    EchoMinOK
  182.         mov    dx,ICMPHDRLEN+6
  183.   EchoMinOK:
  184.         mov    ax,EchoSize
  185.         neg    ax
  186.         jns    EchoSizeNeg
  187.         mov    dx,GIANTTR
  188.         neg    ax
  189.         mov    MsgEchoSweep,' '
  190.   EchoSizeNeg:
  191.                 mov     cx,MyGiant
  192.                 sub     cx,HWHDRLEN+IPHDRLEN+ICMPHDRLEN+6
  193.                 sub     ax,IPHDRLEN+ICMPHDRLEN+6
  194.                 cmp     ax,cx
  195.         jbe    EchoOkSize
  196.         xor    ax,ax
  197.   EchoOkSize:
  198.         add    ax,ICMPHDRLEN+6
  199.         mov    EchoSize,ax
  200.         mov    EchoMaxSize,ax
  201.  
  202.         cmp    dx,ax
  203.         jbe    EchoMinMax
  204.         mov    dx,ax
  205.   EchoMinMax:
  206.         mov    EchoMinSize,dx
  207.  
  208.         cmp    ax,dx
  209.         je    EchoFixedSz
  210.         push    ax
  211.         mov    ax,dx
  212.         add    ax,IPHDRLEN
  213.         mov    di,offset MsgEchoSweep-4
  214.         call    PutNumD4Fb        ;   and the min size used
  215.         pop    ax
  216.   EchoFixedSz:
  217.         add    ax,IPHDRLEN
  218.         mov    di,offset MsgEchoSize
  219.         mov    PutMinDigits,1
  220.         call    PutNum            ;   and the packet size used
  221.  
  222.         mov    ax,EchoInterval
  223.         push    ax
  224.         mov    di,offset MsgEchoMs
  225.         mov    PutMinDigits,6
  226.         call    PutNumFb        ;   and interval in ms
  227.         pop    ax
  228.         test    ArgFlags,MICRO_100
  229.         jz    Echo1ms
  230.         mov    dh,MsgEchoMs+5
  231.         mov    dl,'.'
  232.         mov    word ptr MsgEchoMs+5,dx
  233.         mul    k51200            ; make interval close to 0.1 ms
  234.         jmp    short EchoPrHdr
  235.   Echo1ms:
  236.         mul    k64000            ; make interval close to 1 ms
  237.   EchoPrHdr:
  238.         div    k54925
  239.         mov    EchoInterval,ax
  240.         mov    dx,offset MsgEchoHead
  241.         call    DosPr$
  242. if HOPCHK
  243.         mov    byte ptr MsgEchoStats,' '
  244. endif ; HOPCHK
  245.                 or      GenFlags,ECHO_DISPL
  246.  
  247.  
  248.   EchoLoop:
  249.         call    Something2Do        ; process other things
  250.  
  251.         test    GenFlags,NO_ERR_YET+CONT_ON_ERR    ; want to stop on sequence errs?
  252.         jz    EchoEnd0
  253.  
  254.         call    AnyKey            ; first key stops sending
  255.         jz    EchoNoKey
  256.   EchoEnd0:
  257.         jmp    EchoEnd
  258.   EchoNoKey:
  259.         call    Something2Do        ; called to slow down client
  260.  
  261.         call    HardwareTicks
  262.         mov    es,EchoBufSeg
  263.         mov    di,es:[bx].dPtrUdp
  264.         mov    es:[di].uUdpData,ax
  265.         mov    es:[di].uUdpData+2,dx
  266.  
  267.         call    DblShrX
  268.         mov    dx,EchoNext
  269.         cmp    ax,dx            ; time to send next echo pkt?
  270.         js    EchoLoop
  271.  
  272.         add    dx,EchoInterval
  273.         cmp    dx,ax
  274.         jns    EchoSetNext
  275.         mov    dx,ax
  276.   EchoSetNext:
  277.         mov    EchoNext,dx        ; next send time
  278.  
  279.         mov    dx,es:[di].uUdpData+2
  280.         mov    EchoTxTime,dx
  281.  
  282.         mov    cx,EchoSize        ; compute packet size
  283.         inc    cx
  284.         cmp    cx,EchoMaxSize
  285.         jbe    EchoNormSize
  286.         mov    cx,EchoMinSize
  287.   EchoNormSize:
  288.         mov    EchoSize,cx
  289. if HOPCHK
  290.         test    MoreFlags,HOP_CHK
  291.         jnz    EchoNoVec
  292. endif ; HOPCHK
  293.         mov    di,cx             ; IP data length
  294.         shl    di,1
  295.         inc    word ptr EchoSizeVec[di]
  296.   EchoNoVec:
  297.         mov    ax,EchoTxLo        ; increment send counters
  298.         add    ax,1
  299.         mov    EchoTxLo,ax
  300.         adc    EchoTxHi,0
  301.         mov    EchoFudge,1
  302.  
  303.         mov    ds,EchoBufSeg
  304.         assume    ds:nothing
  305.         mov    di,[bx].dPtrUdp        ; put sequence number
  306.         mov    [di].uUdpData+4,ax
  307.         mov    [bx].dPktLen,cx        ; and IP data length
  308.  
  309.         test    cs:GenFlags,UDP_ECHO
  310.         jnz    EchoUdp
  311.         call    SendIcmpPkt        ; send icmp echo pkt
  312.   EchoLoop8:
  313.         mov    ds,cs:MySegm
  314.         assume    ds:code_s
  315.         jnz    EchoErr         ; any errors?
  316.   EchoLoop9:
  317.         mov    EchoLastErr,cx
  318.         jmp    EchoLoop
  319.  
  320.   EchoErr:
  321.         cmp    cx,EchoLastErr
  322.         je    EchoLoop9
  323.  
  324.         push    cx
  325.         xor    ch,ch
  326.         test    cl,01            ; odd errs not allowed
  327.         jnz    EchoErrUndef
  328.         cmp    cl,SERRTABLEN
  329.         jb    EchoErrExist
  330.   EchoErrUndef:
  331.         mov    cl,SERRTABLEN
  332.   EchoErrExist:
  333.         mov    si,cx
  334.         mov    dx,MsgSerrTbl[si]
  335.         call    DosPr$
  336.         pop    cx
  337.         mov    ds,EchoBufSeg
  338.         test    ArgFlags,TERM_WAIT
  339.         jnz    EchoLoop9
  340.  
  341.   EchoEnd:
  342. if 0
  343.         mov    ds,cs:EchoBufSeg
  344.         assume    ds:nothing
  345.         call    BufRelease        ; release send buffer
  346.         push    cs
  347.         pop    ds
  348. endif
  349.         assume    ds:code_s
  350.         push    cs
  351.         pop    es
  352.  
  353.         mov    EchoFudge,0
  354.         or    ArgFlags,TERM_WAIT    ; allow late packets to arrive
  355.         ret
  356.  
  357.   EchoUdp:
  358. if HOPCHK
  359.         test    cs:MoreFlags,HOP_CHK    ; hop checking?
  360.         jnz    EchoHop
  361. endif ; HOPCHK
  362.         call    SendUdpPkt        ; send udp echo pkt
  363.         jmp    EchoLoop8
  364.  
  365. if HOPCHK
  366.   EchoHop:
  367.         call    HardwareTicks        ; time stamp in ms
  368.         mov    cl,6
  369.         call    DblShl
  370.         or    dh,080h            ;   + 8000h
  371.         xchg    dh,dl
  372.         mov    [di].uUdpDst,dx        ;   into dst port
  373.  
  374.         push    ds
  375.         push    cs
  376.         pop    ds
  377.  
  378.         mov    cx,word ptr HopCycle    ; cl = HopCycle, ch = HopCount
  379.         mov    dx,word ptr HopMax    ; dl = MAXHOP, dh = MAXHOP-1
  380.         mov    ax,word ptr HopStopThis
  381.         div    dl            ; hop cycle into al
  382.         cmp    al,cl            ; limit THIS turn?
  383.         je    HopNextTurn
  384.  
  385.         mov    ah,HopMaxAll
  386.         cmp    ah,dh
  387.         jbe    HopInLim
  388.         mov    ah,dh
  389.   HopInLim:
  390.         cmp    ch,ah            ; reached max hops any turn?
  391.         jb    HopSameTurn
  392.   HopNextTurn:
  393.         mov    HopStopThis,dh        ; ignore host/net unr limit
  394.         xor    ch,ch
  395.         inc    cl
  396.         cmp    cl,3            ; all turns done?
  397.         jb    HopGoOn
  398.         pop    ax            ; (ds)
  399.         jmp    EchoEnd
  400.   HopGoOn:
  401.         mov    HopCycle,cl
  402.  
  403.         push    cx
  404.         mov    dx,19            ; wait 1 sec before next turn
  405.         call    DblShrX
  406.         sub    ax,EchoInterval
  407.         add    EchoNext,ax
  408.         pop    cx
  409. if 1
  410.         mov    dx,EchoMaxSize
  411.         mov    ax,EchoMinSize
  412.         cmp    ax,dx            ; fixed pkt size?
  413.         jne    HopSameTurn
  414.         inc    ax            ; increase by 1 each turn
  415.         mov    dx,MyGiant
  416.         sub    dx,HWHDRLEN+IPHDRLEN
  417.         cmp    ax,dx            ; unless at GIANT already
  418.         ja    HopSameTurn
  419.         mov    EchoMinSize,ax
  420.         mov    EchoMaxSize,ax
  421. endif ; 1
  422.  
  423.   HopSameTurn:
  424.         mov    al,MAXHOP        ; compute cycle and cnt
  425.         mul    cl
  426.         inc    ch
  427.         add    al,ch
  428.         mov    HopCount,ch
  429.  
  430.         pop    ds
  431.  
  432.         mov    ah,al
  433.         mov    al,080h
  434.         mov    [di].uUdpSrc,ax        ; 8000h+cycle into src port
  435.  
  436.         mov    si,[bx].dPtrIp
  437.         mov    [si].iIpTtl,ch        ; set ttl
  438.         or    [si].iIpFlFrag,0040h    ; set don't fragment
  439. if 1
  440.         test    cs:ArgFlags,UDP_DISCARD    ; use *PROTOCOL* unreachable?
  441.         jz    EchoHopUdp
  442.         mov    [si].iIpProt,241
  443.         call    SendIpPkt
  444.         xor    cx,cx
  445.         jmp    EchoLoop8
  446.   EchoHopUdp:
  447. endif ; 1
  448.         call    SendUdpPkt        ; send udp echo pkt
  449.         xor    cx,cx
  450.         jmp    EchoLoop8
  451. endif ; HOPCHK
  452. EchoAwhile    endp
  453.  
  454.  
  455.  
  456. if DEBUG or TBLBUILD
  457. TstEqual    proc    near
  458.         assume    ds:nothing
  459.         jne    TstErr
  460.         ret
  461. TstEqual    endp
  462. endif ; DEBUG or TBLBUILD
  463.  
  464. if DEBUG
  465.  
  466.   TstBufLinks    proc    near
  467.         assume    ds:nothing
  468.         ja    TstErr
  469.         mov    si,di
  470.         inc    cx
  471.     TstLinkLoop:
  472. if DEBUG ge 6
  473.         .386
  474.         push    di
  475.         push    eax
  476.         push    cx
  477.  
  478.         cmp    di,offset FreeBufs
  479.         je    BufChkEnd
  480. ifdef SMALLBUFS
  481.         cmp    di,offset FreeSmal
  482.         je    BufChkEnd
  483. endif ; SMALLBUFS
  484.         push    ax
  485.         mov    cx,(dHwDst-dPtrIp)/4
  486. if DEBUG ge 8
  487.         mov    cx,((dHwDst-dPtrIp)+(BUFSIZESML-DESCRLEN))/4
  488.         mov    ax,es:[di].dHomeList
  489.         cmp    ax,offset FreeBufs
  490.         jne    BufChkSml
  491.         mov    cx,((dHwDst-dPtrIp)+(BUFSIZE-DESCRLEN))/4
  492.   BufChkSml:
  493. endif ; DEBUG ge 8
  494.         lea    di,[di].dPtrPhys
  495.         mov    eax,0aaaaaaaah        ; contents changed?
  496.         scasw
  497.         repe    scasd
  498.         pop    ax
  499.         call    TstEqual
  500.   BufChkEnd:
  501.         pop    cx
  502.         pop    eax
  503.         pop    di
  504.  
  505.         mov    bp,bx
  506.         mov    ax,dx
  507.         mov    bx,di
  508.         mov    dx,es
  509.         .8086
  510. endif ; DEBUG ge 6
  511.         les    di,es:[di].dNext
  512.         lds    si,[si].dPrev
  513.         loop    TstLinkLoop
  514.  
  515.         cmp    si,di
  516.         jne    TstErr
  517.         mov    cx,ds
  518.         cmp    cx,cs:MySegm
  519.         jne    TstErr
  520.         ret
  521. TstBufLinks    endp
  522.  
  523. endif ; DEBUG
  524.  
  525. TstErr:
  526.         call    Terminate
  527.  
  528.  
  529.  
  530. ;************************************************************************
  531. ;*        EchoDisplay
  532. ;************************************************************************
  533.  
  534.  
  535. EchoDisplay    proc    near
  536.         assume    ds:code_s
  537. if DEBUG or TBLBUILD
  538.         mov    al,'s'-'0'
  539.         cmp    word ptr StackLow,0    ; stack overflowed?
  540.         call    TstEqual
  541. endif ; DEBUG or TBLBUILD
  542.  
  543. if DEBUG
  544.         mov    al,'t'-'0'
  545.         cmp    word ptr phd_dioa,0    ; stack overflowed?
  546.         call    TstEqual
  547.  
  548.         mov    al,'i'-'0'
  549.         test    GenFlags,DBGINTERR    ; any interrupt debug errs?
  550.         call    TstEqual
  551.  
  552.         mov    cx,MyNbufs        ; any buffers damaged?
  553.         mov    bx,offset BufStart
  554.         mov    al,'u'-'0'
  555.   BufTestLoop:
  556.         cmp    [bx].dHomeList,offset FreeBufs
  557.         call    TstEqual
  558.         add    bx,MyBufSize
  559.         loop    BufTestLoop
  560.  
  561.         push    cs
  562.         pop    es
  563.         mov    di,offset FreeBufs
  564.         cli
  565.         mov    cx,[di].lBufsAvail
  566.         cmp    cx,MaxAvail
  567.         call    TstBufLinks
  568.         cmp    di,offset FreeBufs
  569.         call    TstEqual
  570.         sti
  571.  
  572. ifdef SMALLBUFS
  573.         mov    bx,offset BufStart+NBUFS*BUFSIZE
  574.         mov    cx,NBUFSMALL
  575.         mov    al,'v'-'0'
  576.   TstSmlLoop:
  577.         cmp    [bx].dHomeList,offset FreeSmal
  578.         call    TstEqual
  579.         add    bx,BUFSIZESML
  580.         loop    TstSmlLoop
  581.  
  582.         cli
  583.         mov    di,offset FreeSmal
  584.         mov    cx,[di].lBufsAvail
  585.         cmp    cx,NBUFSMALL
  586.         call    TstBufLinks
  587.         cmp    di,offset FreeSmal
  588.         call    TstEqual
  589.         sti
  590. endif ; SMALLBUFS
  591.         SHOW_EVENT    'o'
  592. endif ; DEBUG
  593.  
  594. if TBLBUILD
  595.         cmp    EchoTarget,0        ; pinging or hopchecking?
  596.         jne    EchoDispYes
  597.         test    ArgFlags,MAKE_TABLE
  598.         jz    EchoDispYes
  599.         call    TblDisplay        ; -no, must be tbl building
  600.         ret
  601.  
  602.   EchoDispYes:                    ; -yes, we are pinging
  603. endif ; TBLBUILD
  604.                 test    GenFlags,ECHO_DISPL
  605.                 jz      EchoDispRet
  606.  
  607.         mov    cx,SavedTicks
  608.         shr    cx,1
  609.         mov    ax,EchoLastDispl
  610.         shr    ax,1
  611.         cmp    ax,cx            ; time to update display?
  612.         jne    EchoDispNow
  613.   EchoDispRet:
  614.         ret
  615.  
  616.   EchoDispNow:
  617.         cli
  618.         mov    dx,EchoAvgSum
  619.         mov    ax,EchoAvgSum+2
  620.         mov    cx,EchoRxHi
  621.         mov    bx,EchoRxLo
  622.         sti
  623.         call    AdjTo16Bits
  624.         jz    EchoSkipAvg        ; avoid div by zero
  625.  
  626.         div    bx
  627. if DEBUG ge 2
  628.         mov    ax,MinAvail        ; Show min available bufs
  629. endif ; DEBUG ge 2
  630.         mov    EchoAvg,ax        ; average delay
  631.   EchoSkipAvg:
  632.  
  633.         cmp    EchoTarget,0
  634.         jne    $+5
  635.         jmp    EchoDspDiscard
  636.  
  637.         mov    dx,EchoTxHi        ; compute difference tx - rx
  638.         mov    ax,EchoTxLo
  639.         sub    ax,EchoFudge        ;   subtract one if just
  640.         sbb    dx,0            ;   sent a pkt
  641.         push    ax
  642.         push    dx
  643.         cli
  644.         sub    ax,EchoRxLo
  645.         sbb    dx,EchoRxHi
  646.         sti
  647.         push    ax
  648.         push    dx
  649.         jns    EchoDifPos        ; use abs(tx-rx)
  650.         call    NegDxAx
  651.   EchoDifPos:
  652.         mov    EchoDifHi,dx
  653.         mov    EchoDifLo,ax
  654.         pop    dx
  655.         pop    ax
  656.         add    ax,word ptr EchoErRepLo    ; don't count repeated as loss
  657.         adc    dx,word ptr EchoErRepHi
  658.         jns    EchoLossPos        ; use abs(tx-rx+rep)
  659.         call    NegDxAx
  660.   EchoLossPos:
  661.  
  662.         mov    si,EchoMax        ; convert ms delay to ticks
  663.         or    si,si
  664.         jnz    EchoGotMax
  665.         mov    si,2000
  666.   EchoGotMax:
  667.         add    si,500
  668.         mov    cl,5            ;   with a margin
  669.         shr    si,cl
  670.         push    si            ;   and save it
  671.  
  672.         mov    cx,EchoTxTime
  673.         add    cx,si
  674.         add    si,EchoRxTime
  675.         cmp    cx,SavedTicks        ; did we stop a while ago?
  676.         jns    EchoDispNotStop
  677.         and    GenFlags,not PING_DELAY
  678.         jmp    short EchoDispLoss    ; - yes, use diff
  679.   EchoDispNotStop:
  680.         cmp    si,EchoTxTime        ; received anything lately?
  681.         jns    EchoUseLoss        ; - yes
  682.  
  683.         and    GenFlags,not (NO_ERR_YET+NO_DROP_YET) ; - no, he
  684.         jmp    short EchoDispLoss    ;    probably died
  685.   EchoUseLoss:
  686.         mov    ax,word ptr EchoErLosLo    ; use loss cntr
  687.         mov    dx,word ptr EchoErLosHi
  688.   EchoDispLoss:
  689.         mov    cl,2            ; compute packet loss
  690.         call    DblShl            ;   multiply by 100
  691.         mov    si,dx
  692.         mov    bx,ax
  693.         mov    cl,3
  694.         call    DblShl
  695.         add    bx,ax
  696.         adc    si,dx
  697.         shl    ax,1
  698.         rcl    dx,1
  699.         add    ax,bx
  700.         adc    dx,si
  701.  
  702.         pop    si
  703.         pop    cx
  704.         pop    bx
  705.  
  706.         call    AdjTo16Bits
  707.         jz    EchoSkipLoss        ; avoid div by zero
  708.  
  709.         div    bx
  710.         mov    EchoLoss,ax
  711.  
  712.         mov    ax,dx            ;   fractional loss
  713.         mul    k10000
  714.         div    bx
  715.         mov    EchoLoss+2,ax
  716.   EchoSkipLoss:
  717.         shr    si,1
  718.         add    si,EchoErTmpTime
  719.         cmp    si,SavedTicks
  720.         jns    EchoDspTmp
  721.  
  722.         xor    dx,dx
  723.         cli
  724.         mov    ax,word ptr EchoErTmp    ; make temp err permanent
  725.         add    word ptr EchoErLosLo,ax
  726.         adc    word ptr EchoErLosHi,dx
  727.         mov    word ptr EchoErTmp,dx
  728.         sti
  729.   EchoDspTmp:
  730.  
  731.   EchoDspDiscard:
  732.         push    cs            ; edit echo values
  733.         pop    es
  734. if HOPCHK
  735.         test    MoreFlags,HOP_CHK    ; hop check going on?
  736.         jz    EchoDspNoHop
  737.         jmp    HopDisplay
  738.   EchoDspNoHop:
  739. endif ; HOPCHK
  740.         mov    di,offset MsgEcho-1
  741.         mov    si,offset EchoTxHi
  742.         mov     PutNumFiller,' '
  743.         mov    PutMinDigits,9
  744.         mov    cx,3
  745.         call    PutBigNums        ; tx and rx and diff
  746.  
  747.         mov    PutMinDigits,6
  748.         inc    di
  749.         mov    cx,5
  750.   EchoDispLoop:
  751.         push    cx
  752.         mov    cl,1
  753.         call    PutNums         ; delays and loss
  754.         pop    cx
  755.         loop    EchoDispLoop
  756.  
  757.         inc    di
  758.         mov    cl,1
  759.         mov    PutMinDigits,4
  760.         call    PutNumsF0        ; .0000%
  761.  
  762.         call    HardwareTicks
  763.         push    dx
  764.         push    si
  765.  
  766.         dec    EchoLoadCnt        ; update load every 4:th time
  767.         jnz    EchoLoad1st
  768.  
  769.         mov    EchoLoadCnt,4
  770.         mov    bx,ax
  771.         mov    cx,dx
  772.         sub    ax,EchoLoadTime+2
  773.         sbb    dx,EchoLoadTime
  774.         mov    EchoLoadTime+2,bx
  775.         mov    EchoLoadTime,cx
  776.         js    EchoLoad1st        ; avoid overflow
  777.  
  778.         div    kTimerScale
  779.         mov    bx,ax
  780.         cli
  781.         mov    ax,EchoLoad+2
  782.         mov    dx,EchoLoad
  783.         mov    EchoLoad+2,0
  784.         mov    EchoLoad,0
  785.         sti
  786.         mov    cl,3
  787.         call    DblShl
  788.         div    bx
  789.         add    di,3
  790.         inc    PutMinDigits
  791.         call    PutNumFb        ; load kb/s
  792.   EchoLoad1st:
  793.         pop    dx            ; compute elapsed time
  794.         pop    ax
  795.         cmp    dx,EchoLastHi
  796.         jae    EchoLastGrows
  797.  
  798.         mov    bx,EchoLastDispl
  799.         mov    cx,EchoLastHi
  800.         sub    EchoStart+2,bx
  801.         sbb    EchoStart,cx
  802.   EchoLastGrows:
  803.         mov    EchoLastDispl,ax
  804.         mov    EchoLastHi,dx
  805.         sub    ax,EchoStart+2
  806.         sbb    dx,EchoStart
  807.         mov    cl,4            ; divide # of ticks by 18.2
  808.         call    DblShr
  809.         mov    EchoElapsed+2,ax
  810.         mov    EchoElapsed,dx
  811.         mov    cl,3
  812.         call    DblShr
  813.         sub    EchoElapsed+2,ax
  814.         sbb    EchoElapsed,dx
  815.         mov    cl,5
  816.         call    DblShr
  817.         add    ax,EchoElapsed+2
  818.         adc    dx,EchoElapsed
  819.         mov     PutNumFiller,' '
  820.         mov    PutMinDigits,8
  821.         mov    di,offset MsgEchoTime
  822.         call    PutBigNum        ; elapsed time
  823. if DEBUG
  824.         mov    dx,280h+10h        ; assume WD card at IO 280h
  825.         xor    ax,ax
  826.         xor    cx,cx
  827.         cli
  828.         out    dx,al            ; set page 0
  829.         call    IoDelay
  830.         add    dx,0eh
  831.         in    al,dx            ; readnadclear bad CRC ctr
  832.         add    cx,ax
  833.         inc    dx
  834.         in    al,dx            ; readandclear lost frames ctr
  835.         sti
  836.         add    cx,ax
  837.         add    EchoDrop,cx        ; add out of bufs ctr
  838. endif ; DEBUG
  839.         mov    ax,EchoDrop
  840. if DEBUG eq 0
  841.         or    ax,ax
  842.         jz    EchoNoDrops
  843. endif ; DEBUG eq 0
  844.         mov    di,offset MsgEcho+18    ; place before diff
  845.         mov    PutMinDigits,1
  846.         call    PutNum            ; show dropped packets
  847.         mov    byte ptr [di],' '
  848.   EchoNoDrops:
  849.         xor    si,si
  850.         cmp    word ptr EchoErRepLo,si    ; any repeated pkts?
  851.         jz    EchoDispNoRep
  852.         inc    si
  853.   EchoDispNoRep:
  854.         test    GenFlags,NO_DROP_YET
  855.         jz    EchoDispDrop
  856.         mov    dx,word ptr EchoErLosLo    ; any lost packet events?
  857.         or    dx,word ptr EchoErTmp
  858.         jz    EchoDispNoLoss
  859.   EchoDispDrop:
  860.         lodsw                ; add    si,2
  861.   EchoDispNoLoss:
  862.         cmp    word ptr EchoErSeqLo,0    ; any out of sequence?
  863.         jz    EchoDispInSeq
  864.         lodsw                ; add    si,4
  865.         lodsw
  866.   EchoDispInSeq:
  867.         mov    al,EchoErrChar[si]
  868.         mov    MsgEchoSeqEr,al        ; put rep/seq/lost char
  869.  
  870. if 1
  871.         test    ArgFlags,AVOID_HDWR
  872.         jnz    EchoDspDos
  873.  
  874.         call    GetDspRow
  875.         mov    di,EchoDspRow
  876.         push    es
  877.         mov    es,EchoDispPar
  878.         mov    si,offset MsgEcho
  879.         mov    ax,es:[di]
  880.   EchoDspL1:
  881.         lodsb                ; display echo values
  882.         cmp    al,CR
  883.         je    EchoDspLe
  884.         stosw
  885.         jmp    EchoDspL1
  886.   EchoDspLe:
  887.         pop    es
  888.         jmp    short EchoDspRet
  889.   EchoDspDos:
  890. endif
  891.         mov    dx,offset MsgEcho    ; display echo values
  892.         call    DosPr$
  893.   EchoDspRet:
  894.         ret
  895.  
  896. GetDspRow    proc    near
  897.         test    GenFlags,GOT_DSP_ROW
  898.         jnz    EchoDspGotRow
  899.  
  900.         mov    ah,15            ; get screen mode
  901.         int    10h            ; call video BIOS
  902.         cmp    al,7            ; is it mono?
  903.         mov    dx,0b000h
  904.         jz    K1            ; yes
  905.         mov    dh,0b8h            ; no, set for color
  906.   K1:
  907.         mov    EchoDispPar,dx
  908.  
  909.         mov    ah,03h
  910.         xor    bx,bx
  911.         int    10h            ; get current row
  912. if HOPCHK
  913.         mov    HopTopRow,dh
  914.         push    es
  915.         mov    ax,0040h
  916.         mov    es,ax
  917.         mov    al,es:[0084h]        ; get dsp # of rows
  918.         mov    HopMaxRows,al
  919.         pop    es
  920. endif ; HOPCHK
  921.         mov    al,160
  922.         mul    dh
  923.         mov    EchoDspRow,ax
  924.         or    GenFlags,GOT_DSP_ROW
  925.   EchoDspGotRow:
  926.         ret
  927. GetDspRow    endp
  928.  
  929.   EchoDispPar    dw    0b800h
  930.   EchoDspRow    dw    (25-1)*160
  931. EchoDisplay    endp
  932.  
  933.  
  934. ;                  seq   lost   rep
  935. EchoErrChar    db    ' '    ;   0      0     0  space
  936.         db    0eh    ;   0      0     1  double music note
  937.         db    '.'    ;   0      1     0  dot
  938.         db    ':'    ;   0      1     1  colon (double dot)
  939.         db    "'"    ;   1      0     0  apostrophe
  940.         db    '"'    ;   1      0     1  quote (double apostrophe)
  941.         db    '!'    ;   1      1     0  exclamation mark
  942.         db    13h    ;   1      1     1  double exclamation mark
  943.  
  944.  
  945.  
  946. ;************************************************************************
  947. ;*        AdjTo16Bits
  948. ;************************************************************************
  949.  
  950.   Adjust:
  951.         shr    dx,1            ; shift 32 bits right
  952.         rcr    ax,1
  953.         shr    cx,1
  954.         rcr    bx,1
  955. AdjTo16Bits:
  956.         or    cx,cx            ; fits in 16 bits?
  957.         jnz    Adjust
  958.         or    dx,dx
  959.         jnz    Adjust
  960.         or    bx,bx            ; avoid div by zero
  961.         ret
  962.  
  963.  
  964.  
  965. ;************************************************************************
  966. ;*        DblShrX
  967. ;************************************************************************
  968.  
  969. DblShrX     proc    near
  970.         mov    cl,7
  971.         test    cs:ArgFlags,MICRO_100
  972.         jnz    DblShr
  973. DblShr10:
  974.         mov    cl,10
  975. DblShr:
  976.         xor    ch,ch
  977.   DblShrNext:
  978.         shr    dx,1            ; shift 32 bits right
  979.         rcr    ax,1
  980.         loop    DblShrNext
  981.         ret
  982. DblShrX     endp
  983.  
  984.  
  985.  
  986. ;************************************************************************
  987. ;*        DblShl
  988. ;************************************************************************
  989.  
  990. DblShl        proc    near
  991.         xor    ch,ch
  992.   DblShlNext:
  993.         shl    ax,1            ; shift 32 bits left
  994.         rcl    dx,1
  995.         loop    DblShlNext
  996.         ret
  997. DblShl        endp
  998.  
  999.  
  1000.  
  1001. ;************************************************************************
  1002. ;*        EchoCalc
  1003. ;************************************************************************
  1004.  
  1005. EchoCalc    proc    near
  1006.         assume    ds:nothing
  1007.         mov    si,[bx].dPktLen
  1008.  
  1009.         mov    bp,ds            ; save ds in bp until return
  1010.         mov    es,bp
  1011.         mov    ds,cs:MySegm
  1012.         assume    ds:code_s
  1013.  
  1014.         shl    si,1
  1015.         dec    word ptr EchoSizeVec[si] ; update error size table
  1016.  
  1017.         xor    cx,cx
  1018.         add    EchoRxLo,1        ; increment receive counters
  1019.         adc    EchoRxHi,cx
  1020.         mov    EchoFudge,cx
  1021.  
  1022.         mov    ax,EchoSeqRx
  1023.         inc    ax
  1024.         mov    si,es:[di].uUdpData+4
  1025.         cmp    ax,si            ; packet in sequence?
  1026.         jne    EchoCalcChkSeq
  1027.   EchoCalcInSeq:
  1028.         mov    EchoSeqRx,ax        ; "highest" sequence yet
  1029.   EchoCalcDelay:
  1030.         and    si,REPVECLEN-1
  1031.         mov    byte ptr EchoRepVec[si],1 ; mark got this seq #
  1032.  
  1033.         call    HardwareTicks        ; compute delay
  1034.         mov    EchoRxTime,dx
  1035.         sub    ax,es:[di].uUdpData
  1036.         sbb    dx,es:[di].uUdpData+2
  1037.         js    EchoCalcRet
  1038.         div    kTimerScale
  1039.         mov    EchoThis,ax        ; delay for this packet
  1040.  
  1041.         add    EchoAvgSum+2,ax
  1042.         adc    EchoAvgSum,cx
  1043.  
  1044.         cmp    ax,EchoMin
  1045.         jb    EchoSetMin
  1046.   EchoChkMax:
  1047.         cmp    ax,EchoMax
  1048.         ja    EchoSetMax
  1049.   EchoCalcRet:
  1050.         ret
  1051.  
  1052.   EchoSetMin:
  1053.         mov    EchoMin,ax        ; minimum delay
  1054.         jmp    short EchoChkMax
  1055.   EchoSetMax:
  1056.         mov    EchoMax,ax        ; maximum delay
  1057.         ret
  1058.  
  1059.   EchoCalcChkSeq:
  1060.         js    EchoCalcDrop        ; missing packet(s)?
  1061.  
  1062.         and    si,REPVECLEN-1
  1063.         cmp    byte ptr EchoRepVec[si],cl ; a repeated packet?
  1064.         jne    EchoCalcRepeat
  1065.  
  1066.         add    word ptr EchoErSeqLo,1    ; inc out of sequence cntr
  1067.         adc    word ptr EchoErSeqHi,cx
  1068.  
  1069.         mov    ax,word ptr EchoErTmp
  1070.         dec    ax
  1071.         js    EchoLostMain        ; use tmp or main counter?
  1072.         mov    word ptr EchoErTmp,ax
  1073.   EchoCalcNonSeq:
  1074.         and    GenFlags,not NO_ERR_YET    ; note err detected
  1075.         jmp    short EchoCalcDelay
  1076.  
  1077.   EchoLostMain:
  1078.         mov    ax,word ptr EchoErLosLo
  1079.         or    ax,word ptr EchoErLosHi    ; too much out of seq repeats
  1080.         jz    EchoCalcRepeat        ;   have occured?
  1081.  
  1082.         sub    word ptr EchoErLosLo,1    ; after all: wasn't lost
  1083.         sbb    word ptr EchoErLosHi,cx
  1084.         jmp    short EchoCalcNonSeq
  1085.  
  1086.   EchoCalcRepeat:
  1087.         add    word ptr EchoErRepLo,1    ; inc repeated pkts cntr
  1088.         adc    word ptr EchoErRepHi,cx
  1089.         jmp    short EchoCalcNonSeq
  1090.   
  1091.   EchoCalcDrop:
  1092.         push    si
  1093.         mov    EchoSeqRx,si        ; "highest" sequence yet
  1094.         sub    si,ax            ; # of pkts missing
  1095.         mov    dx,word ptr EchoErTmp
  1096.         or    dx,dx            ; old or new occurance?
  1097.         jnz    EchoDropMore
  1098.         mov    cx,SavedTicks
  1099.         mov    EchoErTmpTime,cx    ; -new, initialize timer
  1100.         xor    cx,cx
  1101.   EchoDropMore:
  1102.         add    dx,si
  1103.         mov    word ptr EchoErTmp,dx    ; add to temporary drops
  1104.         xchg    ax,si
  1105.   EchoDropL:
  1106.         and    si,REPVECLEN-1
  1107.         mov    byte ptr EchoRepVec[si],cl ; mark as not received
  1108.         inc    si
  1109.         dec    ax
  1110.         jnz    EchoDropL        ; repeat for all "lost" pkts
  1111.  
  1112.         pop    si
  1113.         jmp    EchoCalcNonSeq
  1114. EchoCalc    endp
  1115.  
  1116.  
  1117.  
  1118. ;========================================================================
  1119. ;        endinclude
  1120.