<commit_before>/*
 * Copyright (c) 2015 SPUDlib authors.  See LICENSE file.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>

#include "ls_sockaddr.h"
#include "ls_log.h"

LS_API size_t ls_sockaddr_get_length(const struct sockaddr *addr)
{
    switch (addr->sa_family) {
    case AF_INET:
        return sizeof(struct sockaddr_in);
    case AF_INET6:
        return sizeof(struct sockaddr_in6);
    default:
        assert(false);
        return -1;
    }
}

LS_API bool ls_sockaddr_get_remote_ip_addr(struct sockaddr_in6 *remoteAddr,
                                           const char *fqdn,
                                           const char *port,
                                           ls_err *err)
{
    struct addrinfo hints, *res, *p;
    int status;
    bool found = false;

    memset(&hints, 0, sizeof hints);
    hints.ai_flags = AI_V4MAPPED;
    hints.ai_family = AF_INET6; // use AI_V4MAPPED for v4 addresses
    hints.ai_protocol = IPPROTO_UDP;
    hints.ai_socktype = SOCK_DGRAM;

    if ((status = getaddrinfo(fqdn, port, &hints, &res)) != 0) {
        ls_log(LS_LOG_ERROR, "getaddrinfo: %s\n", gai_strerror(status));
        return false;
    }

    for(p = res;p != NULL; p = p->ai_next) {
        // copy the first match
        if (p->ai_family == AF_INET6) { // Should always be v6 (mapped for v4)
            memcpy(remoteAddr, p->ai_addr, sizeof(struct sockaddr_in6));
            found = true;
            break;
        }
    }
    freeaddrinfo(res); // free the linked list
    return found;
}

LS_API void ls_sockaddr_v6_any(struct sockaddr_in6 * sa, int port)
{
    memset(sa, 0, sizeof(*sa));
    sa->sin6_family = AF_INET6;
    sa->sin6_addr = in6addr_any;
    //sa->sin6_len = sizeof(*sa);
    sa->sin6_port = htons(port);
}

LS_API void ls_sockaddr_copy(const struct sockaddr *src, struct sockaddr *dest) {
    memcpy(dest, src, ls_sockaddr_get_length(src));
}<commit_msg>narrowing it down<commit_after>/*
 * Copyright (c) 2015 SPUDlib authors.  See LICENSE file.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>

#include "ls_sockaddr.h"
#include "ls_log.h"

LS_API size_t ls_sockaddr_get_length(const struct sockaddr *addr)
{
    switch (addr->sa_family) {
    case AF_INET:
        return sizeof(struct sockaddr_in);
    case AF_INET6:
        return sizeof(struct sockaddr_in6);
    default:
        assert(false);
        return -1;
    }
}

LS_API bool ls_sockaddr_get_remote_ip_addr(struct sockaddr_in6 *remoteAddr,
                                           const char *fqdn,
                                           const char *port,
                                           ls_err *err)
{
    struct addrinfo hints, *res, *p;
    int status;
    bool found = false;

printf("%s:%d\n", __func__, __LINE__);
    memset(&hints, 0, sizeof hints);
    hints.ai_flags = AI_V4MAPPED;
    hints.ai_family = AF_INET6; // use AI_V4MAPPED for v4 addresses
    hints.ai_protocol = IPPROTO_UDP;
    hints.ai_socktype = SOCK_DGRAM;
printf("%s:%d\n", __func__, __LINE__);
    if ((status = getaddrinfo(fqdn, port, &hints, &res)) != 0) {
        ls_log(LS_LOG_ERROR, "getaddrinfo: %s\n", gai_strerror(status));
        return false;
    }
printf("%s:%d\n", __func__, __LINE__);
    for(p = res;p != NULL; p = p->ai_next) {
        // copy the first match
        if (p->ai_family == AF_INET6) { // Should always be v6 (mapped for v4)
            memcpy(remoteAddr, p->ai_addr, sizeof(struct sockaddr_in6));
            found = true;
            break;
        }
    }
printf("%s:%d\n", __func__, __LINE__);
    freeaddrinfo(res); // free the linked list
printf("%s:%d\n", __func__, __LINE__);
    return found;
}

LS_API void ls_sockaddr_v6_any(struct sockaddr_in6 * sa, int port)
{
    memset(sa, 0, sizeof(*sa));
    sa->sin6_family = AF_INET6;
    sa->sin6_addr = in6addr_any;
    //sa->sin6_len = sizeof(*sa);
    sa->sin6_port = htons(port);
}

LS_API void ls_sockaddr_copy(const struct sockaddr *src, struct sockaddr *dest) {
    memcpy(dest, src, ls_sockaddr_get_length(src));
}
{'fraction_non_alphanumeric': 0.12338768556826478, 'fraction_numerical': 0.013141883669992699, 'mean_word_length': 2.4683544303797467, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2834787', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1448, 'n_words': 335}