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-2016 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  * Return port of address.
44  */
45 static inline in_port_t
46 as_address_port(struct sockaddr* addr)
47 {
48  in_port_t port = (addr->sa_family == AF_INET)?
49  ((struct sockaddr_in*)addr)->sin_port :
50  ((struct sockaddr_in6*)addr)->sin6_port;
51  return cf_swap_from_be16(port);
52 }
53 
54 /**
55  * @private
56  * Return size of socket address.
57  */
58 static inline socklen_t
59 as_address_size(struct sockaddr* addr)
60 {
61  return (addr->sa_family == AF_INET)? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
62 }
63 
64 /**
65  * @private
66  * Copy socket address to storage.
67  */
68 static inline void
69 as_address_copy_storage(struct sockaddr* src, struct sockaddr_storage* trg)
70 {
71  size_t size = as_address_size(src);
72  memcpy(trg, src, size);
73 }
74 
75 #ifdef __cplusplus
76 } // end extern "C"
77 #endif