netwerk/base/src/nsNetAddr.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* vim: et ts=2 sw=2 tw=80
michael@0 2 */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsNetAddr.h"
michael@0 8 #include "nsString.h"
michael@0 9 #include "mozilla/net/DNS.h"
michael@0 10
michael@0 11 using namespace mozilla::net;
michael@0 12
michael@0 13 NS_IMPL_ISUPPORTS(nsNetAddr, nsINetAddr)
michael@0 14
michael@0 15 /* Makes a copy of |addr| */
michael@0 16 nsNetAddr::nsNetAddr(NetAddr* addr)
michael@0 17 {
michael@0 18 NS_ASSERTION(addr, "null addr");
michael@0 19 mAddr = *addr;
michael@0 20 }
michael@0 21
michael@0 22 /* readonly attribute unsigned short family; */
michael@0 23 NS_IMETHODIMP nsNetAddr::GetFamily(uint16_t *aFamily)
michael@0 24 {
michael@0 25 switch(mAddr.raw.family) {
michael@0 26 case AF_INET:
michael@0 27 *aFamily = nsINetAddr::FAMILY_INET;
michael@0 28 break;
michael@0 29 case AF_INET6:
michael@0 30 *aFamily = nsINetAddr::FAMILY_INET6;
michael@0 31 break;
michael@0 32 #if defined(XP_UNIX)
michael@0 33 case AF_LOCAL:
michael@0 34 *aFamily = nsINetAddr::FAMILY_LOCAL;
michael@0 35 break;
michael@0 36 #endif
michael@0 37 default:
michael@0 38 return NS_ERROR_UNEXPECTED;
michael@0 39 }
michael@0 40
michael@0 41 return NS_OK;
michael@0 42 }
michael@0 43
michael@0 44 /* readonly attribute AUTF8String address; */
michael@0 45 NS_IMETHODIMP nsNetAddr::GetAddress(nsACString & aAddress)
michael@0 46 {
michael@0 47 switch(mAddr.raw.family) {
michael@0 48 /* PR_NetAddrToString can handle INET and INET6, but not LOCAL. */
michael@0 49 case AF_INET:
michael@0 50 aAddress.SetCapacity(kIPv4CStrBufSize);
michael@0 51 NetAddrToString(&mAddr, aAddress.BeginWriting(), kIPv4CStrBufSize);
michael@0 52 aAddress.SetLength(strlen(aAddress.BeginReading()));
michael@0 53 break;
michael@0 54 case AF_INET6:
michael@0 55 aAddress.SetCapacity(kIPv6CStrBufSize);
michael@0 56 NetAddrToString(&mAddr, aAddress.BeginWriting(), kIPv6CStrBufSize);
michael@0 57 aAddress.SetLength(strlen(aAddress.BeginReading()));
michael@0 58 break;
michael@0 59 #if defined(XP_UNIX)
michael@0 60 case AF_LOCAL:
michael@0 61 aAddress.Assign(mAddr.local.path);
michael@0 62 break;
michael@0 63 #endif
michael@0 64 // PR_AF_LOCAL falls through to default when not XP_UNIX
michael@0 65 default:
michael@0 66 return NS_ERROR_UNEXPECTED;
michael@0 67 }
michael@0 68
michael@0 69 return NS_OK;
michael@0 70 }
michael@0 71
michael@0 72 /* readonly attribute unsigned short port; */
michael@0 73 NS_IMETHODIMP nsNetAddr::GetPort(uint16_t *aPort)
michael@0 74 {
michael@0 75 switch(mAddr.raw.family) {
michael@0 76 case AF_INET:
michael@0 77 *aPort = ntohs(mAddr.inet.port);
michael@0 78 break;
michael@0 79 case AF_INET6:
michael@0 80 *aPort = ntohs(mAddr.inet6.port);
michael@0 81 break;
michael@0 82 #if defined(XP_UNIX)
michael@0 83 case AF_LOCAL:
michael@0 84 // There is no port number for local / connections.
michael@0 85 return NS_ERROR_NOT_AVAILABLE;
michael@0 86 #endif
michael@0 87 default:
michael@0 88 return NS_ERROR_UNEXPECTED;
michael@0 89 }
michael@0 90
michael@0 91 return NS_OK;
michael@0 92 }
michael@0 93
michael@0 94 /* readonly attribute unsigned long flow; */
michael@0 95 NS_IMETHODIMP nsNetAddr::GetFlow(uint32_t *aFlow)
michael@0 96 {
michael@0 97 switch(mAddr.raw.family) {
michael@0 98 case AF_INET6:
michael@0 99 *aFlow = ntohl(mAddr.inet6.flowinfo);
michael@0 100 break;
michael@0 101 case AF_INET:
michael@0 102 #if defined(XP_UNIX)
michael@0 103 case AF_LOCAL:
michael@0 104 #endif
michael@0 105 // only for IPv6
michael@0 106 return NS_ERROR_NOT_AVAILABLE;
michael@0 107 default:
michael@0 108 return NS_ERROR_UNEXPECTED;
michael@0 109 }
michael@0 110
michael@0 111 return NS_OK;
michael@0 112 }
michael@0 113
michael@0 114 /* readonly attribute unsigned long scope; */
michael@0 115 NS_IMETHODIMP nsNetAddr::GetScope(uint32_t *aScope)
michael@0 116 {
michael@0 117 switch(mAddr.raw.family) {
michael@0 118 case AF_INET6:
michael@0 119 *aScope = ntohl(mAddr.inet6.scope_id);
michael@0 120 break;
michael@0 121 case AF_INET:
michael@0 122 #if defined(XP_UNIX)
michael@0 123 case AF_LOCAL:
michael@0 124 #endif
michael@0 125 // only for IPv6
michael@0 126 return NS_ERROR_NOT_AVAILABLE;
michael@0 127 default:
michael@0 128 return NS_ERROR_UNEXPECTED;
michael@0 129 }
michael@0 130
michael@0 131 return NS_OK;
michael@0 132 }
michael@0 133
michael@0 134 /* readonly attribute boolean isV4Mapped; */
michael@0 135 NS_IMETHODIMP nsNetAddr::GetIsV4Mapped(bool *aIsV4Mapped)
michael@0 136 {
michael@0 137 switch(mAddr.raw.family) {
michael@0 138 case AF_INET6:
michael@0 139 *aIsV4Mapped = IPv6ADDR_IS_V4MAPPED(&mAddr.inet6.ip);
michael@0 140 break;
michael@0 141 case AF_INET:
michael@0 142 #if defined(XP_UNIX)
michael@0 143 case AF_LOCAL:
michael@0 144 #endif
michael@0 145 // only for IPv6
michael@0 146 return NS_ERROR_NOT_AVAILABLE;
michael@0 147 default:
michael@0 148 return NS_ERROR_UNEXPECTED;
michael@0 149 }
michael@0 150
michael@0 151 return NS_OK;
michael@0 152 }
michael@0 153
michael@0 154 NS_IMETHODIMP nsNetAddr::GetNetAddr(NetAddr *aResult) {
michael@0 155 memcpy(aResult, &mAddr, sizeof(mAddr));
michael@0 156 return NS_OK;
michael@0 157 }
michael@0 158

mercurial