int SocketClient::bind(const char* bindaddress, unsigned short localport) { if (!my_socket_valid(m_sockfd)) return -1; struct sockaddr_in local; memset(&local, 0, sizeof(local)); local.sin_family = AF_INET; local.sin_port = htons(localport); // Convert ip address presentation format to numeric format if (Ndb_getInAddr(&local.sin_addr, bindaddress)) { return errno ? errno : EINVAL; } if (my_socket_reuseaddr(m_sockfd, true) == -1) { int ret = my_socket_errno(); my_socket_close(m_sockfd); my_socket_invalidate(&m_sockfd); return ret; } if (my_bind_inet(m_sockfd, &local) == -1) { int ret = my_socket_errno(); my_socket_close(m_sockfd); my_socket_invalidate(&m_sockfd); return ret; } return 0; }
TCP_Transporter::TCP_Transporter(TransporterRegistry &t_reg, const TransporterConfiguration* conf) : Transporter(t_reg, tt_TCP_TRANSPORTER, conf->localHostName, conf->remoteHostName, conf->s_port, conf->isMgmConnection, conf->localNodeId, conf->remoteNodeId, conf->serverNodeId, 0, false, conf->checksum, conf->signalId, conf->tcp.sendBufferSize) { maxReceiveSize = conf->tcp.maxReceiveSize; // Initialize member variables my_socket_invalidate(&theSocket); sendCount = receiveCount = 0; sendSize = receiveSize = 0; reportFreq = 4096; sockOptNodelay = 1; setIf(sockOptRcvBufSize, conf->tcp.tcpRcvBufSize, 70080); setIf(sockOptSndBufSize, conf->tcp.tcpSndBufSize, 71540); setIf(sockOptTcpMaxSeg, conf->tcp.tcpMaxsegSize, 0); m_overload_limit = overload_limit(conf); }
SocketClient::SocketClient(const char *server_name, unsigned short port, SocketAuthenticator *sa) : m_connect_timeout_millisec(0) // Blocking connect by default { m_auth= sa; m_port= port; m_server_name= server_name ? strdup(server_name) : 0; my_socket_invalidate(&m_sockfd); }
static int continue_lcp(int error) { int filter[] = { 15, NDB_MGM_EVENT_CATEGORY_INFO, 0 }; NDB_SOCKET_TYPE my_fd; my_socket_invalidate(&my_fd); #ifdef NDB_WIN SOCKET fd; #else int fd; #endif if(error){ fd = ndb_mgm_listen_event(g_restarter.handle, filter); #ifdef NDB_WIN my_fd.s= fd; #else my_fd.fd= fd; #endif require(my_socket_valid(my_fd)); } int args[] = { DumpStateOrd::LCPContinue }; if(g_restarter.dumpStateAllNodes(args, 1) != 0) return -1; if(error){ char *tmp; char buf[1024]; SocketInputStream in(my_fd, 1000); int count = 0; int nodes = g_restarter.getNumDbNodes(); do { tmp = in.gets(buf, 1024); if(tmp) { int id; if(sscanf(tmp, "%*[^:]: LCP: %d ", &id) == 1 && id == error && --nodes == 0){ my_socket_close(my_fd); return 0; } } } while(count++ < 30); my_socket_close(my_fd); } return 0; }
void TCP_Transporter::disconnectImpl() { get_callback_obj()->lock_transporter(remoteNodeId); NDB_SOCKET_TYPE sock = theSocket; receiveBuffer.clear(); my_socket_invalidate(&theSocket); get_callback_obj()->unlock_transporter(remoteNodeId); if(my_socket_valid(sock)) { if(my_socket_close(sock) < 0){ report_error(TE_ERROR_CLOSING_SOCKET); } } }
NDB_SOCKET_TYPE SocketClient::connect(const char *toaddress, unsigned short toport) { if (!my_socket_valid(m_sockfd)) { if (!init()) { return m_sockfd; } } if (toaddress) { if (m_server_name) free(m_server_name); m_server_name = strdup(toaddress); m_port = toport; memset(&m_servaddr, 0, sizeof(m_servaddr)); m_servaddr.sin_family = AF_INET; m_servaddr.sin_port = htons(toport); // Convert ip address presentation format to numeric format if (Ndb_getInAddr(&m_servaddr.sin_addr, m_server_name)) { my_socket_close(m_sockfd); my_socket_invalidate(&m_sockfd); return m_sockfd; } } // Set socket non blocking if (my_socket_nonblock(m_sockfd, true) < 0) { my_socket_close(m_sockfd); my_socket_invalidate(&m_sockfd); return m_sockfd; } // Start non blocking connect int r = my_connect_inet(m_sockfd, &m_servaddr); if (r == 0) goto done; // connected immediately. if (r < 0 && NONBLOCKERR(my_socket_errno())) { // Start of non blocking connect failed my_socket_close(m_sockfd); my_socket_invalidate(&m_sockfd); return m_sockfd; } if (ndb_poll(m_sockfd, true, true, true, m_connect_timeout_millisec > 0 ? m_connect_timeout_millisec : -1) <= 0) { // Nothing has happened on the socket after timeout // or an error occured my_socket_close(m_sockfd); my_socket_invalidate(&m_sockfd); return m_sockfd; } // Activity detected on the socket { // Check socket level error code int so_error = 0; SOCKET_SIZE_TYPE len= sizeof(so_error); if (my_getsockopt(m_sockfd, SOL_SOCKET, SO_ERROR, &so_error, &len) < 0) { my_socket_close(m_sockfd); my_socket_invalidate(&m_sockfd); return m_sockfd; } if (so_error) { my_socket_close(m_sockfd); my_socket_invalidate(&m_sockfd); return m_sockfd; } } done: if (my_socket_nonblock(m_sockfd, true) < 0) { my_socket_close(m_sockfd); my_socket_invalidate(&m_sockfd); return m_sockfd; } if (m_auth) { if (!m_auth->client_authenticate(m_sockfd)) { my_socket_close(m_sockfd); my_socket_invalidate(&m_sockfd); return m_sockfd; } } NDB_SOCKET_TYPE sockfd = m_sockfd; my_socket_invalidate(&m_sockfd); return sockfd; }