0

The man pages don't cover the state of the sockaddr variable passed by reference into the accept() function in case of an error.

Is it safe to assume that if something goes wrong between the time the client connects and the time you accept it, resulting in a return value of less than 0 from accept(), that the sockaddr struct is still populated and the IP information is valid?

RobC
  • 502
  • 4
  • 17
  • I'm guessing a possibility is the accept always works and you'd get an immediate disconnect event. Not sure how to test or trigger an accept error though. – RobC May 22 '17 at 23:42
  • You can test it by putting a breakpoint on `accept()`, or a keyboard input before it; run the client to connect and close; then continue the server. – user207421 May 23 '17 at 00:45

1 Answers1

2

Is it safe to assume that if something goes wrong between the time the client connects and the time you accept it, resulting in a return value of less than 0 from accept(), that the sockaddr struct is still populated and the IP information is valid?

No. If accept() failed, there is no guarantee stated anywhere that anything has happened to the sockaddr struct or length word at all, or that if anything has happened to it, it now means anything. Specifically, man accept says:

This structure is filled in with the address of the peer socket

If there has been a failure, there is no peer socket, ergo no peer socket address, ergo no filling in.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I tested like you recommended but I get the accept working, I get the IP, and an immediate read event with a disconnect. recv() = 0. – RobC May 23 '17 at 15:39
  • The reason I'm asking this question is because I was wondering if it was possible to have an attack that triggered a connection but killed it, and it seemed you should be able to see the IP of that connection even though it is down by the time you are accepting. – RobC May 23 '17 at 15:42
  • Perhaps the answer is if the accept fails you don't, but the accept won't fail in the scenario I'm giving. – RobC May 23 '17 at 15:54