<commit_before>/* net_app.c */

/*
 * Copyright (c) 2017 Intel Corporation.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#if defined(CONFIG_NET_DEBUG_APP)
#define SYS_LOG_DOMAIN "net/app"
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_LOG_ENABLED 1
#endif

#if defined(CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
#define  MBEDTLS_PRINT printf
#else
#include <misc/printk.h>
#define  MBEDTLS_PRINT printk
#endif /* CONFIG_STDOUT_CONSOLE */
#include <zephyr.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

#include <net/net_core.h>
#include <net/net_ip.h>
#include <net/net_if.h>
#include <net/dhcpv4.h>
#include <net/net_mgmt.h>
#include <net/udp.h>

#include <net/net_app.h>

#include "../../ip/udp_internal.h"

#include "net_app_private.h"

#if defined(CONFIG_NET_APP_DTLS_TIMEOUT)
#define DTLS_TIMEOUT K_SECONDS(CONFIG_NET_APP_DTLS_TIMEOUT)
#else
#define DTLS_TIMEOUT K_SECONDS(15)
#endif

#if defined(CONFIG_NET_DEBUG_APP)
static sys_slist_t _net_app_instances;

void _net_app_register(struct net_app_ctx *ctx)
{
	sys_slist_prepend(&_net_app_instances, &ctx->node);
}

void _net_app_unregister(struct net_app_ctx *ctx)
{
	sys_slist_find_and_remove(&_net_app_instances, &ctx->node);
}

static void net_app_foreach(net_app_ctx_cb_t cb, enum net_app_type type,
			    void *user_data)
{
	struct net_app_ctx *ctx;

	SYS_SLIST_FOR_EACH_CONTAINER(&_net_app_instances, ctx, node) {
		if (ctx->is_init && ctx->app_type == type) {
			cb(ctx, user_data);
		}
	}
}

void net_app_server_foreach(net_app_ctx_cb_t cb, void *user_data)
{
	net_app_foreach(cb, NET_APP_SERVER, user_data);
}

void net_app_client_foreach(net_app_ctx_cb_t cb, void *user_data)
{
	net_app_foreach(cb, NET_APP_CLIENT, user_data);
}
#endif /* CONFIG_NET_DEBUG_APP */

#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
int net_app_set_net_pkt_pool(struct net_app_ctx *ctx,
			     net_pkt_get_slab_func_t tx_slab,
			     net_pkt_get_pool_func_t data_pool)
{
	ctx->tx_slab = tx_slab;
	ctx->data_pool = data_pool;

	return 0;
}
#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */

#if defined(CONFIG_NET_DEBUG_APP)
char *_net_app_sprint_ipaddr(char *buf, int buflen,
			     const struct sockaddr *addr)
{
	if (addr->sa_family == AF_INET6) {
#if defined(CONFIG_NET_IPV6)
		char ipaddr[NET_IPV6_ADDR_LEN];

		net_addr_ntop(addr->sa_family,
			      &net_sin6(addr)->sin6_addr,
			      ipaddr, sizeof(ipaddr));
		snprintk(buf, buflen, "[%s]:%u", ipaddr,
			 ntohs(net_sin6(addr)->sin6_port));
#endif
	} else if (addr->sa_family == AF_INET) {
#if defined(CONFIG_NET_IPV4)
		char ipaddr[NET_IPV4_ADDR_LEN];

		net_addr_ntop(addr->sa_family,
			      &net_sin(addr)->sin_addr,
			      ipaddr, sizeof(ipaddr));
		snprintk(buf, buflen, "%s:%u", ipaddr,
			 ntohs(net_sin(addr)->sin_port));
#endif
	}

	return buf;
}

