<commit_before>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

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

#include "esp-tls.h"


static const char *TAG = "esp-tls";

#ifdef ESP_PLATFORM
#include <esp_log.h>
#else
#define ESP_LOGD(TAG, ...) //printf(__VA_ARGS__);
#define ESP_LOGE(TAG, ...) printf(__VA_ARGS__);
#endif

static struct addrinfo *resolve_host_name(const char *host, size_t hostlen, int port)
{
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    char *use_host = (char *)calloc(1, hostlen  + 1);
    if (!use_host) {
        return NULL;
    }
    strncpy(use_host, host, hostlen);
    use_host[hostlen] = '\0';

    char service[6];
    snprintf(service, sizeof(service), "%d", port);

    ESP_LOGD(TAG, "port is :%s: host:%s: strlen %zu\n", service, use_host, hostlen);
    struct addrinfo *res;
    if (getaddrinfo(use_host, service, &hints, &res)) {
        ESP_LOGE(TAG, "couldn't get hostname for :%s:\n", use_host);
        free(use_host);
        return NULL;
    }
    free(use_host);
    return res;
}

static ssize_t tcp_read(struct esp_tls *tls, char *data, size_t datalen)
{
    return recv(tls->sockfd, data, datalen, 0);
}

static ssize_t tls_read(struct esp_tls *tls, char *data, size_t datalen)
{
    return SSL_read(tls->ssl, data, datalen);
}

static int esp_tcp_connect(const char *host, int hostlen, int port)
{
    struct addrinfo *res = resolve_host_name(host, hostlen, port);
    if (!res) {
        return -1;
    }

    int ret = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (ret < 0) {
        goto err_freeaddr;
    }

    int fd = ret;
    ret = connect(fd, res->ai_addr, res->ai_addrlen);
    if (ret < 0) {
        goto err_freesocket;
    }

    freeaddrinfo(res);
    return fd;

err_freesocket:
    close(fd);
err_freeaddr:
    freeaddrinfo(res);
    return -1;
}

static int create_ssl_handle(struct esp_tls *tls)
{
    int ret;

    SSL_CTX *ssl_ctx = SSL_CTX_new(TLSv1_2_client_method());
    if (!ssl_ctx) {
        return -1;
    }<commit_msg>Don't use 'port' number for DNS query<commit_after>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

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

#include "esp-tls.h"


static const char *TAG = "esp-tls";

#ifdef ESP_PLATFORM
#include <esp_log.h>
#else
#define ESP_LOGD(TAG, ...) //printf(__VA_ARGS__);
#define ESP_LOGE(TAG, ...) printf(__VA_ARGS__);
#endif

static struct addrinfo *resolve_host_name(const char *host, size_t hostlen)
{
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    char *use_host = (char *)calloc(1, hostlen  + 1);
    if (!use_host) {
        return NULL;
    }
    strncpy(use_host, host, hostlen);
    use_host[hostlen] = '\0';

    ESP_LOGD(TAG, "host:%s: strlen %zu\n", use_host, hostlen);
    struct addrinfo *res;
    if (getaddrinfo(use_host, NULL, &hints, &res)) {
        ESP_LOGE(TAG, "couldn't get hostname for :%s:\n", use_host);
        free(use_host);
        return NULL;
    }
    free(use_host);
    return res;
}

static ssize_t tcp_read(struct esp_tls *tls, char *data, size_t datalen)
{
    return recv(tls->sockfd, data, datalen, 0);
}

static ssize_t tls_read(struct esp_tls *tls, char *data, size_t datalen)
{
    return SSL_read(tls->ssl, data, datalen);
}

static int esp_tcp_connect(const char *host, int hostlen, int port)
{
    struct addrinfo *res = resolve_host_name(host, hostlen);
    if (!res) {
        return -1;
    }

    int ret = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (ret < 0) {
        goto err_freeaddr;
    }
    int fd = ret;

    void *addr_ptr;
    if (res->ai_family == AF_INET) {
	struct sockaddr_in *p = res->ai_addr;
	p->sin_port = htons(port);
	addr_ptr = p;
    } else if (res->ai_family == AF_INET6) {
	struct sockaddr_in6 *p = res->ai_addr;
	p->sin6_port = htons(port);
	p->sin6_family = AF_INET6;
	addr_ptr = p;
    } else {
	/* Unsupported Protocol Family */
	goto err_freesocket;
    }

    ret = connect(fd, addr_ptr, res->ai_addrlen);
    if (ret < 0) {
        goto err_freesocket;
    }

    freeaddrinfo(res);
    return fd;

err_freesocket:
    close(fd);
err_freeaddr:
    freeaddrinfo(res);
    return -1;
}

static int create_ssl_handle(struct esp_tls *tls)
{
    int ret;

    SSL_CTX *ssl_ctx = SSL_CTX_new(TLSv1_2_client_method());
    if (!ssl_ctx) {
        return -1;
    }
{'fraction_non_alphanumeric': 0.14217391304347826, 'fraction_numerical': 0.006521739130434782, 'mean_word_length': 3.2720519962859798, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7560930', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1773, 'n_words': 418}