예제 #1
0
파일: misc_tools.c 프로젝트: privacee/toxic
/* same as get_nick_truncate but for groupchats */
int get_group_nick_truncate(Tox *m, char *buf, int peernum, int groupnum)
{
    int len = tox_group_peername(m, groupnum, peernum, (uint8_t *) buf);

    if (len == -1) {
        strcpy(buf, UNKNOWN_NAME);
        len = strlen(UNKNOWN_NAME);
    }

    len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1);
    buf[len] = '\0';
    filter_str(buf, len);
    return len;
}
예제 #2
0
파일: misc_tools.c 프로젝트: privacee/toxic
/* puts friendnum's nick in buf, truncating at TOXIC_MAX_NAME_LENGTH if necessary.
   if toxcore API call fails, put UNKNOWN_NAME in buf
   Returns nick len */
size_t get_nick_truncate(Tox *m, char *buf, uint32_t friendnum)
{
    size_t len = tox_friend_get_name_size(m, friendnum, NULL);

    if (len == 0) {
        strcpy(buf, UNKNOWN_NAME);
        len = strlen(UNKNOWN_NAME);
    } else {
        tox_friend_get_name(m, friendnum, (uint8_t *) buf, NULL);
    }

    len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1);
    buf[len] = '\0';
    filter_str(buf, len);
    return len;
}
예제 #3
0
파일: groupchat.c 프로젝트: subliun/toxic
/* Puts two copies of peerlist/lengths in chat instance */
static void copy_peernames(int gnum, uint8_t peerlist[][TOX_MAX_NAME_LENGTH], uint16_t lengths[], int npeers)
{
    /* Assumes these are initiated in init_groupchat_win */
    free(groupchats[gnum].peer_names);
    free(groupchats[gnum].oldpeer_names);
    free(groupchats[gnum].peer_name_lengths);
    free(groupchats[gnum].oldpeer_name_lengths);

    int N = TOX_MAX_NAME_LENGTH;

    groupchats[gnum].peer_names = malloc(sizeof(uint8_t) * npeers * N);
    groupchats[gnum].oldpeer_names = malloc(sizeof(uint8_t) * npeers * N);
    groupchats[gnum].peer_name_lengths = malloc(sizeof(uint16_t) * npeers);
    groupchats[gnum].oldpeer_name_lengths = malloc(sizeof(uint16_t) * npeers);

    if (groupchats[gnum].peer_names == NULL || groupchats[gnum].oldpeer_names == NULL
            || groupchats[gnum].peer_name_lengths == NULL || groupchats[gnum].oldpeer_name_lengths == NULL) {
        exit_toxic_err("failed in copy_peernames", FATALERR_MEMORY);
    }

    uint16_t u_len = strlen(UNKNOWN_NAME);
    int i;

    for (i = 0; i < npeers; ++i) {
        if (!lengths[i]) {
            memcpy(&groupchats[gnum].peer_names[i * N], UNKNOWN_NAME, u_len);
            groupchats[gnum].peer_names[i * N + u_len] = '\0';
            groupchats[gnum].peer_name_lengths[i] = u_len;
        } else {
            uint16_t n_len = MIN(lengths[i], TOXIC_MAX_NAME_LENGTH - 1);
            memcpy(&groupchats[gnum].peer_names[i * N], peerlist[i], n_len);
            groupchats[gnum].peer_names[i * N + n_len] = '\0';
            groupchats[gnum].peer_name_lengths[i] = n_len;
            filter_str((char *) &groupchats[gnum].peer_names[i * N], n_len);
        }
    }

    memcpy(groupchats[gnum].oldpeer_names, groupchats[gnum].peer_names, N * npeers);
    memcpy(groupchats[gnum].oldpeer_name_lengths, groupchats[gnum].peer_name_lengths, sizeof(uint16_t) * npeers);
}
예제 #4
0
파일: options.cpp 프로젝트: ckamm/libzmq
int zmq::options_t::setsockopt (int option_, const void *optval_,
    size_t optvallen_)
{
    bool is_int = (optvallen_ == sizeof (int));
    int value = is_int? *((int *) optval_): 0;
    
    switch (option_) {
        case ZMQ_SNDHWM:
            if (is_int && value >= 0) {
                sndhwm = value;
                return 0;
            }
            break;
        
        case ZMQ_RCVHWM:
            if (is_int && value >= 0) {
                rcvhwm = value;
                return 0;
            }
            break;

        case ZMQ_AFFINITY:
            if (optvallen_ == sizeof (uint64_t)) {
                affinity = *((uint64_t*) optval_);
                return 0;
            }
            break;

        case ZMQ_IDENTITY:
            //  Empty identity is invalid as well as identity longer than
            //  255 bytes. Identity starting with binary zero is invalid
            //  as these are used for auto-generated identities.
            if (optvallen_ > 0 && optvallen_ < 256
            && *((const unsigned char *) optval_) != 0) {
                identity_size = optvallen_;
                memcpy (identity, optval_, identity_size);
                return 0;
            }
            break;

        case ZMQ_RATE:
            if (is_int && value > 0) {
                rate = value;
                return 0;
            }
            break;

        case ZMQ_RECOVERY_IVL:
            if (is_int && value >= 0) {
                recovery_ivl = value;
                return 0;
            }
            break;

        case ZMQ_SNDBUF:
            if (is_int && value >= 0) {
                sndbuf = value;
                return 0;
            }
            break;

        case ZMQ_RCVBUF:
            if (is_int && value >= 0) {
                rcvbuf = value;
                return 0;
            }
            break;

        case ZMQ_LINGER:
            if (is_int && value >= -1) {
                linger = value;
                return 0;
            }
            break;

        case ZMQ_RECONNECT_IVL:
            if (is_int && value >= -1) {
                reconnect_ivl = value;
                return 0;
            }
            break;

        case ZMQ_RECONNECT_IVL_MAX:
            if (is_int && value >= 0) {
                reconnect_ivl_max = value;
                return 0;
            }
            break;

        case ZMQ_BACKLOG:
            if (is_int && value >= 0) {
                backlog = value;
                return 0;
            }
            break;

        case ZMQ_MAXMSGSIZE:
            if (optvallen_ == sizeof (int64_t)) {
                maxmsgsize = *((int64_t *) optval_);
                return 0;
            }
            break;

        case ZMQ_MULTICAST_HOPS:
            if (is_int && value > 0) {
                multicast_hops = value;
                return 0;
            }
            break;

        case ZMQ_RCVTIMEO:
            if (is_int && value >= -1) {
                rcvtimeo = value;
                return 0;
            }
            break;

        case ZMQ_SNDTIMEO:
            if (is_int && value >= -1) {
                sndtimeo = value;
                return 0;
            }
            break;

        /*  Deprecated in favor of ZMQ_IPV6  */
        case ZMQ_IPV4ONLY:
            if (is_int && (value == 0 || value == 1)) {
                ipv6 = (value == 0);
                return 0;
            }
            break;

        /*  To replace the somewhat surprising IPV4ONLY */
        case ZMQ_IPV6:
            if (is_int && (value == 0 || value == 1)) {
                ipv6 = (value != 0);
                return 0;
            }
            break;

        case ZMQ_TCP_KEEPALIVE:
            if (is_int && (value >= -1 || value <= 1)) {
                tcp_keepalive = value;
                return 0;
            }
            break;

        case ZMQ_TCP_KEEPALIVE_CNT:
            if (is_int && (value == -1 || value >= 0)) {
                tcp_keepalive_cnt = value;
                return 0;
            }
            break;

        case ZMQ_TCP_KEEPALIVE_IDLE:
            if (is_int && (value == -1 || value >= 0)) {
                tcp_keepalive_idle = value;
                return 0;
            }
            break;

        case ZMQ_TCP_KEEPALIVE_INTVL:
            if (is_int && (value == -1 || value >= 0)) {
                tcp_keepalive_intvl = value;
                return 0;
            }
            break;
            
        case ZMQ_IMMEDIATE:
            if (is_int && (value == 0 || value == 1)) {
                immediate = value;
                return 0;
            }
            break;

        case ZMQ_TCP_ACCEPT_FILTER:
            if (optvallen_ == 0 && optval_ == NULL) {
                tcp_accept_filters.clear ();
                return 0;
            }
            else
            if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL && *((const char*) optval_) != 0) {
                std::string filter_str ((const char *) optval_, optvallen_);
                tcp_address_mask_t mask;
                int rc = mask.resolve (filter_str.c_str (), ipv6);
                if (rc == 0) {
                    tcp_accept_filters.push_back (mask);
                    return 0;
                }
            }
            break;
            
        case ZMQ_PLAIN_SERVER:
            if (is_int && (value == 0 || value == 1)) {
                as_server = value;
                mechanism = value? ZMQ_PLAIN: ZMQ_NULL;
                return 0;
            }
            break;
            
        case ZMQ_PLAIN_USERNAME:
            if (optvallen_ == 0 && optval_ == NULL) {
                mechanism = ZMQ_NULL;
                return 0;
            }
            else
            if (optvallen_ >= 0 && optvallen_ < 256 && optval_ != NULL) {
                plain_username.assign ((const char *) optval_, optvallen_);
                as_server = 0;
                mechanism = ZMQ_PLAIN;
                return 0;
            }
            break;
            
        case ZMQ_PLAIN_PASSWORD:
            if (optvallen_ == 0 && optval_ == NULL) {
                mechanism = ZMQ_NULL;
                return 0;
            }
            else
            if (optvallen_ >= 0 && optvallen_ < 256 && optval_ != NULL) {
                plain_password.assign ((const char *) optval_, optvallen_);
                as_server = 0;
                mechanism = ZMQ_PLAIN;
                return 0;
            }
            break;
        
        //  If libsodium isn't installed, these options provoke EINVAL
#       ifdef HAVE_LIBSODIUM
        case ZMQ_CURVE_SERVER:
            if (is_int && (value == 0 || value == 1)) {
                as_server = value;
                mechanism = value? ZMQ_CURVE: ZMQ_NULL;
                return 0;
            }
            break;
            
        case ZMQ_CURVE_PUBLICKEY:
            if (optvallen_ == CURVE_KEYSIZE) {
                memcpy (curve_public_key, optval_, CURVE_KEYSIZE);
                mechanism = ZMQ_CURVE;
                return 0;
            }
            else
            if (optvallen_ == CURVE_KEYSIZE_Z85) {
                Z85_decode (curve_public_key, (char *) optval_);
                mechanism = ZMQ_CURVE;
                return 0;
            }
            break;
            
        case ZMQ_CURVE_SECRETKEY:
            if (optvallen_ == CURVE_KEYSIZE) {
                memcpy (curve_secret_key, optval_, CURVE_KEYSIZE);
                mechanism = ZMQ_CURVE;
                return 0;
            }
            else
            if (optvallen_ == CURVE_KEYSIZE_Z85) {
                Z85_decode (curve_secret_key, (char *) optval_);
                mechanism = ZMQ_CURVE;
                return 0;
            }
            break;
            
        case ZMQ_CURVE_SERVERKEY:
            if (optvallen_ == CURVE_KEYSIZE) {
                memcpy (curve_server_key, optval_, CURVE_KEYSIZE);
                as_server = 0;
                mechanism = ZMQ_CURVE;
                return 0;
            }
            else
            if (optvallen_ == CURVE_KEYSIZE_Z85) {
                Z85_decode (curve_server_key, (char *) optval_);
                as_server = 0;
                mechanism = ZMQ_CURVE;
                return 0;
            }
            break;
#       endif
    }
    errno = EINVAL;
    return -1;
}
예제 #5
0
int zmq::options_t::setsockopt (int option_, const void *optval_,
                                size_t optvallen_)
{
    bool is_int = (optvallen_ == sizeof (int));
    int value = is_int? *((int *) optval_): 0;
#if defined (ZMQ_ACT_MILITANT)
    bool malformed = true;          //  Did caller pass a bad option value?
#endif

    switch (option_) {
    case ZMQ_SNDHWM:
        if (is_int && value >= 0) {
            sndhwm = value;
            return 0;
        }
        break;

    case ZMQ_RCVHWM:
        if (is_int && value >= 0) {
            rcvhwm = value;
            return 0;
        }
        break;

    case ZMQ_AFFINITY:
        if (optvallen_ == sizeof (uint64_t)) {
            affinity = *((uint64_t*) optval_);
            return 0;
        }
        break;

    case ZMQ_IDENTITY:
        //  Identity is any binary string from 1 to 255 octets
        if (optvallen_ > 0 && optvallen_ < 256) {
            identity_size = optvallen_;
            memcpy (identity, optval_, identity_size);
            return 0;
        }
        break;

    case ZMQ_RATE:
        if (is_int && value > 0) {
            rate = value;
            return 0;
        }
        break;

    case ZMQ_RECOVERY_IVL:
        if (is_int && value >= 0) {
            recovery_ivl = value;
            return 0;
        }
        break;

    case ZMQ_SNDBUF:
        if (is_int && value >= 0) {
            sndbuf = value;
            return 0;
        }
        break;

    case ZMQ_RCVBUF:
        if (is_int && value >= 0) {
            rcvbuf = value;
            return 0;
        }
        break;

    case ZMQ_TOS:
        if (is_int && value >= 0) {
            tos = value;
            return 0;
        }
        break;

    case ZMQ_LINGER:
        if (is_int && value >= -1) {
            linger = value;
            return 0;
        }
        break;

    case ZMQ_RECONNECT_IVL:
        if (is_int && value >= -1) {
            reconnect_ivl = value;
            return 0;
        }
        break;

    case ZMQ_RECONNECT_IVL_MAX:
        if (is_int && value >= 0) {
            reconnect_ivl_max = value;
            return 0;
        }
        break;

    case ZMQ_BACKLOG:
        if (is_int && value >= 0) {
            backlog = value;
            return 0;
        }
        break;

    case ZMQ_MAXMSGSIZE:
        if (optvallen_ == sizeof (int64_t)) {
            maxmsgsize = *((int64_t *) optval_);
            return 0;
        }
        break;

    case ZMQ_MULTICAST_HOPS:
        if (is_int && value > 0) {
            multicast_hops = value;
            return 0;
        }
        break;

    case ZMQ_RCVTIMEO:
        if (is_int && value >= -1) {
            rcvtimeo = value;
            return 0;
        }
        break;

    case ZMQ_SNDTIMEO:
        if (is_int && value >= -1) {
            sndtimeo = value;
            return 0;
        }
        break;

    /*  Deprecated in favor of ZMQ_IPV6  */
    case ZMQ_IPV4ONLY:
        if (is_int && (value == 0 || value == 1)) {
            ipv6 = (value == 0);
            return 0;
        }
        break;

    /*  To replace the somewhat surprising IPV4ONLY */
    case ZMQ_IPV6:
        if (is_int && (value == 0 || value == 1)) {
            ipv6 = (value != 0);
            return 0;
        }
        break;

    case ZMQ_SOCKS_PROXY:
        if (optval_ == NULL && optvallen_ == 0) {
            socks_proxy_address.clear ();
            return 0;
        }
        else if (optval_ != NULL && optvallen_ > 0 ) {
            socks_proxy_address =
                std::string ((const char *) optval_, optvallen_);
            return 0;
        }
        break;

    case ZMQ_TCP_KEEPALIVE:
        if (is_int && (value == -1 || value == 0 || value == 1)) {
            tcp_keepalive = value;
            return 0;
        }
        break;

    case ZMQ_TCP_KEEPALIVE_CNT:
        if (is_int && (value == -1 || value >= 0)) {
            tcp_keepalive_cnt = value;
            return 0;
        }
        break;

    case ZMQ_TCP_KEEPALIVE_IDLE:
        if (is_int && (value == -1 || value >= 0)) {
            tcp_keepalive_idle = value;
            return 0;
        }
        break;

    case ZMQ_TCP_KEEPALIVE_INTVL:
        if (is_int && (value == -1 || value >= 0)) {
            tcp_keepalive_intvl = value;
            return 0;
        }
        break;

    case ZMQ_IMMEDIATE:
        if (is_int && (value == 0 || value == 1)) {
            immediate = value;
            return 0;
        }
        break;

    case ZMQ_TCP_ACCEPT_FILTER:
        if (optvallen_ == 0 && optval_ == NULL) {
            tcp_accept_filters.clear ();
            return 0;
        }
        else if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL && *((const char*) optval_) != 0) {
            std::string filter_str ((const char *) optval_, optvallen_);
            tcp_address_mask_t mask;
            int rc = mask.resolve (filter_str.c_str (), ipv6);
            if (rc == 0) {
                tcp_accept_filters.push_back (mask);
                return 0;
            }
        }
        break;

#       if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
    case ZMQ_IPC_FILTER_UID:
        if (optvallen_ == 0 && optval_ == NULL) {
            ipc_uid_accept_filters.clear ();
            return 0;
        }
        else if (optvallen_ == sizeof (uid_t) && optval_ != NULL) {
            ipc_uid_accept_filters.insert (*((uid_t *) optval_));
            return 0;
        }
        break;

    case ZMQ_IPC_FILTER_GID:
        if (optvallen_ == 0 && optval_ == NULL) {
            ipc_gid_accept_filters.clear ();
            return 0;
        }
        else if (optvallen_ == sizeof (gid_t) && optval_ != NULL) {
            ipc_gid_accept_filters.insert (*((gid_t *) optval_));
            return 0;
        }
        break;
#       endif

#       if defined ZMQ_HAVE_SO_PEERCRED
    case ZMQ_IPC_FILTER_PID:
        if (optvallen_ == 0 && optval_ == NULL) {
            ipc_pid_accept_filters.clear ();
            return 0;
        }
        else if (optvallen_ == sizeof (pid_t) && optval_ != NULL) {
            ipc_pid_accept_filters.insert (*((pid_t *) optval_));
            return 0;
        }
        break;
#       endif

    case ZMQ_PLAIN_SERVER:
        if (is_int && (value == 0 || value == 1)) {
            as_server = value;
            mechanism = value? ZMQ_PLAIN: ZMQ_NULL;
            return 0;
        }
        break;

    case ZMQ_PLAIN_USERNAME:
        if (optvallen_ == 0 && optval_ == NULL) {
            mechanism = ZMQ_NULL;
            return 0;
        }
        else if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL) {
            plain_username.assign ((const char *) optval_, optvallen_);
            as_server = 0;
            mechanism = ZMQ_PLAIN;
            return 0;
        }
        break;

    case ZMQ_PLAIN_PASSWORD:
        if (optvallen_ == 0 && optval_ == NULL) {
            mechanism = ZMQ_NULL;
            return 0;
        }
        else if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL) {
            plain_password.assign ((const char *) optval_, optvallen_);
            as_server = 0;
            mechanism = ZMQ_PLAIN;
            return 0;
        }
        break;

    case ZMQ_ZAP_DOMAIN:
        if (optvallen_ < 256) {
            zap_domain.assign ((const char *) optval_, optvallen_);
            return 0;
        }
        break;

        //  If libsodium isn't installed, these options provoke EINVAL