void _net_app_print_info(struct net_app_ctx *ctx)
{
#define PORT_STR_LEN sizeof("[]:xxxxx")
	char local[NET_IPV6_ADDR_LEN + PORT_STR_LEN];
	char remote[NET_IPV6_ADDR_LEN + PORT_STR_LEN];

	_net_app_sprint_ipaddr(local, sizeof(local), &ctx->default_ctx->local);
	_net_app_sprint_ipaddr(remote, sizeof(remote),
			       &ctx->default_ctx->remote);

	NET_DBG("net app connect %s %s %s",
		local,<commit_msg>net: app: Print info if address family is not set properly<commit_after>/* net_app.c */

/*
 * Copyright (c) 2017 Intel Corporation.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#if defined(CONFIG_NET_DEBUG_APP)
#define SYS_LOG_DOMAIN "net/app"
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_LOG_ENABLED 1
#endif

#if defined(CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
#define  MBEDTLS_PRINT printf
#else
#include <misc/printk.h>
#define  MBEDTLS_PRINT printk
#endif /* CONFIG_STDOUT_CONSOLE */
#include <zephyr.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

#include <net/net_core.h>
#include <net/net_ip.h>
#include <net/net_if.h>
#include <net/dhcpv4.h>
#include <net/net_mgmt.h>
#include <net/udp.h>

#include <net/net_app.h>

#include "../../ip/udp_internal.h"

#include "net_app_private.h"

#if defined(CONFIG_NET_APP_DTLS_TIMEOUT)
#define DTLS_TIMEOUT K_SECONDS(CONFIG_NET_APP_DTLS_TIMEOUT)
#else
#define DTLS_TIMEOUT K_SECONDS(15)
#endif

#if defined(CONFIG_NET_DEBUG_APP)
static sys_slist_t _net_app_instances;

void _net_app_register(struct net_app_ctx *ctx)
{
	sys_slist_prepend(&_net_app_instances, &ctx->node);
}

void _net_app_unregister(struct net_app_ctx *ctx)
{
	sys_slist_find_and_remove(&_net_app_instances, &ctx->node);
}

static void net_app_foreach(net_app_ctx_cb_t cb, enum net_app_type type,
			    void *user_data)
{
	struct net_app_ctx *ctx;

	SYS_SLIST_FOR_EACH_CONTAINER(&_net_app_instances, ctx, node) {
		if (ctx->is_init && ctx->app_type == type) {
			cb(ctx, user_data);
		}
	}
}

void net_app_server_foreach(net_app_ctx_cb_t cb, void *user_data)
{
	net_app_foreach(cb, NET_APP_SERVER, user_data);
}

void net_app_client_foreach(net_app_ctx_cb_t cb, void *user_data)
{
	net_app_foreach(cb, NET_APP_CLIENT, user_data);
}
#endif /* CONFIG_NET_DEBUG_APP */

#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
int net_app_set_net_pkt_pool(struct net_app_ctx *ctx,
			     net_pkt_get_slab_func_t tx_slab,
			     net_pkt_get_pool_func_t data_pool)
{
	ctx->tx_slab = tx_slab;
	ctx->data_pool = data_pool;

	return 0;
}
#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */

#if defined(CONFIG_NET_DEBUG_APP)
char *_net_app_sprint_ipaddr(char *buf, int buflen,
			     const struct sockaddr *addr)
{
	if (addr->sa_family == AF_INET6) {
#if defined(CONFIG_NET_IPV6)
		char ipaddr[NET_IPV6_ADDR_LEN];

		net_addr_ntop(addr->sa_family,
			      &net_sin6(addr)->sin6_addr,
			      ipaddr, sizeof(ipaddr));
		snprintk(buf, buflen, "[%s]:%u", ipaddr,
			 ntohs(net_sin6(addr)->sin6_port));
#endif
	} else if (addr->sa_family == AF_INET) {
#if defined(CONFIG_NET_IPV4)
		char ipaddr[NET_IPV4_ADDR_LEN];

		net_addr_ntop(addr->sa_family,
			      &net_sin(addr)->sin_addr,
			      ipaddr, sizeof(ipaddr));
		snprintk(buf, buflen, "%s:%u", ipaddr,
			 ntohs(net_sin(addr)->sin_port));
#endif
	} else {
		snprintk(buf, buflen, "<AF_UNSPEC %d>",
			 addr->sa_family);
	}

	return buf;
}

void _net_app_print_info(struct net_app_ctx *ctx)
{
#define PORT_STR_LEN sizeof("[]:xxxxx")
	char local[NET_IPV6_ADDR_LEN + PORT_STR_LEN];
	char remote[NET_IPV6_ADDR_LEN + PORT_STR_LEN];

	_net_app_sprint_ipaddr(local, sizeof(local), &ctx->default_ctx->local);
	_net_app_sprint_ipaddr(remote, sizeof(remote),
			       &ctx->default_ctx->remote);

	NET_DBG("net app connect %s %s %s",
		local,
{'fraction_non_alphanumeric': 0.12744043043812453, 'fraction_numerical': 0.00676402767102229, 'mean_word_length': 5.565085771947528, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 67, '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': '5602659', 'n_tokens_mistral': 2855, 'n_tokens_neox': 2735, 'n_words': 455}