~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to dttools/src/link.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
 
3
Copyright (C) 2005- The University of Notre Dame
 
4
This software is distributed under the GNU General Public License.
 
5
See the file COPYING for details.
 
6
*/
 
7
 
 
8
#ifndef LINK_H
 
9
#define LINK_H
 
10
 
 
11
/** @file link.h A high level TCP connection library.
 
12
A <b>link</b> is a TCP connection to a process on another machine.  This module works at a higher level of abstraction than the socket library, with an easier-to-use API and explicit support for timeouts.
 
13
<p>
 
14
Timeouts are specified using an absolute stoptime.  For example, if you want
 
15
a connection to be attempted for sixty seconds, specify <tt>time(0)+60</tt>.
 
16
The operation will be tried and retried until that absolute limit, giving
 
17
the caller much greater control over program behavior.
 
18
<p>
 
19
Note that this library manipulates IP addresses in the form of strings.
 
20
To convert a hostname into a string IP address, call @ref domain_name_cache_lookup.  For example, here is how to make a simple HTTP request:
 
21
<pre>
 
22
struct link *link;
 
23
time_t stoptime = time(0)+300;
 
24
char addr[LINK_ADDRESS_MAX];
 
25
int result;
 
26
 
 
27
const char *request = "GET / HTTP/1.0\n\n";
 
28
 
 
29
result = domain_name_cache_lookup("www.google.com",addr);
 
30
if(!result) fatal("could not lookup name");
 
31
 
 
32
link = link_connect(addr,80,stoptime);
 
33
if(!link) fatal("could not connect");
 
34
 
 
35
result = link_write(link,request,strlen(request),stoptime);
 
36
if(result<0) fatal("could not send request");
 
37
 
 
38
link_stream_to_file(link,stdout,1000000,stoptime);
 
39
link_close(link);
 
40
</pre>
 
41
 
 
42
*/
 
43
 
 
44
#include "int_sizes.h"
 
45
 
 
46
#include <time.h>
 
47
#include <limits.h>
 
48
#include <stdio.h>
 
49
#include <stdarg.h>
 
50
#include <sys/types.h>
 
51
 
 
52
/** Maximum number of characters in the text representation of a link address.  This must be large enough to accomodate ipv6 in the future. */
 
53
#define LINK_ADDRESS_MAX 48
 
54
 
 
55
/** Value to usewhen any listen port is acceptable */
 
56
#define LINK_PORT_ANY 0
 
57
 
 
58
/** Stoptime to give when you wish to wait forever */
 
59
#define LINK_FOREVER ((time_t)INT_MAX)
 
60
 
 
61
/** Connect to a remote host.
 
62
@param addr IP address of server in string form.
 
63
@param port Port of server.
 
64
@param stoptime Absolute time at which to abort.
 
65
@return On success, returns a pointer to a link object.  On failure, returns a null pointer with errno set appropriately.
 
66
*/ 
 
67
struct link * link_connect( const char *addr, int port, time_t stoptime );
 
68
 
 
69
/** Prepare to accept connections.
 
70
@ref link_serve will accept connections on any network interface,
 
71
which is usually what you want.
 
72
@param port The port number to listen on.
 
73
@return link A server endpoint that can be passed to @ref link_accept, or null on failure.
 
74
*/
 
75
struct link * link_serve( int port );
 
76
 
 
77
/** Prepare to accept connections on one network interface.
 
78
Functions like @ref link_serve, except that the server will
 
79
only be visible on the given network interface.
 
80
@param addr IP address of the network interface.
 
81
@param port The port number to listen on.
 
82
@return link A server endpoint that can be passed to @ref link_accept, or null on failure.
 
83
*/
 
84
struct link * link_serve_address( const char *addr, int port );
 
85
 
 
86
/** Accept one connection.
 
87
@param master A link returned from @ref link_serve or @ref link_serve_address.
 
88
@param stoptime The time at which to abort.
 
89
@return link A connection to a client, or null on failure.
 
90
*/
 
91
struct link * link_accept( struct link *master, time_t stoptime );
 
92
 
 
93
/** Read data from a connection.
 
94
This call will block until the given number of bytes have been read,
 
95
or the connection is dropped.
 
96
@param link The link from which to read.
 
97
@param data A buffer to hold the data.
 
98
@param length The number of bytes to read.
 
99
@param stoptime The time at which to abort.
 
100
@return The number of bytes actually read, or zero if the connection is closed, or less than zero on error.
 
101
*/
 
102
int  link_read( struct link *link, char *data, size_t length, time_t stoptime );
 
103
 
 
104
/** Read available data from a connection.
 
105
This call will read whatever data is immediately available, and then
 
106
return without blocking.
 
107
@param link The link from which to read.
 
108
@param data A buffer to hold the data.
 
109
@param length The number of bytes to read.
 
110
@param stoptime The time at which to abort.
 
111
@return The number of bytes actually read, or zero if the connection is closed, or less than zero on error.
 
112
*/
 
