trying to understand SD-NAT

Francisco Obispo fobispo at isc.org
Thu Feb 23 22:14:44 UTC 2012


trying to understand SD-NAT, led me to code a sample function to generate the mappings… I think that although it seems easy in principle, I believe it's badly documented in section 4.5 of the draft. This is my interpretation:





#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <math.h>

int cgn_mapping(int ,int ,struct in_addr *,int , struct in_addr *);
void usage(char *);


int main (int argc, char *argv[]){
  
  struct in_addr baseCPE,baseCGN;
  
  if(argc<6){
    usage(argv[0]);
    exit(1);
  }
  
  // Convert the third argument (baseCPE) to in_addr
  if( 1 > inet_pton(AF_INET,argv[3],&(baseCPE))){
    perror("<baseCPE> is not a valid ipv4 address");
    exit(1);
  }
  
  // Convert the fourth argument (baseCGN) to in_addr
  if( 1 > inet_pton(AF_INET,argv[5],&(baseCGN))){
     perror("<baseCGN> is not a valid ipv4 address");
     exit(1);
   }
  
   cgn_mapping(atoi(argv[1]),atoi(argv[2]),&baseCPE,atoi(argv[4]),&baseCGN);
  
  return 0;
} 



void usage(char *progname){
  printf("usage: %s <subscriber_index> <maxPort> <cpe_src_ip> <cpe_src_port> <baseCGN>\n",progname);
}


/* Creates a SDNAT mapping using:
 * http://tools.ietf.org/html/draft-penno-softwire-sdnat-01#section-4.5
 */

int cgn_mapping(int i,int maxPort,struct in_addr *baseCPE,int cpe_src_port,struct in_addr *baseCGN){
  
  int cgnSrcPort=0;
  
  char strBaseCPE[INET_ADDRSTRLEN];
  char strBaseCGN[INET_ADDRSTRLEN];

  int P=floor((65536-1024)/maxPort);

  cgnSrcPort=cpe_src_port + floor(i/P) + (maxPort * (i%P));

  inet_ntop(AF_INET,baseCPE,strBaseCPE,INET_ADDRSTRLEN);
  inet_ntop(AF_INET,baseCGN,strBaseCGN,INET_ADDRSTRLEN);

  printf("CPE (%d) %s:%d mapped to %s:%d\n",i,strBaseCPE,cpe_src_port,strBaseCGN,cgnSrcPort);

  return 0;
  
}















Francisco Obispo 
email: fobispo at isc.org
Phone: +1 650 423 1374 || INOC-DBA *3557* NOC
PGP KeyID = B38DB1BE



More information about the sdcpe-devel mailing list