15

First, I want to give thanks for that amazing lib! I love it. A client is connecting himself to a server. The server should save the IP and do stuff with it later on (I really need the IP). I found that answer: http://lists.zeromq.org/pipermail/zeromq-dev/2010-September/006381.html but I don't understand how I get the IP out of the message (a XREP)... I think I am only able to read the ID, but the IP is managed internally by 0MQ. His second solution suggests to send the IP as part of the message, but I don't understand how to get the "public"-IP. I found that post: Get TCP address information in ZeroMQ

 is pass bind a service to an ephemeral port, get a full connection endpoint ("tcp://ipaddress:port")

I don't get how this works. Does he mean something like a web-service? In my opinion, it would be best to get the IP out of 0MQ (it has the IP already). I would even adjust 0MQ for that, if somebody could point to the place where the IP is saved, couldn't find it. The socket types are not that important, at the moment. I would prefer smth REQ-REP like. Thank you!

Community
  • 1
  • 1
  • Would you be able to go into more detail about why you might want teh IP specifically? ZMQ is designed from the ground up to abstract away such low level information. You may be trying to solve the problem incorrectly with ZMQ. – S.Richmond Dec 03 '13 at 03:56
  • I definitively need the IP. Let's not talk about _if_ I need the IP, let's talk about _how_ I get the IP. :) –  Dec 03 '13 at 09:28
  • 2
    To me it seems like A/B problem, where you ask about the solution to the problem that is not the real one you're strugling with. Therefore I'd repeat S.Richmond question: what do you need that IP for? What problem are you trying to solve? – Andrzej H Dec 13 '13 at 09:45
  • An example: you have a server, which hosts a lobby. After connecting, you would receive the ips of other clients and you could start to use a peer to peer connection, without sending data over the server anymore. –  Sep 15 '16 at 21:31
  • I do think the flag `ZMQ_SRCFD` can solve this... look a sample in `$ZMQ_SOURCES/tests/test_srcfd.cpp` – ton Aug 04 '17 at 11:17
  • Sadly it has problems and will be deprecated: https://github.com/zeromq/libzmq/issues/1950 – ton Aug 04 '17 at 11:34
  • How about set the identity as the IP Address or something from which the IP address can be extracted? This should be a simple workaround. – falopsy Mar 21 '18 at 13:14

3 Answers3

17

Summary:

TL;DR answer to your question is: you can't get IP address of the peer that sent a message, using ZeroMQ API.

Explanation:

ZeroMQ does not expose peer IP address because it is irrelevant for the message based communication that ZeroMQ is designed for. When it is possible for ZeroMQ to get IP address of client that is connecting to server (in example using method described here), it is useless. For a longer explanation here is how it works inside ZeroMQ and any other server implementation.

Server side of the connection does not handle connected clients by the means of the hashtable that maps IP to client, but by keeping track of connected "sockets" (socket descriptors) - when a server accepts (using accept()) a connection, it receives from operating system socket descriptor to use to communicate with connected peer. All server has to do is keep that descriptor around to read() from and write() to that client. Another client that connects to server receives another socket descriptor.

To summarize: even if ZeroMQ would be able to provide you with IP of connected peer, you should not depend on it. ZeroMQ hides from you connection management so you can focus on messaging. Connection management includes reconnections, which may result in a change of IP without changing the actual ZeroMQ socket connected on the other side.

Community
  • 1
  • 1
Andrzej H
  • 434
  • 3
  • 9
  • Thanks for the answer, but as you can see at this [link](http://hintjens.com/blog:49#toc3) it must be possible to get the ip. Furthermore I found `bool get_peer_ip_address (fd_t sockfd_, std::string &ip_addr_);` in the `ip.hpp` but somehow I can't use it (header is not in includepath and if I add it, its still not compiled in the lib). If you could include that information in your answer (and if it works :) I will mark it. –  Dec 13 '13 at 12:16
  • You will not be able to use get_peer_ip_address since it is internally used by STREAM sockets available from ZeroMQ 4.0 that are used for authorization purposes - you can authorize based on IP address. – Andrzej H Dec 19 '13 at 09:04
  • One more thing that comes to my mind is that you might use http://api.zeromq.org/4-0:zmq-socket-monitor to get IP address of peers connecting to your socket. That API is also available in 3.2. But once again: by design you should not care about IP of your peers except for security reasons, but that is now built-in into 4.0. – Andrzej H Dec 19 '13 at 09:06
  • 2
    I found that too, but the monitor doesn't tell me the ip... I only get smth like tcp://0.0.0.0:port! –  Dec 22 '13 at 13:41
  • Updated an answer with short version for those downvoting. – Andrzej H Apr 07 '15 at 16:53
  • We don't want to depend on it, but for troubleshooting it would be really convenient in some cases, if we could at least log the clients' IP addresses. – Reunanen Oct 30 '17 at 06:40
7

So here's an example of why you might want to get the ip address a message was delivered from: we have a server whose job it is to synchronize updates onto occasionally-connected clients (think mobile devices here, though this is an extreme example of a mobile deivce.)

When the mobile unit comes onto the network, it sends a list of it's firmware files to the server via a dealer-router connection. The server has a list of all applicable firmware files; if the client needs an update it will initiate an update via a separate mechanism.

Since the IPs for the devices can (and do) change, we need to know the IP address associated with the mobile device FOR THIS CONNECTION, i.e. right now.

Yes, we absolutely can have the client send it's IP address in the message, but that's a waste of another n bytes of valuable satellite air time, and while not pure evil, is sure annoying. Zmq already has this information, if it didn't have it, it wouldn't be able to generate replies. The address is in the socket data, there's no reason the message couldn't (optionally, for all you guys who use wired networks and think disconnects are the exception) include a reference to the socket structure so you can get the address out of it. Other than pedantic religiosity, which is far too common in zmq.

Wexxor
  • 1,919
  • 1
  • 15
  • 17
3

The way ZeroMQ is designed there's no information provided on the remote IP. As far as I know you have to manage this through your application by sending that information as a message of some sort.

The messages themselves use an IP-agnostic ID which has more to do with the instance of ZeroMQ running than any particular interface. This is because there may be more than one transport method and interface connecting the two instances.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Yeah, but somehow 0MQ must know the IP of the client, it has to send the response to... As far as I understood, 0MQ uses a hashtable where it uses the IDs as keys for the IPs. –  Dec 02 '13 at 20:21
  • 1
    ZeroMQ knows but I haven't seen anything in the API that allows you to access that information. I've seen this brought up on the mailing list only to be answered with vague grumblings about "fabric" and how you're not *supposed* to know. – tadman Dec 03 '13 at 02:10