From b5d3373905587aecc53e032e5619c1550952030f Mon Sep 17 00:00:00 2001 From: Ian Barber Date: Tue, 14 Feb 2012 23:10:06 +0000 Subject: [PATCH] Moving to std::string in options --- include/zmq.h | 3 --- src/ipc_listener.cpp | 5 +++-- src/ipc_listener.hpp | 2 +- src/options.cpp | 8 ++++---- src/options.hpp | 5 +++-- src/socket_base.cpp | 5 +++-- src/tcp_listener.cpp | 10 +++++++--- src/tcp_listener.hpp | 2 +- 8 files changed, 22 insertions(+), 18 deletions(-) diff --git a/include/zmq.h b/include/zmq.h index 78b92bc3..325cda6a 100644 --- a/include/zmq.h +++ b/include/zmq.h @@ -203,9 +203,6 @@ ZMQ_EXPORT int zmq_term (void *context); #define ZMQ_DONTWAIT 1 #define ZMQ_SNDMORE 2 -/* Wildcard endpoint support. */ -#define ZMQ_ENDPOINT_MAX 256 - ZMQ_EXPORT void *zmq_socket (void *context, int type); ZMQ_EXPORT int zmq_close (void *s); ZMQ_EXPORT int zmq_setsockopt (void *s, int option, const void *optval, diff --git a/src/ipc_listener.cpp b/src/ipc_listener.cpp index 67973447..668464a3 100644 --- a/src/ipc_listener.cpp +++ b/src/ipc_listener.cpp @@ -95,7 +95,7 @@ void zmq::ipc_listener_t::in_event () send_attach (session, engine, false); } -int zmq::ipc_listener_t::get_address (unsigned char *addr, size_t *len) +int zmq::ipc_listener_t::get_address (std::string *addr_) { struct sockaddr_un sun; int rc; @@ -106,8 +106,9 @@ int zmq::ipc_listener_t::get_address (unsigned char *addr, size_t *len) if (rc != 0) { return rc; } + // Store the address for retrieval by users using wildcards - *len = sprintf((char *)addr, "ipc://%s", sun.sun_path); + *addr_ = std::string("ipc://") + std::string(sun.sun_path); return 0; } diff --git a/src/ipc_listener.hpp b/src/ipc_listener.hpp index 57e04ef8..228e9f24 100644 --- a/src/ipc_listener.hpp +++ b/src/ipc_listener.hpp @@ -50,7 +50,7 @@ namespace zmq int set_address (const char *addr_); // Get the bound address for use with wildcards - int get_address(unsigned char *addr, size_t *len); + int get_address(std::string *addr_); private: diff --git a/src/options.cpp b/src/options.cpp index c8790a85..5f1d4440 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -30,7 +30,6 @@ zmq::options_t::options_t () : rcvhwm (1000), affinity (0), identity_size (0), - last_endpoint_size(0), rate (100), recovery_ivl (10000), multicast_hops (1), @@ -387,12 +386,13 @@ int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_) return 0; case ZMQ_LAST_ENDPOINT: - if (*optvallen_ < last_endpoint_size) { + // don't allow string which cannot contain the entire message + if (*optvallen_ < last_endpoint.size() + 1) { errno = EINVAL; return -1; } - memcpy (optval_, last_endpoint, last_endpoint_size); - *optvallen_ = last_endpoint_size; + memcpy (optval_, last_endpoint.c_str(), last_endpoint.size()+1); + *optvallen_ = last_endpoint.size()+1; return 0; } diff --git a/src/options.hpp b/src/options.hpp index 7feea95e..2bf793ee 100644 --- a/src/options.hpp +++ b/src/options.hpp @@ -23,6 +23,8 @@ #ifndef __ZMQ_OPTIONS_HPP_INCLUDED__ #define __ZMQ_OPTIONS_HPP_INCLUDED__ +#include + #include "stddef.h" #include "stdint.hpp" @@ -48,8 +50,7 @@ namespace zmq unsigned char identity [256]; // Last socket endpoint URI - unsigned char last_endpoint [256]; - size_t last_endpoint_size; + std::string last_endpoint; // Maximum tranfer rate [kb/s]. Default 100kb/s. int rate; diff --git a/src/socket_base.cpp b/src/socket_base.cpp index 74c807fb..fc73ce9c 100644 --- a/src/socket_base.cpp +++ b/src/socket_base.cpp @@ -342,7 +342,7 @@ int zmq::socket_base_t::bind (const char *addr_) return -1; } - rc = listener->get_address (options.last_endpoint, &(options.last_endpoint_size)); + rc = listener->get_address (&options.last_endpoint); launch_child (listener); return 0; } @@ -357,7 +357,8 @@ int zmq::socket_base_t::bind (const char *addr_) delete listener; return -1; } - rc = listener->get_address (options.last_endpoint, &(options.last_endpoint_size)); + + rc = listener->get_address (&options.last_endpoint); launch_child (listener); return 0; } diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp index 191e05c2..8c65ff7f 100644 --- a/src/tcp_listener.cpp +++ b/src/tcp_listener.cpp @@ -22,6 +22,7 @@ #include #include +#include #include "platform.hpp" #include "tcp_listener.hpp" @@ -119,11 +120,12 @@ void zmq::tcp_listener_t::close () s = retired_fd; } -int zmq::tcp_listener_t::get_address (unsigned char *addr, size_t *len) +int zmq::tcp_listener_t::get_address (std::string *addr_) { struct sockaddr sa; char host[INET6_ADDRSTRLEN]; int port, rc; + std::stringstream portnum; // Get the details of the TCP socket socklen_t sl = sizeof(sockaddr); @@ -136,15 +138,17 @@ int zmq::tcp_listener_t::get_address (unsigned char *addr, size_t *len) if ( sa.sa_family == AF_INET ) { inet_ntop(AF_INET, &(((struct sockaddr_in *)&sa)->sin_addr), host, INET6_ADDRSTRLEN); port = ntohs( ((struct sockaddr_in *)&sa)->sin_port); + portnum << port; // Store the address for retrieval by users using wildcards - *len = sprintf((char *)addr, "tcp://%s:%d", host, port); + *addr_ = std::string("tcp://") + std::string(host) + std::string(":") + portnum.str(); } else { inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)&sa)->sin6_addr), host, INET6_ADDRSTRLEN); port = ntohs( ((struct sockaddr_in6 *)&sa)->sin6_port); + portnum << port; // Store the address for retrieval by users using wildcards - *len = sprintf((char *)*addr, "tcp://[%s]:%d", host, port); + *addr_ = std::string("tcp://[") + std::string(host) + std::string("]:") + portnum.str(); } return 0; diff --git a/src/tcp_listener.hpp b/src/tcp_listener.hpp index fc6ebd10..66c4fb2e 100644 --- a/src/tcp_listener.hpp +++ b/src/tcp_listener.hpp @@ -46,7 +46,7 @@ namespace zmq int set_address (const char *addr_); // Get the bound address for use with wildcard - int get_address(unsigned char *addr, size_t *len); + int get_address(std::string *addr_); private: