0
|
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
|
1
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
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 |
*
|
1
|
14 |
* Description:
|
0
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
#include "WinsockUtils.h"
|
|
20 |
|
|
21 |
// Converts TInetAddr into WSockAddr. Returns the size of the sockaddr
|
|
22 |
// structure, zero if conversion failed.
|
|
23 |
int WinsockUtils::ToSockAddr(WSockAddr* aToAddr, const TSockAddr* aFromAddr)
|
|
24 |
{
|
|
25 |
// Windows requires that unused part of sockaddr structure is filled
|
|
26 |
// with zeros.
|
|
27 |
Mem::FillZ(aToAddr, SOCKADDR_SIZE);
|
|
28 |
TInetAddr* from = &TInetAddr::Cast(aFromAddr);
|
|
29 |
if (from->Family() == KAfInet)
|
|
30 |
{
|
|
31 |
aToAddr->AddressIn.sin_family = AF_INET;
|
|
32 |
aToAddr->AddressIn.sin_port = htons((u_short)from->Port());
|
|
33 |
aToAddr->AddressIn.sin_addr.s_addr = htonl(from->Address());
|
|
34 |
return sizeof(struct sockaddr_in);
|
|
35 |
}
|
|
36 |
else if (from->Family() == KAfInet6)
|
|
37 |
{
|
|
38 |
aToAddr->AddressIn6.sin6_family = AF_INET6;
|
|
39 |
aToAddr->AddressIn6.sin6_port = htons((u_short)from->Port());
|
|
40 |
aToAddr->AddressIn6.sin6_flowinfo = from->FlowLabel();
|
|
41 |
aToAddr->AddressIn6.sin6_scope_id = from->Scope();
|
|
42 |
Mem::Copy(&aToAddr->AddressIn6.sin6_addr, &from->Ip6Address(),16);
|
|
43 |
return sizeof(struct sockaddr_in6);
|
|
44 |
}
|
|
45 |
else if (from->Family() == KAFUnspec)
|
|
46 |
{
|
|
47 |
aToAddr->AddressIn.sin_family = AF_INET;
|
|
48 |
aToAddr->AddressIn.sin_port = htons((u_short)from->Port());
|
|
49 |
aToAddr->AddressIn.sin_addr.s_addr = 0;
|
|
50 |
return sizeof(struct sockaddr_in);
|
|
51 |
}
|
|
52 |
else
|
|
53 |
{
|
|
54 |
return 0;
|
|
55 |
}
|
|
56 |
}
|
|
57 |
|
|
58 |
// Converts WSockAddr into TInetAddr
|
|
59 |
TBool WinsockUtils::ToInetAddr(TSockAddr* aToAddr, const WSockAddr* aFromAddr)
|
|
60 |
{
|
|
61 |
TIp6Addr ip6;
|
|
62 |
TInetAddr* to = &TInetAddr::Cast(aToAddr);
|
|
63 |
switch (aFromAddr->Address.sa_family) {
|
|
64 |
case AF_INET:
|
|
65 |
to->SetFamily(KAfInet);
|
|
66 |
to->SetPort(ntohs(aFromAddr->AddressIn.sin_port));
|
|
67 |
to->SetAddress(ntohl(aFromAddr->AddressIn.sin_addr.s_addr));
|
|
68 |
return ETrue;
|
|
69 |
case AF_INET6:
|
|
70 |
to->SetFamily(KAfInet6);
|
|
71 |
to->SetPort(ntohs(aFromAddr->AddressIn6.sin6_port));
|
|
72 |
to->SetFlowLabel(aFromAddr->AddressIn6.sin6_flowinfo);
|
|
73 |
to->SetScope(aFromAddr->AddressIn6.sin6_scope_id);
|
|
74 |
Mem::Copy(&ip6,&aFromAddr->AddressIn6.sin6_addr,16);
|
|
75 |
to->SetAddress(ip6);
|
|
76 |
return ETrue;
|
|
77 |
default:
|
|
78 |
to->SetFamily(KAFUnspec);
|
|
79 |
to->SetPort(0);
|
|
80 |
return EFalse;
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
|
|
85 |
/*
|
|
86 |
* Local Variables:
|
|
87 |
* c-basic-offset: 4
|
|
88 |
* indent-tabs-mode: nil
|
|
89 |
* End:
|
|
90 |
*/
|