A2osX/LIB/LIBTCPIP.S.TCP.txt

524 lines
9.2 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

PR#3
PREFIX /A2OSX.SRC
LOMEM $A00
INC 1
AUTO 6
.LIST OFF
*--------------------------------------
TCP.IN ldy #S.IP.SRC+3
ldx #3
.1 lda (ZPFrameInPtr),y
sta TmpSocket+S.SOCKET.DST.ADDR,x
dey
dex
bpl .1
ldy #S.TCP.SRCPORT
lda (ZPFrameInPtr),y
sta TmpSocket+S.SOCKET.DST.PORT+1
iny
lda (ZPFrameInPtr),y
sta TmpSocket+S.SOCKET.DST.PORT
ldy #S.IP.DST+3
ldx #3
.2 lda (ZPFrameInPtr),y
sta TmpSocket+S.SOCKET.SRC.ADDR,x
dey
dex
bpl .2
ldy #S.TCP.DSTPORT
lda (ZPFrameInPtr),y
sta TmpSocket+S.SOCKET.SRC.PORT+1
iny
lda (ZPFrameInPtr),y
sta TmpSocket+S.SOCKET.SRC.PORT
>LDYA L.SKT.TABLE
>STYA ZPPtrSKT
ldx #0
.3 lda (ZPPtrSKT)
beq .7
cmp #S.SOCKET.SOCK.STREAM
bne .7
ldy #S.SOCKET.SRC.ADDR
.4 lda TmpSocket,y
cmp (ZPPtrSKT),y
bne .7 Not for this socket...
iny
cpy #S.SOCKET.SRC.PORT+2
bne .4
ldy #S.SOCKET.SO
lda (ZPPtrSKT),y
and #S.SOCKET.SO.ACCEPTCONN Listening socket?
beq .5
ldy #S.TCP.OPTIONS yes, only accept SYN packet
lda (ZPFrameOutPtr),y
cmp #S.TCP.OPTIONS.SYN
bne .7 not for this listening socket
jmp TCP.IN.LISTEN.NEW
.5 ldy #S.SOCKET.DST.ADDR
.6 lda TmpSocket,y regular socket, check remote ADDR/PORT
cmp (ZPPtrSKT),y
bne .7
iny
cpy #S.SOCKET.DST.PORT+2
bne .6
jmp TCP.IN.REGULAR
.7 lda ZPPtrSKT
clc
adc #S.SOCKET
sta ZPPtrSKT
bcc .8
inc ZPPtrSKT+1
.8 inx
cpx #K.SKTTABLE.SIZE
bne .3
clc Discard frame
rts
*--------------------------------------
TCP.IN.LISTEN.NEW
lda #S.SOCKET.SOCK.STREAM
sta TmpSocket
stz TmpSocket+S.SOCKET.SO
ldy #S.TCP.SEQ.NUMBER+3
ldx #3
sec
.1 lda (ZPFrameInPtr),y
adc #0
sta TmpSocket+S.SOCKET.SQ.ACKNUM,x
dey
dex
bpl .1
ldx #3
.2 lda A2osX.TIMER16,x
sta TmpSocket+S.SOCKET.SQ.SEQNUM,x
dex
bpl .2
>PUSHW L.TmpSocket
jsr SKT.NEW
bcs .9
phy
pha
txa
jsr SKT.AddToQueueA
pla
sta ZPPtrSKT+1
pla
sta ZPPtrSKT
bcc .3
lda #0 error
sta (ZPPtrSKT) clear this socket
lda #ERR.SKT.OOS
.9 rts
.3 ldy #S.SOCKET.STATUS
lda #S.SOCKET.STATUS.SYNRCVD
sta (ZPPtrSKT),y
jmp TCP.SendSYNACK
*--------------------------------------
TCP.IN.REGULAR stx hSocket
ldy #S.SOCKET.STATUS
lda (ZPPtrSKT),y
asl
tax
jmp (L.TCP.IN.JMP,x)
*--------------------------------------
TCP.IN.JMP.CLOSED
TCP.IN.JMP.LISTEN
TCP.IN.JMP.SYNSENT
clc
rts
*--------------------------------------
TCP.IN.JMP.SYNRCVD
ldy #S.TCP.OPTIONS only accept ACK packet
lda (ZPFrameInPtr),y
cmp #S.TCP.OPTIONS.ACK
bne .8
ldx #3
ldy #S.SOCKET.SQ.SEQNUM+3
sec
.10 lda (ZPPtrSKT),y
adc #0
sta TmpDWord,x
dey
dex
bpl .10
ldx #3
ldy #S.TCP.ACK.NUMBER+3
.11 lda (ZPFrameInPtr),y
cmp TmpDWord,x
bne .8
dey
dex
bpl .11
ldx #3
ldy #S.SOCKET.SQ.SEQNUM+3
.12 lda TmpDWord,x
sta (ZPPtrSKT),y
dey
dex
bpl .12
ldy #S.SOCKET.STATUS
lda #S.SOCKET.STATUS.ESTBLSH
sta (ZPPtrSKT),y
>PUSHW K.TCP.WSIZE
>PUSHBI 0
>SYSCALL GetMem
bcs *
txa
ldy #S.SOCKET.STREAM.hInMem
sta (ZPPtrSKT),y
>PUSHW K.TCP.WSIZE
>PUSHBI 0
>SYSCALL GetMem
bcs *
txa
ldy #S.SOCKET.STREAM.hOutMem
sta (ZPPtrSKT),y
.8 clc
rts
*--------------------------------------
TCP.IN.JMP.ESTBLSH
ldy #S.TCP.OPTIONS
lda (ZPFrameInPtr),y
cmp #S.TCP.OPTIONS.ACK
bne .1
jsr TCP.UpdateSktAckNum
.1 jsr TCP.SetDataInPtrAndLen
jsr TCP.AddDataToSkt
clc
rts
*--------------------------------------
TCP.IN.JMP.CLWAIT
TCP.IN.JMP.LASTACK
TCP.IN.JMP.FINWT1
TCP.IN.JMP.FINWT2
TCP.IN.JMP.CLOSING
TCP.IN.JMP.TIMEWT
clc
rts
*--------------------------------------
TCP.UpdateSktAckNum
ldy #S.SOCKET.SQ.ACKNUM+3
lda (ZPPtrSKT),y
clc
adc ZPDataInLen
sta (ZPPtrSKT),y
dey
lda (ZPPtrSKT),y
adc ZPDataInLen+1
sta (ZPPtrSKT),y
dey
lda (ZPPtrSKT),y
adc #0
sta (ZPPtrSKT),y
dey
lda (ZPPtrSKT),y
adc #0
sta (ZPPtrSKT),y
rts
*--------------------------------------
ldy #S.TCP.ACK.NUMBER
ldx #4
.1 lda (ZPFrameInPtr),y
pha
iny
dex
bne .1
ldy #S.SOCKET.SQ.ACKNUM+3
ldx #4
.2 pla
sta (ZPPtrSKT),y
dey
dex
bne .2
rts
*--------------------------------------
TCP.SetDataInPtrAndLen
ldy #S.TCP.DATAOFFSET
lda (ZPFrameInPtr),y
and #$F0
lsr
lsr
adc #S.TCP
sta TmpOffset
lda #0
* clc
adc /S.TCP
sta TmpOffset+1 TmpOffset=Data Offset in Frame
lda ZPFrameInPtr
* clc
adc TmpOffset
sta ZPDataInPtr
lda ZPFrameInPtr+1
adc #0
sta ZPDataInPtr+1
lda ZPFrameInLen
sec
sbc TmpOffset
sta ZPDataInLen
lda ZPFrameInLen+1
sbc TmpOffset+1
sta ZPDataInLen+1
rts
*--------------------------------------
TCP.AddDataToSkt
ldy #S.SOCKET.STREAM.hInMem
lda (ZPPtrSKT),y
>SYSCALL GetMemPtrA
>STYA ZPTmpPtr1 Make ZPTmpPtr1=buffer base
ldy #S.SOCKET.SQ.INHEAD
lda (ZPPtrSKT),y
tax save LO for compare later
clc
adc ZPTmpPtr1
sta ZPTmpPtr2 Make ZPTmpPtr2=actual head
iny
lda (ZPPtrSKT),y
pha save HI for compare later
adc ZPTmpPtr1+1
sta ZPTmpPtr2+1
lda ZPDataInLen
eor #$ff
sta ZPTmpPtr3 Make ZPTmpPtr3=!count
lda ZPDataInLen+1
eor #$ff
sta ZPTmpPtr3+1
ldy #0
.1 inc ZPTmpPtr3
bne .2
inc ZPTmpPtr3+1
beq .9
.2 lda (ZPDataInPtr),y
sta (ZPTmpPtr2)
iny
bne .3
inc ZPDataInPtr+1
.3 inc ZPTmpPtr2
bne .4
inc ZPTmpPtr2+1
.4 pla
inx
bne .5
inc
.5 pha
cpx #K.TCP.WSIZE
sbc /K.TCP.WSIZE
bcc .1
.6 inc ZPTmpPtr3
bne .7
inc ZPTmpPtr3+1
beq .9
.7 lda (ZPDataInPtr),y
sta (ZPTmpPtr1)
iny
bne .8
inc ZPDataInPtr+1
.8 inc ZPTmpPtr1
bne .6
inc ZPTmpPtr1+1
bra .6
.9 pla
rts
*--------------------------------------
TCP.SendACK lda #S.TCP.OPTIONS.ACK
bra TCP.SendMSG
TCP.SendSYNACK lda #S.TCP.OPTIONS.SYN+S.TCP.OPTIONS.ACK
TCP.SendMSG sta TCP.MSG+S.TCP.OPTIONS
ldy #S.SOCKET.SRC.ADDR+3
ldx #3
.1 lda (ZPPtrSKT),y
sta TCP.MSG+S.IP.SRC,x
dey
dex
bpl .1
ldy #S.SOCKET.SRC.PORT
lda (ZPPtrSKT),y
sta TCP.MSG+S.TCP.SRCPORT+1
iny
lda (ZPPtrSKT),y
sta TCP.MSG+S.TCP.SRCPORT
ldy #S.SOCKET.DST.ADDR+3
ldx #3
.2 lda (ZPPtrSKT),y
sta TCP.MSG+S.IP.DST,x
dey
dex
bpl .2
ldy #S.SOCKET.DST.PORT
lda (ZPPtrSKT),y
sta TCP.MSG+S.TCP.DSTPORT+1
iny
lda (ZPPtrSKT),y
sta TCP.MSG+S.TCP.DSTPORT
ldy #S.SOCKET.SQ.SEQNUM+7
ldx #7
.3 lda (ZPPtrSKT),y
sta TCP.MSG+S.TCP.SEQ.NUMBER,x
dey
dex
bpl .3
>PUSHW L.TCP.MSG
* jsr FRM.SendTCP
clc
rts
*--------------------------------------
TCP.ComputeChecksum
lda #0 RESET.TCP.CHECKSUM
ldy #S.TCP.CHECKSUM
sta (ZPFrameOutPtr),y
iny
sta (ZPFrameOutPtr),y
>LDYA ZPFrameOutPtr
>STYA ZPTmpPtr1
lda ZPFrameOutLen
sec
sbc #S.IP-2
sta ZPDataInLen
lda ZPFrameOutLen+1
sbc /S.IP-2
sta ZPDataInLen+1
clc
* lda ZPDataInLen+1
* adc #0 RESERVED (all zero)
sta IP.CHECKSUM
lda ZPDataInLen
adc #S.IP.PROTOCOL.TCP
sta IP.CHECKSUM+1
ldy #S.IP.SRC
ldx #4 4 words for SRC & DST IP
.10 lda (ZPFrameOutPtr),y
adc IP.CHECKSUM
sta IP.CHECKSUM
iny
lda (ZPFrameOutPtr),y
adc IP.CHECKSUM+1
sta IP.CHECKSUM+1
iny
dex
bne .10
ldy #S.IP
.1 jsr DecDataInLen
beq .8
lda (ZPTmpPtr1),y
adc IP.CHECKSUM
sta IP.CHECKSUM
iny
bne .20
inc ZPTmpPtr1+1
.20 jsr DecDataInLen
bne .2
lda #0
bra .21
.2 lda (ZPTmpPtr1),y
.21 adc IP.CHECKSUM+1
sta IP.CHECKSUM+1
iny
bne .1
inc ZPTmpPtr1+1
bra .1
.8 ldy #S.TCP.CHECKSUM
lda IP.CHECKSUM
adc #0 Don't forget to add last carry!!!
eor #$FF
sta (ZPFrameOutPtr),y
iny
lda IP.CHECKSUM+1
adc #0 Don't forget to add last carry!!!
eor #$FF
sta (ZPFrameOutPtr),y
rts
*--------------------------------------
MAN
SAVE LIB/LIBTCPIP.S.TCP
LOAD LIB/LIBTCPIP.S
ASM