sources/network/ipaddress.cpp

Thu, 11 Dec 2014 05:58:55 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Thu, 11 Dec 2014 05:58:55 +0200
changeset 5
146825d63b9a
child 7
01e4e9ae323a
permissions
-rw-r--r--

- code cleanup
- added network-related classes (bytestream, ipaddress, udp socket)

5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 #include <stdio.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 #include <stdlib.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 #include <string.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 #include <time.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 #include <netinet/in.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 #include <netdb.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 #include "ipaddress.h"
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 const IPAddress localhost (0x7F000001, 0);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 bool IPAddress::sink;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 IPAddress::IPAddress() :
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 host (0),
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 port (0) {}
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 IPAddress::IPAddress (unsigned long host, unsigned short port) :
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 host (host),
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 port (port) {}
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 IPAddress::IPAddress (const IPAddress& other) :
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 host (other.host),
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 port (other.port) {}
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 IPAddress::to_string (WithPort withport) const -> String
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 String val;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 if (withport == IP_WITH_PORT)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 val.sprintf ("%u.%u.%u.%u:%u", octet (0), octet (1), octet (2), octet (3), port);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 else
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 val.sprintf ("%u.%u.%u.%u", octet (0), octet (1), octet (2), octet (3));
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 return val;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 IPAddress::octet (int n) const -> unsigned char
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 return (host >> ((3 - n) * 8)) & 0xFF;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 IPAddress::set_octet (int n, unsigned char oct) -> void
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 // TODO: make a big-endian version
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 host &= ~(0xFF << (3 - n) * 8);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 host |= oct << ((3 - n) * 8);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 IPAddress::compare (const IPAddress& other) const -> bool
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 for (int i = 0; i < 4; ++i)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 if (octet (i) != other.octet (i))
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 return false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 if (port != 0
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 and other.port != 0
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 and port != other.port)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 return false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 return true;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 IPAddress::operator< (const IPAddress& other) const -> bool
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 for (int i = 0; i < 4; ++i)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 if (octet (i) != other.octet (i))
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 return octet (i) < other.octet (i);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 return port < other.port;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 IPAddress::to_sockaddr_in() const -> sockaddr_in
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 sockaddr_in claddr;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 memset (&claddr, 0, sizeof claddr);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 claddr.sin_addr.s_addr = htonl (host);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 claddr.sin_port = htons (port);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 claddr.sin_family = AF_INET;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 return claddr;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 STATIC METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 IPAddress::from_string (String input, bool* ok) -> IPAddress
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 unsigned int parts[4];
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 int colonpos = input.find (":");
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 String addressString = colonpos == -1 ? input : input.mid (0, colonpos);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 IPAddress value;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 // Try scanf the IPv4 host first. If it's not an IP string, it could
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 // be a hostname; thus try resolve it.
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 if (sscanf (addressString, "%u.%u.%u.%u", &parts[0], &parts[1], &parts[2], &parts[3]))
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 for (short i = 0; i < 4; ++i)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 value.set_octet (i, parts[i]);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 else
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 // try resolve it
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 return IPAddress::resolve (addressString, ok);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 if (colonpos != -1)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 value.port = atoi (input.mid (colonpos + 1, -1));
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 return value;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 STATIC METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 IPAddress::resolve (String node, bool* ok) -> IPAddress
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 struct addrinfo hints;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 struct addrinfo* lookup;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 int errorcode;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 memset (&hints, 0, sizeof hints);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 hints.ai_family = AF_INET;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 if ((errorcode = getaddrinfo (node, nullptr, &hints, &lookup)) != 0)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 *ok = false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 return IPAddress();
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 IPAddress result;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 assert (lookup != nullptr);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*> (lookup[0].ai_addr);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 result.host = ntohl (addr->sin_addr.s_addr);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 result.port = ntohs (addr->sin_port);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 freeaddrinfo (lookup);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 *ok = true;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 return result;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 }

mercurial