开发者

is gen_tcp:accept/1 safe?

开发者 https://www.devze.com 2023-01-30 00:40 出处:网络
I am implementing a server which accepts many concurrent connections. I used this structure: loop(Sock) ->

I am implementing a server which accepts many concurrent connections.

I used this structure:

loop(Sock) ->
  case gen_tcp:accept(Sock) of
      {ok, CSock} ->    
          fork_handling_process(CSock);
      {error, Reason} ->
          do_something_else();
  end,
  loop(Sock).

I am won开发者_运维技巧dering if someone sends me a SYN, but never sends me an SYN ACK in response to my server ACK, will my server be blocked forever by that client since I call gen_tcp:accept without a timeout?

By the way I think this situation is hard to emulate, so please let me know if you have ways to try it out.

Thx in advance.


When you listen/accept its a bit different as you describe:

Some client wants to connect: it sends a SYN, then your operating system sends a SYN/ACK (erlang not involvled), when you get the ACK gen_tcp:accept will return.

When someone sends you SYN's and nothing else (that would be a SYN flood attack if done in a great amount) then operating system resources will be reserved but nothing happens in your erlang code because a three way handshake is not complete yet.

Many operating systems are taking special care of SYN flooding attacks avoiding too much resource consumption.


The approach you are using appears to be fine. Your server will not block. If something goes wrong I believe that your forked process will receive the error, not the server.

0

精彩评论

暂无评论...
验证码 换一张
取 消