sources/network/ipaddress.h

Wed, 27 Jan 2021 19:27:23 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 27 Jan 2021 19:27:23 +0200
changeset 189
248d0b85cbda
parent 188
5fc32e4b2a8c
child 190
90bf9049e5eb
permissions
-rw-r--r--

various changes, better network error handling

9
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
1 /*
109
e4966d7e615d Happy new year 2016
Teemu Piippo <crimsondusk64@gmail.com>
parents: 88
diff changeset
2 Copyright 2014 - 2016 Teemu Piippo
9
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
3 All rights reserved.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
4
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
5 Redistribution and use in source and binary forms, with or without
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
6 modification, are permitted provided that the following conditions
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
7 are met:
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
8
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
9 1. Redistributions of source code must retain the above copyright
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
10 notice, this list of conditions and the following disclaimer.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
12 notice, this list of conditions and the following disclaimer in the
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
13 documentation and/or other materials provided with the distribution.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
14 3. Neither the name of the copyright holder nor the names of its
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
15 contributors may be used to endorse or promote products derived from
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
16 this software without specific prior written permission.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
17
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
29 */
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
30
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 #pragma once
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
32 #include <ostream>
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 #include "../main.h"
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 struct sockaddr;
7
01e4e9ae323a - fixed some problems with the ip address
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
36 struct sockaddr_in;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 73
diff changeset
38 BEGIN_ZFC_NAMESPACE
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
40 namespace net
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 {
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
42 using octet_t = std::uint8_t;
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
43 using port_t = std::uint16_t;
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
44 using host_t = std::uint32_t;
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
45 struct ip_address
24
e651d02802c0 - the interface is now able to connect to arbitrary hosts (uses ^N to start the prompt) instead of the hardcoded localhost:10666
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
46 {
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
47 net::host_t host = 0;
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
48 net::port_t port = 0;
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
49 bool operator<(const ip_address& other) const;
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
50 bool operator==(const ip_address& other) const;
24
e651d02802c0 - the interface is now able to connect to arbitrary hosts (uses ^N to start the prompt) instead of the hardcoded localhost:10666
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
51 };
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
52 constexpr ip_address localhost = {0x7f000001, 0};
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
53 int ip_compare(const ip_address& one, const ip_address& other);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
54 net::octet_t ip_octet(const ip_address& address, unsigned char n);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
55 void ip_set_octet(ip_address* address, unsigned char n, net::octet_t octet);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
56 std::optional<unsigned short> ip_parse_port(const char* port_string, std::ostream& errorStream);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
57 std::optional<ip_address> ip_resolve_hostname(const std::string& node, std::ostream& errorStream);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
58 std::optional<ip_address> ip_resolve(const std::string& input_string, std::ostream &errorStream);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
59 sockaddr_in ip_address_to_sockaddr_in(const ip_address& address);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
60 std::string ip_address_to_string(const ip_address& address);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 184
diff changeset
61 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 73
diff changeset
63 END_ZFC_NAMESPACE

mercurial