|
1 // Copyright (c) 2006-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 // rawip_sap.cpp - Raw IP socket |
|
15 // |
|
16 |
|
17 #include "rawip.h" |
|
18 #include <in_pkt.h> |
|
19 #include <in_chk.h> |
|
20 #include "in_net.h" |
|
21 // |
|
22 |
|
23 class CProtocolRawBinder; |
|
24 |
|
25 class CProviderRawIp : public CProviderInet6Network |
|
26 { |
|
27 public: |
|
28 CProviderRawIp(CProtocolInet6Base* aProtocol); |
|
29 ~CProviderRawIp(); |
|
30 virtual TInt SetLocalName(TSockAddr &aAddr); |
|
31 virtual TInt DoWrite(RMBufSendPacket &aPacket, RMBufSendInfo &aInfo, TUint aOptions, TUint aOffset); |
|
32 virtual TInt SecurityCheck(MProvdSecurityChecker *aChecker); |
|
33 virtual void InitL(); |
|
34 private: |
|
35 CProtocolRawBinder *iBinder; |
|
36 }; |
|
37 |
|
38 |
|
39 class CProtocolRawBinder : public CProtocolBase |
|
40 { |
|
41 public: |
|
42 CProtocolRawBinder(CProviderRawIp &aProvider) : iProvider(aProvider) {} |
|
43 virtual void Identify(TServerProtocolDesc *) const; |
|
44 virtual void Process(RMBufChain&, CProtocolBase* aSourceProtocol=NULL); |
|
45 private: |
|
46 CProviderRawIp &iProvider; |
|
47 }; |
|
48 |
|
49 |
|
50 CServProviderBase *RAWIP::NewSAPL(TUint aSockType, CProtocolInet6Base *aProtocol) |
|
51 { |
|
52 LOG(Log::Printf(_L("NewSAPL\t%S SockType=%d"), &aProtocol->ProtocolName(), aSockType)); |
|
53 if (aSockType != KSockRaw) |
|
54 User::Leave(KErrNotSupported); |
|
55 CProviderRawIp *provider = new (ELeave) CProviderRawIp(aProtocol); |
|
56 CleanupStack::PushL(provider); |
|
57 provider->InitL(); |
|
58 CleanupStack::Pop(); |
|
59 LOG(Log::Printf(_L("NewSAPL\t%S SAP[%u] OK"), &aProtocol->ProtocolName(), (TInt)provider)); |
|
60 return provider; |
|
61 } |
|
62 |
|
63 // |
|
64 |
|
65 CProviderRawIp::CProviderRawIp(CProtocolInet6Base* aProtocol) : |
|
66 CProviderInet6Network(aProtocol) |
|
67 { |
|
68 } |
|
69 |
|
70 void CProviderRawIp::InitL() |
|
71 { |
|
72 iBinder = new (ELeave) CProtocolRawBinder(*this); |
|
73 CProviderInet6Network::InitL(); |
|
74 } |
|
75 |
|
76 CProviderRawIp::~CProviderRawIp() |
|
77 { |
|
78 iProtocol->NetworkService()->Protocol()->Unbind(iBinder); |
|
79 delete iBinder; |
|
80 } |
|
81 |
|
82 // SetLocalName (Bind() in application) with non-zero port activates |
|
83 // the incoming path for packets of the specific protocol. |
|
84 TInt CProviderRawIp::SetLocalName(TSockAddr &aAddr) |
|
85 { |
|
86 const TInt id = aAddr.Port(); |
|
87 // Only bind as real upper layer protocol, limit the value... |
|
88 if (id < 0 || id > 255) |
|
89 return KErrNotSupported; |
|
90 if (iProtocol == NULL || iProtocol->NetworkService() == NULL) |
|
91 return KErrNotReady; // Should never really happen... |
|
92 iProtocol->NetworkService()->Protocol()->Unbind(iBinder); |
|
93 if (id > 0) |
|
94 { |
|
95 // Only non-zero protocol is bound. |
|
96 TRAPD(err, iProtocol->NetworkService()->BindL(iBinder, id)); |
|
97 if (err != KErrNone) |
|
98 return err; |
|
99 } |
|
100 // iProtocolId is used to fill in the protocol number |
|
101 // for outgoing packets, if non-zero (if zero, then |
|
102 // the port of the destination address is used as protocol). |
|
103 iProtocolId = id; |
|
104 // Do the default SetLocalName without the port! |
|
105 aAddr.SetPort(0); |
|
106 return CProviderInet6Network::SetLocalName(aAddr); |
|
107 } |
|
108 |
|
109 |
|
110 TInt CProviderRawIp::DoWrite(RMBufSendPacket & /*aPacket*/, RMBufSendInfo &aInfo, TUint /*aOptions*/, TUint /*aOffset*/) |
|
111 { |
|
112 iFlow.SetIcmpType(0, 0); |
|
113 iFlow.SetNotify(this); |
|
114 if (aInfo.iSrcAddr.Family()) |
|
115 iFlow.SetLocalAddr(aInfo.iSrcAddr); |
|
116 if (aInfo.iDstAddr.Family()) |
|
117 iFlow.SetRemoteAddr(aInfo.iDstAddr); |
|
118 return aInfo.iFlow.Open(iFlow, &aInfo); |
|
119 } |
|
120 |
|
121 // Require network control for raw ip sockets |
|
122 TInt CProviderRawIp::SecurityCheck(MProvdSecurityChecker *aChecker) |
|
123 { |
|
124 const TInt res = CProviderInet6Network::SecurityCheck(aChecker); |
|
125 if (res == KErrNone) |
|
126 return CheckPolicy(KPolicyNetworkControl, "TCPIP RAWIP SAP"); |
|
127 return res; |
|
128 } |
|
129 |
|
130 void CProtocolRawBinder::Process(RMBufChain &aPacket,CProtocolBase * /*aSourceProtocol*/) |
|
131 { |
|
132 for (;;) // LOOP ONLY FOR EASY BREAK EXITS! |
|
133 { |
|
134 RMBufRecvInfo *const info = (RMBufRecvInfo *)RMBufPacketBase::PeekInfoInChain(aPacket); |
|
135 if (info == NULL) |
|
136 break; |
|
137 if (info->iIcmp) |
|
138 { |
|
139 // For now, ignore ICMP error reports. |
|
140 // Could do the save icmp error to support the "Last ICMP" error feature, |
|
141 // in socket. That would require translating ICMP error codes into Symbian |
|
142 // error codes (which is done in udp.cpp for example -- should have some |
|
143 // shared code for this ICMP handling). |
|
144 break; |
|
145 } |
|
146 iProvider.Process(aPacket); |
|
147 // ALWAYS TERMINATE THE LOOP |
|
148 break; |
|
149 } |
|
150 aPacket.Free(); |
|
151 } |
|
152 |
|
153 void CProtocolRawBinder::Identify(TServerProtocolDesc *aInfo) const |
|
154 { |
|
155 // If asked, the binder returns the same info as the "rawip" protocol. |
|
156 RAWIP::Identify(*aInfo); |
|
157 } |
|
158 |
|
159 |