int pubnub_generate_uuid_v3_name_md5(
	struct Pubnub_UUID *uuid,
	struct Pubnub_UUID *nsid,
	void *name,
	unsigned namelen
    )
{
    PBMD5_CTX ctx;

    PUBNUB_ASSERT_OPT(uuid != NULL);
    PUBNUB_ASSERT_OPT(nsid != NULL);
    PUBNUB_ASSERT_OPT(name != NULL);

    pbmd5_init(&ctx);
    pbmd5_update(&ctx, nsid, sizeof *nsid);
    pbmd5_update(&ctx, name, namelen);
    pbmd5_final(&ctx, uuid->uuid);

    uuid->uuid[6] &= 0x0F;
    uuid->uuid[6] |= 0x30;
    uuid->uuid[8] &= 0x3F;
    uuid->uuid[8] |= 0x80;

    return 0;
}
Exemple #2
0
enum pubnub_res pubnub_get_decrypted(pubnub_t *pb, char const* cipher_key, char *s, size_t *n)
{
    char *msg;
    size_t msg_len;
    uint8_t decoded_msg[PUBNUB_BUF_MAXLEN];
    pubnub_bymebl_t data = { (uint8_t*)s, *n };
    pubnub_bymebl_t buffer = { decoded_msg, PUBNUB_BUF_MAXLEN };

    PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));
    PUBNUB_ASSERT_OPT(cipher_key != NULL);
    PUBNUB_ASSERT_OPT(s != NULL);
    PUBNUB_ASSERT_OPT(n != NULL);

    msg = (char*)pubnub_get(pb);
    if (NULL == msg) {
        return PNR_INTERNAL_ERROR;
    }
    msg_len = strlen(msg);
    if ((msg[0] != '"') || (msg[msg_len-1] != '"')) {
        return PNR_FORMAT_ERROR;
    }
    msg[msg_len - 1] = '\0';
    ++msg;

    pubnub_json_string_unescape_slash(msg);

    if (0 != pubnub_decrypt_buffered(cipher_key, msg, &data, &buffer)) {
        return PNR_INTERNAL_ERROR;
    }
    *n = data.size;
    data.ptr[data.size] = '\0';

    return PNR_OK;
}
Exemple #3
0
int pubnub_set_transaction_timeout(pubnub_t *p, int duration_ms)
{
    PUBNUB_ASSERT_OPT(p != NULL);
    PUBNUB_ASSERT_OPT(duration_ms > 0);
    p->transaction_timeout_ms = duration_ms;
    return 0;
}
/** Do the DNS QNAME (host) encoding. This strange kind of "run-time
    length encoding" will convert `"www.google.com"` to
    `"\3www\6google\3com"`.
 */
static unsigned char* dns_qname_encode(uint8_t *dns, size_t n, uint8_t const* host) 
{
    uint8_t *dest = dns + 1;
    uint8_t *lpos = dns;
    uint8_t const*const end = dns + n;

    PUBNUB_ASSERT_OPT(n > 0);
    PUBNUB_ASSERT_OPT(host != NULL);
    PUBNUB_ASSERT_OPT(dns != NULL);

    *lpos = '\0';
    while (dest < end) {
        char hc = *host++;
        if ((hc != '.') && (hc != '\0')) {
            *dest++ = hc;
        }
        else {
            size_t d = dest - lpos;
	    *dest++ = '\0';
            if (d > 63) {
                /* label too long */
                return NULL;
            }

            *lpos =  d - 1;
            lpos += d;

            if ('\0' == hc) {
                break;
            }
        }
    }

    return dns;
}
Exemple #5
0
pubnub_t *pubnub_timer_list_remove(pubnub_t *list, pubnub_t *to_remove)
{
    PUBNUB_ASSERT_OPT(list != NULL);
    PUBNUB_ASSERT_OPT(to_remove != NULL);

    if (list == to_remove) {
        list = list->next;
        if (NULL != list) {
            list->previous = NULL;
        }
        to_remove->previous = to_remove->next = NULL;
        return list;
    }

    if (NULL == to_remove->next) {
        to_remove->previous->next = NULL;
        to_remove->previous = NULL;
        return list;
    }

    to_remove->next->timeout_left_ms += to_remove->timeout_left_ms;
    to_remove->previous->next = to_remove->next;
    to_remove->next->previous = to_remove->previous;
    to_remove->previous = to_remove->next = NULL;

    return list;
}
Exemple #6
0
pubnub_t *pubnub_timer_list_as_time_goes_by(pubnub_t **pplist, int time_passed_ms)
{
    pubnub_t *list;
    pubnub_t *expired_list = NULL;

    PUBNUB_ASSERT_OPT(pplist != NULL);
    PUBNUB_ASSERT_OPT(time_passed_ms > 0);

    list = *pplist;
    if (NULL == list) {
        return NULL;
    }
    if (list->timeout_left_ms > time_passed_ms) {
        list->timeout_left_ms -= time_passed_ms;
        return NULL;
    }

    expired_list = list;
    while (list->timeout_left_ms <= time_passed_ms) {
        time_passed_ms -= list->timeout_left_ms;
        if (NULL == list->next) {
            *pplist = NULL;
            return expired_list;
        }
        list = list->next;
    }

    list->timeout_left_ms -= time_passed_ms;
    PUBNUB_ASSERT(list->previous != NULL);
    list->previous->next = NULL;
    list->previous = NULL;
    *pplist = list;

    return expired_list;
}
Exemple #7
0
int pubnub_set_connection_timeout(pubnub_t *p, int duration_s)
{
    PUBNUB_ASSERT_OPT(p != NULL);
    PUBNUB_ASSERT_OPT(duration_s > 0);
    p->connection_timeout_s = duration_s;
    return 0;
}
Exemple #8
0
enum pubnub_res pbcc_set_state_prep(struct pbcc_context *pb, char const *channel, char const *channel_group, const char *uuid, char const *state)
{
    PUBNUB_ASSERT_OPT(uuid != NULL);
    PUBNUB_ASSERT_OPT(state != NULL);

