X-Git-Url: http://www.project-moonshot.org/gitweb/?p=radsecproxy.git;a=blobdiff_plain;f=util.c;h=70d815cfa41371323d35abf330c65344f63d1ed6;hp=77fa004681350a8c86b49ac89b23578a573e24cc;hb=HEAD;hpb=414b916d07f4a1b475d53627092d24cd5a2a0517 diff --git a/util.c b/util.c index 77fa004..70d815c 100644 --- a/util.c +++ b/util.c @@ -1,114 +1,25 @@ -/* - * Copyright (C) 2006 Stig Venaas - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - */ +/* Copyright 2012-2013 NORDUnet A/S. All rights reserved. + See LICENSE for licensing information. */ -#include -#include -#include -#include #include #include -#include -#include -#include -#include "debug.h" +#include +#include +#include "util.h" -void errx(char *format, ...) { - extern int errno; +char * +rs_strdup (struct rs_context *ctx, const char *s) +{ + size_t len; + char *buf; - va_list ap; - va_start(ap, format); - vfprintf(stderr, format, ap); - va_end(ap); - if (errno) { - fprintf(stderr, ": "); - perror(NULL); - fprintf(stderr, "errno=%d\n", errno); - } else - fprintf(stderr, "\n"); - exit(1); -} - -void err(char *format, ...) { - extern int errno; - - va_list ap; - va_start(ap, format); - vfprintf(stderr, format, ap); - va_end(ap); - if (errno) { - fprintf(stderr, ": "); - perror(NULL); - fprintf(stderr, "errno=%d\n", errno); - } else - fprintf(stderr, "\n"); -} - -char *stringcopy(char *s, int len) { - char *r; - if (!len) - len = strlen(s); - r = malloc(len + 1); - if (!r) - debug(DBG_ERR, "stringcopy: malloc failed"); - memcpy(r, s, len); - r[len] = '\0'; - return r; -} - -char *addr2string(struct sockaddr *addr, socklen_t len) { - struct sockaddr_in6 *sa6; - struct sockaddr_in sa4; - static char addr_buf[2][INET6_ADDRSTRLEN]; - static int i = 0; - i = !i; - if (addr->sa_family == AF_INET6) { - sa6 = (struct sockaddr_in6 *)addr; - if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) { - memset(&sa4, 0, sizeof(sa4)); - sa4.sin_family = AF_INET; - sa4.sin_port = sa6->sin6_port; - memcpy(&sa4.sin_addr, &sa6->sin6_addr.s6_addr[12], 4); - addr = (struct sockaddr *)&sa4; - } - } - if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]), - NULL, 0, NI_NUMERICHOST)) { - err("getnameinfo"); - return NULL; - } - return addr_buf[i]; -} - -int connectport(int type, char *host, char *port) { - struct addrinfo hints, *res0, *res; - int s; - - memset(&hints, 0, sizeof(hints)); - hints.ai_socktype = type; - hints.ai_family = AF_UNSPEC; + len = strlen (s); + buf = rs_malloc (ctx, len + 1); - if (getaddrinfo(host, port, &hints, &res0) != 0) { - err("connectport: can't resolve host %s port %s", host, port); - return -1; - } + if (buf != NULL) + memcpy (buf, s, len + 1); + else + rs_err_ctx_push (ctx, RSE_NOMEM, __func__); - for (res = res0; res; res = res->ai_next) { - s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); - if (s < 0) { - err("connectport: socket failed"); - continue; - } - if (connect(s, res->ai_addr, res->ai_addrlen) == 0) - break; - err("connectport: connect failed"); - close(s); - s = -1; - } - freeaddrinfo(res0); - return s; + return buf; }