File: addr.h

package info (click to toggle)
i2util 1.6-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 748 kB
  • ctags: 473
  • sloc: ansic: 5,290; perl: 1,151; makefile: 74; sh: 17
file content (248 lines) | stat: -rw-r--r-- 5,833 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * ex: set tabstop=4 ai expandtab softtabstop=4 shiftwidth=4:
 * -*- mode: c-basic-indent: 4; tab-width: 4; indent-tabls-mode: nil -*-
 *      $Id$
 */
/************************************************************************
 *                                                                      *
 *                           Copyright (C)  2005                        *
 *                               Internet2                              *
 *                           All Rights Reserved                        *
 *                                                                      *
 ************************************************************************/
/*
 **    File:         addr.h
 **
 **    Author:       Jeff W. Boote
 **
 **    Date:         Tue Dec 20 12:04:12 MST 2005
 **
 **    Description:    
 *              Abstraction layer for addresses. Useful for keeping
 *              network address code localized to one part of the
 *              code.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
#ifndef    I2ADDR_H
#define    I2ADDR_H
#include <I2util/util.h>
#include <sys/types.h>
#include <sys/socket.h>

typedef struct I2AddrRec    *I2Addr;

/*
 * The I2AddrBy* functions are used to allow the addr API to more
 * adequately manage the memory associated with the many different ways
 * of specifying an address - and to provide a uniform way to specify an
 * address.
 *
 * These functions return NULL on failure. (They call the error handler
 * to specify the reason.)
 */
extern I2Addr
I2AddrByNode(
        I2ErrHandle eh,
        const char  *node    /* dns or valid char representation of addr */
        );

extern I2Addr
I2AddrByWildcard(
        I2ErrHandle eh,
        int         socktype,
        const char  *servname
        );

extern I2Addr
I2AddrBySAddr(
        I2ErrHandle     eh,
        struct sockaddr *saddr,
        socklen_t       saddr_len,
        int             socktype,
        int             protocol
        );

/*
 * Copies an I2Addr. If the src I2Addr represents a connected socket,
 * the copy will as well. However, as with the AddrBy*FD functions
 * free'ing the copy I2Addr will not close the fd.
 */
extern I2Addr
I2AddrCopy(
        I2Addr  src
        );

/*
 * Return the address for the remote side of a socket connection
 * (getpeername)
 * Only copies the address portion of the I2Addr record, does not
 * represent the actual connected socket.
 */
extern I2Addr
I2AddrBySockFD(
        I2ErrHandle eh,
        int         fd, /* fd must be an already connected socket */
        I2Boolean   close_on_free
        );

/*
 * Return the address for the local side of a socket connection
 * (getsockname)
 * Only copies the address portion of the I2Addr record, does not
 * represent the actual connected socket.
 */
extern I2Addr
I2AddrByLocalSockFD(
        I2ErrHandle eh,
        int         fd,    /* fd must be an already connected socket */
        I2Boolean   close_on_free
        );

/*
 * Addr access functions.
 * The set functions are only valid *before* a real socket is associated
 * with the Addr. So, the internal fd cannot be set upon entrance.
 */
extern I2Boolean
I2AddrSetSAddr(
        I2Addr          addr,
        struct sockaddr *saddr,
        socklen_t       saddr_len
        );

extern I2Boolean
I2AddrSetFD(
        I2Addr      addr,
        int         fd,
        I2Boolean   close_on_free
        );

extern I2Boolean
I2AddrSetPort(
        I2Addr     addr,
        uint16_t   port
        );

extern uint16_t
I2AddrPort(
        I2Addr  addr
        );

extern I2Boolean
I2AddrSetSocktype(
        I2Addr  addr,
        int     so_type
        );

extern int
I2AddrSocktype(
        I2Addr  addr
        );

extern I2Boolean
I2AddrSetProtocol(
        I2Addr  addr,
        int     protocol
        );

extern int
I2AddrProtocol(
        I2Addr  addr
        );

extern I2Boolean
I2AddrSetPassive(
        I2Addr      addr,
        I2Boolean   passive
        );

extern struct addrinfo
*I2AddrAddrInfo(
        I2Addr  addr,
        char    *def_node,
        char    *def_serv
        );

extern struct sockaddr
*I2AddrSAddr(
        I2Addr          addr,
        socklen_t       *saddr_len
        );

extern char *
I2AddrNodeName(
        I2Addr  addr,
        char    *buf,
        size_t  *len    /* in/out parameter for buf len */
        );

extern char *
I2AddrServName(
        I2Addr  addr,
        char    *buf,
        size_t  *len    /* in/out parameter for buf len */
        );

extern char *
I2AddrNodeServName(
        I2Addr  addr,
        char    *buf,
        size_t  *len
        );

/*
 * return FD for given I2Addr or -1 if it doesn't refer to a socket yet.
 */
extern int
I2AddrFD(
        I2Addr    addr
        );

/*
 * return socket address length (for use in calling accept etc...)
 * or 0 if it doesn't refer to a socket yet.
 */
extern socklen_t
I2AddrSockLen(
        I2Addr    addr
        );

extern void
I2AddrFree(
        I2Addr    addr
        );

#ifndef htonll
#define htonll(x)   I2htonll(x)
#endif
#ifndef ntohll
#define ntohll(x)   I2ntohll(x)
#endif

extern uint64_t
I2htonll(
        uint64_t    h64
      );
extern uint64_t
I2ntohll(
        uint64_t    n64
      );

extern int
I2AddrIsLoopback(
        I2Addr    addr
      );

#endif    /* I2ADDR_H */