#       ifdef HAVE_LIBSODIUM
    case ZMQ_CURVE_SERVER:
        if (is_int && (value == 0 || value == 1)) {
            as_server = value;
            mechanism = value? ZMQ_CURVE: ZMQ_NULL;
            return 0;
        }
        break;

    case ZMQ_CURVE_PUBLICKEY:
        //  TODO: refactor repeated code for these three options
        //  into set_curve_key (destination, optval, optlen) method
        //  ==> set_curve_key (curve_public_key, optval_, optvallen_);
        if (optvallen_ == CURVE_KEYSIZE) {
            memcpy (curve_public_key, optval_, CURVE_KEYSIZE);
            mechanism = ZMQ_CURVE;
            return 0;
        }
        else if (optvallen_ == CURVE_KEYSIZE_Z85 + 1) {
            zmq_z85_decode (curve_public_key, (char *) optval_);
            mechanism = ZMQ_CURVE;
            return 0;
        }
        else
            //  Deprecated, not symmetrical with zmq_getsockopt
            if (optvallen_ == CURVE_KEYSIZE_Z85) {
                char z85_key [41];
                memcpy (z85_key, (char *) optval_, CURVE_KEYSIZE_Z85);
                z85_key [CURVE_KEYSIZE_Z85] = 0;
                zmq_z85_decode (curve_public_key, z85_key);
                mechanism = ZMQ_CURVE;
                return 0;
            }
        break;

    case ZMQ_CURVE_SECRETKEY:
        if (optvallen_ == CURVE_KEYSIZE) {
            memcpy (curve_secret_key, optval_, CURVE_KEYSIZE);
            mechanism = ZMQ_CURVE;
            return 0;
        }
        else if (optvallen_ == CURVE_KEYSIZE_Z85 + 1) {
            zmq_z85_decode (curve_secret_key, (char *) optval_);
            mechanism = ZMQ_CURVE;
            return 0;
        }
        else
            //  Deprecated, not symmetrical with zmq_getsockopt
            if (optvallen_ == CURVE_KEYSIZE_Z85) {
                char z85_key [41];
                memcpy (z85_key, (char *) optval_, CURVE_KEYSIZE_Z85);
                z85_key [CURVE_KEYSIZE_Z85] = 0;
                zmq_z85_decode (curve_secret_key, z85_key);
                mechanism = ZMQ_CURVE;
                return 0;
            }
        break;

    case ZMQ_CURVE_SERVERKEY:
        if (optvallen_ == CURVE_KEYSIZE) {
            memcpy (curve_server_key, optval_, CURVE_KEYSIZE);
            mechanism = ZMQ_CURVE;
            as_server = 0;
            return 0;
        }
        else if (optvallen_ == CURVE_KEYSIZE_Z85 + 1) {
            zmq_z85_decode (curve_server_key, (char *) optval_);
            mechanism = ZMQ_CURVE;
            as_server = 0;
            return 0;
        }
        else
            //  Deprecated, not symmetrical with zmq_getsockopt
            if (optvallen_ == CURVE_KEYSIZE_Z85) {
                char z85_key [41];
                memcpy (z85_key, (char *) optval_, CURVE_KEYSIZE_Z85);
                z85_key [CURVE_KEYSIZE_Z85] = 0;
                zmq_z85_decode (curve_server_key, z85_key);
                mechanism = ZMQ_CURVE;
                as_server = 0;
                return 0;
            }
        break;
