Zyre module

The top level namespace for Zyre classes.

Constants

BUILT_WITH_DRAFT_API
BUILT_WITH_DRAFT_CZMQ_API
VERSION

Gem version (semver)

Public Class Methods

allow( *addresses )

Allow (whitelist) a list of IP addresses. For NULL, all clients from these addresses will be accepted. For PLAIN and CURVE, they will be allowed to continue with authentication. You can call this method multiple times to whitelist more IP addresses. If you whitelist one or more addresses, any non-whitelisted addresses are treated as blacklisted:

# File lib/zyre.rb, line 50
def self::allow( *addresses )
        @whitelisted_ips.merge( addresses )
end
authenticator_started? → true or false

Returns true if the ZAUTH authenticator actor is running.

static VALUE
rzyre_s_authenticator_started_p( VALUE module )
{
        if ( auth ) {
                return Qtrue;
        } else {
                return Qfalse;
        }
}
deny( *addresses )

Deny (blacklist) a list of IP addresses. For all security mechanisms, this rejects the connection without any further authentication. Use either a whitelist, or a blacklist, not not both. If you define both a whitelist and a blacklist, only the whitelist takes effect:

# File lib/zyre.rb, line 59
def self::deny( *addresses )
        @blacklisted_ips.merge( addresses )
end
disable_zsys_handler

Disable CZMQ’s default signal handler, which allows for handling it in Ruby instead.

static VALUE
rzyre_s_disable_zsys_handler( VALUE module )
{
        rzyre_log( "info", "Disabling zsys INT/TERM handler." );
        zsys_handler_set( NULL );
        return Qtrue;
}
enable_curve_auth( cert_dir=nil )

Enable CURVE authentication, using the specified cert_dir for allowed public certificates. If no cert_dir is given, any connection presenting a valid CURVE certificate will be allowed.

static VALUE
rzyre_s_enable_curve_auth( int argc, VALUE *argv, VALUE module )
{
        VALUE cert_dir = Qnil;
        char *cert_dir_s;

        rb_scan_args( argc, argv, "01", &cert_dir );

        if ( argc ) {
                cert_dir_s = StringValueCStr( cert_dir );
                zstr_sendx( auth, "CURVE", cert_dir_s, NULL );
        } else {
                zstr_sendx( auth, "CURVE", CURVE_ALLOW_ANY, NULL );
        }

        rb_thread_call_without_gvl2( rzyre_wait_for_auth_without_gvl, 0, RUBY_UBF_IO, 0 );

        return Qtrue;
}
has_draft_apis?()

Returns true if the underlying Zyre library was built with draft APIs.

# File lib/zyre.rb, line 71
def self::has_draft_apis?
        return Zyre::BUILT_WITH_DRAFT_API ? true : false
end
has_draft_czmq_apis?()

Returns true if the underlying Czmq library was built with draft APIs.

# File lib/zyre.rb, line 65
def self::has_draft_czmq_apis?
        return Zyre::BUILT_WITH_DRAFT_CZMQ_API ? true : false
end
interface → string

Return network interface to use for broadcasts, or nil if none was set.

static VALUE
rzyre_s_interface( VALUE module )
{
        const char *interface = zsys_interface();

        if ( strnlen(interface, 1) == 0 ) {
                return Qnil;
        }

        return rb_utf8_str_new_cstr( interface );
}
interface = string

Set network interface name to use for broadcasts.

This lets the interface be configured for test environments where required. For example, on Mac OS X, zbeacon cannot bind to 255.255.255.255 which is the default when there is no specified interface. If the environment variable ZSYS_INTERFACE is set, use that as the default interface name. Setting the interface to “*” means “use all available interfaces”.

static VALUE
rzyre_s_interface_eq( VALUE module, VALUE new_interface )
{
        if ( NIL_P(new_interface) ) {
                zsys_set_interface( "" );
        } else {
                const char *new_interface_s = StringValueCStr( new_interface );
                zsys_set_interface( new_interface_s );
        }

        return Qtrue;
}
interfaces → hash

Return a Hash of broadcast-capable interfaces on the local host keyed by name. The values contain other information about the interface:

{ “<interface name>” => { address: “<interface address>”, broadcast: “<interface broadcast address>”, netmask: “<interface network mask>”, } }

