|
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 // inet.h - main definitions for inet protocols |
|
15 // |
|
16 |
|
17 |
|
18 |
|
19 /** |
|
20 @internalComponent |
|
21 */ |
|
22 #ifndef __INET_H__ |
|
23 #define __INET_H__ |
|
24 |
|
25 #include <f32file.h> |
|
26 #include <e32math.h> |
|
27 |
|
28 #include <es_sock.h> |
|
29 #include <es_prot.h> |
|
30 #include <es_mbuf.h> |
|
31 #include <es_ver.h> |
|
32 #include <in_sock.h> |
|
33 #include <nifman.h> // For Nif:: calls! |
|
34 # include <comms-infras/nifif.h> // ..for CNifIfBase in Epoc R6 and later |
|
35 |
|
36 #include <nifmbuf.h> |
|
37 |
|
38 #include <flow.h> |
|
39 #include <in_bind.h> |
|
40 #include "in_fmly.h" |
|
41 #include "inet6log.h" |
|
42 #include <es_prot_internal.h> |
|
43 |
|
44 // Minimum MTU that must be supported by all IPv4 links |
|
45 const TUint KInetStandardMtu = 576; |
|
46 |
|
47 // Minimum MTU that must be supported by all IPv6 links |
|
48 const TUint KInet6StandardMtu = 1280; |
|
49 |
|
50 // Size of SAP hash table. |
|
51 const TUint KInet6SAPTableSize = 67; |
|
52 |
|
53 class CProtocolInet6Base; |
|
54 class CProtocolInet6Transport; |
|
55 class CProviderInet6Base; |
|
56 class TInet6SAPIter; |
|
57 |
|
58 // |
|
59 // RMBuf paket queue with automatic asynchronous notification. |
|
60 // |
|
61 class RMBufAsyncPktQ : public RMBufPktQ |
|
62 { |
|
63 public: |
|
64 RMBufAsyncPktQ() : iCallBack(NULL) {} |
|
65 ~RMBufAsyncPktQ() { Cancel(); delete iCallBack; } |
|
66 void InitL(TCallBack& aCallBack, TInt aPriority = KInet6DefaultPriority) |
|
67 { |
|
68 RMBufPktQ::Init(); |
|
69 iCallBack = new (ELeave) CAsyncCallBack(aCallBack, aPriority); |
|
70 } |
|
71 |
|
72 // |
|
73 // The automatic wakeup of the reader has been removed. |
|
74 // Call Wake() explicitly when you want to wake up the |
|
75 // reader, e.g., after adding packets to the queue. -ML |
|
76 // |
|
77 void Wake() { if (iCallBack != NULL) iCallBack->CallBack(); } |
|
78 void Cancel() { if (iCallBack != NULL) iCallBack->Cancel(); } |
|
79 CAsyncCallBack *AsyncCallBack() { return iCallBack; } |
|
80 |
|
81 private: |
|
82 CAsyncCallBack* iCallBack; |
|
83 }; |
|
84 |
|
85 // |
|
86 // Base for Internet Protocols |
|
87 // |
|
88 |
|
89 class CProtocolInet6Base : public CProtocolInet6Binder |
|
90 { |
|
91 friend class TInet6SAPIter; |
|
92 |
|
93 public: |
|
94 CProtocolInet6Base(); |
|
95 virtual ~CProtocolInet6Base(); |
|
96 virtual void InitL(TDesC& aTag); |
|
97 virtual void StartL(); |
|
98 |
|
99 virtual void BindL(CProtocolBase *aProtocol, TUint aId); |
|
100 virtual void Unbind(CProtocolBase *aProtocol, TUint aId = 0); |
|
101 virtual void Error(TInt anError,CProtocolBase* aSourceProtocol=NULL); |
|
102 |
|
103 virtual void BindProvider(CProviderInet6Base* aSAP); |
|
104 virtual void QueueBindProvider(CProviderInet6Base* aSAP); |
|
105 virtual void UnbindProvider(CProviderInet6Base* aSAP); |
|
106 virtual CProviderInet6Base* LocateProvider(TUint aPort); |
|
107 inline MInterfaceManager *Interfacer() const { return iNetwork->Interfacer(); } |
|
108 inline TUint SapCount() const { return iSapCount; } |
|
109 void IncSAPs(); |
|
110 void DecSAPs(); |
|
111 protected: |
|
112 virtual TUint ProviderHashKey(TUint aPort) { return aPort % KInet6SAPTableSize; } |
|
113 TUint iSapCount;// SAP count for this protocol. Includes embryonic sockets. |
|
114 private: |
|
115 CProviderInet6Base* iSAP[KInet6SAPTableSize]; |
|
116 #ifdef _LOG |
|
117 // The protocol name is cached here for log purposes |
|
118 // (to avoid exessive stack use for TServerProtocolDesc). |
|
119 // Initilized in base InitL from a call to Identify. |
|
120 TProtocolName iName; |
|
121 public: |
|
122 const TDesC &ProtocolName() const { return iName; } |
|
123 #endif |
|
124 }; |
|
125 |
|
126 |
|
127 // |
|
128 // Internet Socket Provider Base |
|
129 // |
|
130 |
|
131 class CProviderInet6Base : public CServProviderBase, public MProviderNotify |
|
132 , public MProvdSecurityChecker |
|
133 { |
|
134 friend class CProtocolInet6Base; |
|
135 friend class CProtocolInet6Transport; |
|
136 friend class TInet6SAPIter; |
|
137 |
|
138 public: |
|
139 virtual void InitL(); |
|
140 CProviderInet6Base(CProtocolInet6Base* aProtocol); |
|
141 virtual ~CProviderInet6Base(); |
|
142 |
|
143 virtual void AutoBind() { } |
|
144 virtual void LocalName(TSockAddr &aAddr) const { aAddr = iFlow.FlowContext()->LocalAddr(); } |
|
145 virtual TInt SetLocalName(TSockAddr &aAddr) { iFlow.SetLocalAddr(aAddr); return 0; } |
|
146 virtual void RemName(TSockAddr &aAddr) const { aAddr = iFlow.FlowContext()->RemoteAddr(); } |
|
147 virtual TInt SetRemName(TSockAddr &aAddr) { iFlow.SetRemoteAddr(aAddr); return 0; } |
|
148 virtual void ActiveOpen(); |
|
149 virtual void ActiveOpen(const TDesC8 &aConnectionData); |
|
150 virtual TInt PassiveOpen(TUint aQueSize); |
|
151 virtual TInt PassiveOpen(TUint aQueSize,const TDesC8 &aConnectionData); |
|
152 virtual void Shutdown(TCloseType option,const TDesC8 &aDisconnectionData); |
|
153 virtual TInt SetOption(TUint aLevel, TUint aName, const TDesC8& aOption); |
|
154 virtual TInt GetOption(TUint aLevel, TUint aName, TDes8& aOption) const; |
|
155 virtual void Ioctl(TUint aLevel, TUint aName, TDes8* anOption); |
|
156 virtual void CancelIoctl(TUint aLevel, TUint aName); |
|
157 virtual void Start(); |
|
158 |
|
159 virtual void Process(RMBufChain& aPacket, CProtocolBase *aSourceProtocol = NULL); |
|
160 |
|
161 virtual TUint Write(const TDesC8& aDesc,TUint options, TSockAddr* anAddr=NULL); |
|
162 virtual TInt Write(RMBufChain& aData, TUint aOptions, TSockAddr* anAddr=NULL); |
|
163 |
|
164 virtual void CanSend(); |
|
165 virtual void NoBearer(const TDesC8& aConnectionParams); |
|
166 virtual void Bearer(const TDesC8 &aConnectionInfo); |
|
167 virtual TInt SecurityCheck(MProvdSecurityChecker *aSecurityChecker); |
|
168 inline void NoSecurityChecker() { iSecurityChecker = NULL;} |
|
169 inline TBool HasNetworkServices() { return iHasNetworkServices; } |
|
170 virtual void Error(TInt aError, TUint aOperationMask = MSocketNotify::EErrorAllOperations); |
|
171 virtual void SaveIcmpError(TInt aType, TInt aCode, const TInetAddr& aSrcAddr, |
|
172 const TInetAddr& aDstAddr, const TInetAddr& aErrAddr); |
|
173 |
|
174 inline TBool FatalState() { return (iErrorMask & (MSocketNotify::EErrorFatal|MSocketNotify::EErrorConnect)) != 0; } |
|
175 protected: |
|
176 static TInt GetOptionInt(const TDesC8 &anOption, TInt &aVal); |
|
177 static TInt SetOptionInt(TDes8 &anOption, TInt aVal); |
|
178 |
|
179 virtual TInt CheckPolicy(const TSecurityPolicy& aPolicy, const char *aDiagnostic); |
|
180 /** |
|
181 * Complete write processing for a datagram protocol |
|
182 * |
|
183 * The base class Write() implements a common processing for simple datagram |
|
184 * socket. After this setup work, the Write() calls DoWrite. |
|
185 * |
|
186 * @param aPacket The packet to be sent |
|
187 * @param aInfo The info block of the packet |
|
188 * @param aOptions The options as defined for CServProviderBase::Write |
|
189 * @param aOffset Offset to the beginning of the upper layer header |
|
190 * @return Result of the write |
|
191 * |
|
192 * The aOffset has following semantics: |
|
193 * - aOffset == 0, normal datagram without header included (DoWrite needs to add upper layer header) |
|
194 * - aOffset > 0, header included was present, and offset indicates upper layer header in packet). |
|
195 * |
|
196 * RMBufSendPacket has been prepared from the parameters of the Write |
|
197 * function and the information block is initialized as follows: |
|
198 * |
|
199 * - iSrcAddr Family() == 0, Port() == 0 |
|
200 * - iDstAddr Family() == 0, Port() == 0 |
|
201 * - iProtocol == aProtocol |
|
202 * - iFlags == aOptions & (KIpHeaderIncluded | KIpDontFragment) |
|
203 * - iLength == aPacket.Length() |
|
204 * - iFlow == 0 |
|
205 * |
|
206 * If KIpHeaderIncluded is set, the following is true |
|
207 * |
|
208 * - iSrcAddr Family() == KAfInet6 and address is loaded from the IPv4 or IPv6 header |
|
209 * - iDstAddr Family() == KAfInet6 and address is loaded from the IPv4 or IPv6 header |
|
210 * - iProtocol == KProtocolInetIP or KProtocolInet6Ip, depending on IP version. |
|
211 * |
|
212 * If aToAddr and KIpHeaderIncluded are both set, the aToAddr overrides whatever is |
|
213 * stored in the destination address of the included header. |
|
214 * |
|
215 * If aToAddr is defined, the scope and flow label (if present) are preserved in |
|
216 * the iDstAddr. |
|
217 * |
|
218 * The return value, 'err' |
|
219 * - err == KErrNoRMBufs, a special error return that blocks the socket |
|
220 * - err < 0, causes Error(err, MSocketNotify::EErrorSend) to be called for the socket. |
|
221 * - err == 0, the aPacket is sent out |
|
222 * - err > 0, blocks the socket (current packet is retried later) |
|
223 */ |
|
224 virtual TInt DoWrite(RMBufSendPacket &/*aPacket*/, RMBufSendInfo &/*aInfo*/, TUint /*aOptions*/, TUint /*aOffset*/) |
|
225 { |
|
226 return KErrNotSupported; |
|
227 } |
|
228 |
|
229 #ifdef _LOG |
|
230 inline const TDesC &ProtocolName() const {return iProtocol->ProtocolName();} |
|
231 #endif |
|
232 |
|
233 // Basic socket info |
|
234 CProtocolInet6Base *const iProtocol; |
|
235 |
|
236 // Socket error status |
|
237 TSoInetLastErr iLastError; |
|
238 TUint iErrorMask; |
|
239 |
|
240 // Flow context |
|
241 RFlowContext iFlow; |
|
242 |
|
243 // SAP db hash link |
|
244 CProviderInet6Base* iNextSAP; |
|
245 |
|
246 // Interface and route enumeration state |
|
247 TUint iInterfaceIndex; |
|
248 TUint iRouteIndex; |
|
249 MProvdSecurityChecker *iSecurityChecker; |
|
250 private: |
|
251 |
|
252 // iIsUser = 1, when SAP is counted as a "user" for the network layer (IncUsers done) |
|
253 // iIsUser = 0, when SAP is not counted as a "user" for the network layer |
|
254 // By default, the this class reqisters all SAPs as users for the network layer, and |
|
255 // this state can be explicitly cancelled by the application through a socket option. |
|
256 // (the user count affects the automatic shutdown process of the stack) |
|
257 TUint iIsUser:1; |
|
258 protected: |
|
259 TUint iHasNetworkServices:1;// If true, socket has NetworkServices capability |
|
260 TUint iRawMode:1; // If true, user receives datagram packets with IP header |
|
261 TUint iHeaderIncluded:1; // If true, user sends datagram packets with IP header |
|
262 TUint iSynchSend:1; // If true, block socket write to UDP socket in PENDING and HOLD states |
|
263 TInt iProtocolId; |
|
264 }; |
|
265 |
|
266 class TInet6SAPIter |
|
267 { |
|
268 public: |
|
269 inline TInet6SAPIter(CProtocolInet6Base *aProto) : iProto(aProto), iSap(NULL), iKey(0) {} |
|
270 inline CProviderInet6Base* operator++(TInt) |
|
271 { |
|
272 CProviderInet6Base *ptr; |
|
273 for (ptr = iSap; ptr == NULL; ptr = iProto->iSAP[iKey++]) |
|
274 if (iKey >= KInet6SAPTableSize) |
|
275 return NULL; |
|
276 iSap = ptr->iNextSAP; |
|
277 return ptr; |
|
278 } |
|
279 private: |
|
280 CProtocolInet6Base *iProto; |
|
281 CProviderInet6Base *iSap; |
|
282 TUint iKey; |
|
283 }; |
|
284 |
|
285 // security policies |
|
286 _LIT_SECURITY_POLICY_C1(KPolicyNetworkServices, ECapabilityNetworkServices); |
|
287 _LIT_SECURITY_POLICY_C1(KPolicyNetworkControl, ECapabilityNetworkControl); |
|
288 |
|
289 #endif |