static void set_get(NDB_SOCKET_TYPE fd, int level, int optval, const char *optname, int val) { int actual = 0, defval = 0; SOCKET_SIZE_TYPE len = sizeof(actual); my_getsockopt(fd, level, optval, (char*)&defval, &len); if (my_setsockopt(fd, level, optval, (char*)&val, sizeof(val)) < 0) { #ifdef DEBUG_TRANSPORTER g_eventLogger->error("setsockopt(%s, %d) errno: %d %s", optname, val, errno, strerror(errno)); #endif } len = sizeof(actual); if ((my_getsockopt(fd, level, optval, (char*)&actual, &len) == 0) && actual != val) { #ifdef DEBUG_TRANSPORTER g_eventLogger->error("setsockopt(%s, %d) - actual %d default: %d", optname, val, actual, defval); #endif } }
int Ndb_check_socket_hup(NDB_SOCKET_TYPE sock) { #ifdef HAVE_POLL struct pollfd pfd[1]; int r; pfd[0].fd= sock.fd; // FIXME: THIS IS A BUG pfd[0].events= POLLHUP | POLLIN | POLLOUT | POLLNVAL; pfd[0].revents= 0; r= poll(pfd,1,0); if(pfd[0].revents & (POLLHUP|POLLERR)) return 1; return 0; #else /* HAVE_POLL */ fd_set readfds, writefds, errorfds; struct timeval tv= {0,0}; int s_err; SOCKET_SIZE_TYPE s_err_size= sizeof(s_err); FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&errorfds); my_FD_SET(sock, &readfds); my_FD_SET(sock, &writefds); my_FD_SET(sock, &errorfds); if(select(my_socket_nfds(sock,0)+1, &readfds, &writefds, &errorfds, &tv)<0) return 1; if(my_FD_ISSET(sock,&errorfds)) return 1; s_err=0; if (my_getsockopt(sock, SOL_SOCKET, SO_ERROR, &s_err, &s_err_size) != 0) return(1); if (s_err) { /* getsockopt could succeed */ return(1); /* but return an error... */ } return 0; #endif /* HAVE_POLL */ }
int sctp_get_no_strms(int sock_fd, struct sockaddr *to, socklen_t tolen) { socklen_t retsz; struct sctp_status status; retsz = sizeof(status); bzero(&status, sizeof(status)); //这句出问题. //听说可以改成:tatus.sstat_assoc_id = sri.sinfo_assoc_id; status.sstat_assoc_id = sctp_address_to_associd(sock_fd, to, tolen); my_getsockopt(sock_fd, IPPROTO_SCTP, SCTP_STATUS, &status, &retsz); return (status.sstat_outstrms); }
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; }