IIn an unmanaged or dumb network, adding a new device is often easy. However, when we are dealing with smart networks that use managed switches, some extra effort will be needed to ensure good compatibility.
One such scenario is when we are using static IP addresses on the Tibbo devices in a managed network. Since the traffic is routed based on the routing table, so, in turn, the packets to all unknow destinations are dropped (unless it is ARP). If the Tibbo device is simply connected physically to the network but does not initiate any communication such as a DHCP request or data connection, the existence of it is not known to the managed switches. To get around this issue, we recommend implementing an ARP mechanism into your application, specifically, Gratuitous ARP.
ARP packets usually exist in three forms: ARP, Proxy ARP, and Gratuitous ARP. The first one is the one we are most familiar with, and it is used to find out the hardware address (MAC Address) of a certain IP address. So, first, there’s an ARP request and then an ARP reply.
Next, Proxy ARP is used when a router is replying on behalf of another device, such as in NAT implementations.
Lastly, Gratuitous ARP is sent when a device announces itself to the network without any ARP request. It is a good way to ensure that the MAC-to-IP Address information is always up to date on the network. So, there will not be a request but a reply packet directed at itself. This is what we want to generate through the Tibbo BASIC code.
So, one might be a bit confused as to how to even begin generating this as we usually can only send “data” via TCP or UDP but are not able to create a lower-level packet such as ARP. There’s a hidden and undocumented feature under the sock.protocol property, the RAW mode.
Let’s take a look at the socket setup code below and there are a few things to note:
- We need to have the IP address, net mask, and gateway IP predefined.
- A socket will need to be dedicated to this task.
- Choose the third option for protocol, PL_SOCK_PROTOCOL_RAW.
- We only need to assign one page of buffer to TX and does not need to assign RX buffer.
- Notice the difference between target port for the different platforms.
- Sock.connect must be called before we can send the Gratuitous ARP packet.
net.ip = "192.168.1.71"
net.netmask = "255.255.255.0"
net.gatewayip = "192.168.1.1"
sock.num=0
sock.protocol=PL_SOCK_PROTOCOL_RAW
sock.txbuffrq(1)
'sock.targetport ="2054" 'EM1000 & EM500 based
sock.targetport ="1544" 'EM2000 & EM510 based
sys.buffalloc
sock.connect
To construct the ARP packet and send it out, we just need to insert the following code into an appropriate location in your application. We recommend doing this at intervals of at least 30 seconds apart.
'Grattuitos ARP Reply
dim x as string
x = "\x00\x01\x08\x00\x06\x04\x00\x02"+ddval(net.mac)+ddval(net.ip)+"\xff\xff\xff\xff\xff\xff"+ddval(net.ip)+"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
sock.num=0
sock.setdata(x)
sock.send()
These should be enought to get your device recognized quickly on a new network and be ready to accept incoming connections.
Comments
0 comments
Please sign in to leave a comment.