-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddress.h
207 lines (173 loc) · 6.02 KB
/
address.h
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
/**
* libcap_utils - DPMI capture utilities
* Copyright (C) 2003-2013 (see AUTHORS)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef CAPUTILS_ADDRESS_H
#define CAPUTILS_ADDRESS_H
#include <stdio.h>
#include <netinet/ether.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
/**
* Supported address format:
*
* Types:
* - ethernet address (hwaddr)
* - IP address:port
* - filename
*
* Ethernet address:
* - Optional but recommended colon or dash delimiter.
* - Can fill in blanks by using double color, e.g:
* 01::01 is read as 01:00:00:00:00:01
*
* IP address:
* - tcp://127.0.0.1:4711
* - Supports tcp and udp.
* - Parsed using inet_aton.
*
* Filename:
* - Absolute or relative.
* - For filters limited to 22 chars.
* - No limit on local machine.
* - NOTE: For local addresses the string is referenced and must be retained
* during the lifetime of the address.
*
* To force a specific type use a prefix, e.g. file://127.0.0.1 reference a file
* called "127.0.0.1". TCP/UDP is only supported using prefix.
*
*/
#ifdef CAPUTILS_EXPORT
#pragma GCC visibility push(default)
#endif
#ifdef __cplusplus
extern "C" {
#endif
struct stream_addr {
union {
/* raw buffer for backwards compability (may not be null-terminated) (includes old port) */
unsigned char buffer[22 + 4];
/* for ethernet streams */
struct ether_addr ether_addr;
/* for capfiles (null-terminated) */
char filename[22];
/* for locally stored capfiles (null-terminated) */
/* these cannot be sent across network */
const char* local_filename;
/* used together with STREAM_ADDR_DUPLICATE */
char* int_filename;
/* Using existing FILE pointer. By default the user has to close this
* stream after stream_close. To automatically close it use
* STREAM_ADDR_FCLOSE flag. */
FILE* fp;
/* for TCP/UDP streams */
struct sockaddr_in ipv4;
} __attribute__((packed));
uint16_t _type;
uint16_t _flags;
} __attribute__((packed));
typedef struct stream_addr stream_addr_t;
#define STREAM_ADDR_INITIALIZER {{{0,}},}
enum AddressType {
/**
* If the format of the address isn't know, this flag can be set to have it
* guess. Essentially it works like following:
* - If it is parsable as an ethernet address, STREAM_ADDR_ETHERNET is used.
* - If is begins with tcp:// or udp://, STREAM_ADD_{TCP,UDP} is used.
* - Otwerwise STREAM_ADDR_CAPFILE with STREAM_ADDR_LOCAL flag is used.
*
* However, if the user have a file which is named as an ethernet address
* confusion might happen.
*/
STREAM_ADDR_GUESS = -1,
/* fixed format */
STREAM_ADDR_CAPFILE = 0,
STREAM_ADDR_ETHERNET,
STREAM_ADDR_UDP,
STREAM_ADDR_TCP,
STREAM_ADDR_FP,
STREAM_ADDR_FIFO,
};
enum AddressFlags {
/* set to indicate that the capfile path is local (and can thus be longer
* than a regular filename of 22 chars). Memory is referenced so the caller
* must ensure the lifetime of the string is as long as the lifetime as the
* filter holding this address. */
STREAM_ADDR_LOCAL = (1<<0),
/* force the stream to be flushed for every write. useful for low-traffic
* streams or to ensure real-time data. Not necessarily implemented for all
* types.*/
STREAM_ADDR_FLUSH = (1<<1),
/* For files, FIFOs and similar, unlink the file in stream_close. This is
* most useful for FIFO which is created dynamically and should not really
* be used by end-users. */
STREAM_ADDR_UNLINK = (1<<2),
/* Normally the file is closed automatically but when using STREAM_ADDR_FP
* that is not the case. Using this flag the FILE pointer will be closed
* automatically. */
STREAM_ADDR_FCLOSE = (1<<3),
/* The local filename is duplicated and automatically freed. */
STREAM_ADDR_DUPLICATE = (1<<4),
};
/**
* Convert string to stream_addr_t.
* @param dst Pointer to an existig destination_t.
* @param src String representing an address.
* @param type What kind of address it represents.
* @param flags Special flags, can be set to zero. @see DestinationFlags.
* @return Zero if successful, errno on errors.
*/
int stream_addr_aton(stream_addr_t* dst, const char* src, enum AddressType type, int flags);
/**
* Set address to a local string (only referencing the memory)
*/
int stream_addr_str(stream_addr_t* dst, const char* src, int flags);
/**
* Set address to an existing local file pointer. User must ensure it is open in the
* correct mode for the operation.
*/
int stream_addr_fp(stream_addr_t* dst, FILE* fp, int flags);
/**
* Convert destination to string. The string is returned in a statically
* allocated buffer, which subsequent calls will overwrite.
*/
const char* stream_addr_ntoa(const stream_addr_t* src);
/**
* Like destination_ntoa but writes into buf.
* @param bytes Size of buf.
*/
const char* stream_addr_ntoa_r(const stream_addr_t* src, char* buf, size_t bytes);
enum AddressType stream_addr_type(const stream_addr_t* addr) __attribute__((pure));
int stream_addr_flags(const stream_addr_t* addr) __attribute__((pure));
int stream_addr_have_flag(const stream_addr_t* addr, enum AddressFlags flag);
/**
* Initialize address to zero.
* Some address whose types allocates resources will be free'd.
*/
void stream_addr_reset(stream_addr_t* addr);
/**
* Check if an address is set or not.
*/
int stream_addr_is_set(const stream_addr_t* addr);
#ifdef __cplusplus
}
#endif
#ifdef CAPUTILS_EXPORT
#pragma GCC visibility pop
#endif
#endif /* CAPUTILS_ADDRESS_H */