networkingtestandutils/ipv6to4tunnel/src/6to4_tunnel.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Name        : 6to4_tunnel.cpp
       
    15 // Part of     : 6to4 plugin / 6to4.prt
       
    16 // Implements 6to4 automatic and configured tunnels, see
       
    17 // RFC 3056 & RFC 2893
       
    18 // Version     : 0.2
       
    19 //
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 // INCLUDE FILES
       
    25 #include "6to4_tunnel.h"
       
    26 
       
    27 // EXTERNAL DATA STRUCTURES
       
    28 // EXTERNAL FUNCTION PROTOTYPES  
       
    29 // CONSTANTS
       
    30 // MACROS
       
    31 // LOCAL CONSTANTS AND MACROS
       
    32 // MODULE DATA STRUCTURES
       
    33 // LOCAL FUNCTION PROTOTYPES
       
    34 // FORWARD DECLARATIONS
       
    35 
       
    36 // ============================= LOCAL FUNCTIONS ==============================
       
    37 
       
    38 // ----------------------------------------------------------------------------
       
    39 // TInetAddress6Only
       
    40 // A simple help class which overrides the Input method and
       
    41 // guarantees that the result is always KAfInet6 on valid
       
    42 // input.
       
    43 // ----------------------------------------------------------------------------
       
    44 class TInetAddr6Only : public TInetAddr
       
    45 	{
       
    46 	public:
       
    47 	TInt Input(const TDesC &aBuf);
       
    48 	};
       
    49 
       
    50 TInt TInetAddr6Only::Input(const TDesC &aBuf)
       
    51 	{
       
    52 	const TInt ret = TInetAddr::Input(aBuf);
       
    53 	if (ret == KErrNone && Family() == KAfInet)
       
    54 		ConvertToV4Mapped();
       
    55 	return ret;
       
    56 	}
       
    57 
       
    58 // ============================ MEMBER FUNCTIONS ==============================
       
    59 
       
    60 // ----------------------------------------------------------------------------
       
    61 // CTunnel::CTunnel
       
    62 // C++ default constructor can NOT contain any code, that
       
    63 // might leave.
       
    64 // ----------------------------------------------------------------------------
       
    65 //
       
    66 CTunnel::CTunnel ()
       
    67 	{
       
    68 	}
       
    69 
       
    70 // Destructor
       
    71 CTunnel::~CTunnel ()
       
    72 	{
       
    73 	delete iName;
       
    74 	}
       
    75 
       
    76 // ----------------------------------------------------------------------------
       
    77 // CTunnel::SetNameL
       
    78 // Sets a name for the tunnel.
       
    79 // ----------------------------------------------------------------------------
       
    80 //
       
    81 void CTunnel::SetNameL (const TDesC & aName)
       
    82 	{
       
    83 	delete iName;
       
    84 	iName = NULL;
       
    85 	iName = HBufC::NewMaxL (aName.Length ());
       
    86 	*iName = aName;
       
    87 	}
       
    88 
       
    89 // ----------------------------------------------------------------------------
       
    90 // TTunnelParser::TTunnelParser
       
    91 // C++ default constructor can NOT contain any code, that
       
    92 // might leave.
       
    93 // ----------------------------------------------------------------------------
       
    94 //
       
    95 TTunnelParser::TTunnelParser () : iToken(), iTokenVal(0), iTunnelQueue()
       
    96 	{
       
    97 	iTunnelQueue.SetOffset (_FOFF (CTunnel, iDLink));
       
    98 	}
       
    99 
       
   100 // Destructor
       
   101 TTunnelParser::~TTunnelParser ()
       
   102 	{
       
   103 	CTunnel *tunnel = NULL;
       
   104 	TTunnelQueueIter iter (iTunnelQueue);
       
   105 
       
   106 	while ((tunnel = iter++) != NULL)
       
   107 		{
       
   108 		tunnel->iDLink.Deque();
       
   109 		delete tunnel;
       
   110 		}
       
   111 	}
       
   112 
       
   113 // ----------------------------------------------------------------------------
       
   114 // TTunnelParser::ParseL
       
   115 // Parses the tunnel configuration file and creates tunnel objects accordingly.
       
   116 //
       
   117 // File syntax example. 
       
   118 // Keywords: tunnels, tunnel, endpoint_address, virtual_if_address,
       
   119 //           route_address, route_prefix_length, STRING, INTEGER.
       
   120 // #
       
   121 // # Write comments like this to the beginning of the file
       
   122 // #
       
   123 //
       
   124 // tunnels = {
       
   125 //	 tunnel_1,
       
   126 //	 tunnel_2,
       
   127 //	 tunnel_3
       
   128 //	}
       
   129 //
       
   130 // tunnel tunnel_1 = {
       
   131 //	 STRING endpoint_address = 192.168.0.3,
       
   132 //	 STRING virtual_if_address = 2000:abcd::1,
       
   133 //	 STRING route_address = 2000:abcd::,
       
   134 //	 INTEGER route_prefix_length = 32
       
   135 // }
       
   136 //
       
   137 // tunnel tunnel_2 = {
       
   138 // 	 STRING endpoint_address = 192.168.0.3,
       
   139 //   STRING virtual_if_address = 2000:1111::1,	
       
   140 //	 STRING route_address = 2000:1111::,
       
   141 //	 INTEGER route_prefix_length = 32
       
   142 // }
       
   143 //
       
   144 // tunnel tunnel_3 = {
       
   145 //	 STRING endpoint_address = 192.168.0.5,
       
   146 //   STRING virtual_if_address = 2000:2222::1,
       
   147 //	 STRING route_address = 2000:2222::,
       
   148 // 	 INTEGER route_prefix_length = 32
       
   149 // }
       
   150 // ----------------------------------------------------------------------------
       
   151 //
       
   152 TInt TTunnelParser::ParseL (const TDesC & aTunnelData)
       
   153 	{
       
   154 
       
   155 	Assign (aTunnelData);
       
   156 
       
   157 	// Parse the list of tunnels first and create and add them to the list.
       
   158 	TTokenType token;
       
   159 
       
   160 	while ((token = NextToken()) == ETokenTypeComment)
       
   161 		{
       
   162 				// Intentionally left blank
       
   163 		}
       
   164 	if (token == ETokenTypeString)
       
   165 		{
       
   166 		if (iToken.Compare (_L ("tunnels")) != 0)
       
   167 			{
       
   168 			return KErrGeneral;
       
   169 			}
       
   170 		}
       
   171 	else
       
   172 		{
       
   173 		return token == ETokenTypeEof ? KErrNone : KErrGeneral;
       
   174 		}
       
   175 
       
   176 	if ((token = NextToken ()) != ETokenTypeEqual)
       
   177 		{
       
   178 		return KErrGeneral;
       
   179 		}
       
   180 
       
   181 	if ((token = NextToken ()) != ETokenTypeBraceLeft)
       
   182 		{
       
   183 		return KErrGeneral;
       
   184 		}
       
   185 
       
   186 	do
       
   187 		{
       
   188 		token = NextToken ();
       
   189 		if (token == ETokenTypeBraceRight)
       
   190 			{
       
   191 			// Tunnels done
       
   192 			break;
       
   193 			}
       
   194 
       
   195 		if (token == ETokenTypeString)
       
   196 			{
       
   197 			CTunnel *tunnel = new (ELeave) CTunnel;
       
   198 
       
   199 			tunnel->SetNameL (iToken);
       
   200 
       
   201 			iTunnelQueue.AddLast (*tunnel);
       
   202 			}
       
   203 		else
       
   204 			{
       
   205 			return KErrGeneral;
       
   206 			}
       
   207 		}
       
   208 	while ((token = NextToken ()) == ETokenTypeComma);
       
   209 
       
   210 	if (token != ETokenTypeBraceRight)
       
   211 		{
       
   212 		return KErrGeneral;
       
   213 		}
       
   214 
       
   215 	// Parse individual tunnels. Search for the list for a match and if
       
   216 	// found, add parameters there.
       
   217 	while ((token = NextToken ()) == ETokenTypeString)
       
   218 		{
       
   219 		if (iToken.Compare (_L ("tunnel")) != 0)
       
   220 			{
       
   221 			return KErrGeneral;
       
   222 			}
       
   223 
       
   224 		TInetAddr6Only address;
       
   225 		CTunnel *tunnel = NULL;
       
   226 
       
   227 		if ((token = NextToken ()) == ETokenTypeString)
       
   228 			{
       
   229 			TTunnelQueueIter iter (iTunnelQueue);
       
   230 
       
   231 			while ((tunnel = iter++) != NULL)
       
   232 				{
       
   233 				if (tunnel->Name().Compare (iToken) == 0)
       
   234 					{
       
   235 					// found.
       
   236 					break;
       
   237 					}
       
   238 				}
       
   239 			if (tunnel == NULL)
       
   240 				{
       
   241 				return KErrGeneral;
       
   242 				}
       
   243 			}
       
   244 		else
       
   245 			{
       
   246 			return KErrGeneral;
       
   247 			}
       
   248 
       
   249 		if ((token = NextToken ()) != ETokenTypeEqual)
       
   250 			{
       
   251 			return KErrGeneral;
       
   252 			}
       
   253 
       
   254 		if ((token = NextToken ()) != ETokenTypeBraceLeft)
       
   255 			{
       
   256 			return KErrGeneral;
       
   257 			}
       
   258 
       
   259 		if ((token = NextToken ()) == ETokenTypeString)
       
   260 			{
       
   261 			if (iToken.Compare (_L ("STRING")) != 0)
       
   262 				{
       
   263 				return KErrGeneral;
       
   264 				}
       
   265 			}
       
   266 		else
       
   267 			{
       
   268 			return KErrGeneral;
       
   269 			}
       
   270 
       
   271 		if ((token = NextToken ()) == ETokenTypeString)
       
   272 			{
       
   273 			if (iToken.Compare (_L ("endpoint_address")) != 0)
       
   274 				{
       
   275 				return KErrGeneral;
       
   276 				}
       
   277 			}
       
   278 		else
       
   279 			{
       
   280 			return KErrGeneral;
       
   281 			}
       
   282 
       
   283 		if ((token = NextToken ()) != ETokenTypeEqual)
       
   284 			{
       
   285 			return KErrGeneral;
       
   286 			}
       
   287 
       
   288 		if ((token = NextToken ()) == ETokenTypeString)
       
   289 			{
       
   290 			// Parse endpoint address
       
   291 			if (address.Input (iToken) != KErrNone)
       
   292 				{
       
   293 				return KErrGeneral;
       
   294 				}
       
   295 			tunnel->EndpointAddr().SetAddress (address.Ip6Address(), address.Scope());
       
   296 			}
       
   297 		else
       
   298 			{
       
   299 			return KErrGeneral;
       
   300 			}
       
   301 
       
   302 		if ((token = NextToken ()) != ETokenTypeComma)
       
   303 			{
       
   304 			return KErrGeneral;
       
   305 			}
       
   306 
       
   307 		if ((token = NextToken ()) == ETokenTypeString)
       
   308 			{
       
   309 			if (iToken.Compare (_L ("STRING")) != 0)
       
   310 				{
       
   311 				return KErrGeneral;
       
   312 				}
       
   313 			}
       
   314 		else
       
   315 			{
       
   316 			return KErrGeneral;
       
   317 			}
       
   318 
       
   319 		if ((token = NextToken ()) == ETokenTypeString)
       
   320 			{
       
   321 			if (iToken.Compare (_L ("virtual_if_address")) != 0)
       
   322 				{
       
   323 				return KErrGeneral;
       
   324 				}
       
   325 			}
       
   326 		else
       
   327 			{
       
   328 			return KErrGeneral;
       
   329 			}
       
   330 
       
   331 		if ((token = NextToken ()) != ETokenTypeEqual)
       
   332 			{
       
   333 			return KErrGeneral;
       
   334 			}
       
   335 
       
   336 		if ((token = NextToken ()) == ETokenTypeString)
       
   337 			{
       
   338 			// Parse inner virtual address
       
   339 			if (address.Input (iToken) != KErrNone)
       
   340 				{
       
   341 				return KErrGeneral;
       
   342 				}
       
   343 			tunnel->VirtualIfAddr().SetAddress(address.Ip6Address(), address.Scope());
       
   344 			}
       
   345 		else
       
   346 			{
       
   347 			return KErrGeneral;
       
   348 			}
       
   349 
       
   350 		if ((token = NextToken ()) != ETokenTypeComma)
       
   351 			{
       
   352 			return KErrGeneral;
       
   353 			}
       
   354 
       
   355 		if ((token = NextToken ()) == ETokenTypeString)
       
   356 			{
       
   357 			if (iToken.Compare (_L ("STRING")) != 0)
       
   358 				{
       
   359 				return KErrGeneral;
       
   360 				}
       
   361 			}
       
   362 		else
       
   363 			{
       
   364 			return KErrGeneral;
       
   365 			}
       
   366 
       
   367 		if ((token = NextToken ()) == ETokenTypeString)
       
   368 			{
       
   369 			if (iToken.Compare (_L ("route_address")) != 0)
       
   370 				{
       
   371 				return KErrGeneral;
       
   372 				}
       
   373 			}
       
   374 		else
       
   375 			{
       
   376 			return KErrGeneral;
       
   377 			}
       
   378 
       
   379 		if ((token = NextToken ()) != ETokenTypeEqual)
       
   380 			{
       
   381 			return KErrGeneral;
       
   382 			}
       
   383 
       
   384 		if ((token = NextToken ()) == ETokenTypeString)
       
   385 			{
       
   386 			// Parse route prefix as "address"
       
   387 			if (address.Input (iToken) != KErrNone)
       
   388 				{
       
   389 				return KErrGeneral;
       
   390 				}
       
   391 			tunnel->RouteAddr() = address.Ip6Address();
       
   392 			}
       
   393 		else
       
   394 			{
       
   395 			return KErrGeneral;
       
   396 			}
       
   397 
       
   398 		if ((token = NextToken ()) != ETokenTypeComma)
       
   399 			{
       
   400 			return KErrGeneral;
       
   401 			}
       
   402 
       
   403 		if ((token = NextToken ()) == ETokenTypeString)
       
   404 			{
       
   405 			if (iToken.Compare (_L ("INTEGER")) != 0)
       
   406 				{
       
   407 				return KErrGeneral;
       
   408 				}
       
   409 			}
       
   410 		else
       
   411 			{
       
   412 			return KErrGeneral;
       
   413 			}
       
   414 
       
   415 		if ((token = NextToken ()) == ETokenTypeString)
       
   416 			{
       
   417 			if (iToken.Compare (_L ("route_prefix_length")) != 0)
       
   418 				{
       
   419 				return KErrGeneral;
       
   420 				}
       
   421 			}
       
   422 		else
       
   423 			{
       
   424 			return KErrGeneral;
       
   425 			}
       
   426 
       
   427 		if ((token = NextToken ()) != ETokenTypeEqual)
       
   428 			{
       
   429 			return KErrGeneral;
       
   430 			}
       
   431 
       
   432 		if (Val (iTokenVal) != KErrNone)
       
   433 			return KErrGeneral;
       
   434 		tunnel->SetRoutePrefixLength(iTokenVal);
       
   435 
       
   436 		if ((token = NextToken ()) != ETokenTypeBraceRight)
       
   437 			{
       
   438 			return KErrGeneral;
       
   439 			}
       
   440 		}
       
   441 
       
   442 	// Scan through the list and make sure all tunnels have been configured.
       
   443 	TTunnelQueueIter iter (iTunnelQueue);
       
   444 	CTunnel *tunnel = NULL;
       
   445 
       
   446 	while ((tunnel = iter++) != NULL)
       
   447 		{
       
   448 		if (tunnel->EndpointAddr().IsUnspecified ())
       
   449 			{
       
   450 			// not configured.
       
   451 			return KErrGeneral;
       
   452 			}
       
   453 		}
       
   454 
       
   455 	return KErrNone;
       
   456 	}
       
   457 
       
   458 // ----------------------------------------------------------------------------
       
   459 // TTunnelParser::NextToken
       
   460 // Returns the next token type in the input stream. iToken gets the value of 
       
   461 // the (string) token.
       
   462 // ----------------------------------------------------------------------------
       
   463 //
       
   464 TTunnelParser::TTokenType TTunnelParser::NextToken ()
       
   465 	{
       
   466 	TChar ch;
       
   467 	TTokenType val;
       
   468 
       
   469 	SkipSpaceAndMark ();
       
   470 	if (Eos ())
       
   471 		val = ETokenTypeEof;
       
   472 	else
       
   473 		{
       
   474 		ch = Get ();
       
   475 		switch (ch)
       
   476 			{
       
   477 			case '{':
       
   478 				val = ETokenTypeBraceLeft;
       
   479 				break;
       
   480 			case '}':
       
   481 				val = ETokenTypeBraceRight;
       
   482 				break;
       
   483 			case '(':
       
   484 				val = ETokenTypeParLeft;
       
   485 				break;
       
   486 			case ')':
       
   487 				val = ETokenTypeParRight;
       
   488 				break;
       
   489 			case '=':
       
   490 				val = ETokenTypeEqual;
       
   491 				break;
       
   492 			case ',':
       
   493 				val = ETokenTypeComma;
       
   494 				break;
       
   495 				case '#':
       
   496 						val = ETokenTypeComment;
       
   497 						while (!Eos())
       
   498 						{
       
   499 								ch = Get();
       
   500 								if (ch == '\n' || ch == '\r')
       
   501 										break;
       
   502 						}
       
   503 						break;
       
   504 			default:
       
   505 				// Integers, ip addresses, etc. are mapped to strings.
       
   506 				val = ETokenTypeString;
       
   507 				while (!Eos ())
       
   508 					{
       
   509 					ch = Peek ();
       
   510 					if (ch == '{' || ch == '}' ||
       
   511 						ch == '(' || ch == ')' ||
       
   512 						ch == '=' || ch == '#' ||
       
   513 						ch == ',' || ch.IsSpace ())
       
   514 						break;
       
   515 					Inc ();
       
   516 					}
       
   517 			}
       
   518 		}
       
   519 
       
   520 	iToken.Set (MarkedToken ());
       
   521 	SkipSpaceAndMark ();
       
   522 
       
   523 	return val;
       
   524 	}
       
   525 
       
   526 // ========================== OTHER EXPORTED FUNCTIONS ========================
       
   527 
       
   528 //  End of File