Reputation: 67
I have a ZeroMQ socket that is receiving data from multiple processes on different machines. Without changing the contents of the data, is there any way to identify the source of the data? Specifically, I'd like the IP address of the sender if it came from a TCP connection.
Upvotes: 3
Views: 3045
Reputation: 4369
No, there is no way to get the senders IP from the ZeroMq socket. That information is hidden within the implementation layers of ZeroMq. You have a couple of choices to handle solve this, one is to change the message being passed and simply add the senders IP to the message itself, another is to use Multi-Part messages.
From the ZeroMq zmq_send() Api docs (3.2.2):
A ØMQ message is composed of 1 or more message parts. Each message part is an independent zmq_msg_t in its own right. ØMQ ensures atomic delivery of messages: peers shall receive either all message parts of a message or none at all. The total number of message parts is unlimited except by available memory.
Multi-Part messages are actually atomic messages but separated into several logical messages. I.e. you receive all parts or no parts. If you cannot modify the original message, you can prepend the message (on the sender side) with the IP of the sender. The receiver can then extract the first part as the senders IP and the second part as the original, unmodified, message. It will be delivered as a single message but is logically separated into two discreet parts.
In your case, you could do something like this:
// Send a multi-part message consisting of sender IP plus another message
zmq_msg_send (&my_ip, my_socket, ZMQ_SNDMORE);
zmq_msg_send (&my_message, my_socket, 0);
For the receiver, see the documentation for zmq_msg_recv().
Upvotes: 7