Skip to content

Commit 4d20131

Browse files
committed
Fix incorrect HeaderV2_IPV4 declaration, and various tweaks
1 parent 8cd25e5 commit 4d20131

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

proxyproto.nimble

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "0.1.2"
3+
version = "0.1.3"
44
author = "Huy Doan"
55
description = "PROXY Protocol enabler for aged programs"
66
license = "MIT"

src/proxyproto.nim

+20-31
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ from dynlib import LibHandle, symAddr
66

77
const
88
v1sig = "PROXY".cstring
9-
v2sig = "\c\n\c\n\x00\c\nQUIT\n"
10-
11-
proc c_memcmp(a, b: pointer, size: csize): cint {.importc: "memcmp", header: "<string.h>", noSideEffect.}
9+
# v2 \c \n \c \n \0 \c \n Q U I T \n
10+
v2sig = [0x0D.byte, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A]
1211

1312
type
14-
HeaderV2_IPV4 {.union.} = object
13+
HeaderV2_IPV4 = object
1514
src_addr: uint32
1615
dst_addr: uint32
1716
src_port: uint16
@@ -51,8 +50,9 @@ type
5150

5251
proc pp_handshake*(fd: SocketHandle, sa: ptr SockAddr, sl: ptr Socklen): int =
5352
var
54-
size: int
5553
hdr: Header
54+
src: Sockaddr_in
55+
src6: Sockaddr_in6
5656

5757
while true:
5858
result = recv(fd, addr hdr, sizeof(hdr), 0)
@@ -61,21 +61,16 @@ proc pp_handshake*(fd: SocketHandle, sa: ptr SockAddr, sl: ptr Socklen): int =
6161
if result == -1:
6262
return if (errno == EAGAIN): 0 else: -1
6363

64-
if result >= 16 and c_memcmp(addr hdr.v2, v2sig.cstring, 12) == 0 and (hdr.v2.ver_cmd and 0xF0) == 0x20:
65-
size = 16 + ntohs(hdr.v2.length).int
66-
if result < size:
67-
return -1
64+
if result >= 16 and hdr.v2.sig == v2sig and (hdr.v2.ver_cmd and 0xF0) == 0x20 and result >= 16 + ntohs(hdr.v2.length).int:
6865
case hdr.v2.ver_cmd and 0xF
6966
of 0x1: # PROXY command
7067
case hdr.v2.fam
7168
of 0x11: # TCPv4
72-
var src: Sockaddr_in
7369
src.sin_family = AF_INET.TSa_Family
7470
src.sin_addr.s_addr = hdr.v2.address.ip4.src_addr
7571
src.sin_port = hdr.v2.address.ip4.src_port
7672
copyMem(sa, addr src, sl[])
7773
of 0x21: # TCPv6
78-
var src6: Sockaddr_in6
7974
src6.sin6_family = AF_INET6.TSa_Family
8075
copyMem(addr src6.sin6_addr, addr hdr.v2.address.ip6.src_addr, 16)
8176
src6.sin6_port = hdr.v2.address.ip6.src_port
@@ -86,7 +81,7 @@ proc pp_handshake*(fd: SocketHandle, sa: ptr SockAddr, sl: ptr Socklen): int =
8681
return -1
8782
else:
8883
return -1
89-
elif result >= 8 and c_memcmp(addr hdr, v1sig, 5) == 0:
84+
elif result >= 8 and cmpMem(addr hdr, v1sig, 5) == 0:
9085
let backslash_r = hdr.v1.find('\r')
9186
if backslash_r == -1 or hdr.v1[backslash_r + 1] != '\n':
9287
return -1
@@ -95,65 +90,59 @@ proc pp_handshake*(fd: SocketHandle, sa: ptr SockAddr, sl: ptr Socklen): int =
9590
tmp: array[40, char]
9691
step = SRC_IP
9792
idx = 0
98-
zeroMem(addr tmp, sizeof(tmp))
93+
c: char
9994
if hdr.v1[9] == '4':
100-
var src: Sockaddr_in
10195
src.sin_family = AF_INET.TSa_Family
10296
for i in 11..backslash_r:
103-
var c = hdr.v1[i]
97+
c = hdr.v1[i]
10498
if c == ' ' or c == '\0':
10599
case step
106100
of SRC_IP:
107101
step = DST_IP
102+
zeroMem(addr tmp, sizeof(tmp))
108103
src.sin_addr.s_addr = cast[uint32](parseIPAddress($cast[cstring](addr tmp)).address_v4)
109-
of DST_IP:
110-
# temporary ignore destination ip address
104+
of DST_IP: # temporary ignore destination ip address
111105
step = SRC_PORT
112106
of SRC_PORT:
113107
step = DST_PORT
108+
zeroMem(addr tmp, sizeof(tmp))
114109
src.sin_port = htons(parseInt($cast[cstring](addr tmp)).uint16)
115110
copyMem(sa, addr src, sl[])
116111
return backslash_r
117-
of DST_PORT:
118-
# temporary ignore destination port
112+
of DST_PORT: # temporary ignore destination port
119113
break
120-
zeroMem(addr tmp, sizeof(tmp))
121114
idx = 0
122115
else:
123116
tmp[idx] = c
124117
inc(idx)
125118
elif hdr.v1[0] == '6':
126-
var src6: Sockaddr_in6
127119
src6.sin6_family = AF_INET6.TSa_Family
128120
for i in 11..backslash_r:
129-
var c = hdr.v1[i]
121+
c = hdr.v1[i]
130122
if c == ' ' or c == '\0':
131123
case step
132124
of SRC_IP:
133125
step = DST_IP
134126
var ip = parseIPAddress($cast[cstring](addr tmp))
127+
zeroMem(addr tmp, sizeof(tmp))
135128
copyMem(addr src6.sin6_addr, addr ip.address_v6, 16)
136-
of DST_IP:
137-
# temporary ignore destination ip address
129+
of DST_IP: # temporary ignore destination ip address
138130
step = SRC_PORT
139131
of SRC_PORT:
140132
step = DST_PORT
141133
src6.sin6_port = htons(parseInt($cast[cstring](addr tmp)).uint16)
134+
zeroMem(addr tmp, sizeof(tmp))
142135
copyMem(sa, addr src6, sl[])
143136
return backslash_r
144-
of DST_PORT:
145-
# temporary ignore destination port
137+
of DST_PORT: # temporary ignore destination port
146138
break
147-
zeroMem(addr tmp, sizeof(tmp))
148139
idx = 0
149140
else:
150141
tmp[idx] = c
151142
inc(idx)
152-
else:
153-
# not supported protocol
143+
else: # not supported protocol
154144
return -1
155-
else:
156-
## Wrong protocol
145+
else: # Wrong protocol
157146
return -1
158147

159148
when isMainModule:

0 commit comments

Comments
 (0)