blob: dbd8672e0b0b43155db87898f891d27a9661a315 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//
18// Internet address classes. Modeled after Java classes.
19//
20#ifndef _RUNTIME_INET_ADDRESS_H
21#define _RUNTIME_INET_ADDRESS_H
22
23#ifdef HAVE_ANDROID_OS
24#error DO NOT USE THIS FILE IN THE DEVICE BUILD
25#endif
26
27
28namespace android {
29
30/*
31 * This class holds Internet addresses. Perhaps more useful is its
32 * ability to look up addresses by name.
33 *
34 * Invoke one of the static factory methods to create a new object.
35 */
36class InetAddress {
37public:
38 virtual ~InetAddress(void);
39
40 // create from w.x.y.z or foo.bar.com notation
41 static InetAddress* getByName(const char* host);
42
43 // copy-construction
44 InetAddress(const InetAddress& orig);
45
46 const void* getAddress(void) const { return mAddress; }
47 int getAddressLength(void) const { return mLength; }
48 const char* getHostName(void) const { return mName; }
49
50private:
51 InetAddress(void);
52 // assignment (private)
53 InetAddress& operator=(const InetAddress& addr);
54
55 // use a void* here so we don't have to expose actual socket headers
56 void* mAddress; // this is really a ptr to sockaddr_in
57 int mLength;
58 char* mName;
59};
60
61
62/*
63 * Base class for socket addresses.
64 */
65class SocketAddress {
66public:
67 SocketAddress() {}
68 virtual ~SocketAddress() {}
69};
70
71
72/*
73 * Internet address class. This combines an InetAddress with a port.
74 */
75class InetSocketAddress : public SocketAddress {
76public:
77 InetSocketAddress() :
78 mAddress(0), mPort(-1)
79 {}
80 ~InetSocketAddress(void) {
81 delete mAddress;
82 }
83
84 // Create an address with a host wildcard (useful for servers).
85 bool create(int port);
86 // Create an address with the specified host and port.
87 bool create(const InetAddress* addr, int port);
88 // Create an address with the specified host and port. Does the
89 // hostname lookup.
90 bool create(const char* host, int port);
91
92 const InetAddress* getAddress(void) const { return mAddress; }
93 const int getPort(void) const { return mPort; }
94 const char* getHostName(void) const { return mAddress->getHostName(); }
95
96private:
97 InetAddress* mAddress;
98 int mPort;
99};
100
101}; // namespace android
102
103#endif // _RUNTIME_INET_ADDRESS_H