113
int  link_read_avail( struct link *link, char *data, size_t length, time_t stoptime );
 
114
 
 
115
/** Write data to a connection.
 
116
@param link The link to write.
 
117
@param data A pointer to the data.
 
118
@param length The number of bytes to write.
 
119
@param stoptime The time at which to abort.
 
120
@return The number of bytes actually written, or less than zero on error.
 
121
*/
 
122
int  link_write( struct link *link, const char *data, size_t length, time_t stoptime );
 
123
 
 
124
/* Write a string of length len to a connection. All data is written until
 
125
 * finished or an error is encountered.
 
126
@param link The link to write.
 
127
@param str A pointer to the string.
 
128
@param len Length of the string.
 
129
@param stoptime The time at which to abort.
 
130
@return The number of bytes actually written, or less than zero on error.
 
131
*/
 
132
int  link_putlstring( struct link *link, const char *str, size_t len, time_t stoptime);
 
133
 
 
134
/* Write a C string to a connection. All data is written until finished or an
 
135
   error is encountered. It is defined as a macro.
 
136
@param link The link to write.
 
137
@param str A pointer to the string.
 
138
@param stoptime The time at which to abort.
 
139
@return The number of bytes actually written, or less than zero on error.
 
140
*/
 
141
#define link_putstring(l,s,t)  (link_putlstring(l,s,strlen(s),t))
 
142
 
 
143
/* Write a C literal string to a connection. All data is written until finished
 
144
   or an error is encountered. It is defined as a macro.
 
145
@param link The link to write.
 
146
@param str A pointer to the string.
 
147
@param stoptime The time at which to abort.
 
148
@return The number of bytes actually written, or less than zero on error.
 
149
*/
 
150
#define link_putliteral(l,s,t)  (link_putlstring(l,s "",((sizeof(s))-1),t))
 
151
 
 
152
/** Write formatted data to a connection. All data is written until finished
 
153
  * or an error is encountered.
 
154
@param link The link to write.
 
155
@param fmt A pointer to the data.
 
156
@param stoptime The time at which to abort.
 
157
@param ... Format arguments.
 
158
@return The number of bytes actually written, or less than zero on error.
 
159
*/
 
160
int  link_putfstring( struct link *link, const char *fmt, time_t stoptime, ... );
 
161
 
 
162
/** Write formatted data to a connection. All data is written until finished
 
163
  * or an error is encountered.
 
164
@param link The link to write.
 
165
@param fmt A pointer to the data.
 
166
@param stoptime The time at which to abort.
 
167
@param va Format arguments.
 
168
@return The number of bytes actually written, or less than zero on error.
 
169
*/
 
170
int  link_putvfstring( struct link *link, const char *fmt, time_t stoptime, va_list va );
 
171
 
 
172
/** Block until a link is readable or writable.
 
173
@param link The link to wait on.
 
174
@param usec The maximum number of microseconds to wait.
 
175
@param reading Wait for the link to become readable.
 
176
@param writing Wait for the link to become writable.
 
177
@return One if the link becomes readable or writable before the timeout expires, zero otherwise.
 
178
*/
 
179
int  link_usleep( struct link *link, int usec, int reading, int writing );
 
180
 
 
181
/** Close a connection.
 
182
@param link The connection to close.
 
183
*/
 
184
void link_close( struct link *link );
 
185
 
 
186
/** Set the TCP window size to be used for all links.
 
187
Takes effect on future calls to @ref link_connect or @ref link_accept.
 
188
Default value is set by the system or by the environment variable
 
189
TCP_WINDOW_SIZE.
 
190
Note that the operating system may place limits on the buffer
 
191
sizes actually allocated.  Use @ref link_window_get to retrieve
 
192
the buffer actually allocated for a given link.
 
193
@param send_window The size of the send window, in bytes.
 
194
@param recv_window The size of the recv window, in bytes.
 
195
*/
 
196
 
 
197
void link_window_set( int send_window, int recv_window );
 
198
 
 
199
/** Get the TCP window size actually allocated for this link.
 
200
@param link The link to examine.
 
201
@param send_window A pointer where to store the send window.
 
202
@param recv_window A pointer where to store the receive window.
 
203
*/
 
204
 
 
205
void link_window_get( struct link *link, int *send_window, int *recv_window );
 
