/* * Write a string to a socket. * Return 1 if all bytes where successfully written, and 0 if not. * Assert Class: 2 */ int sock_write_string(SOCKET sockfd, const char *buff) { int write_bytes = 0, res = 0, len = ice_strlen(buff); if (!sock_valid(sockfd)) { fprintf(stderr, "ERROR: sock_write_string() called with invalid socket\n"); return -1; } else if (!buff) { fprintf(stderr, "ERROR: sock_write_string() called with NULL format\n"); return -1; } /* * Never use send() to sockets 0 or 1 in Win32. */ if (sockfd == 1 || sockfd == 0) { if (running == SERVER_RUNNING) { write_bytes = fprintf(stdout, "%s", buff); fflush(stdout); } } else { while (write_bytes < len) { res = send(sockfd, &buff[write_bytes], len - write_bytes, 0); if (res < 0 && !is_recoverable(errno)) return 0; if (res > 0) write_bytes += res; else my_sleep(30000); } } return (write_bytes == len ? 1 : 0); }
int fd_write (int fd, const char *fmt, ...) { char buff[BUFSIZE]; va_list ap; va_start(ap, fmt); vsnprintf(buff, BUFSIZE, fmt, ap); va_end (ap); if (fd == 1 || fd == 0) { if (running == SERVER_RUNNING) { fprintf(stdout, "%s", buff); fflush(stdout); return 1; #ifndef _WIN32 } else { return write(fd, buff, ice_strlen(buff)); } #else } #endif } else {
/* * Connect to hostname on specified port and return the created socket. * Assert Class: 3 */ SOCKET sock_connect_wto(const char *hostname, const int port, const int timeout) { SOCKET sockfd; struct sockaddr_in sin, server; struct hostent *host; struct hostent hostinfo; char buf[BUFSIZE]; int error; if (!hostname || !hostname[0]) { write_log(LOG_DEFAULT, "ERROR: sock_connect() called with NULL or empty hostname"); return INVALID_SOCKET; } else if (port <= 0) { write_log(LOG_DEFAULT, "ERROR: sock_connect() called with invalid port number"); return INVALID_SOCKET; } sockfd = sock_socket(AF_INET, SOCK_STREAM, 0); if (sockfd == INVALID_SOCKET) { sock_close(sockfd); return INVALID_SOCKET; } if (info.myhostname != NULL) { struct sockaddr_in localsin; memset(&localsin, 0, sizeof (struct sockaddr_in)); xa_debug(2, "DEBUG: Trying to bind to %s", info.myhostname); localsin.sin_addr = localaddr; localsin.sin_family = AF_INET; localsin.sin_port = 0; if (bind (sockfd, (struct sockaddr *) &localsin, sizeof (localsin)) == SOCKET_ERROR) { xa_debug(2, "DEBUG: Unable to bind", info.myhostname); write_log(LOG_DEFAULT, "ERROR: Bind to local address %s failed", info.myhostname); sock_close(sockfd); return INVALID_SOCKET; } } memset(&sin, 0, sizeof (sin)); memset(&server, 0, sizeof (struct sockaddr_in)); if (isdigit((int) hostname[0]) && isdigit((int) hostname[ice_strlen(hostname) - 1])) { if (inet_aton(hostname, (struct in_addr *) &sin.sin_addr) == 0) { write_log(LOG_DEFAULT, "ERROR: Invalid ip number %s", hostname); sock_close(sockfd); return INVALID_SOCKET; } memcpy(&server.sin_addr, &sin.sin_addr, sizeof (sin)); } else { host = ice_gethostbyname(hostname, &hostinfo, buf, BUFSIZE, &error); if (host == NULL) { xa_debug(1, "DEBUG: gethostbyname %s failed", hostname); sock_close(sockfd); ice_clean_hostent(); return INVALID_SOCKET; } memcpy(&server.sin_addr, host->h_addr, host->h_length); ice_clean_hostent(); } server.sin_family = AF_INET; server.sin_port = htons(port); { char buf[50]; makeasciihost(&server.sin_addr, buf); xa_debug(1, "Trying to connect to %s:%d", buf, port); } if (timeout > 0) { fd_set wfds; struct timeval tv; int retval; int val; socklen_t valsize = sizeof (int); xa_debug(3, "DEBUG: sock_connect(): doing a connection w/ timeout"); FD_ZERO(&wfds); FD_SET(sockfd, &wfds); tv.tv_sec = timeout; tv.tv_usec = 0; sock_set_blocking(sockfd, SOCK_NONBLOCK); retval = connect(sockfd, (struct sockaddr *) &server, sizeof (server)); if (retval == 0) { xa_debug(3, "DEBUG: sock_connect(): non blocking connect returned 0!"); sock_set_blocking(sockfd, SOCK_BLOCK); return sockfd; } else { #ifdef _WIN32 if (WSAGetLastError() == WSAEINPROGRESS) { #else if (!is_recoverable(errno)) { #endif xa_debug(3, "DEBUG: sock_connect(): connect didn't return EINPROGRESS!, was: %d", errno); sock_close(sockfd); return SOCKET_ERROR; } } if (select(sockfd + 1, NULL, &wfds, NULL, &tv)) { retval = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *) &val, (socklen_t *) & valsize); if ((retval == 0) && (val == 0)) { sock_set_blocking(sockfd, SOCK_BLOCK); return sockfd; } else { xa_debug(3, "DEBUG: sock_connect(): getsockopt returned %i, val = %i, valsize = %i, errno = %i!", retval, val, valsize, errno); sock_close(sockfd); return SOCKET_ERROR; } } else { xa_debug(3, "DEBUG: sock_connect(): select returned 0"); sock_close(sockfd); return SOCKET_ERROR; } } else { if (connect
/* * Create a socket for all incoming requests on specified port. * If info.myhostname is NULL, bind it to INADDR_ANY (all available interfaces). * If info.myhostname is not NULL, resolv it (if needed), and bind to that. * Return the socket for bound socket, or INVALID_SOCKET if failed. * Assert Class: 3 */ SOCKET sock_get_server_socket(const int port) { struct sockaddr_in sin; int sin_len, error; SOCKET sockfd; if (port < 0) { write_log(LOG_DEFAULT, "ERROR: Invalid port number %d. Cannot listen for requests, this is bad!", port); return INVALID_SOCKET; } xa_debug (2, "DEBUG: Getting socket for port %d", port); /* * get socket descriptor */ sockfd = sock_socket (AF_INET, SOCK_STREAM, 0); if (sockfd == INVALID_SOCKET) return INVALID_SOCKET; /* * Setup socket */ #ifdef HAVE_SETSOCKOPT { int tmp = 1; if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *) &tmp, sizeof (tmp)) != 0) write_log(LOG_DEFAULT, "ERROR: setsockopt() failed to set SO_REUSEADDR flag. (mostly harmless)"); } #endif /* * setup sockaddr structure */ sin_len = sizeof (sin); memset(&sin, 0, sin_len); sin.sin_family = AF_INET; if (info.myhostname != NULL && info.myhostname[0]) { if (isdigit((int) info.myhostname[0]) && isdigit((int) info. myhostname[ice_strlen(info.myhostname) - 1])) { if (inet_aton (info.myhostname, (struct in_addr *) &localaddr) == 0) { write_log(LOG_DEFAULT, "ERROR: Invalid ip number %s, will die now", info.myhostname); clean_shutdown(&info); } sin.sin_addr.s_addr = localaddr.s_addr; } else { struct hostent *hostinfoptr, hostinfo; char buf[BUFSIZE]; int error; hostinfoptr = ice_gethostbyname(info.myhostname, &hostinfo, buf, BUFSIZE, &error); if (hostinfoptr == NULL) { write_log(LOG_DEFAULT, "Unknown host %s, that's it for me!", info.myhostname); ice_clean_hostent(); clean_shutdown(&info); } sin.sin_addr.s_addr = localaddr.s_addr; memcpy((void *) &localaddr, hostinfoptr->h_addr, sizeof (localaddr)); ice_clean_hostent(); } } else { sin.sin_addr.s_addr = htonl(INADDR_ANY); } sin.sin_port = htons(port); /* * bind socket to port */ error = bind(sockfd, (struct sockaddr *) &sin, sin_len); if (error == SOCKET_ERROR) { write_log(LOG_DEFAULT, "Bind to socket on port %d failed. Shutting down now.", port); clean_shutdown(&info); } return sockfd; }