On Thu, Jan 26, 2012 at 2:57 PM, Martin Lucina <[email protected]> wrote:

>
> zmq_bind(foo, "tcp://XXXX:*");    // "tcp://*:*" if you want INADDR_ANY
> char endpoint [ZMQ_ENDPOINT_MAX];
> zmq_getsockopt(foo, ZMQ_GET_ENDPOINT, endpoint, sizeof endpoint);
>
>
Hi all,

I have knocked up a relatively simple patch to implement this on IPC and
TCP transports, would appreciate comments as I'm pretty sure there's some
dodgy stuff in there! I've made a pull req, though mainly for comment
(please don't pull it chuck/mikko/pieter!):
https://github.com/zeromq/libzmq/pull/238

The TCP is handled by binding to port 0 then using getsockname to retrieve
the details in tcp_listener. This has an added get_address function which
is called from socket_base to populate a field in options. That field is
ZMQ_LAST_ENDPOINT just to make very explicit that it will get overwritten
with subsequent socket calls. The IPC uses tempnam, and otherwise the same
mechanism. At the moment is only looks for the wildcards on bind, connect
looked a bit trickier, but I'm sure it would just be finding the right
place. Inproc should be fairly straightforward, but I don't think there's
as much of a use case there.

#include "zmq.h"
#include <stdio.h>

int main (void)
{
        size_t len = 255;
        char endpoint[len];
        void *context = zmq_init(1);

        void *sock = zmq_socket(context, ZMQ_REQ);
        // Test 1: test with specified host portion
        zmq_bind(sock, "tcp://127.0.0.1:*");
        zmq_getsockopt(sock, ZMQ_LAST_ENDPOINT, &endpoint, &len);
        printf("%d - %s\n", (int)len, endpoint);

        // Test 2: Test with IPC
        len = 255; // reset the length
        zmq_bind(sock, "ipc://*");
        zmq_getsockopt(sock, ZMQ_LAST_ENDPOINT, &endpoint, &len);
        printf("%d - %s\n", (int)len, endpoint);


        zmq_close(sock);
        zmq_term(context);
        return 0;
}

Ian
_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev

Reply via email to