    if (NULL == channel) {
        if (NULL == channel_group) {
            return PNR_INVALID_CHANNEL;
        }
        channel = ",";
    }
    if (pb->msg_ofs < pb->msg_end) {
        return PNR_RX_BUFF_NOT_EMPTY;
    }

    pb->http_buf_len = snprintf(
        pb->http_buf, sizeof pb->http_buf,
        "/v2/presence/sub-key/%s/channel/%s/uuid/%s/data?pnsdk=%s&state=%s",
        pb->subscribe_key, channel, uuid,
        pubnub_uname(), state
        );
    APPEND_URL_PARAM_M(pb, "channel-group", channel_group, '&');
    APPEND_URL_PARAM_M(pb, "auth", pb->auth, '&');

    return PNR_STARTED;
}
void pbpal_ntf_callback_poller_deinit(struct pbpal_poll_data** data)
{
    PUBNUB_ASSERT_OPT(data != NULL);
    PUBNUB_ASSERT_OPT(*data != NULL);

    free(*data);
    *data = NULL;
}
int pbpal_ntf_poll_away(struct pbpal_poll_data* data, int ms)
{
    int            i;
    int            rslt;
    fd_set         readfds;
    fd_set         writefds;
    fd_set         exceptfds;
    struct timeval timeout;

    if (0 == data->size) {
        return 0;
    }

    timeout.tv_sec  = ms / 1000;
    timeout.tv_usec = (ms % 1000) * 1000;

    memcpy(&readfds, &data->readfds, sizeof readfds);
    memcpy(&writefds, &data->writefds, sizeof writefds);
    memcpy(&exceptfds, &data->exceptfds, sizeof exceptfds);
    rslt = select(data->nfds + 1, &readfds, &writefds, &exceptfds, &timeout);
    if (SOCKET_ERROR == rslt) {
        int last_err =
#if defined(_WIN32)
            WSAGetLastError()
#else
            errno
#endif
            ;
        /* error? what to do about it? */
        PUBNUB_LOG_WARNING(
            "poll size = %u, error = %d\n", (unsigned)data->size, last_err);
        return -1;
    }
    for (i = 0; (i < (int)data->size) && (rslt > 0); ++i) {
        bool should_process = false;
        if (FD_ISSET(data->asocket[i], &readfds)) {
            should_process = true;
            --rslt;
        }
        if (FD_ISSET(data->asocket[i], &writefds)) {
            should_process = true;
            --rslt;
            PUBNUB_ASSERT_OPT(rslt >= 0);
        }
        if (FD_ISSET(data->asocket[i], &exceptfds)) {
            should_process = true;
            --rslt;
            PUBNUB_ASSERT_OPT(rslt >= 0);
        }
        if (should_process) {
            pbntf_requeue_for_processing(data->apb[i]);
        }
    }
    PUBNUB_ASSERT_OPT(0 == rslt);

    return rslt;
}
void pubnub_subloop_stop(pubnub_subloop_t* pbsld)
{
    PUBNUB_ASSERT_OPT(NULL != pbsld);

    pubnub_mutex_lock(pbsld->monitor);
    PUBNUB_ASSERT_OPT(NULL != pbsld->pbp);
    pubnub_register_callback(
        pbsld->pbp, pbsld->saved_context_cb, pbsld->saved_context_user_data);
    pubnub_cancel(pbsld->pbp);
    pbsld->saved_context_cb        = NULL;
    pbsld->saved_context_user_data = NULL;
    pubnub_mutex_unlock(pbsld->monitor);
}
Exemple #12
0
pubnub_t *pubnub_timer_list_add(pubnub_t *list, pubnub_t *to_add)
{
    int timeout_to_add_ms = to_add->transaction_timeout_ms;
    pubnub_t *pbp;

    PUBNUB_ASSERT_OPT(to_add != NULL);

    if (NULL == list) {
        list = to_add;
        to_add->previous = to_add->next = NULL;
        to_add->timeout_left_ms = timeout_to_add_ms;
        return list;
    }

    PUBNUB_ASSERT_OPT(list != to_add);
    if (timeout_to_add_ms < list->timeout_left_ms) {
        list->timeout_left_ms -= timeout_to_add_ms;
        to_add->next = list;
        to_add->previous = NULL;
        list->previous = to_add;
        to_add->timeout_left_ms = timeout_to_add_ms;
        return to_add;
    }

    pbp = list;
    while (timeout_to_add_ms >= pbp->timeout_left_ms) {
        timeout_to_add_ms -= pbp->timeout_left_ms;
        if (NULL == pbp->next) {
            pbp->next = to_add;
            to_add->previous = pbp;
            to_add->next = NULL;
            to_add->timeout_left_ms = timeout_to_add_ms;
            return list;
        }
        pbp = pbp->next;
        PUBNUB_ASSERT_OPT(pbp != to_add);
    }

    pbp->timeout_left_ms -= timeout_to_add_ms;
    to_add->next = pbp;
    to_add->previous = pbp->previous;
    PUBNUB_ASSERT(NULL != pbp->previous);
    pbp->previous->next = to_add;

    pbp->previous = to_add;
    to_add->timeout_left_ms = timeout_to_add_ms;

    return list;
}
Exemple #13
0
bool pnfntst_got_message_on_channel(pubnub_t *p, char const *message, char const *channel)
{
	char const *msg;
	char const *chan;
    PUBNUB_ASSERT(pb_valid_ctx_ptr(p));
    PUBNUB_ASSERT_OPT(NULL != message);
    PUBNUB_ASSERT_OPT(NULL != channel);

	msg = pubnub_get(p);
	chan = pubnub_get_channel(p);
	if ((NULL == msg) || (NULL == chan)) {
		return false;
	}
    return (strcmp(msg, message) == 0) && (strcmp(chan, channel) == 0);
}
Exemple #14
0
pubnub_bymebl_t pubnub_get_decrypted_alloc(pubnub_t *pb, char const* cipher_key)
{
    char *msg;
    size_t msg_len;
    pubnub_bymebl_t result = { NULL, 0 };

    PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));
    PUBNUB_ASSERT_OPT(cipher_key != NULL);

    msg = (char*)pubnub_get(pb);
    if (NULL == msg) {
        return result;
    }
    msg_len = strlen(msg);
    if ((msg[0] != '"') || (msg[msg_len-1] != '"')) {
        return result;
    }
    msg[msg_len - 1] = '\0';
    ++msg;

    pubnub_json_string_unescape_slash(msg);

    result = pubnub_decrypt_alloc(cipher_key, msg);
    if (NULL != result.ptr) {
        result.ptr[result.size] = '\0';
    }

    return result;
}
static void prepare_port_and_hostname(pubnub_t *pb, uint16_t* p_port, char const** p_origin)
{
    PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));
    PUBNUB_ASSERT_OPT((pb->state == PBS_READY) || (pb->state == PBS_WAIT_DNS_SEND));
    *p_origin = PUBNUB_ORIGIN_SETTABLE ? pb->origin : PUBNUB_ORIGIN;
