@@ -1007,7 +1007,7 @@ make-TLS-ctx: does [ context [
1007
1007
bin: binary 64 ;temporary binary
1008
1008
1009
1009
port-data: make binary! 32000 ;this holds received decrypted application data
1010
-
1010
+ rest: make binary! 8 ;packet may not e fully processed, this value is used to keep temporary leftover
1011
1011
reading?: false ;if client is reading or writing data
1012
1012
;server?: false ;always FALSE now as we have just a client
1013
1013
protocol: none ;current protocol state. One of: [HANDSHAKE APPLICATION ALERT]
@@ -1091,15 +1091,15 @@ TLS-read-data: function [
1091
1091
] [
1092
1092
;log-more ["read-data:^[[1m" length? port-data "^[[22mbytes"]
1093
1093
1094
- ;probe copy/part ctx/in/buffer 10
1094
+ inp: ctx/in
1095
1095
1096
- binary/write ctx/in port-data ;- fills input buffer with received data
1096
+ binary/write inp ctx/rest ;- possible leftover from previous packet
1097
+ binary/write inp port-data ;- fills input buffer with received data
1097
1098
clear port-data
1099
+ clear ctx/rest
1098
1100
1099
1101
ctx/reading?: true
1100
1102
1101
- inp: ctx/in
1102
-
1103
1103
while [ctx/reading? and ((available: length? inp/buffer ) >= 5 )][
1104
1104
;?? available
1105
1105
log-debug ["Data starts: " mold copy/part inp/buffer 10 ]
@@ -1110,7 +1110,16 @@ TLS-read-data: function [
1110
1110
version: UI16
1111
1111
len: UI16
1112
1112
]
1113
- log-debug ["fragment type: ^[ [1m" type "^[ [22mver:^[ [1m" version "^[ [22mbytes:^[ [1m" len "^[ [22mbytes" ]
1113
+ log-debug ["fragment type: ^[ [1m" type "^[ [22mver:^[ [1m" version *Protocol-version/name version "^[ [22mbytes:^[ [1m" len "^[ [22mbytes" ]
1114
+
1115
+ if all [
1116
+ ctx/server-version
1117
+ version <> ctx/server-version
1118
+ ][
1119
+ log-error ["Version mismatch:^[ [22m" version "<>" ctx/server-version ]
1120
+ ctx/critical-error: *Alert/Internal_error
1121
+ return false
1122
+ ]
1114
1123
1115
1124
if available < len [
1116
1125
;probe inp/buffer
@@ -1217,6 +1226,10 @@ TLS-read-data: function [
1217
1226
1218
1227
;?? ctx/state
1219
1228
log-debug "continue reading..."
1229
+ unless empty? ctx/in/buffer [
1230
+ ; keeping rest of unprocessed data for later use
1231
+ ctx/rest: copy ctx/in/buffer
1232
+ ]
1220
1233
return true
1221
1234
]
1222
1235
@@ -1538,25 +1551,23 @@ TLS-awake: function [event [event!]] [
1538
1551
TLS-error error-id
1539
1552
]
1540
1553
log-debug ["Read complete?" complete?]
1541
- if complete? [
1542
- ;? TLS-Port/state
1543
- ;? TLS-port/state/connection
1544
- TLS-port/data: TLS-port/state/port-data
1545
- binary/init TLS-port/state/in none ; resets input buffer
1554
+ unless complete? [
1555
+ read port
1556
+ return false
1546
1557
]
1558
+ TLS-port/data: TLS-port/state/port-data
1559
+ binary/init TLS-port/state/in none ; resets input buffer
1547
1560
either 'APPLICATION = TLS-port/state/protocol [
1548
1561
send-event 'read TLS-port
1549
- ] [
1550
- read port
1551
- ]
1552
- return complete?
1562
+ ][ read port ]
1563
+ return true
1553
1564
]
1554
1565
close [
1555
1566
log-info "CLOSE"
1556
1567
send-event 'close TLS-port
1557
1568
return true
1558
1569
]
1559
- ] [
1570
+ ][
1560
1571
;try [close port/state/connection]
1561
1572
close port
1562
1573
do make error! rejoin ["Unexpected TLS event: " event/type ]
0 commit comments