A link 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.
Timeouts are specified using an absolute stoptime. For example, if you want a connection to be attempted for sixty seconds, specify time(0)+60
. The operation will be tried and retried until that absolute limit, giving the caller much greater control over program behavior.
Note that this library manipulates IP addresses in the form of strings. To convert a hostname into a string IP address, call domain_name_cache_lookup. For example, here is how to make a simple HTTP request:
struct link *link; time_t stoptime = time(0)+300; char addr[LINK_ADDRESS_MAX]; int result;
const char *request = "GET / HTTP/1.0\n\n";
result = domain_name_lookup_cache("www.google.com",addr); if(result!=0) fatal("could not lookup name");
link = link_connect(addr,80,stoptime); if(!link) fatal("could not connect");
result = link_write(link,request,strlen(request),stoptime); if(result<0) fatal("could not send request");
link_stream_to_file(link,stdout,1000000,stoptime); link_close(link);
#include "int_sizes.h"
#include <time.h>
#include <limits.h>
#include <stdio.h>
#include <sys/types.h>
Go to the source code of this file.
Data Structures | |
struct | link_info |
Defines | |
#define | LINK_ADDRESS_MAX 48 |
Maximum number of characters in the text representation of a link address. | |
#define | LINK_PORT_ANY 0 |
Value to usewhen any listen port is acceptable. | |
#define | LINK_FOREVER ((time_t)INT_MAX) |
Stoptime to give when you wish to wait forever. | |
Functions | |
link * | link_connect (const char *addr, int port, time_t stoptime) |
Connect to a remote host. | |
link * | link_serve (int port) |
Prepare to accept connections. | |
link * | link_serve_address (const char *addr, int port) |
Prepare to accept connections on one network interface. | |
link * | link_accept (struct link *master, time_t stoptime) |
Accept one connection. | |
int | link_read (struct link *link, char *data, int length, time_t stoptime) |
Read data from a connection. | |
int | link_read_avail (struct link *link, char *data, int length, time_t stoptime) |
Read available data from a connection. | |
int | link_write (struct link *link, const char *data, int length, time_t stoptime) |
Write data to a connection. | |
int | link_usleep (struct link *link, int usec, int reading, int writing) |
Block until a link is readable or writable. | |
void | link_close (struct link *link) |
Close a connection. | |
void | link_window_set (int send_window, int recv_window) |
Set the TCP window size to be used for all links. | |
void | link_window_get (struct link *link, int *send_window, int *recv_window) |
Get the TCP window size actually allocated for this link. | |
int | link_readline (struct link *link, char *line, int length, time_t stoptime) |
Read a line of text from a link. | |
int | link_fd (struct link *link) |
Get the underlying file descriptor of a link. | |
int | link_address_local (struct link *link, char *addr, int *port) |
Return the local address of the link in text format. | |
int | link_address_remote (struct link *link, char *addr, int *port) |
Return the remote address of the link in text format. |
|
Maximum number of characters in the text representation of a link address. This must be large enough to accomodate ipv6 in the future. |
|
Value to usewhen any listen port is acceptable.
|
|
Stoptime to give when you wish to wait forever.
|
|
Connect to a remote host.
|
|
Prepare to accept connections. link_serve will accept connections on any network interface, which is usually what you want.
|
|
Prepare to accept connections on one network interface. Functions like link_serve, except that the server will only be visible on the given network interface.
|
|
Accept one connection.
|
|
Read data from a connection. This call will block until the given number of bytes have been read, or the connection is dropped.
|
|
Read available data from a connection. This call will read whatever data is immediately available, and then return without blocking.
|
|
Write data to a connection.
|
|
Block until a link is readable or writable.
|
|
Close a connection.
|
|
Set the TCP window size to be used for all links. Takes effect on future calls to link_connect or link_accept. Default value is set by the system or by the environment variable TCP_WINDOW_SIZE. Note that the operating system may place limits on the buffer sizes actually allocated. Use link_window_get to retrieve the buffer actually allocated for a given link.
|
|
Get the TCP window size actually allocated for this link.
|
|
Read a line of text from a link. Reads a line of text, up to and including a newline, interpreted as either LF or CR followed by LF. The line actually returned is null terminated and does not contain the newline indicator. An internal buffer is used so that readline can usually complete with zero or one system calls.
|
|
Get the underlying file descriptor of a link.
|
|
Return the local address of the link in text format.
|
|
Return the remote address of the link in text format.
|