HelenOS sources
This source file includes following definitions.
- inet_hostport_parse
- inet_hostport_format
- inet_hostport_destroy
- inet_hostport_lookup_one
- inet_hostport_plookup_one
#include <assert.h>
#include <errno.h>
#include <inet/addr.h>
#include <inet/dnsr.h>
#include <inet/endpoint.h>
#include <inet/hostname.h>
#include <inet/hostport.h>
#include <stdio.h>
#include <stdlib.h>
#include <str.h>
errno_t inet_hostport_parse(const char *str, inet_hostport_t **rhp,
char **endptr)
{
inet_hostport_t *hp;
inet_addr_t addr;
uint16_t port;
char *name;
char *aend;
const char *pend;
errno_t rc;
hp = calloc(1, sizeof(inet_hostport_t));
if (hp == NULL)
return ENOMEM;
if (str[0] == '[') {
rc = inet_addr_parse(str + 1, &addr, &aend);
if (rc == EOK) {
if (*aend == ']') {
hp->hform = inet_host_addr;
hp->host.addr = addr;
++aend;
}
goto have_host;
}
}
rc = inet_addr_parse(str, &addr, &aend);
if (rc == EOK) {
hp->hform = inet_host_addr;
hp->host.addr = addr;
goto have_host;
}
rc = inet_hostname_parse(str, &name, &aend);
if (rc == EOK) {
hp->hform = inet_host_name;
hp->host.name = name;
goto have_host;
}
free(hp);
return EINVAL;
have_host:
if (*aend == ':') {
rc = str_uint16_t(aend + 1, &pend, 10, false, &port);
if (rc != EOK) {
free(hp);
return EINVAL;
}
} else {
port = 0;
pend = aend;
}
hp->port = port;
if (*pend != '\0' && endptr == NULL) {
free(hp);
return EINVAL;
}
*rhp = hp;
if (endptr != NULL)
*endptr = (char *)pend;
return EOK;
}
errno_t inet_hostport_format(inet_hostport_t *hp, char **rstr)
{
errno_t rc;
int ret;
char *astr, *str;
char *hstr = NULL;
switch (hp->hform) {
case inet_host_addr:
rc = inet_addr_format(&hp->host.addr, &astr);
if (rc != EOK) {
assert(rc == ENOMEM);
return ENOMEM;
}
if (hp->host.addr.version != ip_v4) {
hstr = astr;
} else {
ret = asprintf(&hstr, "[%s]", astr);
if (ret < 0) {
free(astr);
return ENOMEM;
}
free(astr);
}
break;
case inet_host_name:
hstr = str_dup(hp->host.name);
if (hstr == NULL)
return ENOMEM;
break;
}
ret = asprintf(&str, "%s:%u", hstr, hp->port);
if (ret < 0)
return ENOMEM;
free(hstr);
*rstr = str;
return EOK;
}
void inet_hostport_destroy(inet_hostport_t *hp)
{
if (hp == NULL)
return;
switch (hp->hform) {
case inet_host_addr:
break;
case inet_host_name:
free(hp->host.name);
break;
}
free(hp);
}
errno_t inet_hostport_lookup_one(inet_hostport_t *hp, ip_ver_t version,
inet_ep_t *ep)
{
dnsr_hostinfo_t *hinfo = NULL;
errno_t rc;
inet_ep_init(ep);
switch (hp->hform) {
case inet_host_addr:
ep->addr = hp->host.addr;
break;
case inet_host_name:
rc = dnsr_name2host(hp->host.name, &hinfo, ip_any);
if (rc != EOK) {
dnsr_hostinfo_destroy(hinfo);
return ENOENT;
}
ep->addr = hinfo->addr;
dnsr_hostinfo_destroy(hinfo);
break;
}
ep->port = hp->port;
return EOK;
}
errno_t inet_hostport_plookup_one(const char *str, ip_ver_t version, inet_ep_t *ep,
char **endptr, const char **errmsg)
{
inet_hostport_t *hp = NULL;
char *eptr;
errno_t rc;
rc = inet_hostport_parse(str, &hp, endptr != NULL ? &eptr : NULL);
if (rc != EOK) {
switch (rc) {
case EINVAL:
if (errmsg != NULL)
*errmsg = "Invalid format";
goto error;
case ENOMEM:
if (errmsg != NULL)
*errmsg = "Out of memory";
goto error;
case EOK:
break;
default:
assert(false);
}
}
rc = inet_hostport_lookup_one(hp, version, ep);
if (rc != EOK) {
rc = ENOENT;
if (errmsg != NULL)
*errmsg = "Name resolution failed";
goto error;
}
inet_hostport_destroy(hp);
if (endptr != NULL)
*endptr = eptr;
return EOK;
error:
inet_hostport_destroy(hp);
return rc;
}
HelenOS homepage, sources at GitHub