2007-09-08 21:50:59 +02:00
|
|
|
/****************************************************************************
|
2014-06-26 22:23:21 +02:00
|
|
|
* net/utils/utils.h
|
2007-09-08 21:50:59 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-09-08 21:50:59 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-09-08 21:50:59 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-09-08 21:50:59 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
#ifndef __NET_UTILS_UTILS_H
|
|
|
|
#define __NET_UTILS_UTILS_H
|
|
|
|
|
2007-09-08 21:50:59 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2015-05-31 16:34:03 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2015-02-06 15:11:09 +01:00
|
|
|
#include <nuttx/net/ip.h>
|
2007-09-08 21:50:59 +02:00
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-05-29 15:17:15 +02:00
|
|
|
/* These values control the behavior of net_timeval2desc */
|
|
|
|
|
|
|
|
enum tv2ds_remainder_e
|
|
|
|
{
|
|
|
|
TV2DS_TRUNC = 0, /* Truncate microsecond remainder */
|
|
|
|
TV2DS_ROUND, /* Round to the nearest full decisecond */
|
|
|
|
TV2DS_CEIL /* Force to next larger full decisecond */
|
|
|
|
};
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
2007-09-08 21:50:59 +02:00
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
#define EXTERN extern "C"
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#else
|
|
|
|
#define EXTERN extern
|
|
|
|
#endif
|
2007-09-08 21:50:59 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-26 22:23:21 +02:00
|
|
|
* Public Function Prototypes
|
2007-09-08 21:50:59 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-29 01:25:18 +02:00
|
|
|
struct net_driver_s; /* Forward reference */
|
|
|
|
struct timeval; /* Forward reference */
|
|
|
|
|
2018-07-07 03:49:05 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_breaklock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Break the lock, return information needed to restore re-entrant lock
|
|
|
|
* state.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int net_breaklock(FAR unsigned int *count);
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-01-31 19:09:01 +01:00
|
|
|
* Name: net_restorelock
|
2018-07-07 03:49:05 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Restore the locked state
|
|
|
|
*
|
2019-04-14 17:37:25 +02:00
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* failured (probably -ECANCELED).
|
|
|
|
*
|
2018-07-07 03:49:05 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2019-04-14 17:37:25 +02:00
|
|
|
int net_restorelock(unsigned int count);
|
2018-07-07 03:49:05 +02:00
|
|
|
|
2007-09-08 21:50:59 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_dsec2timeval
|
2007-09-08 21:50:59 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2014-07-05 23:15:40 +02:00
|
|
|
* Convert a decisecond value to a struct timeval. Needed by getsockopt()
|
|
|
|
* to report timeout values.
|
2007-09-08 21:50:59 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-09-08 21:50:59 +02:00
|
|
|
* dsec The decisecond value to convert
|
|
|
|
* tv The struct timeval to receive the converted value
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
void net_dsec2timeval(uint16_t dsec, FAR struct timeval *tv);
|
2014-06-29 01:25:18 +02:00
|
|
|
|
2014-07-05 23:15:40 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_dsec2tick
|
2014-07-05 23:15:40 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert a decisecond value to a system clock ticks. Used by IGMP logic.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2014-07-05 23:15:40 +02:00
|
|
|
* dsec The decisecond value to convert
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The decisecond value expressed as system clock ticks
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
unsigned int net_dsec2tick(int dsec);
|
|
|
|
|
2014-06-29 01:25:18 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_timeval2dsec
|
2014-06-29 01:25:18 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert a struct timeval to deciseconds. Needed by setsockopt() to
|
|
|
|
* save new timeout values.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-05-29 15:17:15 +02:00
|
|
|
* tv - The struct timeval to convert
|
|
|
|
* remainder - Determines how to handler the microsecond remainder
|
2014-06-29 01:25:18 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The converted value
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-05-29 15:17:15 +02:00
|
|
|
unsigned int net_timeval2dsec(FAR struct timeval *tv,
|
|
|
|
enum tv2ds_remainder_e remainder);
|
2014-06-26 22:23:21 +02:00
|
|
|
|
2016-01-09 22:56:08 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_ipv6_mask2pref
|
|
|
|
*
|
|
|
|
* Description:
|
2020-10-19 06:09:06 +02:00
|
|
|
* Convert a 128-bit netmask to a prefix length. The NuttX IPv6
|
2016-01-09 22:56:08 +01:00
|
|
|
* networking uses 128-bit network masks internally. This function
|
|
|
|
* converts the IPv6 netmask to a prefix length.
|
|
|
|
*
|
|
|
|
* The prefix length is the number of MS '1' bits on in the netmask.
|
|
|
|
* This, of course, assumes that all MS bits are '1' and all LS bits are
|
|
|
|
* '0' with no intermixed 1's and 0's. This function searches from the MS
|
|
|
|
* bit until the first '0' is found (this does not necessary mean that
|
|
|
|
* there might not be additional '1' bits following the firs '0', but that
|
|
|
|
* will be a malformed netmask.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2016-01-09 22:56:08 +01:00
|
|
|
* mask Points to an IPv6 netmask in the form of uint16_t[8]
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2016-01-09 22:56:08 +01:00
|
|
|
* The prefix length, range 0-128 on success; This function will not
|
|
|
|
* fail.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
uint8_t net_ipv6_mask2pref(FAR const uint16_t *mask);
|
|
|
|
#endif
|
|
|
|
|
2015-02-06 15:11:09 +01:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_ipv6_pref2mask
|
2015-02-06 15:11:09 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert a IPv6 prefix length to a network mask. The prefix length
|
|
|
|
* specifies the number of MS bits under mask (0-128)
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-02-06 15:11:09 +01:00
|
|
|
* preflen - Determines the width of the netmask (in bits). Range 0-128
|
|
|
|
* mask - The location to return the netmask.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
void net_ipv6_pref2mask(uint8_t preflen, net_ipv6addr_t mask);
|
|
|
|
#endif
|
|
|
|
|
2017-04-02 22:30:01 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: chksum
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the raw change some over the memory region described by
|
|
|
|
* data and len.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-10-19 06:30:19 +02:00
|
|
|
* sum - Partial calculations carried over from a previous call to chksum.
|
2017-04-02 22:30:01 +02:00
|
|
|
* This should be zero on the first time that check sum is called.
|
|
|
|
* data - Beginning of the data to include in the checksum.
|
|
|
|
* len - Length of the data to include in the checksum.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The updated checksum value.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
uint16_t chksum(uint16_t sum, FAR const uint8_t *data, uint16_t len);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_chksum
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the Internet checksum over a buffer.
|
|
|
|
*
|
|
|
|
* The Internet checksum is the one's complement of the one's complement
|
|
|
|
* sum of all 16-bit words in the buffer.
|
|
|
|
*
|
|
|
|
* See RFC1071.
|
|
|
|
*
|
|
|
|
* If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be
|
|
|
|
* provided by architecture-specific logic.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* buf - A pointer to the buffer over which the checksum is to be computed.
|
|
|
|
*
|
2020-10-19 06:30:19 +02:00
|
|
|
* len - Length of the buffer over which the checksum is to be computed.
|
2017-04-02 22:30:01 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The Internet checksum of the buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
uint16_t net_chksum(FAR uint16_t *data, uint16_t len);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_upperlayer_chksum
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform the checksum calcaultion over the IPv4, protocol headers, and
|
|
|
|
* data payload as necessary.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The network driver instance. The packet data is in the d_buf
|
|
|
|
* of the device.
|
|
|
|
* proto - The protocol being supported
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The calculated checksum
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-30 08:45:15 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2017-04-02 22:30:01 +02:00
|
|
|
uint16_t ipv4_upperlayer_chksum(FAR struct net_driver_s *dev, uint8_t proto);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv6_upperlayer_chksum
|
|
|
|
*
|
|
|
|
* Description:
|
2018-07-07 19:39:55 +02:00
|
|
|
* Perform the checksum calculation over the IPv6, protocol headers, and
|
2017-04-02 22:30:01 +02:00
|
|
|
* data payload as necessary.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The network driver instance. The packet data is in the d_buf
|
|
|
|
* of the device.
|
|
|
|
* proto - The protocol being supported
|
2018-11-05 15:56:07 +01:00
|
|
|
* iplen - The size of the IPv6 header. This may be larger than
|
|
|
|
* IPv6_HDRLEN the IPv6 header if IPv6 extension headers are
|
|
|
|
* present.
|
2017-04-02 22:30:01 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The calculated checksum
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-30 08:45:15 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2018-11-05 15:56:07 +01:00
|
|
|
uint16_t ipv6_upperlayer_chksum(FAR struct net_driver_s *dev,
|
|
|
|
uint8_t proto, unsigned int iplen);
|
2017-04-02 22:30:01 +02:00
|
|
|
#endif
|
|
|
|
|
2014-06-28 01:51:32 +02:00
|
|
|
/****************************************************************************
|
2015-01-18 15:56:05 +01:00
|
|
|
* Name: tcp_chksum, tcp_ipv4_chksum, and tcp_ipv6_chksum
|
2014-06-28 01:51:32 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the TCP checksum of the packet in d_buf and d_appdata.
|
|
|
|
*
|
|
|
|
* The TCP checksum is the Internet checksum of data contents of the
|
|
|
|
* TCP segment, and a pseudo-header as defined in RFC793.
|
|
|
|
*
|
|
|
|
* Note: The d_appdata pointer that points to the packet data may
|
|
|
|
* point anywhere in memory, so it is not possible to simply calculate
|
|
|
|
* the Internet checksum of the contents of the d_buf buffer.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The TCP checksum of the TCP segment in d_buf and pointed to by
|
|
|
|
* d_appdata.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-18 15:56:05 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
uint16_t tcp_ipv4_chksum(FAR struct net_driver_s *dev);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
uint16_t tcp_ipv6_chksum(FAR struct net_driver_s *dev);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
|
2014-06-28 01:51:32 +02:00
|
|
|
uint16_t tcp_chksum(FAR struct net_driver_s *dev);
|
2015-01-18 15:56:05 +01:00
|
|
|
#elif defined(CONFIG_NET_IPv4)
|
|
|
|
# define tcp_chksum(d) tcp_ipv4_chksum(d)
|
|
|
|
#else /* if defined(CONFIG_NET_IPv6) */
|
|
|
|
# define tcp_chksum(d) tcp_ipv6_chksum(d)
|
|
|
|
#endif
|
|
|
|
|
2014-06-28 01:51:32 +02:00
|
|
|
/****************************************************************************
|
2015-01-23 16:33:18 +01:00
|
|
|
* Name: udp_ipv4_chksum
|
2014-06-28 01:51:32 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2015-01-23 16:33:18 +01:00
|
|
|
* Calculate the UDP/IPv4 checksum of the packet in d_buf and d_appdata.
|
2014-06-28 01:51:32 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-23 16:33:18 +01:00
|
|
|
#if defined(CONFIG_NET_UDP_CHECKSUMS) && defined(CONFIG_NET_IPv4)
|
|
|
|
uint16_t udp_ipv4_chksum(FAR struct net_driver_s *dev);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: udp_ipv6_chksum
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the UDP/IPv6 checksum of the packet in d_buf and d_appdata.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_UDP_CHECKSUMS) && defined(CONFIG_NET_IPv6)
|
|
|
|
uint16_t udp_ipv6_chksum(FAR struct net_driver_s *dev);
|
2014-06-28 01:51:32 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmp_chksum
|
|
|
|
*
|
|
|
|
* Description:
|
2015-01-15 01:34:28 +01:00
|
|
|
* Calculate the checksum of the IPv4 ICMP message
|
2014-06-28 01:51:32 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-11-25 15:03:33 +01:00
|
|
|
#ifdef CONFIG_NET_ICMP
|
2014-06-28 01:51:32 +02:00
|
|
|
uint16_t icmp_chksum(FAR struct net_driver_s *dev, int len);
|
2015-01-15 01:34:28 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmpv6_chksum
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the checksum of the ICMPv6 message
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
2018-11-05 15:56:07 +01:00
|
|
|
uint16_t icmpv6_chksum(FAR struct net_driver_s *dev, unsigned int iplen);
|
2015-01-15 01:34:28 +01:00
|
|
|
#endif
|
2014-06-28 01:51:32 +02:00
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
#undef EXTERN
|
|
|
|
#ifdef __cplusplus
|
2007-09-08 21:50:59 +02:00
|
|
|
}
|
2014-06-26 22:23:21 +02:00
|
|
|
#endif
|
2007-09-08 21:50:59 +02:00
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
#endif /* __NET_UTILS_UTILS_H */
|