#if PUBNUB_USE_SSL
    if (pb->flags.trySSL) {
        PUBNUB_ASSERT(pb->options.useSSL);
        *p_port = TLS_PORT;
    }
#endif
#if PUBNUB_PROXY_API
    switch (pb->proxy_type) {
    case pbproxyHTTP_CONNECT:
        if (!pb->proxy_tunnel_established) {
            *p_origin = pb->proxy_hostname;
        }
        *p_port = pb->proxy_port;
        break;
    case pbproxyHTTP_GET:
        *p_origin = pb->proxy_hostname;
        *p_port = pb->proxy_port;
        PUBNUB_LOG_TRACE("Using proxy: %s : %hu\n", *p_origin, *p_port);
        break;
    default:
        break;
    }
#endif
    return;
}
enum pubnub_res pbpal_resolv_and_connect(pubnub_t *pb)
{
    struct freertos_sockaddr addr;

    PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));
    PUBNUB_ASSERT_OPT((pb->state == PBS_IDLE) || (pb->state == PBS_WAIT_DNS));
    
    addr.sin_port = FreeRTOS_htons(HTTP_PORT);
    addr.sin_addr = FreeRTOS_gethostbyname(PUBNUB_ORIGIN_SETTABLE ? pb->origin : PUBNUB_ORIGIN);
    if (addr.sin_addr == 0) {
        return PNR_CONNECT_FAILED;
    }

    pb->pal.socket = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP);
    if (pb->pal.socket == SOCKET_INVALID) {
        return PNR_CONNECT_FAILED;
    }
    if (FreeRTOS_connect(pb->pal.socket, &addr, sizeof addr) != 0) {
        FreeRTOS_closesocket(pb->pal.socket);
        pb->pal.socket = SOCKET_INVALID;
        return PNR_CONNECT_FAILED;
    }

    {
        TickType_t tmval = pdMS_TO_TICKS(pb->transaction_timeout_ms);
        FreeRTOS_setsockopt(pb->pal.socket, 0, FREERTOS_SO_RCVTIMEO, &tmval, sizeof tmval);
    }

    return PNR_OK;
}
Exemple #17
0
enum pubnub_publish_res pubnub_parse_publish_result(char const *result)
{
    PUBNUB_ASSERT_OPT(result != NULL);

