nat.c File Reference

NAT (IP Network Address Translator) More...

#include "core/net.h"
#include "nat/nat.h"
#include "nat/nat_misc.h"
#include "debug.h"

Go to the source code of this file.

Macros

#define TRACE_LEVEL   NAT_TRACE_LEVEL
 

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.

Description

Network Address Translation (NAT) is a method of mapping one IP address space to another by modifying network address information in the IP header of packets. Refer to the following RFCs for complete details:

  • RFC 2663: NAT Terminology and Considerations
  • RFC 3022: Traditional IP Network Address Translator (Traditional NAT)
  • RFC 4787: NAT Behavioral Requirements for Unicast UDP
  • RFC 5382: NAT Behavioral Requirements for TCP
  • RFC 5508: NAT Behavioral Requirements for ICMP
Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.5.0

Definition in file nat.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   NAT_TRACE_LEVEL

Definition at line 43 of file nat.c.

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.