All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
as_address.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2025 Aerospike, Inc.
3 *
4 * Portions may be licensed to Aerospike, Inc. under one or more contributor
5 * license agreements.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17#pragma once
18
19#include <citrusleaf/cf_byte_order.h>
20#include <string.h>
21
22#if !defined(_MSC_VER)
23#include <arpa/inet.h>
24#include <netinet/in.h>
25#include <sys/socket.h>
26#else
27#include <winsock2.h>
28#include <ws2tcpip.h>
29#define in_addr_t ULONG
30#endif
31
32#define AS_IP_ADDRESS_SIZE 64
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38/**
39 * @private
40 * Convert socket address (including port) to a string.
41 *
42 * Formats:
43 * ~~~~~~~~~~{.c}
44 * IPv4: xxx.xxx.xxx.xxx:<port>
45 * IPv6: [xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:<port>
46 * ~~~~~~~~~~
47 */
48AS_EXTERN void
49as_address_name(struct sockaddr* addr, char* name, socklen_t size);
50
51/**
52 * @private
53 * Convert socket address to a string without brackets or a port.
54 *
55 * Formats:
56 * ~~~~~~~~~~{.c}
57 * IPv4: xxx.xxx.xxx.xxx
58 * IPv6: xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
59 * ~~~~~~~~~~
60 */
61AS_EXTERN void
62as_address_short_name(struct sockaddr* addr, char* name, socklen_t size);
63
64/**
65 * @private
66 * Are socket addresses equal. The port is not included in the comparison.
67 */
68AS_EXTERN bool
69as_address_equals(struct sockaddr* addr1, struct sockaddr* addr2);
70
71/**
72 * @private
73 * Return port of address.
74 */
75static inline uint16_t
76as_address_port(struct sockaddr* addr)
77{
78 uint16_t port = (addr->sa_family == AF_INET)?
79 ((struct sockaddr_in*)addr)->sin_port :
80 ((struct sockaddr_in6*)addr)->sin6_port;
81 return cf_swap_from_be16(port);
82}
83
84/**
85 * @private
86 * Return size of socket address.
87 */
88static inline socklen_t
89as_address_size(struct sockaddr* addr)
90{
91 return (addr->sa_family == AF_INET)? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
92}
93
94/**
95 * @private
96 * Copy socket address to storage.
97 */
98static inline void
99as_address_copy_storage(struct sockaddr* src, struct sockaddr_storage* trg)
100{
101 size_t size = as_address_size(src);
102 memcpy(trg, src, size);
103}
104
105/**
106 * @private
107 * Return if socket address is localhost.
108 */
109static inline bool
110as_address_is_local(struct sockaddr* addr)
111{
112 if (addr->sa_family == AF_INET) {
113 struct sockaddr_in* a = (struct sockaddr_in*)addr;
114 return (cf_swap_to_be32(a->sin_addr.s_addr) & 0xff000000) == 0x7f000000;
115 }
116 else {
117 struct sockaddr_in6* a = (struct sockaddr_in6*)addr;
118 return memcmp(&a->sin6_addr, &in6addr_loopback, sizeof(struct in6_addr)) == 0;
119 }
120}
121
122#ifdef __cplusplus
123} // end extern "C"
124#endif
AS_EXTERN bool as_address_equals(struct sockaddr *addr1, struct sockaddr *addr2)
static bool as_address_is_local(struct sockaddr *addr)
Definition as_address.h:110
static uint16_t as_address_port(struct sockaddr *addr)
Definition as_address.h:76
static socklen_t as_address_size(struct sockaddr *addr)
Definition as_address.h:89
static void as_address_copy_storage(struct sockaddr *src, struct sockaddr_storage *trg)
Definition as_address.h:99
AS_EXTERN void as_address_short_name(struct sockaddr *addr, char *name, socklen_t size)
AS_EXTERN void as_address_name(struct sockaddr *addr, char *name, socklen_t size)
#define AS_EXTERN
Definition as_std.h:25