1
0
mirror of https://gitlab.xiph.org/xiph/icecast-common.git synced 2024-12-04 14:46:31 -05:00

Feature: Added igloo_socketaddr_*2*() to convert between domain, type, and protocol and strings

This commit is contained in:
Philipp Schafft 2019-10-03 08:41:28 +00:00
parent ad28e91c79
commit a361306bbc
2 changed files with 169 additions and 0 deletions

View File

@ -82,6 +82,17 @@ typedef enum {
igloo_SOCKETADDR_PROTOCOL_UDPLITE igloo_SOCKETADDR_PROTOCOL_UDPLITE
} igloo_socketaddr_protocol_t; } igloo_socketaddr_protocol_t;
/* This set of functions convert Domain, Type, and Protocol representations between enum and string values.
*
* Those functions are very helpful for parsing and writing config files, and for debug and log output.
*/
igloo_error_t igloo_socketaddr_domain2str(const char **str, igloo_socketaddr_domain_t domain);
igloo_error_t igloo_socketaddr_str2domain(igloo_socketaddr_domain_t *domain, const char *str);
igloo_error_t igloo_socketaddr_type2str(const char **str, igloo_socketaddr_type_t type);
igloo_error_t igloo_socketaddr_str2type(igloo_socketaddr_type_t *type, const char *str);
igloo_error_t igloo_socketaddr_protocol2str(const char **str, igloo_socketaddr_protocol_t protocol);
igloo_error_t igloo_socketaddr_str2protocol(igloo_socketaddr_protocol_t *protocol, const char *str);
/* This creates a new address object. /* This creates a new address object.
* Parameters: * Parameters:
* name, associated, instance * name, associated, instance

View File

@ -204,6 +204,164 @@ int igloo_socketaddr_get_sysid_protocol(igloo_socketaddr_protocol_t protocol)
return -1; return -1;
} }
igloo_error_t igloo_socketaddr_domain2str(const char **str, igloo_socketaddr_domain_t domain)
{
const char *ret = NULL;
if (!str)
return igloo_ERROR_FAULT;
switch (domain) {
case igloo_SOCKETADDR_DOMAIN_UNSPEC:
ret = "unspec";
break;
case igloo_SOCKETADDR_DOMAIN_UNIX:
ret = "unix";
break;
case igloo_SOCKETADDR_DOMAIN_INET4:
ret = "inet4";
break;
case igloo_SOCKETADDR_DOMAIN_INET6:
ret = "inet6";
break;
}
if (ret) {
*str = ret;
return igloo_ERROR_NONE;
} else {
return igloo_ERROR_NOENT;
}
}
igloo_error_t igloo_socketaddr_str2domain(igloo_socketaddr_domain_t *domain, const char *str)
{
if (!domain || !str)
return igloo_ERROR_FAULT;
if (strcasecmp(str, "unspec") == 0) {
*domain = igloo_SOCKETADDR_DOMAIN_UNSPEC;
} else if (strcasecmp(str, "unix") == 0) {
*domain = igloo_SOCKETADDR_DOMAIN_UNIX;
} else if (strcasecmp(str, "inet4") == 0 || strcasecmp(str, "legacy-inet") == 0) {
*domain = igloo_SOCKETADDR_DOMAIN_INET4;
} else if (strcasecmp(str, "inet6") == 0) {
*domain = igloo_SOCKETADDR_DOMAIN_INET6;
}
return igloo_ERROR_NOENT;
}
igloo_error_t igloo_socketaddr_type2str(const char **str, igloo_socketaddr_type_t type)
{
const char *ret = NULL;
if (!str)
return igloo_ERROR_FAULT;
switch (type) {
case igloo_SOCKETADDR_TYPE_UNSPEC:
ret = "unspec";
break;
case igloo_SOCKETADDR_TYPE_STREAM:
ret = "stream";
break;
case igloo_SOCKETADDR_TYPE_DGRAM:
ret = "dgram";
break;
case igloo_SOCKETADDR_TYPE_SEQPACK:
ret = "seqpack";
break;
case igloo_SOCKETADDR_TYPE_RDM:
ret = "rdm";
break;
}
if (ret) {
*str = ret;
return igloo_ERROR_NONE;
} else {
return igloo_ERROR_NOENT;
}
}
igloo_error_t igloo_socketaddr_str2type(igloo_socketaddr_type_t *type, const char *str)
{
if (!type || !str)
return igloo_ERROR_FAULT;
if (strcasecmp(str, "unspec") == 0) {
*type = igloo_SOCKETADDR_TYPE_UNSPEC;
} else if (strcasecmp(str, "stream") == 0) {
*type = igloo_SOCKETADDR_TYPE_STREAM;
} else if (strcasecmp(str, "dgram") == 0 || strcasecmp(str, "datagram") == 0) {
*type = igloo_SOCKETADDR_TYPE_DGRAM;
} else if (strcasecmp(str, "seqpack") == 0 || strcasecmp(str, "seqpacket") == 0) {
*type = igloo_SOCKETADDR_TYPE_SEQPACK;
} else if (strcasecmp(str, "rdm") == 0) {
*type = igloo_SOCKETADDR_TYPE_RDM;
}
return igloo_ERROR_NOENT;
}
igloo_error_t igloo_socketaddr_protocol2str(const char **str, igloo_socketaddr_protocol_t protocol)
{
const char *ret = NULL;
if (!str)
return igloo_ERROR_FAULT;
switch (protocol) {
case igloo_SOCKETADDR_PROTOCOL_UNSPEC:
ret = "unspec";
break;
case igloo_SOCKETADDR_PROTOCOL_TCP:
ret = "tcp";
break;
case igloo_SOCKETADDR_PROTOCOL_UDP:
ret = "udp";
break;
case igloo_SOCKETADDR_PROTOCOL_DCCP:
ret = "dccp";
break;
case igloo_SOCKETADDR_PROTOCOL_SCTP:
ret = "sctp";
break;
case igloo_SOCKETADDR_PROTOCOL_UDPLITE:
ret = "udplite";
break;
}
if (ret) {
*str = ret;
return igloo_ERROR_NONE;
} else {
return igloo_ERROR_NOENT;
}
}
igloo_error_t igloo_socketaddr_str2protocol(igloo_socketaddr_protocol_t *protocol, const char *str)
{
if (!protocol || !str)
return igloo_ERROR_FAULT;
if (strcasecmp(str, "unspec") == 0) {
*protocol = igloo_SOCKETADDR_PROTOCOL_UNSPEC;
} else if (strcasecmp(str, "tcp") == 0) {
*protocol = igloo_SOCKETADDR_PROTOCOL_TCP;
} else if (strcasecmp(str, "udp") == 0) {
*protocol = igloo_SOCKETADDR_PROTOCOL_UDP;
} else if (strcasecmp(str, "dccp") == 0) {
*protocol = igloo_SOCKETADDR_PROTOCOL_DCCP;
} else if (strcasecmp(str, "sctp") == 0) {
*protocol = igloo_SOCKETADDR_PROTOCOL_SCTP;
} else if (strcasecmp(str, "udplite") == 0 || strcasecmp(str, "udp-lite") == 0) {
*protocol = igloo_SOCKETADDR_PROTOCOL_UDPLITE;
}
return igloo_ERROR_NOENT;
}
void igloo_socketaddr_complete(igloo_socketaddr_t *addr) void igloo_socketaddr_complete(igloo_socketaddr_t *addr)
{ {
if (!igloo_RO_IS_VALID(addr, igloo_socketaddr_t)) if (!igloo_RO_IS_VALID(addr, igloo_socketaddr_t))