#       endif

    case ZMQ_CONFLATE:
        if (is_int && (value == 0 || value == 1)) {
            conflate = (value != 0);
            return 0;
        }
        break;

        //  If libgssapi isn't installed, these options provoke EINVAL
#       ifdef HAVE_LIBGSSAPI_KRB5
    case ZMQ_GSSAPI_SERVER:
        if (is_int && (value == 0 || value == 1)) {
            as_server = value;
            mechanism = ZMQ_GSSAPI;
            return 0;
        }
        break;

    case ZMQ_GSSAPI_PRINCIPAL:
        if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL) {
            gss_principal.assign ((const char *) optval_, optvallen_);
            mechanism = ZMQ_GSSAPI;
            return 0;
        }
        break;

    case ZMQ_GSSAPI_SERVICE_PRINCIPAL:
        if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL) {
            gss_service_principal.assign ((const char *) optval_, optvallen_);
            mechanism = ZMQ_GSSAPI;
            as_server = 0;
            return 0;
        }
        break;

    case ZMQ_GSSAPI_PLAINTEXT:
        if (is_int && (value == 0 || value == 1)) {
            gss_plaintext = (value != 0);
            return 0;
        }
        break;
#       endif

    case ZMQ_HANDSHAKE_IVL:
        if (is_int && value >= 0) {
            handshake_ivl = value;
            return 0;
        }
        break;

    default:
#if defined (ZMQ_ACT_MILITANT)
        //  There are valid scenarios for probing with unknown socket option
        //  values, e.g. to check if security is enabled or not. This will not
        //  provoke a militant assert. However, passing bad values to a valid
        //  socket option will, if ZMQ_ACT_MILITANT is defined.
        malformed = false;
#endif
        break;
    }
#if defined (ZMQ_ACT_MILITANT)
    //  There is no valid use case for passing an error back to the application
    //  when it sent malformed arguments to a socket option. Use ./configure
    //  --with-militant to enable this checking.
    if (malformed)
        zmq_assert (false);
#endif
    errno = EINVAL;
    return -1;
}
예제 #6
0
static void friendlist_onDraw(ToxWindow *self, Tox *m)
{
    curs_set(0);
    werase(self->window);
    int x2, y2;
    getmaxyx(self->window, y2, x2);

    bool fix_statuses = x2 != self->x;    /* true if window max x value has changed */

    wattron(self->window, COLOR_PAIR(CYAN));
    wprintw(self->window, " Press the");
    wattron(self->window, A_BOLD);
    wprintw(self->window, " h ");
    wattroff(self->window, A_BOLD);
    wprintw(self->window, "key for help\n\n");
    wattroff(self->window, COLOR_PAIR(CYAN));

    if (blocklist_view == 1) {
        blocklist_onDraw(self, m, y2, x2);
        return;
    }

    uint64_t cur_time = time(NULL);
    struct tm cur_loc_tm = *localtime((const time_t *) &cur_time);

    wattron(self->window, A_BOLD);
    wprintw(self->window, " Online: ");
    wattroff(self->window, A_BOLD);

    wprintw(self->window, "%d/%d \n\n", Friends.num_online, Friends.num_friends);

    if ((y2 - FLIST_OFST) <= 0)
        return;

    uint32_t selected_num = 0;

    /* Determine which portion of friendlist to draw based on current position */
    pthread_mutex_lock(&Winthread.lock);
    int page = Friends.num_selected / (y2 - FLIST_OFST);
    pthread_mutex_unlock(&Winthread.lock);

    int start = (y2 - FLIST_OFST) * page;
    int end = y2 - FLIST_OFST + start;

    pthread_mutex_lock(&Winthread.lock);
    size_t num_friends = Friends.num_friends;
    pthread_mutex_unlock(&Winthread.lock);

    int i;

    for (i = start; i < num_friends && i < end; ++i) {
        pthread_mutex_lock(&Winthread.lock);
        uint32_t f = Friends.index[i];
        bool is_active = Friends.list[f].active;
        int num_selected = Friends.num_selected;
        pthread_mutex_unlock(&Winthread.lock);

        bool f_selected = false;

        if (is_active) {
            if (i == num_selected) {
                wattron(self->window, A_BOLD);
                wprintw(self->window, " > ");
                wattroff(self->window, A_BOLD);
                selected_num = f;
                f_selected = true;
            } else {
                wprintw(self->window, "   ");
            }

            pthread_mutex_lock(&Winthread.lock);
            TOX_CONNECTION connection_status = Friends.list[f].connection_status;
            TOX_USER_STATUS status = Friends.list[f].status;
            pthread_mutex_unlock(&Winthread.lock);

            if (connection_status != TOX_CONNECTION_NONE) {
                int colour = MAGENTA;

                switch (status) {
                    case TOX_USER_STATUS_NONE:
                        colour = GREEN;
                        break;
                    case TOX_USER_STATUS_AWAY:
                        colour = YELLOW;
                        break;
                    case TOX_USER_STATUS_BUSY:
                        colour = RED;
                        break;
                }

                wattron(self->window, COLOR_PAIR(colour) | A_BOLD);
                wprintw(self->window, "%s ", ONLINE_CHAR);
                wattroff(self->window, COLOR_PAIR(colour) | A_BOLD);

                if (f_selected)
                    wattron(self->window, COLOR_PAIR(BLUE));

                wattron(self->window, A_BOLD);
                pthread_mutex_lock(&Winthread.lock);
                wprintw(self->window, "%s", Friends.list[f].name);
                pthread_mutex_unlock(&Winthread.lock);
                wattroff(self->window, A_BOLD);

                if (f_selected)
                    wattroff(self->window, COLOR_PAIR(BLUE));

                /* Reset Friends.list[f].statusmsg on window resize */
                if (fix_statuses) {
                    char statusmsg[TOX_MAX_STATUS_MESSAGE_LENGTH];

                    pthread_mutex_lock(&Winthread.lock);
                    tox_friend_get_status_message(m, Friends.list[f].num, (uint8_t *) statusmsg, NULL);
                    size_t s_len = tox_friend_get_status_message_size(m, Friends.list[f].num, NULL);
                    pthread_mutex_unlock(&Winthread.lock);

                    statusmsg[s_len] = '\0';

                    filter_str(statusmsg, s_len);

                    pthread_mutex_lock(&Winthread.lock);
                    snprintf(Friends.list[f].statusmsg, sizeof(Friends.list[f].statusmsg), "%s", statusmsg);
                    Friends.list[f].statusmsg_len = strlen(Friends.list[f].statusmsg);
                    pthread_mutex_unlock(&Winthread.lock);
                }

                /* Truncate note if it doesn't fit on one line */
                size_t maxlen = x2 - getcurx(self->window) - 2;

                pthread_mutex_lock(&Winthread.lock);

                if (Friends.list[f].statusmsg_len > maxlen) {
                    Friends.list[f].statusmsg[maxlen - 3] = '\0';
                    strcat(Friends.list[f].statusmsg, "...");
                    Friends.list[f].statusmsg[maxlen] = '\0';
                    Friends.list[f].statusmsg_len = maxlen;
                }

                if (Friends.list[f].statusmsg_len > 0)
                    wprintw(self->window, " %s", Friends.list[f].statusmsg);

                pthread_mutex_unlock(&Winthread.lock);

                wprintw(self->window, "\n");
            } else {
                wprintw(self->window, "%s ", OFFLINE_CHAR);

                if (f_selected)
                    wattron(self->window, COLOR_PAIR(BLUE));

                wattron(self->window, A_BOLD);
                pthread_mutex_lock(&Winthread.lock);
                wprintw(self->window, "%s", Friends.list[f].name);
                pthread_mutex_unlock(&Winthread.lock);
                wattroff(self->window, A_BOLD);

                if (f_selected)
                    wattroff(self->window, COLOR_PAIR(BLUE));

                pthread_mutex_lock(&Winthread.lock);
                uint64_t last_seen = Friends.list[f].last_online.last_on;
                pthread_mutex_unlock(&Winthread.lock);

                if (last_seen != 0) {
                    pthread_mutex_lock(&Winthread.lock);

                    int day_dist = (
                            cur_loc_tm.tm_yday - Friends.list[f].last_online.tm.tm_yday
                        + ((cur_loc_tm.tm_year - Friends.list[f].last_online.tm.tm_year) * 365)
                    );
                    const char *hourmin = Friends.list[f].last_online.hour_min_str;

                    pthread_mutex_unlock(&Winthread.lock);

                    switch (day_dist) {
                        case 0:
                            wprintw(self->window, " Last seen: Today %s\n", hourmin);
                            break;

                        case 1:
                            wprintw(self->window, " Last seen: Yesterday %s\n", hourmin);
                            break;

                        default:
                            wprintw(self->window, " Last seen: %d days ago\n", day_dist);
                            break;
                    }
                } else {
                    wprintw(self->window, " Last seen: Never\n");
                }
            }
        }
    }

    self->x = x2;

    if (num_friends) {
        wmove(self->window, y2 - 1, 1);

        wattron(self->window, A_BOLD);
        wprintw(self->window, "Key: ");
        wattroff(self->window, A_BOLD);

        int i;

        for (i = 0; i < TOX_PUBLIC_KEY_SIZE; ++i)
            wprintw(self->window, "%02X", Friends.list[selected_num].pub_key[i] & 0xff);
    }

    wrefresh(self->window);
    draw_del_popup();

    if (self->help->active)
        help_onDraw(self);
}
예제 #7
0
파일: options.cpp 프로젝트: JonDyte/libzmq
int zmq::options_t::setsockopt (int option_, const void *optval_,
    size_t optvallen_)
{
    bool valid = true;
    bool is_int = (optvallen_ == sizeof (int));
    int value = is_int? *((int *) optval_): 0;
    
    switch (option_) {
        case ZMQ_SNDHWM:
            if (is_int && value >= 0)
                sndhwm = value;
            else
                valid = false;
            break;
        
        case ZMQ_RCVHWM:
            if (is_int && value >= 0)
                rcvhwm = value;
            else
                valid = false;
            break;

        case ZMQ_AFFINITY:
            if (optvallen_ == sizeof (uint64_t))
                affinity = *((uint64_t*) optval_);
            else
                valid = false;
            break;

        case ZMQ_IDENTITY:
            //  Empty identity is invalid as well as identity longer than
            //  255 bytes. Identity starting with binary zero is invalid
            //  as these are used for auto-generated identities.
            if (optvallen_ > 0 && optvallen_ < 256
            && *((const unsigned char *) optval_) != 0) {
                identity_size = optvallen_;
                memcpy (identity, optval_, identity_size);
            }
            else
                valid = false;
            break;

        case ZMQ_RATE:
            if (is_int && value > 0)
                rate = value;
            else
                valid = false;
            break;

        case ZMQ_RECOVERY_IVL:
            if (is_int && value >= 0)
                recovery_ivl = value;
            else
                valid = false;

        case ZMQ_SNDBUF:
            if (is_int && value >= 0)
                sndbuf = value;
            else
                valid = false;
            break;

        case ZMQ_RCVBUF:
            if (is_int && value >= 0)
                rcvbuf = value;
            else
                valid = false;
            break;

        case ZMQ_LINGER:
            if (is_int && value >= -1)
                linger = value;
            else
                valid = false;
            break;

        case ZMQ_RECONNECT_IVL:
            if (is_int && value >= -1)
                reconnect_ivl = value;
            else
                valid = false;
            break;

        case ZMQ_RECONNECT_IVL_MAX:
            if (is_int && value >= 0)
                reconnect_ivl_max = value;
            else 
                valid = false;
            break;

        case ZMQ_BACKLOG:
            if (is_int && value >= 0)
                backlog = value;
            else
                valid = false;
            break;

        case ZMQ_MAXMSGSIZE:
            if (optvallen_ == sizeof (int64_t))
                maxmsgsize = *((int64_t *) optval_);
            else
                valid = false;
            break;

        case ZMQ_MULTICAST_HOPS:
            if (is_int && value > 0)
                multicast_hops = value;
            else
                valid = false;
            break;

        case ZMQ_RCVTIMEO:
            if (is_int && value >= -1)
                rcvtimeo = value;
            else
                valid = false;
            break;

        case ZMQ_SNDTIMEO:
            if (is_int && value >= -1)
                sndtimeo = value;
            else
                valid = false;
            break;

        /*  Deprecated in favor of ZMQ_IPV6  */
        case ZMQ_IPV4ONLY:
            if (is_int && (value == 0 || value == 1))
                ipv6 = (value == 0);
            else
                valid = false;
            break;

        /*  To replace the somewhat surprising IPV4ONLY */
        case ZMQ_IPV6:
            if (is_int && (value == 0 || value == 1))
                ipv6 = (value != 0);
            else
                valid = false;
            break;

        case ZMQ_TCP_KEEPALIVE:
            if (is_int && (value >= -1 || value <= 1))
                tcp_keepalive = value;
            else
                valid = false;
            break;

        case ZMQ_TCP_KEEPALIVE_CNT:
            if (is_int && (value == -1 || value >= 0))
                tcp_keepalive_cnt = value;
            else
                valid = false;
            break;

        case ZMQ_TCP_KEEPALIVE_IDLE:
            if (is_int && (value == -1 || value >= 0))
                tcp_keepalive_idle = value;
            else
                valid = false;
            break;

        case ZMQ_TCP_KEEPALIVE_INTVL:
            if (is_int && (value == -1 || value >= 0))
                tcp_keepalive_intvl = value;
            else
                valid = false;
            break;
            
        case ZMQ_IMMEDIATE:
            if (is_int && (value == 0 || value == 1))
                immediate = value;
            else
                valid = false;
            break;

        case ZMQ_TCP_ACCEPT_FILTER:
            if (optvallen_ == 0 && optval_ == NULL)
                tcp_accept_filters.clear ();
            else
            if (optvallen_ < 1 || optvallen_ > 255 || optval_ == NULL || *((const char*) optval_) == 0)
                valid = false;
            else {
                std::string filter_str ((const char *) optval_, optvallen_);
                tcp_address_mask_t mask;
                int rc = mask.resolve (filter_str.c_str (), ipv6);
                if (rc == 0)
                    tcp_accept_filters.push_back (mask);
                else
                    valid = false;
            }
            break;
            
        default:
            valid = false;
            break;
    }
    if (valid)
        return 0;
    else {
        errno = EINVAL;
        return -1;
    }
}
예제 #8
0
int zmq::options_t::setsockopt (int option_, const void *optval_,
    size_t optvallen_)
{
    switch (option_) {

    case ZMQ_SNDHWM:
        if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
            errno = EINVAL;
            return -1;
        }
        sndhwm = *((int*) optval_);
        return 0;

    case ZMQ_RCVHWM:
        if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
            errno = EINVAL;
            return -1;
        }
        rcvhwm = *((int*) optval_);
        return 0;

    case ZMQ_AFFINITY:
        if (optvallen_ != sizeof (uint64_t)) {
            errno = EINVAL;
            return -1;
        }
        affinity = *((uint64_t*) optval_);
        return 0;

    case ZMQ_IDENTITY:

        //  Empty identity is invalid as well as identity longer than
        //  255 bytes. Identity starting with binary zero is invalid
        //  as these are used for auto-generated identities.
        if (optvallen_ < 1 || optvallen_ > 255 ||
              *((const unsigned char*) optval_) == 0) {
            errno = EINVAL;
            return -1;
        }
        identity_size = optvallen_;
        memcpy (identity, optval_, identity_size);
        return 0;

    case ZMQ_RATE:
        if (optvallen_ != sizeof (int) || *((int*) optval_) <= 0) {
            errno = EINVAL;
            return -1;
        }
        rate = *((int*) optval_);
        return 0;

    case ZMQ_RECOVERY_IVL:
        if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
            errno = EINVAL;
            return -1;
        }
        recovery_ivl = *((int*) optval_);
        return 0;

    case ZMQ_SNDBUF:
        if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
            errno = EINVAL;
            return -1;
        }
        sndbuf = *((int*) optval_);
        return 0;

    case ZMQ_RCVBUF:
        if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
            errno = EINVAL;
            return -1;
        }
        rcvbuf = *((int*) optval_);
        return 0;

    case ZMQ_LINGER:
        if (optvallen_ != sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        linger = *((int*) optval_);
        return 0;

    case ZMQ_RECONNECT_IVL:
        if (optvallen_ != sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        if (*((int*) optval_) < -1) {
            errno = EINVAL;
            return -1;
        }
        reconnect_ivl = *((int*) optval_);
        return 0;

    case ZMQ_RECONNECT_IVL_MAX:
        if (optvallen_ != sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        if (*((int*) optval_) < 0) {
            errno = EINVAL;
            return -1;
        }
        reconnect_ivl_max = *((int*) optval_);
        return 0;

    case ZMQ_BACKLOG:
        if (optvallen_ != sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        backlog = *((int*) optval_);
        return 0;

    case ZMQ_MAXMSGSIZE:
        if (optvallen_ != sizeof (int64_t)) {
            errno = EINVAL;
            return -1;
        }
        maxmsgsize = *((int64_t*) optval_);
        return 0;

    case ZMQ_MULTICAST_HOPS:
        if (optvallen_ != sizeof (int) || *((int*) optval_) <= 0) {
            errno = EINVAL;
            return -1;
        }
        multicast_hops = *((int*) optval_);
        return 0;

    case ZMQ_RCVTIMEO:
        if (optvallen_ != sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        rcvtimeo = *((int*) optval_);
        return 0;

    case ZMQ_SNDTIMEO:
        if (optvallen_ != sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        sndtimeo = *((int*) optval_);
        return 0;

    case ZMQ_IPV4ONLY:
        {
            if (optvallen_ != sizeof (int)) {
                errno = EINVAL;
                return -1;
            }
            int val = *((int*) optval_);
            if (val != 0 && val != 1) {
                errno = EINVAL;
                return -1;
            }
            ipv4only = val;
            return 0;
        }

    case ZMQ_TCP_KEEPALIVE:
        {
            if (optvallen_ != sizeof (int)) {
                errno = EINVAL;
                return -1;
            }
            int val = *((int*) optval_);
            if (val != -1 && val != 0 && val != 1) {
                errno = EINVAL;
                return -1;
            }
#if defined ZMQ_HAVE_SO_KEEPALIVE
            tcp_keepalive = val;
#endif
            return 0;
        }

    case ZMQ_TCP_KEEPALIVE_CNT:
        {
            if (optvallen_ != sizeof (int)) {
                errno = EINVAL;
                return -1;
            }
            int val = *((int*) optval_);
            if (val <= 0 && val != -1) {
                errno = EINVAL;
                return -1;
            }
#if defined ZMQ_HAVE_SO_KEEPALIVE && defined ZMQ_HAVE_TCP_KEEPCNT
            tcp_keepalive_cnt = val;
#endif
            return 0;
        }

    case ZMQ_TCP_KEEPALIVE_IDLE:
        {
            if (optvallen_ != sizeof (int)) {
                errno = EINVAL;
                return -1;
            }
            int val = *((int*) optval_);
            if (val <= 0 && val != -1) {
                errno = EINVAL;
                return -1;
            }

#if defined ZMQ_HAVE_SO_KEEPALIVE && (defined ZMQ_HAVE_TCP_KEEPIDLE || defined ZMQ_HAVE_TCP_KEEPALIVE)
            tcp_keepalive_idle = val;
#endif
            return 0;
        }

    case ZMQ_TCP_KEEPALIVE_INTVL:
        {
            if (optvallen_ != sizeof (int)) {
                errno = EINVAL;
                return -1;
            }
            int val = *((int*) optval_);
            if (val <= 0 && val != -1) {
                errno = EINVAL;
                return -1;
            }
#if defined ZMQ_HAVE_SO_KEEPALIVE && defined ZMQ_HAVE_TCP_KEEPINTVL
            tcp_keepalive_intvl = val;
#endif
            return 0;
        }

    case ZMQ_TCP_ACCEPT_FILTER:
        {
            if (optvallen_ == 0 && optval_ == NULL) {
                tcp_accept_filters.clear ();
                return 0;
            }
            else
            if (optvallen_ < 1 || optvallen_ > 255 || optval_ == NULL || *((const char*) optval_) == 0) {
                errno = EINVAL;
                return -1;
            }
            else {
                std::string filter_str ((const char*) optval_, optvallen_);

                tcp_address_mask_t filter;
                int rc = filter.resolve (filter_str.c_str (), ipv4only ? true : false);
                if (rc != 0) {
                    errno = EINVAL;
                    return -1;
                }
                tcp_accept_filters.push_back(filter);

                return 0;
            }
        }
    }
    errno = EINVAL;
    return -1;
}