    if (strcmp(result, "\"Sent\"") == 0) {
        return PNPUB_SENT;
    }
    if (strcmp(result, "\"Invalid JSON\"") == 0) {
        return PNPUB_INVALID_JSON;
    }
    if (strncmp(result, "\"Invalid Character in Channel Name", 34) == 0) {
        return PNPUB_INVALID_CHAR_IN_CHAN_NAME;
    }
    if (strncmp(result, "\"Account quota exceeded", 23) == 0) {
        return PNPUB_ACCOUNT_QUOTA_EXCEEDED;
    }
    if (strcmp(result, "\"Message Too Large\"") == 0) {
        return PNPUB_MESSAGE_TOO_LARGE;
    }
    if (strcmp(result, "\"Invalid Key\"") == 0) {
        return PNPUB_INVALID_PUBLISH_KEY;
    }
    if (strncmp(result, "\"message\":", 10) == 0) {
        if (strncmp(result + 10, "\"Invalid Subscribe Key\"", 23) == 0) {
            return PNPUB_INVALID_SUBSCRIBE_KEY;
        }
        return PNPUB_UNKNOWN_JSON_OBJECT;
    }
    return PNPUB_UNKNOWN_ERROR;
}
static void save_socket(struct SocketWatcherData *watcher, pubnub_t *pb)
{
    size_t i;
    pbpal_native_socket_t sockt = pubnub_get_native_socket(pb);

    PUBNUB_ASSERT(watcher->apoll_size <= watcher->apoll_cap);

    if (INVALID_SOCKET == sockt) {
        return;
    }
    for (i = 0; i < watcher->apoll_size; ++i) {
        PUBNUB_ASSERT_OPT(watcher->apoll[i].fd != sockt);
    }
    if (watcher->apoll_size == watcher->apoll_cap) {
        size_t newcap = watcher->apoll_cap + 2;
        WSAPOLLFD *npapoll = (WSAPOLLFD*)realloc(watcher->apoll, sizeof watcher->apoll[0] * newcap);
        if (NULL == npapoll) {
            return;
        }
        else {
            pubnub_t **npapb = (pubnub_t**)realloc(watcher->apb, sizeof watcher->apb[0] * newcap);
            watcher->apoll = npapoll;
            if (NULL == npapb) {
                return;
            }
            watcher->apb = npapb;
        }
        watcher->apoll_cap = newcap;
    }

    watcher->apoll[watcher->apoll_size].fd = sockt;
    watcher->apoll[watcher->apoll_size].events = POLLOUT;
    watcher->apb[watcher->apoll_size] = pb;
    ++watcher->apoll_size;
}
static void sublup_context_callback(pubnub_t*         pb,
                                    enum pubnub_trans trans,
                                    enum pubnub_res   result,
                                    void*             user_data)
{
    pubnub_subloop_t* pbsld = (pubnub_subloop_t*)user_data;

    PUBNUB_ASSERT_OPT(pbsld != NULL);

    pubnub_mutex_lock(pbsld->monitor);
    if (PBTT_SUBSCRIBE == trans) {
        if (PNR_OK == result) {
            char const* msg;
            for (msg = pubnub_get(pb); msg != NULL; msg = pubnub_get(pb)) {
                pbsld->cb(pb, msg, PNR_OK);
            }
        }
        else {
            pbsld->cb(pb, NULL, result);
        }
        result = pubnub_subscribe_ex(pbsld->pbp, pbsld->channel, pbsld->options);
        if (result != PNR_STARTED) {
            PUBNUB_LOG_ERROR("Failed to re-subscribe in the subscribe loop, "
                             "error code = %d\n",
                             result);
        }
    }
    pubnub_mutex_unlock(pbsld->monitor);
}
Exemple #20
0
/* Do the DNS label decoding. Apart from the RLE decoding of
   `3www6google3com0` -> `www.google.com`, it also has a
   "(de)compression" scheme in which a label can be shared with
   another in the same buffer.
*/
static unsigned char *dns_label_decode(uint8_t *label, size_t n, uint8_t const*src, uint8_t const* buffer, size_t buffer_size, size_t *o_bytes_to_skip)
{
    uint8_t *dest = label;
    uint8_t const*const end = label + n;
    
    PUBNUB_ASSERT_OPT(n > 0);
    PUBNUB_ASSERT_OPT(src != NULL);
    PUBNUB_ASSERT_OPT(buffer != NULL);
    PUBNUB_ASSERT_OPT(label != NULL);
    PUBNUB_ASSERT_OPT(o_bytes_to_skip != NULL);
    
    *o_bytes_to_skip = 0;
    while (dest < end) {
        uint8_t b = *src;
        if (b & 0xC0) {
            uint16_t offset = (b & 0x3F) * 256 + src[1];
            if (0 == *o_bytes_to_skip) {
                *o_bytes_to_skip = dest - label + 2;
            }
            if (offset >= buffer_size) {
                *dest = '\0';
                break;
            }
            src = buffer + offset;
        }
        else if (0 == b) {
            break;
        }
        else {
            if (dest != label) {
                *dest++ = '.';
            }
            if (dest + b >= end) {
                *dest = '\0';
                break;
            }
            memcpy(dest, src+1, b);
            dest[b] = '\0';
            dest += b;
            src += b+1;
        }
    }
    if (*o_bytes_to_skip == 0) {
        *o_bytes_to_skip = dest - label;
    }
    return label;
}
Exemple #21
0
bool pbpal_read_over(pubnub_t *pb)
{
    unsigned to_read = 0;
    WATCH_ENUM(pb->sock_state);
    WATCH_USHORT(pb->readlen);
    WATCH_USHORT(pb->left);
    WATCH_UINT(pb->len);

    if (pb->readlen == 0) {
        int recvres;
        to_read =  pb->len - pbpal_read_len(pb);
        if (to_read > pb->left) {
            to_read = pb->left;
        }
        recvres = socket_recv(pb->pal.socket, (char*)pb->ptr, to_read, 0);
        if (recvres <= 0) {
            /* This is error or connection close, which may be handled
               in some way...
             */
            return false;
        }
        pb->sock_state = STATE_READ;
        pb->readlen = recvres;
    } 


    to_read = pb->len;
    if (pb->readlen < to_read) {
        to_read = pb->readlen;
    }
    pb->ptr += to_read;
    pb->readlen -= to_read;
    PUBNUB_ASSERT_OPT(pb->left >= to_read);
    pb->left -= to_read;
    pb->len -= to_read;

    if (pb->len == 0) {
        pb->sock_state = STATE_NONE;
        return true;
    }

    if (pb->left == 0) {
        /* Buffer has been filled, but the requested block has not been
         * read.  We have to "reset" this "mini-fsm", as otherwise we
         * won't read anything any more. This means that we have lost
         * the current contents of the buffer, which is bad. In some
         * general code, that should be reported, as the caller could
         * save the contents of the buffer somewhere else or simply
         * decide to ignore this block (when it does end eventually).
         */
        pb->sock_state = STATE_NONE;
    }
    else {
        pb->sock_state = STATE_NEWDATA_EXHAUSTED;
        return false;
    }

    return true;
}
enum pbpal_resolv_n_connect_result pbpal_check_resolv_and_connect(pubnub_t *pb)
{
    /* Under OpenSSL, this function should never be called.  Either
       we're synchrnous and just connected or not, or we're async, in
       which case, pbpal_check_connect() will be called.
     */
    PUBNUB_ASSERT_OPT(pb == NULL);
    return pbpal_connect_failed;
}
Exemple #23
0
size_t pbjson_element_strcpy(struct pbjson_elem const* p, char* s, size_t n)
{
    size_t len;

    PUBNUB_ASSERT_OPT(p != NULL);
    PUBNUB_ASSERT_OPT(s != NULL);
    PUBNUB_ASSERT_OPT(n > 0);

    len = p->end - p->start;
    if (len >= n) {
        len = n - 1;
    }
    if (len > 0) {
        memcpy(s, p->start, len);
    }
    s[len] = '\0';

    return len + 1;
}
Exemple #24
0
int pbntf_got_socket(pubnub_t *pb, pb_socket_t socket)
{
    size_t i = find_pbp(&m_watcher, pb);
    PUBNUB_ASSERT_OPT(i == m_watcher.apb_size);
    if (i == m_watcher.apb_size) {
        save_socket(&m_watcher, pb);
        if (PUBNUB_TIMERS_API) {
            m_watcher.timer_head = pubnub_timer_list_add(m_watcher.timer_head, pb);
        }
    }
    return +1;
}
Exemple #25
0
enum pubnub_res pbcc_channel_registry_prep(struct pbcc_context *pb, char const *channel_group, char const *param, char const *channel)
{
    PUBNUB_ASSERT_OPT(channel_group != NULL);

