Swanand
Swanand

Reputation: 4115

Simulating Keep Alive Signal

I am working on Connecting an Embedded Circuit board to PC via TCP.

The board contains a chip which, sadly, doesn't generate any interrupt on Receiving data. But it does generates an interrupt on receiving "Keep-Alive" signal.

Currently I have to poll for data. Instead, I am thinking that, I will send data from PC and then a KeepAlive Signal. Whenever a KeepAlive is received, I will read data too. I do understand that this might generate false alarms but it's better than continuous polling.

I observed a Keep-Alive packet on Wireshark, it has One byte of Data and it is "00". Keep Alive Signal on Wireshark

And then I tried to send TCP Packet with Data as "00": Simulated Keep Alive Signal

I can see, Only Flag Section is different.

I got Two questions:

  1. (Broadly) How to manually send a Keep-Alive Signal?
  2. How to change that flag setting? (Flags in send and sendto are different)

Update: I have tried RawSockets, but that didn't help me or I missed something. I just change Flag to ACK in RAW Sockets header.

Upvotes: 3

Views: 1531

Answers (1)

MvG
MvG

Reputation: 60908

RFC 1122 section 4.2.3.6 might be worth reading.

It states that keepalive is an optional feature of the TCP implementation. It also states that keepalive signals should be limited to at most one every two hours. So manually emitting one from your application isn't a desired feature in general.

Furthermore, it describes details about the implementation, in particular pointing out the sequence number involved. This is one difference visible in your screen shots which you apparently failed to notice: the real keepalive packet has a very high relative sequence number, which is simply the unsigned representation of -1. To reproduce this with raw sockets, I think you'd have to somehow get your hands on the current TCP sequence number of the existing connection. Haven't worked enough with RawSockets to know details on how to do this.

The supported means to have the system send keepalives periodically is using the SO_KEEPALIVE option. But that won't be of much use to emit such a signal at a specific moment in time, I think.

Upvotes: 3

Related Questions