206
 
 
207
/** Read a line of text from a link.
 
208
Reads a line of text, up to and including a newline, interpreted as either LF
 
209
or CR followed by LF.  The line actually returned is null terminated and
 
210
does not contain the newline indicator.   An internal buffer is used so that
 
211
readline can usually complete with zero or one system calls.
 
212
@param link The link to read from.
 
213
@param line A pointer to a buffer to fill with data.
 
214
@param length The length of the buffer in bytes.
 
215
@param stoptime The absolute time at which to abort.
 
216
@return If greater than zero, a line was read, and the return value indicates the length in bytes.  If equal to zero, end of stream was reached.  If less than zero, an error occurred.
 
217
*/
 
218
int  link_readline( struct link *link, char *line, size_t length, time_t stoptime );
 
219
 
 
220
/** Get the underlying file descriptor of a link.
 
221
@param link The link to examine.
 
222
@return The integer file descriptor of the link.
 
223
*/
 
224
int  link_fd( struct link *link );
 
225
 
 
226
int  link_nonblocking( struct link *link, int onoff );
 
227
 
 
228
/** Return the local address of the link in text format.
 
229
@param link The link to examine.
 
230
@param addr Pointer to a string of at least @ref LINK_ADDRESS_MAX bytes, which will be filled with a text representation of the local IP address.
 
231
@param port Pointer to an integer, which will be filled with the TCP port number.
 
232
@return Positive on success, zero on failure.
 
233
*/
 
234
int  link_address_local( struct link *link, char *addr, int *port );
 
235
 
 
236
/** Return the remote address of the link in text format.
 
237
@param link The link to examine.
 
238
@param addr Pointer to a string of at least @ref LINK_ADDRESS_MAX bytes, which will be filled with a text representation of the remote IP address.
 
239
@param port Pointer to an integer, which will be filled with the TCP port number.
 
240
@return Positive on success, zero on failure.
 
241
*/
 
242
int  link_address_remote( struct link *link, char *addr, int *port );
 
243
 
 
244
INT64_T link_stream_to_buffer( struct link *link, char **buffer, time_t stoptime );
 
245
 
 
246
INT64_T link_stream_to_fd( struct link *link, int fd, INT64_T length, time_t stoptime );
 
247
INT64_T link_stream_to_file( struct link *link, FILE *file, INT64_T length, time_t stoptime );
 
248
 
 
249
INT64_T link_stream_from_fd( struct link *link, int fd, INT64_T length, time_t stoptime );
 
250
INT64_T link_stream_from_file( struct link *link, FILE *file, INT64_T length, time_t stoptime );
 
251
 
 
252
INT64_T link_soak( struct link *link, INT64_T length, time_t stoptime );
 
253
 
 
254
/** Options for link performance tuning. */
 
255
typedef enum {
 
256
        LINK_TUNE_INTERACTIVE,  /**< Data is sent immediately to optimze interactive latency. */
 
257
        LINK_TUNE_BULK          /**< Data may be buffered to improve throughput of large transfers. */
 
258
} link_tune_t;
 
259
 
 
260
/** Tune a link for interactive or bulk performance.  A link may be tuned at any point in its lifecycle.
 
261
@ref LINK_TUNE_INTERACTIVE is best used for building latency-sensitive interactive or RPC applications.
 
262
@ref LINK_TUNE_BULK is best used to large data transfers.
 
263
@param link The link to be tuned.
 
264
@param mode The desired tuning mode.
 
265
*/
 
266
int  link_tune( struct link *link, link_tune_t mode );
 
267
 
 
268
/** Indicates a link is ready to read via @ref link_poll.*/
 
269
#define LINK_READ 1
 
270
 
 
271
/** Indicates a link is ready to write via @ref link_poll.*/
 
272
#define LINK_WRITE 2
 
273
 
 
274
/** Activity structure passed to @ref link_poll. */
 
275
struct link_info {
 
276
        struct link *link;  /**< The link to be polled. */
 
277
        int events;         /**< The events to wait for (@ref LINK_READ or @ref LINK_WRITE) */
 
278
        int revents;        /**< The events returned (@ref LINK_READ or @ref LINK_WRITE) */
 
279
};
 
280
 
 
281
/**
 
282
Wait for a activity on a an array of links.
 
283
@param array Pointer to an array of @ref link_info structures.  Each one should contain a pointer to a valid link and have the events field set to the events (@ref LINK_READ or @ref LINK_WRITE) of interest.  Upon return, each one will have the revents field filled with the events that actually occurred.
 
284
@param nlinks The length of the array.
 
285
@param msec The number of milliseconds to wait for activity.  Zero indicates do not wait at all, while -1 indicates wait forever.
 
286
@return The number of links available to read or write.
 
287
*/
 
288
 
 
289
int  link_poll( struct link_info *array, int nlinks, int msec );
 
290
 
 
291
#endif
 
292
 
 
293
 
 
294
 
 
295
 
 
296
 
 
297
 
 
298
 
 
299
 
 
300
 
 
301
 
 
302
 
 
303
 
 
304