    pb->http_buf_len = snprintf(
        pb->http_buf, sizeof pb->http_buf,
        "/v1/channel-registration/sub-key/%s/channel-group/%s?pnsdk=%s",
        pb->subscribe_key, channel_group, pubnub_uname()
        );
    if (NULL != param) {
        enum pubnub_res rslt;
        PUBNUB_ASSERT_OPT(channel != NULL);
        rslt = append_url_param(pb, param, strlen(param), channel, '&');
        if (rslt != PNR_OK) {
            return rslt;
        }
    }
    APPEND_URL_PARAM_M(pb, "auth", pb->auth, '&');

    return PNR_STARTED;
}
Exemple #26
0
int pbcrypto_signature(struct pbcc_context *pbcc, char const *channel, char const* msg, char *signature, size_t n)
{
    PBMD5_CTX md5;
    char s[2] = { '/', '\0' };
    uint8_t digest[16];

    PUBNUB_ASSERT_OPT(channel != NULL);
    PUBNUB_ASSERT_OPT(msg != NULL);
    PUBNUB_ASSERT_OPT(signature != NULL);
    PUBNUB_ASSERT_OPT(n > 32);

    if (!PUBNUB_CRYPTO_API || (NULL == pbcc->secret_key)) {
        return -1;
    }

    pbmd5_init(&md5);
    pbmd5_update_str(&md5, pbcc->publish_key);
    pbmd5_update(&md5, s, 1);
    pbmd5_update_str(&md5, pbcc->subscribe_key);
    pbmd5_update(&md5, s, 1);
    pbmd5_update_str(&md5, pbcc->secret_key);
    pbmd5_update(&md5, s, 1);
    pbmd5_update_str(&md5, channel);
    pbmd5_update(&md5, s, 1);
    pbmd5_update_str(&md5, msg);
    s[0] = '\0';
    pbmd5_update(&md5, s, 1);

    pbmd5_final(&md5, digest);

    snprintf(signature, n, 
             "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
             digest[0], digest[1], digest[2], digest[3],
             digest[4], digest[5], digest[6], digest[7],
             digest[8], digest[9], digest[10], digest[11],
             digest[12], digest[13], digest[14], digest[15]
        );

    return 0;
}
Exemple #27
0
void pballoc_free_at_last(pubnub_t* pb)
{
    PUBNUB_LOG_TRACE("pballoc_free_at_last(%p)\n", pb);

    PUBNUB_ASSERT_OPT(pb != NULL);

    PUBNUB_LOG_TRACE("pubnub_free_at_last(%p)\n", pb);

    pubnub_mutex_lock(pb->monitor);
    pubnub_mutex_init_static(m_lock);
    pubnub_mutex_lock(m_lock);

    PUBNUB_ASSERT_OPT(pb->state == PBS_NULL);

    pbcc_deinit(&pb->core);
    pbpal_free(pb);
    remove_allocated(pb);
    pubnub_mutex_unlock(pb->monitor);
    pubnub_mutex_destroy(pb->monitor);
    pubnub_mutex_unlock(m_lock);
    free(pb);
}
enum pubnub_res pubnub_subloop_start(pubnub_subloop_t* pbsld)
{
    enum pubnub_res   rslt;
    pubnub_callback_t saved_ctx_cb;
    void*             saved_ctx_data;

