nat.h File Reference

NAT (IP Network Address Translator) More...

#include "core/net.h"

Go to the source code of this file.

Data Structures

struct  NatIpPacket
 IP packet. More...
 
struct  NatPortFwdRule
 Port redirection rule. More...
 
struct  NatSession
 NAT session. More...
 
struct  NatSettings
 NAT settings. More...
 
struct  NatContext
 NAT context. More...
 

Macros

#define NAT_SUPPORT   DISABLED
 
#define NAT_MAX_PRIVATE_INTERFACES   4
 
#define NAT_TICK_INTERVAL   1000
 
#define NAT_TCP_SESSION_TIMEOUT   120000
 
#define NAT_UDP_SESSION_TIMEOUT   120000
 
#define NAT_ICMP_SESSION_TIMEOUT   10000
 
#define NAT_TCP_UDP_PORT_MIN   32768
 
#define NAT_TCP_UDP_PORT_MAX   49151
 
#define NAT_ICMP_QUERY_ID_MIN   32768
 
#define NAT_ICMP_QUERY_ID_MAX   65535
 

Functions

void natGetDefaultSettings (NatSettings *settings)
 Initialize settings with default values. More...
 
error_t natInit (NatContext *context, const NatSettings *settings)
 NAT initialization. More...
 
error_t natSetPublicInterface (NatContext *context, NetInterface *publicInterface)
 Specify the NAT public interface. More...
 
error_t natSetPortFwdRule (NatContext *context, uint_t index, Ipv4Protocol protocol, uint16_t publicPort, NetInterface *privateInterface, Ipv4Addr privateIpAddr, uint16_t privatePort)
 Add port redirection rule. More...
 
error_t natSetPortRangeFwdRule (NatContext *context, uint_t index, Ipv4Protocol protocol, uint16_t publicPortMin, uint16_t publicPortMax, NetInterface *privateInterface, Ipv4Addr privateIpAddr, uint16_t privatePortMin)
 Add port range redirection rule. More...
 
error_t natClearPortFwdRule (NatContext *context, uint_t index)
 Remove port redirection rule. More...
 
error_t natStart (NatContext *context)
 Start NAT operation. More...
 
error_t natStop (NatContext *context)
 Stop NAT operation. More...
 
void natDeinit (NatContext *context)
 Release NAT context. More...
 

Detailed Description

NAT (IP Network Address Translator)

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneTCP Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.5.0

Definition in file nat.h.

Macro Definition Documentation

◆ NAT_ICMP_QUERY_ID_MAX

#define NAT_ICMP_QUERY_ID_MAX   65535

Definition at line 102 of file nat.h.

◆ NAT_ICMP_QUERY_ID_MIN

#define NAT_ICMP_QUERY_ID_MIN   32768

Definition at line 95 of file nat.h.

◆ NAT_ICMP_SESSION_TIMEOUT

#define NAT_ICMP_SESSION_TIMEOUT   10000

Definition at line 74 of file nat.h.

◆ NAT_MAX_PRIVATE_INTERFACES

#define NAT_MAX_PRIVATE_INTERFACES   4

Definition at line 46 of file nat.h.

◆ NAT_SUPPORT

#define NAT_SUPPORT   DISABLED

Definition at line 39 of file nat.h.

◆ NAT_TCP_SESSION_TIMEOUT

#define NAT_TCP_SESSION_TIMEOUT   120000

Definition at line 60 of file nat.h.

◆ NAT_TCP_UDP_PORT_MAX

#define NAT_TCP_UDP_PORT_MAX   49151

Definition at line 88 of file nat.h.

◆ NAT_TCP_UDP_PORT_MIN

#define NAT_TCP_UDP_PORT_MIN   32768

Definition at line 81 of file nat.h.

◆ NAT_TICK_INTERVAL

#define NAT_TICK_INTERVAL   1000

Definition at line 53 of file nat.h.

◆ NAT_UDP_SESSION_TIMEOUT

#define NAT_UDP_SESSION_TIMEOUT   120000

Definition at line 67 of file nat.h.

Function Documentation

◆ natClearPortFwdRule()

error_t natClearPortFwdRule ( NatContext context,
uint_t  index 
)

Remove port redirection rule.

Parameters
[in]contextPointer to the NAT context
[in]indexZero-based index identifying a given entry
Returns
Error code

Definition at line 273 of file nat.c.

◆ natDeinit()

void natDeinit ( NatContext context)

Release NAT context.

Parameters
[in]contextPointer to the NAT context

Definition at line 386 of file nat.c.

◆ natGetDefaultSettings()

void natGetDefaultSettings ( NatSettings settings)

Initialize settings with default values.

Parameters
[out]settingsStructure that contains NAT settings

Definition at line 60 of file nat.c.

◆ natInit()

error_t natInit ( NatContext context,
const NatSettings settings 
)

NAT initialization.

Parameters
[in]contextPointer to the NAT context
[in]settingsNAT specific settings
Returns
Error code

Definition at line 87 of file nat.c.

◆ natSetPortFwdRule()

error_t natSetPortFwdRule ( NatContext context,
uint_t  index,
Ipv4Protocol  protocol,
uint16_t  publicPort,
NetInterface privateInterface,
Ipv4Addr  privateIpAddr,
uint16_t  privatePort 
)

Add port redirection rule.

Parameters
[in]contextPointer to the NAT context
[in]indexZero-based index identifying a given entry
[in]protocolTransport protocol (IPV4_PROTOCOL_TCP or IPV4_PROTOCOL_UDP)
[in]publicPortPublic port to be redirected
[in]privateInterfaceDestination interface
[in]privateIpAddrDestination IP address
[in]privatePortDestination port
Returns
Error code

Definition at line 196 of file nat.c.

◆ natSetPortRangeFwdRule()

error_t natSetPortRangeFwdRule ( NatContext context,
uint_t  index,
Ipv4Protocol  protocol,
uint16_t  publicPortMin,
uint16_t  publicPortMax,
NetInterface privateInterface,
Ipv4Addr  privateIpAddr,
uint16_t  privatePortMin 
)

Add port range redirection rule.

Parameters
[in]contextPointer to the NAT context
[in]indexZero-based index identifying a given entry
[in]protocolTransport protocol (IPV4_PROTOCOL_TCP or IPV4_PROTOCOL_UDP)
[in]publicPortMinPublic port range to be redirected (lower value)
[in]publicPortMaxPublic port range to be redirected (upper value)
[in]privateInterfaceDestination interface
[in]privateIpAddrDestination IP address
[in]privatePortMinDestination port (lower value)
Returns
Error code

Definition at line 224 of file nat.c.

◆ natSetPublicInterface()

error_t natSetPublicInterface ( NatContext context,
NetInterface publicInterface 
)

Specify the NAT public interface.

Parameters
[in]contextPointer to the NAT context
[in]publicInterfaceNAT public interface
Returns
Error code

Definition at line 154 of file nat.c.

◆ natStart()

error_t natStart ( NatContext context)

Start NAT operation.

Parameters
[in]contextPointer to the NAT context
Returns
Error code

Definition at line 301 of file nat.c.

◆ natStop()

error_t natStop ( NatContext context)

Stop NAT operation.

Parameters
[in]contextPointer to the NAT context
Returns
Error code

Definition at line 345 of file nat.c.