How to get my machine’s IP address from Ruby without leveraging from other IP address?

Isn’t the solution you are looking for just: require ‘socket’ addr_infos = Socket.ip_address_list Since a machine can have multiple interfaces and multiple IP Addresses, this method returns an array of Addrinfo. You can fetch the exact IP addresses like this: addr_infos.each do |addr_info| puts addr_info.ip_address end You can further filter the list by rejecting loopback … Read more

IPPROTO_IP vs IPPROTO_TCP/IPPROTO_UDP

Documentation for socket() on Linux is split between various manpages including ip(7) that specifies that you have to use 0 or IPPROTO_UDP for UDP and 0 or IPPROTO_TCP for TCP. When you use 0, which happens to be the value of IPPROTO_IP, UDP is used for SOCK_DGRAM and TCP is used for SOCK_STREAM. In my … Read more

Sending data with PACKET_MMAP and PACKET_TX_RING is slower than “normal” (without)

Many interfaces to the linux kernel are not well documented. Or even if they seem well documented, they can be pretty complex and that can make it hard to understanding what the functional or, often even harder, nonfunctional properties of the interface are. For this reason, my advice to anyone wanting a solid understanding of … Read more

bind socket to network interface

You can bind to a specific interface by setting SO_BINDTODEVICE socket option. struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), “eth0”); if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) { … error handling … } Warning: You have to be root or have the CAP_NET_RAW capability in order to use this option. The second … Read more

What is a message boundary?

No, message boundaries have nothing to do with destinations or ports. A “message boundary” is the separation between two messages being sent over a protocol. UDP preserves message boundaries. If you send “FOO” and then “BAR” over UDP, the other end will receive two datagrams, one containing “FOO” and the other containing “BAR”. If you … Read more

“java.net.BindException: Address already in use” when trying to do rapid Socket creation and destruction for load testing

You are exhausing the space of outbound ports by opening that many outbound sockets within the TIME_WAIT period of two minutes. The first question you should ask yourself is does this represent a realistic load test at all? Is a real client really going to do that? If not, you just need to revise your … Read more

tech