int pbntf_got_socket(pubnub_t* pb) { #if PUBNUB_BLOCKING_IO_SETTABLE pbpal_set_blocking_io(pb); #endif return +1; }
int pbntf_got_socket(pubnub_t *pb, pb_socket_t socket) { PUBNUB_UNUSED(socket); if (PUBNUB_BLOCKING_IO_SETTABLE) { pbpal_set_blocking_io(pb); } return +1; }
int pbntf_got_socket(pubnub_t *pb, pb_socket_t socket) { EnterCriticalSection(&m_watcher.mutw); save_socket(&m_watcher, pb); if (PUBNUB_TIMERS_API) { m_watcher.timer_head = pubnub_timer_list_add(m_watcher.timer_head, pb); } pb->options.use_blocking_io = false; pbpal_set_blocking_io(pb); LeaveCriticalSection(&m_watcher.mutw); return +1; }
static enum pbpal_resolv_n_connect_result connect_TCP_socket(pubnub_t *pb, struct sockaddr_in dest, const uint16_t port) { pb->pal.socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (SOCKET_INVALID == pb->pal.socket) { return pbpal_connect_resource_failure; } pb->options.use_blocking_io = false; pbpal_set_blocking_io(pb); socket_disable_SIGPIPE(pb->pal.socket); dest.sin_port = htons(port); if (SOCKET_ERROR == connect(pb->pal.socket, (struct sockaddr*)&dest, sizeof dest)) { return socket_would_block() ? pbpal_connect_wouldblock : pbpal_connect_failed; } return pbpal_connect_success; }
enum pbpal_resolv_n_connect_result pbpal_resolv_and_connect(pubnub_t *pb) { int error; uint16_t port = HTTP_PORT; char const* origin; #ifdef PUBNUB_CALLBACK_API struct sockaddr_in dest; prepare_port_and_hostname(pb, &port, &origin); #if PUBNUB_PROXY_API if (0 != pb->proxy_ip_address.ipv4[0]) { struct pubnub_ipv4_address* p_dest_addr = (struct pubnub_ipv4_address*)&(dest.sin_addr.s_addr); memset(&dest, '\0', sizeof dest); memcpy(p_dest_addr->ipv4, pb->proxy_ip_address.ipv4, sizeof p_dest_addr->ipv4); dest.sin_family = AF_INET; return connect_TCP_socket(pb, dest, port); } #endif if (SOCKET_INVALID == pb->pal.socket) { pb->pal.socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); } if (SOCKET_INVALID == pb->pal.socket) { return pbpal_resolv_resource_failure; } pb->options.use_blocking_io = false; pbpal_set_blocking_io(pb); dest.sin_family = AF_INET; dest.sin_port = htons(DNS_PORT); get_dns_ip(&dest); error = send_dns_query(pb->pal.socket, (struct sockaddr*)&dest, origin); if (error < 0) { return pbpal_resolv_failed_send; } else if (error > 0) { return pbpal_resolv_send_wouldblock; } return pbpal_resolv_sent; #else char port_string[20]; struct addrinfo *result; struct addrinfo *it; struct addrinfo hint; hint.ai_socktype = SOCK_STREAM; hint.ai_family = AF_UNSPEC; hint.ai_protocol = hint.ai_flags = hint.ai_addrlen = 0; hint.ai_addr = NULL; hint.ai_canonname = NULL; hint.ai_next = NULL; prepare_port_and_hostname(pb, &port, &origin); snprintf(port_string, sizeof port_string, "%hu", port); error = getaddrinfo(origin, port_string, &hint, &result); if (error != 0) { return pbpal_resolv_failed_processing; } for (it = result; it != NULL; it = it->ai_next) { pb->pal.socket = socket(it->ai_family, it->ai_socktype, it->ai_protocol); if (pb->pal.socket == SOCKET_INVALID) { continue; } pbpal_set_blocking_io(pb); if (connect(pb->pal.socket, it->ai_addr, it->ai_addrlen) == SOCKET_ERROR) { if (socket_would_block()) { error = 1; break; } else { PUBNUB_LOG_WARNING("socket connect() failed, will try another IP address, if available\n"); socket_close(pb->pal.socket); pb->pal.socket = SOCKET_INVALID; continue; } } break; } freeaddrinfo(result); if (NULL == it) { return pbpal_connect_failed; } socket_set_rcv_timeout(pb->pal.socket, pb->transaction_timeout_ms); socket_disable_SIGPIPE(pb->pal.socket); return error ? pbpal_connect_wouldblock : pbpal_connect_success; #endif /* PUBNUB_CALLBACK_API */ }
enum pbpal_resolv_n_connect_result pbpal_resolv_and_connect(pubnub_t *pb) { int error; char const* origin = PUBNUB_ORIGIN_SETTABLE ? pb->origin : PUBNUB_ORIGIN; PUBNUB_ASSERT(pb_valid_ctx_ptr(pb)); PUBNUB_ASSERT_OPT((pb->state == PBS_READY) || (pb->state == PBS_WAIT_DNS_SEND) || (pb->state == PBS_WAIT_DNS_RCV)); if (PUBNUB_USE_ADNS) { struct sockaddr_in dest; int skt = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (SOCKET_INVALID == skt) { return pbpal_resolv_resource_failure; } pb->options.use_blocking_io = false; pbpal_set_blocking_io(pb); dest.sin_family = AF_INET; dest.sin_port = htons(DNS_PORT); inet_pton(AF_INET, "8.8.8.8", &dest.sin_addr.s_addr); error = send_dns_query(skt, (struct sockaddr*)&dest, (unsigned char*)origin); if (error < 0) { socket_close(skt); return pbpal_resolv_failed_send; } else if (error > 0) { return pbpal_resolv_send_wouldblock; } pb->pal.socket = skt; return pbpal_resolv_sent; } else { struct addrinfo *result; struct addrinfo *it; struct addrinfo hint; hint.ai_socktype = SOCK_STREAM; hint.ai_family = AF_UNSPEC; hint.ai_protocol = hint.ai_flags = hint.ai_addrlen = 0; hint.ai_addr = NULL; hint.ai_canonname = NULL; hint.ai_next = NULL; error = getaddrinfo(origin, HTTP_PORT_STRING, &hint, &result); if (error != 0) { return pbpal_resolv_failed_processing; } for (it = result; it != NULL; it = it->ai_next) { pb->pal.socket = socket(it->ai_family, it->ai_socktype, it->ai_protocol); if (pb->pal.socket == SOCKET_INVALID) { continue; } pbpal_set_blocking_io(pb); if (connect(pb->pal.socket, it->ai_addr, it->ai_addrlen) == SOCKET_ERROR) { if (socket_would_block()) { error = 1; break; } else { PUBNUB_LOG_WARNING("socket connect() failed, will try another IP address, if available\n"); socket_close(pb->pal.socket); pb->pal.socket = SOCKET_INVALID; continue; } } break; } freeaddrinfo(result); if (NULL == it) { return pbpal_connect_failed; } socket_set_rcv_timeout(pb->pal.socket, pb->transaction_timeout_ms); return error ? pbpal_connect_wouldblock : pbpal_connect_success; } }