int sock_del (SOCKET s) { ice_socket_t *is, *ds; xa_debug (3, "DEBUG: sock_del(): Removing socket %d", s); if (!sock_valid (s)) { xa_debug (1, "WARNING: Tried to remove invalid socket %d", s); return -1; } is = sock_find (s); if (!is) { xa_debug (1, "WARNING: Tried to remove a nonlisted socket %d", s); return -1; } thread_mutex_lock (&sock_mutex); ds = avl_delete (sock_sockets, is); if (ds) { nfree (ds); thread_mutex_unlock (&sock_mutex); return 1; } thread_mutex_unlock (&sock_mutex); return -1; }
int sock_set_no_linger(SOCKET sockfd) { struct linger lin = { 0, 0 }; int res; xa_debug (4, "DEBUG: Setting socket %d to no linger", sockfd); res = setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (void *) &lin, sizeof (struct linger)); if (res == -1) { xa_debug (1, "WARNING: sock_set_no_linger() failed"); return -1; } # ifdef DEBUG_SOCKETS { ice_socket_t *is = sock_find (sockfd); if (!is) { write_log (LOG_DEFAULT, "WARNING: sock_set_no_linger() setting unknown socket"); } else { is->linger = 0; } } # endif return res; }
/* * Set the socket's KEEPALIVE flag * this will detech idle connections from blocking * forever if the host crashes */ int sock_set_keepalive(SOCKET sockfd, const int keepalive) { int optval = keepalive; int res; xa_debug (4, "DEBUG: Setting socket %d keepalive to %d", sockfd, keepalive); res = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (void *) &optval, sizeof (int)); if (res == -1) { xa_debug (1, "WARNING: sock_set_keepalive() failed"); return -1; } #ifdef DEBUG_SOCKETS { ice_socket_t *is = sock_find (sockfd); if (!is) { write_log (LOG_DEFAULT, "WARNING: sock_set_keepalive() setting unknown socket"); } else { is->keepalive = keepalive; } } #endif return res; }
static int sock_autobind(struct socket *s) { static struct sockaddrs sa; sa.addr = INADDR_ANY; do { sa.port = ntohs(nextauto++); } while(sock_find(s->s_type, SADDR_SRC, &sa)); memcpy(&s->s_addr[SADDR_SRC], &sa, sizeof(sa)); return net_bind(s); }
/* * Set or the socket to blocking or nonblocking. * Assert Class: 1 */ int sock_set_blocking(SOCKET sockfd, const int block) { #ifdef _WIN32 int varblock = block; #else int res; #endif xa_debug(3, "Setting fd %d to %s", sockfd, (block == SOCK_BLOCK) ? "blocking" : "nonblocking"); if (!sock_valid(sockfd)) { xa_debug(1, "ERROR: sock_set_blocking() called with invalid socket"); return SOCKET_ERROR; } else if ((block < 0) || (block > 1)) { xa_debug(1, "ERROR: sock_set_blocking() called with invalid block value"); return SOCKET_ERROR; } #ifdef _WIN32 return ioctlsocket(sockfd, FIONBIO, &varblock); #else res = fcntl(sockfd, F_SETFL, (block == SOCK_BLOCK) ? 0 : O_NONBLOCK); if (res == -1) { xa_debug (1, "WARNING: sock_set_blocking() on socket %d failed", sockfd); return -1; } # ifdef DEBUG_SOCKETS { ice_socket_t *is = sock_find (sockfd); if (!is) { write_log (LOG_DEFAULT, "WARNING: sock_set_blocking(): Unknown socket %d", sockfd); return -1; } else { is->blocking = block; } } # endif return res; #endif }