Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML TCP Receive Locks on invalid data.  (Read 2094 times)

0 Members and 1 Guest are viewing this topic.

flyffwaste

  • Newbie
  • *
  • Posts: 6
    • View Profile
SFML TCP Receive Locks on invalid data.
« on: May 24, 2014, 07:55:10 pm »
I am working with the 2.1 code found at the following URL: ( http://www.sfml-dev.org/documentation/2.1/classsf_1_1SocketSelector.php ).

Using the code verbatim, I have ran into the following issue.

When you connect with any client, telnet for example, and simply type one letter the packet gets sent and since its the wrong format for SFML the client locks up on

if (client.receive(packet) == sf::Socket::Done)

I know I can receive into a byte array instead of directly into a packet, but I wanted to use this method to deal with packet fragmentation (When I tested it without SFML the server is sending packets over TCP and splitting them randomly and breaking things). 

I cannot have a potential connection block the whole server because they send a little invalid data.

Any advice would be great.

Best Regards.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: SFML TCP Receive Locks on invalid data.
« Reply #1 on: May 24, 2014, 08:37:06 pm »
sf::Packet is only meant to be used with sf::Packet. Mixing sf::Packet with any other protocol (yes... telnet is a protocol) will yield undefined behaviour. Don't do it. Either use sf::Packet on both ends or don't use it at all.

If you are feeling brave, you can implement the sf::Packet protocol in whatever other client you want and you can use that to communicate with an SFML application using sf::Packet, but why bother if you can directly use sf::Packet there as well anyway...

And just so you know, the examples weren't written with a real world scenario in mind. They are just there to demonstrate the capabilities of a certain class. They are not secure, don't perform any sanity checks and probably are also very inefficient at what they do. If you are planning on using sf::SocketSelector or sf::Packet or sf::TcpSocket, don't copy and paste from the examples...
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

flyffwaste

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: SFML TCP Receive Locks on invalid data.
« Reply #2 on: May 24, 2014, 08:57:12 pm »
I see, well is that a general issue with receiving them directly into packet? Or something I am doing wrong?

Both client and server will use sf sockets, but I have to consider any other problems that will arise if say a user sends a malformed packet.  In this case it locks up.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: SFML TCP Receive Locks on invalid data.
« Reply #3 on: May 24, 2014, 09:08:01 pm »
If you want sanity checking, then don't use sf::Packet. It doesn't do it. You will have to implement your own way of sending and receiving data that is tamper-proof. Since this isn't really something that is specific to SFML, I'm sure you will be able to find enough information around the internet about this topic.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

flyffwaste

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: SFML TCP Receive Locks on invalid data.
« Reply #4 on: May 25, 2014, 12:12:37 am »
Thanks for the information, I was able to resolve the issue I was having by using a buffer for the buffer that reads the length data sfml prepends to the packets and waits for all data etc.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: SFML TCP Receive Locks on invalid data.
« Reply #5 on: May 27, 2014, 01:05:58 am »
That doesn't sound bullet-proof.
What if you get bogus length data?

 

anything