sdkcreationmw/sdkruntimes/wsock/src/WinsockProtocolFamily.cpp
changeset 0 b26acd06ea60
child 1 ac50fd48361b
equal deleted inserted replaced
-1:000000000000 0:b26acd06ea60
       
     1 /*
       
     2 * Copyright (c) 2004-2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #define TRACE_PREFIX "WSOCK: ProtocolFamily: "
       
    20 #include "wsock.h"
       
    21 #include "WinsockProtocol.h"
       
    22 #include "WinsockProtocolFamily.h"
       
    23 #include <winsock2.h>
       
    24 
       
    25 #define IPPROTO_ICMPV6 58
       
    26 
       
    27 const TUint KProtocolInetRes = 0xF01;
       
    28 
       
    29 // Initializer for TWinProtocolDesc structure
       
    30 class TWinProtocolDescInit: public TWinProtocolDesc
       
    31 {
       
    32 public:
       
    33     TWinProtocolDescInit(const TText * aName,
       
    34         TUint aAddrFamily, TUint aSockType, TUint aProtocol,
       
    35         TUint aServiceInfo, TInt aMessageSize, TInt aServiceTypeInfo,
       
    36         TUint aWinSockType, TUint aWinProtocol)
       
    37     {
       
    38         iName = aName;
       
    39         iAddrFamily = aAddrFamily;
       
    40         iSockType = aSockType;
       
    41         iProtocol = aProtocol;
       
    42         iVersion = TVersion(1,0,0);
       
    43         iByteOrder = EBigEndian;
       
    44         iServiceInfo = aServiceInfo;
       
    45         iNamingServices = KNSNameResolution;
       
    46         iSecurity = KSocketNoSecurity;
       
    47         iMessageSize = aMessageSize;
       
    48         iServiceTypeInfo = aServiceTypeInfo;
       
    49         iNumSockets = KUnlimitedSockets;
       
    50         iWinSockType = aWinSockType;
       
    51         iWinProtocol = aWinProtocol;
       
    52     }
       
    53 };
       
    54 
       
    55 TWinProtocolDescInit TCP(_S("tcp"),
       
    56     KAfInet,  KSockStream,  KProtocolInetTcp, KSIStreamBased | KSIInOrder |
       
    57     KSIReliable | KSIGracefulClose | KSIPeekData | KSIUrgentData, 
       
    58     KSocketMessageSizeIsStream, ESocketSupport,
       
    59     SOCK_STREAM, IPPROTO_TCP);
       
    60 
       
    61 TWinProtocolDescInit UDP(_S("udp"),
       
    62     KAfInet,  KSockDatagram,  KProtocolInetUdp, 
       
    63     KSIConnectionLess | KSIDatagram | KSIGracefulClose | KSIPeekData,
       
    64     65516, ESocketSupport,
       
    65     SOCK_DGRAM, IPPROTO_UDP);
       
    66 
       
    67 TWinProtocolDescInit ICMP4(_S("icmp"),
       
    68     KAfInet,  KSockDatagram,  KProtocolInetIcmp, KSIConnectionLess |
       
    69     KSIMessageBased | KSIBroadcast | KSIPeekData | KSIGracefulClose,
       
    70     65000, ESocketSupport | ETransport | EPreferDescriptors | EUseCanSend,
       
    71     SOCK_RAW, IPPROTO_ICMP);
       
    72 
       
    73 TWinProtocolDescInit ICMP6(_S("icmp6"),
       
    74     KAfInet,  KSockDatagram,  KProtocolInet6Icmp, KSIConnectionLess |
       
    75     KSIMessageBased | KSIBroadcast | KSIPeekData | KSIGracefulClose,
       
    76     65000, ESocketSupport | ETransport | EPreferDescriptors | EUseCanSend,
       
    77     SOCK_RAW, IPPROTO_ICMPV6);
       
    78 
       
    79 #ifdef WSOCK_IPV6
       
    80 TWinProtocolDescInit TCP6(_S("tcp"),
       
    81     KAfInet6,  KSockStream,  KProtocolInetTcp, KSIStreamBased | KSIInOrder |
       
    82     KSIReliable | KSIGracefulClose | KSIPeekData | KSIUrgentData, 
       
    83     KSocketMessageSizeIsStream, ESocketSupport,
       
    84     SOCK_STREAM, IPPROTO_TCP);
       
    85 
       
    86 TWinProtocolDescInit UDP6(_S("udp"),
       
    87     KAfInet6,  KSockDatagram,  KProtocolInetUdp, 
       
    88     KSIConnectionLess | KSIDatagram |KSIGracefulClose | KSIPeekData,
       
    89     65516, ESocketSupport,
       
    90     SOCK_DGRAM, IPPROTO_UDP);
       
    91 
       
    92 TWinProtocolDescInit ICMP46(_S("icmp"),
       
    93     KAfInet6,  KSockDatagram,  KProtocolInetIcmp, KSIConnectionLess |
       
    94     KSIMessageBased | KSIBroadcast | KSIPeekData | KSIGracefulClose,
       
    95     65000, ESocketSupport | ETransport | EPreferDescriptors | EUseCanSend,
       
    96     SOCK_RAW, IPPROTO_ICMP);
       
    97 
       
    98 TWinProtocolDescInit ICMP66(_S("icmp6"),
       
    99     KAfInet6,  KSockDatagram,  KProtocolInet6Icmp, KSIConnectionLess |
       
   100     KSIMessageBased | KSIBroadcast | KSIPeekData | KSIGracefulClose,
       
   101     65000, ESocketSupport | ETransport | EPreferDescriptors | EUseCanSend,
       
   102     SOCK_RAW, IPPROTO_ICMPV6);
       
   103 
       
   104 #endif // WSOCK_IPV6
       
   105 
       
   106 // NOTE: the order of protocols must match the order of entries in the
       
   107 // ip.tcp.esk file (at the time of thie writing this file was located 
       
   108 // in c:\private\101f7989\ESock directory).
       
   109 static const TWinProtocolDesc* ipProtocols[] = {
       
   110     &TCP,&UDP,&ICMP4,&ICMP6,
       
   111 #ifdef WSOCK_IPV6
       
   112     &TCP6,&UDP6,&ICMP46,&ICMP66
       
   113 #endif // WSOCK_IPV6
       
   114 };
       
   115 
       
   116 CWinsockProtocolFamily::CWinsockProtocolFamily(TUint aAddrFamily) :
       
   117 iAddrFamily(aAddrFamily)
       
   118 {
       
   119     TRACE2("[%08X] created (0x%04x)",this,aAddrFamily);
       
   120 }
       
   121 
       
   122 CWinsockProtocolFamily::~CWinsockProtocolFamily()
       
   123 {
       
   124     TRACE1("[%08X] destroyed",this);
       
   125 }
       
   126 
       
   127 TInt CWinsockProtocolFamily::Install()
       
   128 {
       
   129     TRACE1("[%08X] Install",this);
       
   130     return KErrNone;
       
   131 }
       
   132 
       
   133 // Access to the list of protocols
       
   134 TInt CWinsockProtocolFamily::ProtocolCount()
       
   135 {
       
   136     return N(ipProtocols);
       
   137 }
       
   138 
       
   139 const TServerProtocolDesc* CWinsockProtocolFamily::ProtocolDesc(TInt aIndex)
       
   140 {
       
   141     ASSERT(aIndex >= 0 && aIndex < N(ipProtocols));
       
   142     if (aIndex >= 0 && aIndex < N(ipProtocols))
       
   143     {
       
   144         return ipProtocols[aIndex];
       
   145     }
       
   146     else
       
   147     {
       
   148         return NULL;
       
   149     }
       
   150 }
       
   151 
       
   152 // Returns comma separated list of protocol names
       
   153 void CWinsockProtocolFamily::ProtocolNames(TDes& aProtocols)
       
   154 {
       
   155     aProtocols.SetLength(0);
       
   156     for (TInt i=0; i<N(ipProtocols); i++)
       
   157     {
       
   158         TBool alreadyThere = EFalse;
       
   159         for (TInt k=0; k<i; k++)
       
   160         {
       
   161             if (!ipProtocols[i]->iName.Compare(ipProtocols[k]->iName))
       
   162             {
       
   163                 alreadyThere = ETrue;
       
   164                 break;
       
   165             }
       
   166         }
       
   167         if (!alreadyThere)
       
   168         {
       
   169             if (i > 0) aProtocols.Append(',');
       
   170             aProtocols.Append(ipProtocols[i]->iName);
       
   171         }
       
   172     }
       
   173     TRACE1("ProtocolNames: %S",&aProtocols);
       
   174 }
       
   175 
       
   176 // CProtocolFamilyBase
       
   177 TUint CWinsockProtocolFamily::ProtocolList(TServerProtocolDesc*& aProtocolList)
       
   178 {
       
   179     // we allocate slightly more, but that's OK
       
   180     TInt count = 0;
       
   181     aProtocolList = new(ELeave)TServerProtocolDesc[N(ipProtocols)];
       
   182     if (aProtocolList)
       
   183     {
       
   184         for (TInt i=0; i<N(ipProtocols); i++)
       
   185         {
       
   186             if (ipProtocols[i]->iAddrFamily == iAddrFamily)
       
   187             {
       
   188                 const TServerProtocolDesc* src = ipProtocols[i];
       
   189                 aProtocolList[count++] = *src;
       
   190             }
       
   191         }
       
   192 
       
   193         if (!count)
       
   194         {
       
   195             delete aProtocolList;
       
   196             aProtocolList =  NULL;
       
   197         }
       
   198     }
       
   199     
       
   200     TRACE2("[%08X] ProtocolList - %d protocols",this,count);
       
   201     return count;
       
   202 }
       
   203 
       
   204 CProtocolBase* CWinsockProtocolFamily::NewProtocolL(TUint aSockType,
       
   205                                                     TUint aProtocol)
       
   206 {
       
   207     TRACE3("[%08X] NewProtocolL(%d,%d)",this,aSockType,aProtocol);
       
   208     if (aProtocol == KUndefinedProtocol)
       
   209     {
       
   210         switch (aSockType)
       
   211         {
       
   212         case KSockDatagram:
       
   213             aProtocol = KProtocolInetUdp;
       
   214             break;
       
   215         case KSockStream:
       
   216             aProtocol = KProtocolInetTcp;
       
   217             break;
       
   218         default:
       
   219             LEAVE(KErrNotSupported);
       
   220             break;
       
   221         }
       
   222     }
       
   223 
       
   224     // Finds requested protocol
       
   225     for (TInt i=0; i<N(ipProtocols); i++)
       
   226     {
       
   227         if (ipProtocols[i]->iAddrFamily == iAddrFamily &&
       
   228             ipProtocols[i]->iSockType == aSockType &&
       
   229             ipProtocols[i]->iProtocol == aProtocol)
       
   230         {
       
   231             return new(ELeave)CWinsockProtocol(ipProtocols[i]);
       
   232         }
       
   233     }
       
   234 
       
   235     LEAVE(KErrNotSupported);
       
   236     return NULL;   
       
   237 }
       
   238 
       
   239 /**
       
   240  * Local Variables:
       
   241  * c-basic-offset: 4
       
   242  * indent-tabs-mode: nil
       
   243  * End:
       
   244  */