X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fnetutils.c;h=8226e4f32d8c926ea3b2e746babda0d0f56f1da9;hb=566c9bf8bfcc08b60c3cb9ae2edce8b516128203;hp=255822bb436f9cd7f0cf35494c701370acc477cc;hpb=1b4a582b04fc39d9d4d930acb4d0803bdedfb32e;p=deb_shairplay.git diff --git a/src/lib/netutils.c b/src/lib/netutils.c index 255822b..8226e4f 100644 --- a/src/lib/netutils.c +++ b/src/lib/netutils.c @@ -1,3 +1,17 @@ +/** + * Copyright (C) 2011-2012 Juho Vähä-Herttua + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + #include #include #include @@ -137,3 +151,39 @@ netutils_get_address(void *sockaddr, int *length) return NULL; } +int +netutils_parse_address(int family, const char *src, void *dst, int dstlen) +{ + struct addrinfo *result; + struct addrinfo *ptr; + struct addrinfo hints; + int length; + int ret; + + if (family != AF_INET && family != AF_INET6) { + return -1; + } + if (!src || !dst) { + return -1; + } + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = family; + hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; + + ret = getaddrinfo(src, NULL, &hints, &result); + if (ret != 0) { + return -1; + } + + length = -1; + for (ptr=result; ptr!=NULL; ptr=ptr->ai_next) { + if (family == ptr->ai_family && (unsigned int)dstlen >= ptr->ai_addrlen) { + memcpy(dst, ptr->ai_addr, ptr->ai_addrlen); + length = ptr->ai_addrlen; + break; + } + } + freeaddrinfo(result); + return length; +}