2016-06-14 15:00:55 +08:00
|
|
|
/** @file
|
|
|
|
@brief UDP data handler
|
|
|
|
|
|
|
|
This is not to be included by the application.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-06-14 15:00:55 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UDP_H
|
|
|
|
#define __UDP_H
|
|
|
|
|
Introduce new sized integer typedefs
This is a start to move away from the C99 {u}int{8,16,32,64}_t types to
Zephyr defined u{8,16,32,64}_t and s{8,16,32,64}_t. This allows Zephyr
to define the sized types in a consistent manor across all the
architectures we support and not conflict with what various compilers
and libc might do with regards to the C99 types.
We introduce <zephyr/types.h> as part of this and have it include
<stdint.h> for now until we transition all the code away from the C99
types.
We go with u{8,16,32,64}_t and s{8,16,32,64}_t as there are some
existing variables defined u8 & u16 as well as to be consistent with
Zephyr naming conventions.
Jira: ZEP-2051
Change-Id: I451fed0623b029d65866622e478225dfab2c0ca8
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-19 23:32:08 +08:00
|
|
|
#include <zephyr/types.h>
|
2016-06-14 15:00:55 +08:00
|
|
|
|
|
|
|
#include <net/net_core.h>
|
|
|
|
#include <net/net_ip.h>
|
2017-04-03 23:14:35 +08:00
|
|
|
#include <net/net_pkt.h>
|
2016-06-27 17:26:39 +08:00
|
|
|
#include <net/net_context.h>
|
2016-06-14 15:00:55 +08:00
|
|
|
|
|
|
|
#include "connection.h"
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-06-27 17:26:39 +08:00
|
|
|
#if defined(CONFIG_NET_UDP)
|
|
|
|
/**
|
2017-04-05 14:37:44 +08:00
|
|
|
* @brief Append UDP packet into net_pkt
|
2016-06-27 17:26:39 +08:00
|
|
|
*
|
2017-04-05 14:37:44 +08:00
|
|
|
* @param pkt Network packet
|
2016-11-22 14:41:58 +08:00
|
|
|
* @param src_port Source port in host byte order.
|
|
|
|
* @param dst_port Destination port in host byte order.
|
2016-06-27 17:26:39 +08:00
|
|
|
*
|
2017-04-05 14:37:44 +08:00
|
|
|
* @return Return network packet that contains the UDP packet.
|
2016-06-27 17:26:39 +08:00
|
|
|
*/
|
2017-04-05 14:37:44 +08:00
|
|
|
static inline struct net_pkt *net_udp_append_raw(struct net_pkt *pkt,
|
2017-04-21 22:27:50 +08:00
|
|
|
u16_t src_port,
|
|
|
|
u16_t dst_port)
|
2016-06-27 17:26:39 +08:00
|
|
|
{
|
2017-04-10 19:03:41 +08:00
|
|
|
NET_UDP_HDR(pkt)->src_port = htons(src_port);
|
|
|
|
NET_UDP_HDR(pkt)->dst_port = htons(dst_port);
|
2016-06-27 17:26:39 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
net_buf_add(pkt->frags, sizeof(struct net_udp_hdr));
|
2016-06-27 17:26:39 +08:00
|
|
|
|
2017-04-10 19:03:41 +08:00
|
|
|
NET_UDP_HDR(pkt)->len = htons(net_pkt_get_len(pkt) -
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_ip_hdr_len(pkt));
|
2016-06-27 17:26:39 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
net_pkt_set_appdata(pkt, net_pkt_udp_data(pkt) +
|
2017-04-03 23:14:35 +08:00
|
|
|
sizeof(struct net_udp_hdr));
|
2016-06-27 17:26:39 +08:00
|
|
|
|
2017-04-05 14:37:44 +08:00
|
|
|
return pkt;
|
2016-06-27 17:26:39 +08:00
|
|
|
}
|
2016-11-22 14:41:58 +08:00
|
|
|
|
|
|
|
/**
|
2017-04-05 14:37:44 +08:00
|
|
|
* @brief Append UDP packet into net_pkt
|
2016-11-22 14:41:58 +08:00
|
|
|
*
|
|
|
|
* @param context Network context for a connection
|
2017-04-05 14:37:44 +08:00
|
|
|
* @param pkt Network packet
|
2016-11-22 14:41:58 +08:00
|
|
|
* @param port Destination port in host byte order.
|
|
|
|
*
|
2017-04-05 14:37:44 +08:00
|
|
|
* @return Return network packet that contains the UDP packet.
|
2016-11-22 14:41:58 +08:00
|
|
|
*/
|
2017-04-05 14:37:44 +08:00
|
|
|
static inline struct net_pkt *net_udp_append(struct net_context *context,
|
|
|
|
struct net_pkt *pkt,
|
2017-04-21 22:27:50 +08:00
|
|
|
u16_t port)
|
2016-11-22 14:41:58 +08:00
|
|
|
{
|
2017-04-05 14:37:44 +08:00
|
|
|
return net_udp_append_raw(pkt,
|
2016-11-22 14:41:58 +08:00
|
|
|
ntohs(net_sin((struct sockaddr *)
|
|
|
|
&context->local)->sin_port),
|
|
|
|
port);
|
|
|
|
}
|
2016-06-27 17:26:39 +08:00
|
|
|
#else
|
2017-04-05 14:37:44 +08:00
|
|
|
#define net_udp_append_raw(pkt, src_port, dst_port) (pkt)
|
|
|
|
#define net_udp_append(context, pkt, port) (pkt)
|
2016-06-27 17:26:39 +08:00
|
|
|
#endif /* CONFIG_NET_UDP */
|
|
|
|
|
2016-06-14 15:00:55 +08:00
|
|
|
/**
|
|
|
|
* @brief Register a callback to be called when UDP packet
|
|
|
|
* is received corresponding to received packet.
|
|
|
|
*
|
|
|
|
* @param remote_addr Remote address of the connection end point.
|
|
|
|
* @param local_addr Local address of the connection end point.
|
|
|
|
* @param remote_port Remote port of the connection end point.
|
|
|
|
* @param local_port Local port of the connection end point.
|
|
|
|
* @param cb Callback to be called
|
|
|
|
* @param user_data User data supplied by caller.
|
|
|
|
* @param handle UDP handle that can be used when unregistering
|
|
|
|
*
|
|
|
|
* @return Return 0 if the registration succeed, <0 otherwise.
|
|
|
|
*/
|
2016-06-22 21:33:56 +08:00
|
|
|
static inline int net_udp_register(const struct sockaddr *remote_addr,
|
|
|
|
const struct sockaddr *local_addr,
|
2017-04-21 22:27:50 +08:00
|
|
|
u16_t remote_port,
|
|
|
|
u16_t local_port,
|
2016-06-14 15:00:55 +08:00
|
|
|
net_conn_cb_t cb,
|
|
|
|
void *user_data,
|
2016-11-04 05:18:35 +08:00
|
|
|
struct net_conn_handle **handle)
|
2016-06-14 15:00:55 +08:00
|
|
|
{
|
|
|
|
return net_conn_register(IPPROTO_UDP, remote_addr, local_addr,
|
|
|
|
remote_port, local_port, cb, user_data,
|
|
|
|
handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Unregister UDP handler.
|
|
|
|
*
|
|
|
|
* @param handle Handle from registering.
|
|
|
|
*
|
|
|
|
* @return Return 0 if the unregistration succeed, <0 otherwise.
|
|
|
|
*/
|
2016-11-04 05:18:35 +08:00
|
|
|
static inline int net_udp_unregister(struct net_conn_handle *handle)
|
2016-06-14 15:00:55 +08:00
|
|
|
{
|
|
|
|
return net_conn_unregister(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_UDP)
|
|
|
|
static inline void net_udp_init(void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define net_udp_init(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* __UDP_H */
|