Loading...
Searching...
No Matches
as_address.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2024 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 * Return port of address.
67 */
68static inline uint16_t
69as_address_port(struct sockaddr* addr)
70{
71 uint16_t port = (addr->sa_family == AF_INET)?
72 ((struct sockaddr_in*)addr)->sin_port :
73 ((struct sockaddr_in6*)addr)->sin6_port;
74 return cf_swap_from_be16(port);
75}
76
77/**
78 * @private
79 * Return size of socket address.
80 */
81static inline socklen_t
82as_address_size(struct sockaddr* addr)
83{
84 return (addr->sa_family == AF_INET)? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
85}
86
87/**
88 * @private
89 * Copy socket address to storage.
90 */
91static inline void
92as_address_copy_storage(struct sockaddr* src, struct sockaddr_storage* trg)
93{
94 size_t size = as_address_size(src);
95 memcpy(trg, src, size);
96}
97
98/**
99 * @private
100 * Return if socket address is localhost.
101 */
102static inline bool
103as_address_is_local(struct sockaddr* addr)
104{
105 if (addr->sa_family == AF_INET) {
106 struct sockaddr_in* a = (struct sockaddr_in*)addr;
107 return (cf_swap_to_be32(a->sin_addr.s_addr) & 0xff000000) == 0x7f000000;
108 }
109 else {
110 struct sockaddr_in6* a = (struct sockaddr_in6*)addr;
111 return memcmp(&a->sin6_addr, &in6addr_loopback, sizeof(struct in6_addr)) == 0;
112 }
113}
114
115#ifdef __cplusplus
116} // end extern "C"
117#endif
static bool as_address_is_local(struct sockaddr *addr)
Definition as_address.h:103
static uint16_t as_address_port(struct sockaddr *addr)
Definition as_address.h:69
static socklen_t as_address_size(struct sockaddr *addr)
Definition as_address.h:82
static void as_address_copy_storage(struct sockaddr *src, struct sockaddr_storage *trg)
Definition as_address.h:92
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