aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sockets.c27
-rw-r--r--sockets.h22
2 files changed, 49 insertions, 0 deletions
diff --git a/sockets.c b/sockets.c
index 72a388a..ef35220 100644
--- a/sockets.c
+++ b/sockets.c
@@ -802,6 +802,33 @@ sock_address_list_create( const char* hostname,
return list;
}
+SockAddress**
+sock_address_list_create2(const char* host_and_port, unsigned flags )
+{
+ char host_name[512];
+ const char* actual_host_name = "localhost";
+ // Parse host and port name.
+ const char* port_name = strchr(host_and_port, ':');
+ if (port_name != NULL) {
+ int to_copy = MIN(sizeof(host_name)-1, port_name - host_and_port);
+ if (to_copy != 0) {
+ memcpy(host_name, host_and_port, to_copy);
+ host_name[to_copy] = '\0';
+ actual_host_name = host_name;
+ port_name++;
+ } else {
+ return NULL;
+ }
+ } else {
+ port_name = host_and_port;
+ }
+ // Make sure that port_name is not empty.
+ if (port_name[0] == '\0') {
+ return NULL;
+ }
+ return sock_address_list_create(actual_host_name, port_name, flags);
+}
+
void
sock_address_list_free( SockAddress** list )
{
diff --git a/sockets.h b/sockets.h
index f04458e..785a37e 100644
--- a/sockets.h
+++ b/sockets.h
@@ -280,6 +280,28 @@ SockAddress** sock_address_list_create( const char* hostname,
const char* port,
unsigned flags );
+/* resolve a string containing host and port name into a list of SockAddress
+ * objects. Parameter host_and_port should be in format [host:]port, where
+ * 'host' addresses the machine and must be resolvable into an IP address, and
+ * 'port' is a decimal numeric value for the port. 'host' is optional, and if
+ * ommited, localhost will be used.
+ * returns a NULL-terminated array of SockAddress pointers on success,
+ * or NULL in case of failure, with the value of errno set to one of the
+ * following:
+ *
+ * EINVAL : invalid argument
+ * EHOSTDOWN : could not reach DNS server
+ * ENOENT : no host with this name, or host doesn't have IP address
+ * ENOMEM : not enough memory to perform request
+ *
+ * other system-level errors can also be set depending on the host sockets
+ * implementation.
+ *
+ * This function loops on EINTR so the caller shouldn't have to check for it.
+ */
+SockAddress** sock_address_list_create2(const char* host_and_port,
+ unsigned flags );
+
void sock_address_list_free( SockAddress** list );
/* create a new socket, return the socket number of -1 on failure */