    PUBNUB_ASSERT_OPT(NULL != pbsld);

    pubnub_mutex_lock(pbsld->monitor);
    PUBNUB_ASSERT_OPT(NULL != pbsld->pbp);
    saved_ctx_cb   = pubnub_get_callback(pbsld->pbp);
    saved_ctx_data = pubnub_get_user_data(pbsld->pbp);
    rslt = pubnub_register_callback(pbsld->pbp, sublup_context_callback, pbsld);
    if (PNR_OK == rslt) {
        pbsld->saved_context_cb        = saved_ctx_cb;
        pbsld->saved_context_user_data = saved_ctx_data;
        rslt = pubnub_subscribe_ex(pbsld->pbp, pbsld->channel, pbsld->options);
    }
    pubnub_mutex_unlock(pbsld->monitor);

    return rslt;
}
Exemple #29
0
void pbproxy_handle_http_header(pubnub_t *p, char const* header)
{
    char scheme_basic[] = "Basic";
    char scheme_NTLM[] = "NTLM";
    char proxy_auth[] = "Proxy-Authenticate: ";
    char const* contents;

    PUBNUB_ASSERT_OPT(p != NULL);
    PUBNUB_ASSERT_OPT(header != NULL);

    if (strncmp(p->core.http_buf, proxy_auth, sizeof proxy_auth - 1) != 0) {
        return;
    }
    contents = p->core.http_buf + sizeof proxy_auth - 1;

    PUBNUB_LOG_TRACE("pbproxy_handle_http_header(header='%s', contents='%s')\n", header, contents);

    if (0 == strncmp(contents, scheme_basic, sizeof scheme_basic -1)) {
        /* We ignore the "realm" for now */
        PUBNUB_LOG_TRACE("pbproxy_handle_http_header() Basic authentication\n");
        p->proxy_auth_scheme = pbhtauBasic;
        p->proxy_authorization_sent = false;
    }
    else if (0 == strncmp(contents, scheme_NTLM, sizeof scheme_NTLM -1)) {
        if (pbhtauNTLM != p->proxy_auth_scheme) {
            pbntlm_core_init(p);
            p->proxy_auth_scheme = pbhtauNTLM;
            p->proxy_authorization_sent = false;
        }
        else {
            char const *base64_msg = contents + sizeof scheme_NTLM;
            pbntlm_core_handle(p, base64_msg, strcspn(base64_msg, " \r\n"));
        }
    }
    else {
        PUBNUB_LOG_ERROR("Proxy Authentication '%s' not supported\n", contents);
        p->proxy_auth_scheme = pbhtauNone;
    }
}
Exemple #30
0
enum pubnub_res pbcc_remove_channel_group_prep(struct pbcc_context *pb, char const *channel_group)
{
    PUBNUB_ASSERT_OPT(channel_group != NULL);

    pb->http_buf_len = snprintf(
        pb->http_buf, sizeof pb->http_buf,
        "/v1/channel-registration/sub-key/%s/channel-group/%s/remove?pnsdk=%s",
        pb->subscribe_key, channel_group, pubnub_uname()
        );
    APPEND_URL_PARAM_M(pb, "auth", pb->auth, '&');

    return PNR_STARTED;
}