forked from PolySat/libproc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc.h
315 lines (283 loc) · 9.81 KB
/
ipc.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright PolySat, California Polytechnic State University, San Luis Obispo. [email protected]
* This file is part of libproc, a PolySat library.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file ipc.h Inter-process communication header file.
*
* Header for IPC component of the polysat library.
*
* @author Greg Manyak <[email protected]>
*/
#ifndef IPC_H
#define IPC_H
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef __cplusplus
extern "C" {
#endif
/// Maximum size of an IP packet
#define MAX_IP_PACKET_SIZE 65535
/**
* Gets a file descriptor for a socket based on a named service.
* Named services can be used by adding an entry to the /etc/services file.
*
* @param service Name of the service to open the UDP port of.
*
* @return A socket file descriptor.
*
* @retval -1 On error.
*/
int socket_named_init(const char * service);
/**
* Initializes a UDP socket on the specified port.
*
* @param port Host order port number to open socket on.
*
* @return A socket file descriptor.
*
* @retval -1 On error.
*/
int socket_init(int port);
/**
* Initializes a listening TCP socket on the specified port.
*
* @param port Host order port number to open socket on.
*
* @return A socket file descriptor.
*
* @retval -1 On error.
*/
int socket_tcp_init(int port);
/**
* Reads data from a specified socket fd.
* Loads source of data into src pointer.
*
* @param fd A socket file descriptor.
* @param buf Pointer to memory for putting recv'd data into.
* @param bufSize Number of bytes available at buf.
* @param src Source address of data.
*
* @return Number of bytes read.
*
* @retval -1 On error.
*/
int socket_read(int fd, void * buf, size_t bufSize, struct sockaddr_in * src);
/**
* Writes data to a socket based on the service name.
*
* @param fd A socket file descriptor.
* @param buf Pointer to data to be sent.
* @param bufSize Number of bytes to be sent.
* @param name Name of service to send to.
*
* @return Number of bytes written to socket.
* @retval -1 On error
*/
int socket_named_write(int fd, void * buf, size_t bufSize, const char * name);
/**
* Writes data on the provided socket fd to the dest sockaddr.
*
* @param fd A socket file descriptor.
* @param buf Pointer to data to be sent.
* @param bufSize Number of bytes to be sent.
* @param dest Destination socket address.
*
* @return Number of bytes written.
*
* @retval -1 On error.
*/
int socket_write(int fd, void * buf, size_t bufSize, struct sockaddr_in * dest);
/**
* Closes a socket.
*
* Acts as a wrapper to close right now, may be more useful later.
*
* @param fd A socket file descriptor.
*
* @retval 0 On success.
* @retval -1 On failure.
*/
int socket_close(int fd);
/**
* Looks up a udp service by name and returns port in host order.
*
* @param service Name of service to be looked up in /etc/services.
*
* @return UDP port number in host order.
*
* @retval -1 On error.
*/
int socket_get_addr_by_name(const char * service);
/**
* Looks up a system service by name and returns the associated multicast
* source address.
*
* @param service Name of service to be looked up
*
* @return IP Adress in network order.
*
* @retval 0.0.0.0 on error
*/
struct in_addr socket_multicast_addr_by_name(const char * service);
uint16_t socket_multicast_port_by_name(const char * service);
/**
* Looks up a udp service name by port number in the socket address.
* Will try internal look up table if /etc/services lookup fails.
*
* NOTE: Port number should be in network order,
* this is already the case if it was returned from a socket operation.
*
* @param addr Socket address structure.
* @param buf Buffer for service name.
* @param bufSize Number of bytes in the buffer available.
*
* @return Length of the name of the service, in bytes.
*
* @retval 0 On failed lookup.
* @retval -1 On error.
*/
int socket_get_name_by_addr(struct sockaddr_in * addr, char * buf, size_t bufSize);
/**
* Uses blocking system calls to setup an IPC port, transmit the given
* UDP data, and receive a single response. This is a utility function meant
* to simplify development of client decoder programs. Because it is blocking
* you should not use it in FSW service processes
*
* @param dstAddr The name (e.g., watchdog) of the destination process
* @param dstProc The IP address, in a dotted quad string, of the
* destination host. Pass NULL to use the current host.
* @param txCmd The bytes to send in the UDP packet, including the command
* byte.
* @param txCmdLen The total length of the txCmd buffer
* @param rxResp A pointer to an allocated buffer to hold the response,
* or NULL to forgo receiving a response.
* @param rxRespLen The maximum length of the rxResp buffer
* @param responseTimeoutMS The amount of time to wait, in ms, for the
* response before giving up
*
* @return The actual number of bytes in the received response, or a
* negative number if an error occured.
*/
int socket_send_packet_and_read_response(const char *dstAddr,
const char *dstProc, void *txCmd, size_t txCmdLen,
void *rxResp, size_t rxRespLen, int responseTimeoutMS);
int socket_send_packet_and_read_xdr(const char *dstAddr,
const char *dstProc, void *txCmd, size_t txCmdLen,
void *rxResp, size_t rxRespLen, int responseTimeoutMS);
/**
* Resolve a host name or dotted-quad into an in_addr
*/
int socket_resolve_host(const char *host, struct in_addr *addr);
// Structure to use for buffering output prior to sending to a socket
struct IPCBuffer;
/**
* Allocates a new IPCBuffer
*
* @return The newly allocated buffer or NULL if an error occurs.
*/
struct IPCBuffer *ipc_alloc_buffer(void);
/**
* Destroys an IPC buffer. An IPC buffer can not be used after it is
* destroyed.
*
* @param goner The buffer to destroy.
*/
void ipc_destroy_buffer(struct IPCBuffer **goner);
/**
* Removes all accumulated data from a buffer, reseting it to a 0 length.
*
* @param buffer The buffer to reset
*/
void ipc_reset_buffer(struct IPCBuffer *buffer);
/**
* Writes the contents of the data to the provided file descriptor
* synchronously. Does not write empty buffers.
*
* @param fd The file descriptor to write to.
* @param buffer The data to send.
*
* @return Error indication flag
*
* @retval -1 On error.
*/
int ipc_write_buffer_sync(int fd, struct IPCBuffer *buffer);
/**
* Appends the contents of the format string to an IPC buffer
*
* @param buffer The buffer to add the string to
* @param fmt The format specifier
* @param ... The parameters for the format specifier
*
* @return The number of bytes added to the string
*/
int ipc_printf_buffer(struct IPCBuffer *buffer, const char *fmt, ...)
__attribute__ ((format (printf, 2, 3)));
/**
* Appends the contents of the buffer to an IPC buffer
*
* @param buffer The buffer to add the string to
* @param data The data to append
* @param len The size of the data to append
*
* @return The number of bytes added to the string
*/
int ipc_append_buffer(struct IPCBuffer *buffer, const void *data, int len);
/**
* Returns the number of bytes in the buffer.
*
* @param buffer The buffer whose size you want to know
*
* @return The number of bytes in the buffer
*/
size_t ipc_buffer_size(struct IPCBuffer *buffer);
typedef size_t (*ipc_buffer_cb)(const char *data, size_t dataLen, void *arg);
/**
* This function processes data contained in a buffer via a callback function.
* The buffer is processed until the callback indicates there is nothing else
* that can be processed.
*
* @param buffer The buffer to process data from
* @param cb The callback function to use to process the data. The function
* returns the number of bytes processed. It will be called in a
* loop until it returns 0.
* @param arg The parameter to pass into the callback function
*
* @return The number of bytes processed from the buffer
*/
int ipc_process_buffer(struct IPCBuffer *buffer, ipc_buffer_cb cb, void *arg);
struct ProcessData;
struct IPC_Command;
enum IPC_CB_TYPE { IPC_CB_TYPE_COOKED = 1, IPC_CB_TYPE_RAW = 2 };
typedef void (*IPC_command_callback)(struct ProcessData *proc, int timeout, void *arg, char *resp_buff, size_t resp_len, enum IPC_CB_TYPE cb_type);
extern int IPC_command_blocking(uint32_t command,
void *params, uint32_t param_type,
struct sockaddr_in dest, IPC_command_callback cb, void *,
enum IPC_CB_TYPE cb_type, unsigned int timeout);
extern int IPC_command(struct ProcessData*, uint32_t command, void *params,
uint32_t param_type,
struct sockaddr_in dest, IPC_command_callback cb, void *,
enum IPC_CB_TYPE cb_type, unsigned int timeout);
extern void IPC_response(struct ProcessData *proc, struct IPC_Command *cmd,
uint32_t param_type, void *params, struct sockaddr_in *dest);
extern void IPC_error(struct ProcessData *proc, struct IPC_Command *cmd,
uint32_t error_code, struct sockaddr_in *dest);
#ifdef __cplusplus
}
#endif
#endif