mirror of
https://github.com/A2osX/A2osX.git
synced 2024-09-10 22:54:59 +00:00
638 lines
11 KiB
Plaintext
638 lines
11 KiB
Plaintext
PR#3
|
||
PREFIX /A2OSX.SRC
|
||
LOMEM $900
|
||
INC 1
|
||
AUTO 6
|
||
.LIST OFF
|
||
*--------------------------------------
|
||
TCP.IN jsr IP.FillSKT.TemplateSrcDst
|
||
|
||
ldy #S.TCP.SRCPORT
|
||
lda (ZPFrameInPtr),y
|
||
sta SKT.Template+S.SOCKET.DST.PORT+1
|
||
iny
|
||
lda (ZPFrameInPtr),y
|
||
sta SKT.Template+S.SOCKET.DST.PORT
|
||
|
||
ldy #S.TCP.DSTPORT
|
||
lda (ZPFrameInPtr),y
|
||
sta SKT.Template+S.SOCKET.SRC.PORT+1
|
||
iny
|
||
lda (ZPFrameInPtr),y
|
||
sta SKT.Template+S.SOCKET.SRC.PORT
|
||
|
||
lda hSocketTable
|
||
>SYSCALL GetMemPtrA
|
||
>STYA ZPPtrSKT
|
||
|
||
ldx #0
|
||
|
||
.3 lda (ZPPtrSKT)
|
||
beq .7
|
||
cmp #S.SOCKET.SOCK.STREAM
|
||
bne .7
|
||
|
||
ldy #S.SOCKET.SRC.ADDR
|
||
|
||
.4 lda SKT.Template,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 no, go check if Dst Match
|
||
|
||
ldy #S.TCP.OPTIONS yes, only accept SYN packet
|
||
lda (ZPFrameInPtr),y
|
||
|
||
and #S.TCP.OPTIONS.SYN
|
||
beq .7 maybe a regular frame for a regular socket
|
||
|
||
jmp TCP.IN.JMP.LISTEN
|
||
|
||
.5 ldy #S.SOCKET.DST.ADDR
|
||
|
||
.6 lda SKT.Template,y regular socket, check remote ADDR/PORT
|
||
cmp (ZPPtrSKT),y
|
||
bne .7
|
||
iny
|
||
cpy #S.SOCKET.DST.PORT+2
|
||
bne .6
|
||
|
||
stx hSocket
|
||
|
||
ldy #S.TCP.OPTIONS
|
||
lda (ZPFrameInPtr),y
|
||
and #S.TCP.OPTIONS.RST RST ? go Kill this socket.
|
||
bne TCP.IN.RST
|
||
|
||
ldy #S.SOCKET.TCP.STATUS
|
||
lda (ZPPtrSKT),y
|
||
asl
|
||
tax
|
||
jmp (L.TCP.IN.JMP,x)
|
||
|
||
.7 lda ZPPtrSKT
|
||
clc
|
||
adc #S.SOCKET
|
||
sta ZPPtrSKT
|
||
bcc .8
|
||
inc ZPPtrSKT+1
|
||
|
||
.8 inx
|
||
cpx #K.SKTTABLE.SIZE
|
||
bne .3
|
||
|
||
.9 clc Discard frame
|
||
rts
|
||
*--------------------------------------
|
||
TCP.IN.RST txa
|
||
jsr SKT.CloseA
|
||
clc
|
||
rts
|
||
*--------------------------------------
|
||
TCP.IN.JMP.CLOSED
|
||
clc
|
||
rts
|
||
*--------------------------------------
|
||
TCP.IN.JMP.LISTEN
|
||
lda #S.SOCKET.SOCK.STREAM
|
||
sta SKT.Template+S.SOCKET.SOCK
|
||
|
||
stz SKT.Template+S.SOCKET.SO
|
||
|
||
>PUSHW L.SKT.Template
|
||
|
||
jsr SKT.New
|
||
bcs .9
|
||
|
||
phy
|
||
pha
|
||
txa
|
||
|
||
jsr SKT.AddToQueueA
|
||
|
||
pla
|
||
sta ZPPtrSKT+1
|
||
pla
|
||
sta ZPPtrSKT
|
||
|
||
bcc .3 Queued successfully ?
|
||
|
||
lda #0 error
|
||
sta (ZPPtrSKT) clear this socket
|
||
lda #ERR.SKT.OOS
|
||
.9 rts
|
||
|
||
.3 jsr ARP.AddFromFrameInPtr
|
||
|
||
ldy #S.TCP.SEQ.NUMBER+3
|
||
ldx #3
|
||
|
||
sec
|
||
|
||
.1 lda (ZPFrameInPtr),y Set ACK=SEQ+1 for new socket...
|
||
adc #0
|
||
sta TmpDWord,x
|
||
dey
|
||
dex
|
||
bpl .1
|
||
|
||
ldx #3
|
||
ldy #S.SOCKET.SQ.ACKNUM+3
|
||
|
||
.4 lda TmpDWord,x Update new socket with ACK number
|
||
sta (ZPPtrSKT),y
|
||
dey
|
||
dex
|
||
bpl .4
|
||
|
||
ldy #S.SOCKET.TCP.STATUS
|
||
lda #S.SOCKET.TCP.STATUS.SYNRCVD
|
||
sta (ZPPtrSKT),y
|
||
|
||
ldy #S.SOCKET.TCP.OPTIONS
|
||
lda #S.TCP.OPTIONS.SYN+S.TCP.OPTIONS.ACK
|
||
sta (ZPPtrSKT),y
|
||
|
||
jsr TCP.Out
|
||
bcs .9
|
||
|
||
ldy #S.SOCKET.TCP.STATUS
|
||
lda #S.SOCKET.TCP.STATUS.SYNSENT
|
||
sta (ZPPtrSKT),y
|
||
|
||
|
||
clc
|
||
rts
|
||
*--------------------------------------
|
||
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.TCP.STATUS
|
||
lda #S.SOCKET.TCP.STATUS.ESTBLSH
|
||
sta (ZPPtrSKT),y
|
||
|
||
|
||
.8 clc
|
||
rts
|
||
*--------------------------------------
|
||
TCP.IN.JMP.ESTBLSH
|
||
ldy #S.TCP.OPTIONS
|
||
lda (ZPFrameInPtr),y
|
||
bit #S.TCP.OPTIONS.ACK
|
||
beq .2
|
||
|
||
ldy #S.TCP.ACK.NUMBER+3 Get new ACK number from FrameIn...
|
||
ldx #3
|
||
|
||
.1 lda (ZPFrameInPtr),y
|
||
sta TmpDWord,x
|
||
dey
|
||
dex
|
||
bpl .1
|
||
|
||
ldy #S.SOCKET.SQ.ACKNUM Substract old Ack from socket
|
||
sec
|
||
lda (ZPPtrSKT),y
|
||
sbc TmpDWord
|
||
pha save result LO for FreeData
|
||
|
||
lda TmpDWord
|
||
sta (ZPPtrSKT),y and store new ACK in skt
|
||
|
||
iny
|
||
lda (ZPPtrSKT),y
|
||
sbc TmpDWord+1
|
||
|
||
tax save result HI for FreeData
|
||
|
||
lda TmpDWord+1 we stop here, will never be more than K.TCP.WSIZE
|
||
sta (ZPPtrSKT),y
|
||
|
||
iny
|
||
lda TmpDWord+2
|
||
sta (ZPPtrSKT),y
|
||
|
||
iny
|
||
lda TmpDWord+3
|
||
sta (ZPPtrSKT),y
|
||
|
||
pla
|
||
jsr SKT.FreeDataFromSktOutAX
|
||
*----------
|
||
.2 lda ZPDataInLen
|
||
ora ZPDataInLen+1
|
||
beq .8 No data in this incoming frame...
|
||
|
||
ldy #S.TCP.SEQ.NUMBER+3
|
||
ldx #3
|
||
|
||
.3 lda (ZPFrameInPtr),y
|
||
sta TmpDWord,x
|
||
dey
|
||
dex
|
||
bpl .3
|
||
|
||
jsr TCP.SetDataInPtrAndLen
|
||
|
||
jsr SKT.AddDataToSktIn
|
||
|
||
|
||
.8 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.OUT ldy #S.SOCKET.TCP.OPTIONS
|
||
lda (ZPPtrSKT),y
|
||
bit #S.TCP.OPTIONS.SYN
|
||
beq TCP.OUT.ESTBLSH
|
||
|
||
>LDYAI 0
|
||
jsr TCP.NewFrame
|
||
bcs .9
|
||
|
||
ldy #S.TCP.OPTIONS
|
||
lda #S.TCP.OPTIONS.SYN+S.TCP.OPTIONS.ACK
|
||
sta (ZPFrameOutPtr),y
|
||
|
||
jsr TCP.SendFrame
|
||
bcs .9
|
||
|
||
ldy #S.TCP.OPTIONS
|
||
lda #0
|
||
sta (ZPFrameOutPtr),y
|
||
|
||
ldy #S.SOCKET.TCP.STATUS
|
||
lda #S.SOCKET.TCP.STATUS.SYNSENT
|
||
sta (ZPFrameOutPtr),y
|
||
|
||
clc
|
||
.9 rts
|
||
*--------------------------------------
|
||
TCP.OUT.ESTBLSH ldy #S.SOCKET.SQ.OUTUSED
|
||
lda (ZPPtrSKT),y
|
||
|
||
pha
|
||
|
||
iny
|
||
lda (ZPPtrSKT),y
|
||
ply
|
||
bne .1
|
||
|
||
tax
|
||
beq .2 Y,A=0 : nothing to send....
|
||
|
||
.1 cpy #K.TCP.MSS
|
||
pha
|
||
sbc /K.TCP.MSS OUTDATA > MSS ?
|
||
pla
|
||
bcs .2 no....get data len
|
||
|
||
>LDYAI K.TCP.MSS yes get only MSS
|
||
|
||
.2 >STYA TmpOffset
|
||
|
||
jsr TCP.NewFrame
|
||
bcs .9
|
||
|
||
jsr SKT.GetDataFromSktOut
|
||
|
||
ldy #S.SOCKET.SQ.SEQNUM compute new SEQ.NUM
|
||
lda (ZPPtrSKT),y
|
||
clc
|
||
adc TmpOffset
|
||
sta TmpDWord
|
||
|
||
iny
|
||
lda (ZPPtrSKT),y
|
||
adc TmpOffset+1
|
||
sta TmpDWord+1
|
||
|
||
iny
|
||
lda (ZPPtrSKT),y
|
||
adc #0
|
||
sta TmpDWord+2
|
||
|
||
iny
|
||
lda (ZPPtrSKT),y
|
||
adc #0
|
||
sta TmpDWord+3
|
||
|
||
ldy #S.TCP.SEQ.NUMBER+3
|
||
ldx #3
|
||
|
||
.3 lda TmpDWord,x
|
||
sta (ZPFrameOutPtr),y
|
||
dey
|
||
dex
|
||
bpl .3
|
||
|
||
ldy #S.SOCKET.TCP.OPTIONS
|
||
lda (ZPPtrSKT),y
|
||
bit #S.TCP.OPTIONS.ACK
|
||
beq .7
|
||
|
||
ldy #S.TCP.OPTIONS
|
||
lda (ZPFrameOutPtr),y
|
||
ora #S.TCP.OPTIONS.ACK
|
||
sta (ZPFrameOutPtr),y
|
||
|
||
.7 jsr TCP.SendFrame
|
||
bcs .9
|
||
|
||
ldy #S.SOCKET.TCP.OPTIONS
|
||
lda (ZPPtrSKT),y
|
||
and #$ff^S.TCP.OPTIONS.ACK
|
||
sta (ZPPtrSKT),y
|
||
|
||
ldx #3
|
||
ldy #S.SOCKET.SQ.SEQNUM+3
|
||
|
||
.71 lda TmpDWord,x
|
||
sta (ZPPtrSKT),y
|
||
dey
|
||
dex
|
||
bpl .71
|
||
|
||
.8 clc
|
||
.9 rts
|
||
*--------------------------------------
|
||
TCP.GetSktAckNum
|
||
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.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
|
||
*--------------------------------------
|
||
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.NewFrame ldx #S.IP.PROTOCOL.TCP
|
||
jsr FRM.NewIP
|
||
bcs .9
|
||
|
||
jsr SKT.SetFrameOutDstIP
|
||
|
||
ldy #S.SOCKET.SRC.PORT
|
||
lda (ZPPtrSKT),y
|
||
tax
|
||
iny
|
||
lda (ZPPtrSKT),y
|
||
ldy #S.TCP.SRCPORT
|
||
sta (ZPFrameOutPtr),y
|
||
iny
|
||
txa
|
||
sta (ZPFrameOutPtr),y
|
||
|
||
ldy #S.SOCKET.DST.PORT
|
||
lda (ZPPtrSKT),y
|
||
tax
|
||
iny
|
||
lda (ZPPtrSKT),y
|
||
ldy #S.TCP.DSTPORT
|
||
sta (ZPFrameOutPtr),y
|
||
iny
|
||
txa
|
||
sta (ZPFrameOutPtr),y
|
||
|
||
ldy #S.TCP.DATAOFFSET
|
||
lda #$50 Header size = 5 DWORDS
|
||
sta (ZPFrameOutPtr),y
|
||
|
||
ldy #S.SOCKET.SQ.INFREE
|
||
lda (ZPPtrSKT),y
|
||
pha
|
||
iny
|
||
lda (ZPPtrSKT),y
|
||
|
||
ldy #S.TCP.WINDOW
|
||
sta (ZPFrameOutPtr),y
|
||
iny
|
||
pla
|
||
sta (ZPFrameOutPtr),y
|
||
|
||
|
||
clc
|
||
.9 rts
|
||
*--------------------------------------
|
||
TCP.SendFrame ldy #S.SOCKET.SQ.SEQNUM+7
|
||
ldx #7
|
||
|
||
.1 lda (ZPPtrSKT),y
|
||
pha
|
||
dey
|
||
dex
|
||
bpl .1
|
||
|
||
ldy #S.TCP.SEQ.NUMBER
|
||
|
||
.2 pla
|
||
sta (ZPFrameOutPtr),y
|
||
iny
|
||
cpy #S.TCP.SEQ.NUMBER+8
|
||
bne .2
|
||
|
||
jmp FRM.SendIP
|
||
*--------------------------------------
|
||
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
|