1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
/*
* decodenetnum - return a net number (this is crude, but careful)
*/
#include <config.h>
#include <sys/types.h>
#include <ctype.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include "ntp_stdlib.h"
#include "ntp_assert.h"
int
decodenetnum(
const char *num,
sockaddr_u *netnum
)
{
struct addrinfo hints, *ai = NULL;
register int err;
register const char *cp;
char name[80];
char *np;
NTP_REQUIRE(num != NULL);
if (strlen(num) >= sizeof(name)) {
return 0;
}
if ('[' != num[0])
cp = num;
else {
cp = num + 1;
np = name;
while (*cp && ']' != *cp)
*np++ = *cp++;
*np = 0;
cp = name;
}
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICHOST;
err = getaddrinfo(cp, NULL, &hints, &ai);
if (err != 0)
return 0;
memcpy(netnum, ai->ai_addr, ai->ai_addrlen);
freeaddrinfo(ai);
return 1;
}
|