我们在做tcp服务器的时候,通常会从安全考虑,限制封包的大小,预防被无端攻击或者避免极端的请求对业务造成损害。 我们的tcp服务器通常是erlang做的,那么就涉及到gen_tcp如何限制封包的大小.
gen_tcp对封包的获取有2种方式: 1. {active, false} 封包透过gen_tcp:recv(Socket, Length) -> {ok, Packet} | {error, Reason} 来接收。 2. {active, true} 封包以消息方式投递。
对于第一种方式:gen_tcp:recv(Socket, Length) 我们开看下代码实现:
#define TCP_MAX_PACKET_SIZE 0x4000000 /* 64 M */
static
ErlDrvSSizeT tcp_inet_ctl(ErlDrvData e, unsigned
int
cmd,
char
* buf, ErlDrvSizeT len,
char
** rbuf, ErlDrvSizeT rsize)
DEBUGF((
"tcp_inet_ctl(%ld): RECV\r\n"
, (
long
)desc->inet.port));
if
(!IS_CONNECTED(INETP(desc))) {
if
(desc->tcp_add_flags & TCP_ADDF_DELAYED_CLOSE_RECV) {
desc->tcp_add_flags &= ~(TCP_ADDF_DELAYED_CLOSE_RECV|
TCP_ADDF_DELAYED_CLOSE_SEND);
return
ctl_reply(INET_REP_ERROR,
"closed"
, 6, rbuf, rsize);
return
ctl_error(ENOTCONN, rbuf, rsize);
if
(desc->inet.active || (len != 8))
return
ctl_error(EINVAL, rbuf, rsize);
timeout = get_int32(buf);
DEBUGF((
"tcp_inet_ctl(%ld) timeout = %d, n = %d\r\n"
,
(
long
)desc->inet.port,timeout,n));
(
long
)desc->inet.port,timeout,n));
if
((desc->inet.htype != TCP_PB_RAW) && (n != 0))
return
ctl_error(EINVAL, rbuf, rsize);
if
(n > TCP_MAX_PACKET_SIZE)
return
ctl_error(ENOMEM, rbuf, rsize);
if
(enq_async(INETP(desc), tbuf, TCP_REQ_RECV) < 0)
return
ctl_error(EALREADY, rbuf, rsize);
if
(INETP(desc)->is_ignored || tcp_recv(desc, n) == 0) {
逻辑上很简单,如果封包的类型是TCP_PB_RAW,就需要显式的指定长度,否则封包的长度是对端决定的,长度只能设置为0。然后就调用tcp_recv来异步接收数据。在前面的博文 里面讲过,tcp_recv数据的时候,需要分配接收缓冲区,缓冲区的大小正是n, 所以这里做了个限定,不能超过TCP_MAX_PACKET_SIZE, 也就是说最大64M, 超过了会报ENOMEM错误!
对于第二种消息主动投递的情况:
从文档inet:setopts 可以看到:
{packet_size, Integer}(TCP/IP sockets) Sets the max allowed length of the packet body. If the packet header indicates that the length of the packet is longer than the max allowed length, the packet is considered invalid. The same happens if the packet header is too big for the socket receive buffer.
For line oriented protocols (line,http*), option packet_size also guarantees that lines up to the indicated length are accepted and not considered invalid due to internal buffer limitations.
那这个限定如何发挥作用呢,同样看代码:
static
int
inet_set_opts(inet_descriptor* desc,
char
* ptr,
int
len)
case
INET_LOPT_PACKET_SIZE:
DEBUGF((
"inet_set_opts(%ld): s=%d, PACKET_SIZE=%d\r\n"
,
(
long
)desc->port, desc->s, ival));
desc->psize = (unsigned
int
)ival;
static
ErlDrvData inet_start(ErlDrvPort port,
int
size,
int
protocol)
static
tcp_descriptor* tcp_inet_copy(tcp_descriptor* desc,SOCKET s,
ErlDrvTermData owner,
int
* err)
copy_desc->inet.psize = desc->inet.psize;
static
int
tcp_remain(tcp_descriptor* desc,
int
* len)
DEBUGF((
"tcp_remain(%ld): s=%d, n=%d, nfill=%d nsz=%d\r\n"
,
(
long
)desc->inet.port, desc->inet.s, n, nfill, nsz));
tlen = packet_get_length(desc->inet.htype, ptr, n,
desc->inet.psize, desc->i_bufsz,
DEBUGF((
" => nothing remain packet=%d\r\n"
, tlen));
if
(tcp_expand_buffer(desc, tlen) < 0)
DEBUGF((
" => remain=%d\r\n"
, *len));
if
(desc->inet.psize != 0 && desc->inet.psize > nfill) {
if
(tcp_expand_buffer(desc, desc->inet.psize) < 0)
DEBUGF((
" => restart more=%d\r\n"
, nfill - n));
DEBUGF((
" => more=%d \r\n"
, nsz));
DEBUGF((
" => packet error\r\n"
));
static
int
tcp_recv(tcp_descriptor* desc,
int
request_len)
else
if
(desc->i_remain == 0) {
if
((nread = tcp_remain(desc, &len)) < 0)
return
tcp_recv_error(desc, EMSGSIZE);
return
tcp_deliver(desc, len);
int
packet_get_length(
enum
PacketParseType htype,
const
char
* ptr, unsigned n,
if
((ptr2 =
memchr
(ptr,
'\n'
, n)) == NULL) {
if
(n > max_plen && max_plen != 0) {
DEBUGF((
" => packet full (no NL)=%d\r\n"
, n));
else
if
(n >= trunc_len && trunc_len!=0) {
DEBUGF((
" => line buffer full (no NL)=%d\r\n"
, n));
if
(((plen == 1) && NL(ptr)) || ((plen == 2) && CRNL(ptr)))
const
char
* ptr2 =
memchr
(ptr1,
'\n'
, len);
if
(max_plen != 0 && plen > max_plen)
if
((max_plen != 0 && plen > max_plen)
从代码我们可以看出在主动模式下包限制的几点: 1. 默认情况下 psize为0, 代表不限制包长度。 2. psize是继承的,也就是说accept出来的gen_tcp会继承listen的那个gen_tcp的属性。 3. 如文档所说,psize会限制 http/line类包的最大行的长度, 限制{packet, 1 | 2 | 4} 类型的包的长度。 4. 如果超过限制,返回的错误码是EMSGSIZE.
所以总结起来就是packet_size用来限制包的大小,默认不限制, 在被动模式下除了主动模式的限制外还有最大64M的限制。 触碰到限制后,返回的出错码是EMSGSIZE或者ENOMEM, 需要程序来判定。
小结: 源码面前无秘密!
祝玩得开心!