openenvutils/telnetserver/src/connect.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 2007-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 // dhcp client to start a connection.
       
    15 // 
       
    16 //
       
    17 
       
    18 /*
       
    19 * ==============================================================================
       
    20 Redistribution and use in source and binary forms, with or without 
       
    21 modification, are permitted provided that the following conditions are met:
       
    22  * Redistributions of source code must retain the above copyright notice, this 
       
    23    list of conditions and the following disclaimer. 
       
    24  * Redistributions in binary form must reproduce the above copyright notice, 
       
    25    this list of conditions and the following disclaimer in the documentation 
       
    26    and/or other materials provided with the distribution. 
       
    27  * Neither the name of the <ORGANIZATION> nor the names of its contributors 
       
    28    may be used to endorse or promote products derived from this software 
       
    29    without specific prior written permission. 
       
    30    
       
    31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
       
    32 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
       
    33 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
       
    34 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
       
    35 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
       
    36 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
       
    37 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
       
    38 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
       
    39 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
       
    40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    41 * ==============================================================================
       
    42 */
       
    43 
       
    44 #include <e32cmn.h>
       
    45 //
       
    46 #include <es_sock.h>
       
    47 #include <e32std.h>
       
    48 
       
    49 //For ipconfig
       
    50 #include <e32cons.h>
       
    51 #include <in_sock.h>
       
    52 #include <in_iface.h>
       
    53 #include <nifman.h>
       
    54 
       
    55 
       
    56 // dhcp client - To initiate a request for an IP to a
       
    57 // dhcp server on the same network(via ethernet)
       
    58 
       
    59 #ifdef __cplusplus
       
    60 extern "C" {
       
    61 #endif
       
    62 
       
    63 void GetIpFromDHCPServer(void);
       
    64 void ProgramL(CConsoleBase& aCons);
       
    65 void PrintFeature(CConsoleBase& aCons, TUint& aMask, TUint aFeature, const TDesC& aDes);
       
    66 
       
    67 
       
    68 TInt getConnectionUp()
       
    69     {
       
    70 	__UHEAP_MARK;
       
    71 	// Standard stuff
       
    72 	CTrapCleanup* trap = CTrapCleanup::New();
       
    73 	if(trap==NULL)
       
    74 		return KErrNoMemory;
       
    75 
       
    76 	CConsoleBase* cons=NULL;
       
    77 	TRAPD(e, cons=Console::NewL(_L("Telnetd ipconfig utility"),TSize(KConsFullScreen,KConsFullScreen));)
       
    78 	if(e!=KErrNone)
       
    79 		{
       
    80 		delete trap;
       
    81 		return e;
       
    82 		}
       
    83 
       
    84 	TRAPD(res, ProgramL(*cons);)
       
    85 	if(res!=KErrNone)
       
    86 		{
       
    87 		cons->Printf(_L("Error %d\n"), res);
       
    88 		cons->Printf(_L("Press any key to exit"));
       
    89 		}
       
    90 
       
    91 	cons->Getch();
       
    92 	
       
    93 	delete cons;
       
    94 	delete trap;
       
    95 
       
    96 	__UHEAP_MARKEND;
       
    97 	return res;
       
    98     }
       
    99 
       
   100 void PrintFeature(CConsoleBase& aCons, TUint& aMask, TUint aFeature, const TDesC& aDes)
       
   101 	{
       
   102 	if(aMask&aFeature)
       
   103 		{
       
   104 		aMask&=~aFeature;
       
   105 		aCons.Printf(aDes);
       
   106 		if(aMask)
       
   107 			aCons.Printf(_L(","));
       
   108 		}
       
   109 	}
       
   110 void GetIpFromDHCPServer(void)
       
   111     {
       
   112     RSocketServ rSockServer;
       
   113     if(KErrNone==rSockServer.Connect())
       
   114         {
       
   115         RConnection rConnect;
       
   116         if(KErrNone==rConnect.Open(rSockServer))
       
   117             {
       
   118             TRequestStatus status; 
       
   119             rConnect.Start(status);
       
   120                 
       
   121             User::WaitForRequest(status); 
       
   122             }
       
   123         } 
       
   124     }
       
   125 
       
   126 
       
   127 	void ProgramL(CConsoleBase& aCons)
       
   128     {
       
   129 	GetIpFromDHCPServer();
       
   130     TAutoClose<RSocketServ> ss;
       
   131     User::LeaveIfError(ss.iObj.Connect());
       
   132     ss.PushL();
       
   133 
       
   134     TAutoClose<RSocket> sock;
       
   135     User::LeaveIfError(sock.iObj.Open(ss.iObj, _L("udp")));
       
   136     sock.PushL();
       
   137 
       
   138     User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
       
   139 
       
   140     TProtocolDesc in;
       
   141     User::LeaveIfError(sock.iObj.Info(in));
       
   142     aCons.Printf(_L("Mobile device IP Configuration:\n\n"));
       
   143 
       
   144     TPckgBuf<TSoInetInterfaceInfo> info, next;
       
   145 
       
   146     TInt res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info);
       
   147     if(res!=KErrNone)
       
   148         User::Leave(res);
       
   149     TBool hasIP=EFalse;
       
   150 	while(res==KErrNone)
       
   151         {
       
   152 		
       
   153         res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, next);
       
   154 		// || info().iAddress.IsLinkLocal() || info().iAddress.IsSiteLocal()
       
   155 	if(info().iState != EIfUp || info().iFeatures&KIfIsLoopback  || (info().iAddress.Family() == KAfInet6 && info().iAddress.IsV4Mapped() == EFalse)) //(info().iAddress.IsV4Compat() == EFalse || 
       
   156 		{
       
   157 		info = next; continue;	
       
   158 		}
       
   159 	hasIP=1;
       
   160 		aCons.Printf(_L("%S:\n"), &info().iName);
       
   161         
       
   162        
       
   163             TName address;
       
   164 	        info().iAddress.Output(address);
       
   165 	        aCons.Printf(_L("IP Address:%S\n"),&address);
       
   166        
       
   167         
       
   168         info().iNetMask.Output(address);
       
   169         aCons.Printf(_L("Subnet mask:%S\n\n"), &address);
       
   170         
       
   171         if(res==KErrNone)
       
   172             {
       
   173             info = next;
       
   174          	}
       
   175         }
       
   176     if(hasIP == EFalse)
       
   177 	{
       
   178 	aCons.Printf(_L("No IP address obtained."));
       
   179 	User::Leave(KErrNotFound );
       
   180 	}
       
   181 	aCons.Printf(_L("Telnetd to start on port 23.\nPress any key..."));
       
   182     sock.Pop();
       
   183     ss.Pop();
       
   184     }	
       
   185 #ifdef __cplusplus
       
   186 } //extern "C" {
       
   187 #endif