static VALUE
rzyre_s_interfaces( VALUE module )
{

        ziflist_t *iflist = ziflist_new();
        ziflist_reload( iflist );

        const VALUE rval = rb_hash_new();
        const char *iface = ziflist_first( iflist );

        while ( iface ) {
                const char *address_s = ziflist_address( iflist );
                const char *broadcast_s = ziflist_broadcast( iflist );
                const char *netmask_s = ziflist_netmask( iflist );
                const VALUE info_hash = rb_hash_new();

                rzyre_log( "debug", "Getting info for %s", address_s );
                rb_hash_aset( info_hash, ID2SYM(rb_intern("address")), rb_usascii_str_new_cstr(address_s) );
                rb_hash_aset( info_hash, ID2SYM(rb_intern("broadcast")), rb_usascii_str_new_cstr(broadcast_s) );
                rb_hash_aset( info_hash, ID2SYM(rb_intern("netmask")), rb_usascii_str_new_cstr(netmask_s) );

                rb_hash_aset( rval, rb_usascii_str_new_cstr(iface), info_hash );

                iface = ziflist_next( iflist );
        }

        ziflist_destroy( &iflist );

        return rval;
}
normalize_headers( headers )

Transform the given headers hash into a form that can be passed to Zyre.

# File lib/zyre.rb, line 37
def self::normalize_headers( headers )
        return headers.
                transform_keys {|k| Zyre.transform_header_key(k) }.
                transform_values {|v| v.to_s.encode('us-ascii') }
end
start_authenticator → true

Start the ZAUTH authenticator actor. If it’s already running, this call is silently ignored.

static VALUE
rzyre_s_start_authenticator( VALUE module )
{
        if ( !auth ) {
                rzyre_log_obj( rzyre_mZyre, "info", "starting up the ZAUTH actor." );
                auth = zactor_new( zauth, NULL );
                assert( auth );
        }

        return Qtrue;
}
stop_authenticator → true

Stop the ZAUTH authenticator actor if it is running. If it’s not running, this call is silently ignored.

static VALUE
rzyre_s_stop_authenticator( VALUE module )
{
        if ( auth ) {
                rzyre_log_obj( rzyre_mZyre, "info", "shutting down the ZAUTH actor." );
                zactor_destroy( &auth );
        }

        return Qtrue;
}
transform_header_key( key )

If the given key is a Symbol, transform it into an RFC822-style header key. If it’s not a Symbol, returns it unchanged.

# File lib/zyre.rb, line 27
def self::transform_header_key( key )
        if key.is_a?( Symbol )
                key = key.to_s.gsub( /_/, '-' ).capitalize
        end

        return key
end
verbose_auth!

Enable the ZAUTH actor’s verbose logging.

static VALUE
rzyre_s_verbose_auth_bang( VALUE module )
{
        if ( auth ) {
                zstr_sendx( auth, "VERBOSE", NULL );
                rb_thread_call_without_gvl2( rzyre_wait_for_auth_without_gvl, 0, RUBY_UBF_IO, 0 );
        } else {
                rb_raise( rb_eRuntimeError, "can't enable verbose auth: authenticator is not started." );
        }

        return Qtrue;
}
z85_decode( string ) → data

Return the data decoded from the specified Z85-encoded binary string. If there is a problem decoding the string, returns nil.

static VALUE
rzyre_s_z85_decode( VALUE module, VALUE string )
{
#if HAVE_ZMQ_Z85_DECODE
        const char *data_str = StringValueCStr( string );
        const long len = RSTRING_LEN( string );
        const long res_len = (len * 0.8) + 1;
        char *decoded = NULL;
        VALUE result = Qnil;

        if ( len % 5 ) return Qnil;

        decoded = RB_ZALLOC_N( char, res_len );
        zmq_z85_decode( (unsigned char *)decoded, data_str );

        if ( decoded != NULL ) {
                result = rb_str_new( decoded, res_len - 1 );
        }

        ruby_xfree( decoded );

        return result;
#else
        rb_notimplement();
#endif
}
z85_encode( data ) → string

Return the specified binary data as a Z85-encoded binary string. The size of the data must be divisible by 4. If there is a problem encoding the data, returns nil.

static VALUE
rzyre_s_z85_encode( VALUE module, VALUE data )
{
#if HAVE_ZMQ_Z85_ENCODE
        const char *data_str = StringValuePtr( data );
        const long len = RSTRING_LEN( data );
        const long res_len = (len * 1.25) + 1;
        char *encoded = NULL;
        VALUE result = Qnil;

        if ( len % 4 ) return Qnil;

        encoded = RB_ZALLOC_N( char, res_len );
        zmq_z85_encode( encoded, (unsigned char *)data_str, len );

        if ( encoded != NULL ) {
                result = rb_usascii_str_new( encoded, res_len - 1 );
        }

        ruby_xfree( encoded );

        return result;
#else
        rb_notimplement();
#endif
}
zyre_version → int

Return the version of the underlying libzyre.

static VALUE
rzyre_s_zyre_version( VALUE _mod )
{
        static uint64_t version;

        version = zyre_version();

        return LONG2NUM( version );
}