45#include <netinet/in.h>
46#include <sys/socket.h>
48#include "utilities/qio.h"
49#include "utilities/qstring.h"
50#include "utilities/qsocket.h"
66 struct sockaddr_in addr;
73 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
78 int flags = fcntl(sockfd, F_GETFL, 0);
80 fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
83 int status = connect(sockfd, (
struct sockaddr *) &addr,
sizeof(addr));
85 && (errno != EINPROGRESS
93 fcntl(sockfd, F_SETFL, flags);
110 if (timeoutms >= 0 && shutdown(sockfd, SHUT_WR) == 0) {
113 ssize_t read =
qio_read(sockfd, buf,
sizeof(buf), timeoutms);
114 DEBUG(
"Throw %zu bytes from dummy input stream.", read);
120 if (close(sockfd) == 0)
136 memset((
void *) addr, 0,
sizeof(
struct sockaddr_in));
137 if (!inet_aton(hostname, &addr->sin_addr)) {
139 if ((hp = gethostbyname(hostname)) == 0)
141 memcpy(&addr->sin_addr, hp->h_addr,
sizeof(
struct in_addr));
143 addr->sin_family = AF_INET;
144 addr->sin_port = htons(port);
155 char hostname[63 + 1];
156 if (gethostname(hostname,
sizeof(hostname)) != 0)
159 struct hostent *hostentry = gethostbyname(hostname);
160 if (hostentry == NULL)
163 char *localip = inet_ntoa(*(
struct in_addr *) *hostentry->h_addr_list);
167 qstrcpy(buf, bufsize, localip);
ssize_t qio_read(int fd, void *buf, size_t nbytes, int timeoutms)
Read from a file descriptor.
int qio_wait_writable(int fd, int timeoutms)
Wait until a file descriptor is ready for writing.
bool qsocket_get_addr(struct sockaddr_in *addr, const char *hostname, int port)
Convert a hostname to a sockaddr_in structure.
bool qsocket_close(int sockfd, int timeoutms)
Close socket.
int qsocket_open(const char *hostname, int port, int timeoutms)
Create a TCP socket for a remote host and port.
char * qsocket_get_localaddr(char *buf, size_t bufsize)
Get the local IP address.
char * qstrcpy(char *dst, size_t size, const char *src)
Copy src string to dst.