Skip to content

Commit f73e6c0

Browse files
authored
Fix double incrementing of the packet number (#117)
This commit fixes an issue where the packet number field of the packet header was incorrectly incremented twice. This resulted in the packet number fo a single LOGIN7 packet being 2, rather 1. This violated the TDS protocol definition. Furthermore, firewall rules that restrict traffic based on the TDS protocol filtered out such violating TDS packets. Also, it resulted in tools such as Wireshark not being able to properly parse out the TDS header.
1 parent 11e2f91 commit f73e6c0

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

lib/tds/messages.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ defmodule Tds.Messages do
601601
end
602602

603603
def encode_packets(type, data, id) do
604-
header = encode_header(type, data, id: id + 1, status: 1)
604+
header = encode_header(type, data, id: id, status: 1)
605605
[header <> data]
606606
end
607607

test/messages_test.exs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
defmodule MessagesTest do
2+
use ExUnit.Case, async: true
3+
4+
describe "encode_packets" do
5+
test "data length < 4088 is encoded into one packet" do
6+
assert [<<
7+
# type
8+
0x10,
9+
# status
10+
0x1,
11+
# length
12+
0x0,
13+
0x9,
14+
# channel
15+
0x0,
16+
0x0,
17+
# packet number
18+
0x1,
19+
# window
20+
0x0,
21+
# data
22+
0xFF
23+
>>] == Tds.Messages.encode_packets(0x10, <<0xFF>>)
24+
end
25+
26+
test "data length == 4087 is encoded into one packet" do
27+
data = :binary.copy(<<0xFF>>, 4087)
28+
assert [<<
29+
# type
30+
0x10,
31+
# status
32+
0x1,
33+
# length
34+
0x0F,
35+
0xFF,
36+
# channel
37+
0x0,
38+
0x0,
39+
# packet number
40+
0x1,
41+
# window
42+
0x0,
43+
data::binary
44+
>>] == Tds.Messages.encode_packets(0x10, data)
45+
end
46+
47+
test "data length == 4088 is encoded into one packet" do
48+
data = :binary.copy(<<0xFF>>, 4088)
49+
assert [[<<
50+
# type
51+
0x10,
52+
# status
53+
0x1,
54+
# length
55+
0x10,
56+
0x00,
57+
# channel
58+
0x0,
59+
0x0,
60+
# packet number
61+
0x1,
62+
# window
63+
0x0
64+
>>, <<
65+
# data
66+
data::binary
67+
>>]] == Tds.Messages.encode_packets(0x10, data)
68+
end
69+
70+
test "data length == 4089 is encoded into two packets " do
71+
part1 = :binary.copy(<<0xFF>>, 4088)
72+
part2 = :binary.copy(<<0xFF>>, 1)
73+
data = part1 <> part2
74+
assert [[<<
75+
# type
76+
0x10,
77+
# status
78+
0x0,
79+
# length
80+
0x10,
81+
0x00,
82+
# channel
83+
0x0,
84+
0x0,
85+
# packet number
86+
0x1,
87+
# window
88+
0x0 >>,
89+
<<
90+
# data
91+
part1::binary
92+
>>],
93+
<<
94+
# type
95+
0x10,
96+
# status
97+
0x1,
98+
# length
99+
0x0,
100+
0x9,
101+
# channel
102+
0x0,
103+
0x0,
104+
# packet number
105+
0x2,
106+
# window
107+
0x0,
108+
# data
109+
0xFF
110+
>>] == Tds.Messages.encode_packets(0x10, data)
111+
end
112+
end
113+
end

0 commit comments

Comments
 (0)