All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_address.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2017 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 <netinet/in.h>
21 #include <string.h>
22 #include <sys/socket.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /**
29  * @private
30  * Convert socket address (including port) to a string.
31  *
32  * Formats:
33  * ~~~~~~~~~~{.c}
34  * IPv4: xxx.xxx.xxx.xxx:<port>
35  * IPv6: [xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:<port>
36  * ~~~~~~~~~~
37  */
38 void
39 as_address_name(struct sockaddr* addr, char* name, socklen_t size);
40 
41 /**
42  * @private
43  * Convert socket address to a string without brackets or a port.
44  *
45  * Formats:
46  * ~~~~~~~~~~{.c}
47  * IPv4: xxx.xxx.xxx.xxx
48  * IPv6: xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
49  * ~~~~~~~~~~
50  */
51 void
52 as_address_short_name(struct sockaddr* addr, char* name, socklen_t size);
53 
54 /**
55  * @private
56  * Return port of address.
57  */
58 static inline in_port_t
59 as_address_port(struct sockaddr* addr)
60 {
61  in_port_t port = (addr->sa_family == AF_INET)?
62  ((struct sockaddr_in*)addr)->sin_port :
63  ((struct sockaddr_in6*)addr)->sin6_port;
64  return cf_swap_from_be16(port);
65 }
66 
67 /**
68  * @private
69  * Return size of socket address.
70  */
71 static inline socklen_t
72 as_address_size(struct sockaddr* addr)
73 {
74  return (addr->sa_family == AF_INET)? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
75 }
76 
77 /**
78  * @private
79  * Copy socket address to storage.
80  */
81 static inline void
82 as_address_copy_storage(struct sockaddr* src, struct sockaddr_storage* trg)
83 {
84  size_t size = as_address_size(src);
85  memcpy(trg, src, size);
86 }
87 
88 #ifdef __cplusplus
89 } // end extern "C"
90 #endif
static in_port_t as_address_port(struct sockaddr *addr)
Definition: as_address.h:59
void as_address_short_name(struct sockaddr *addr, char *name, socklen_t size)
static void as_address_copy_storage(struct sockaddr *src, struct sockaddr_storage *trg)
Definition: as_address.h:82
static socklen_t as_address_size(struct sockaddr *addr)
Definition: as_address.h:72
void as_address_name(struct sockaddr *addr, char *name, socklen_t size)