39 #include <sys/types.h> 40 #include <sys/socket.h> 41 #include <netinet/in.h> 42 #include <arpa/inet.h> 70 for (port = IPPORT_USERRESERVED; port < 1 << 16; port++)
74 memset (&a, 0,
sizeof (a));
76 a.sin_family = PF_INET;
77 a.sin_addr.s_addr = INADDR_ANY;
78 a.sin_port = htons (port);
80 if (bind (sock, (
struct sockaddr *) &a,
sizeof (a)) >= 0)
84 return port < 1 << 16 ? port : -1;
91 void *
addr, socklen_t * addr_len,
u32 ip4_default_address)
101 struct sockaddr_un *su =
addr;
102 su->sun_family = PF_LOCAL;
104 clib_min (
sizeof (su->sun_path), 1 + strlen (config)));
105 *addr_len =
sizeof (su[0]);
113 struct sockaddr_in *sa =
addr;
122 if (
unformat (&i,
"%s:%d", &host_name, &port)
123 ||
unformat (&i,
"%s:0x%x", &host_name, &port))
125 else if (
unformat (&i,
"%s", &host_name))
136 sa->sin_family = PF_INET;
137 *addr_len =
sizeof (sa[0]);
139 sa->sin_port = htons (port);
145 struct in_addr host_addr;
148 if (!strcmp (host_name,
"localhost"))
149 sa->sin_addr.s_addr = htonl (INADDR_LOOPBACK);
151 else if (inet_aton (host_name, &host_addr))
152 sa->sin_addr = host_addr;
154 else if (host_name && strlen (host_name) > 0)
156 struct hostent *host = gethostbyname (host_name);
160 clib_memcpy (&sa->sin_addr.s_addr, host->h_addr_list[0],
165 sa->sin_addr.s_addr = htonl (ip4_default_address);
192 tx_len =
vec_len (s->tx_buffer);
193 written = write (fd, s->tx_buffer, tx_len);
208 else if (written > 0)
210 if (written == tx_len)
211 _vec_len (s->tx_buffer) = 0;
218 else if (written == 0 && tx_len > 64 * 1024)
240 vec_add2 (sock->rx_buffer, buf, n_bytes);
242 if ((n_read = read (fd, buf, n_bytes)) < 0)
258 _vec_len (sock->rx_buffer) += n_read - n_bytes;
266 if (close (s->fd) < 0)
288 struct sockaddr_un su;
290 socklen_t addr_len = 0;
296 ? INADDR_LOOPBACK : INADDR_ANY));
302 s->fd = socket (addr.sa.sa_family, SOCK_STREAM, 0);
310 if (addr.sa.sa_family == PF_INET)
311 port = ((
struct sockaddr_in *) &addr)->sin_port;
317 if (addr.sa.sa_family == PF_INET)
330 if (addr.sa.sa_family == PF_LOCAL)
331 unlink (((
struct sockaddr_un *) &addr)->sun_path);
336 if (setsockopt (s->fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof (v)) < 0)
340 if (need_bind && bind (s->fd, &addr.sa, addr_len) < 0)
346 if (listen (s->fd, 5) < 0)
355 && fcntl (s->fd, F_SETFL, O_NONBLOCK) < 0)
361 if (connect (s->fd, &addr.sa, addr_len) < 0
363 errno == EINPROGRESS))
384 memset (client, 0,
sizeof (client[0]));
387 client->fd = accept (server->fd, 0, 0);
392 if (fcntl (client->fd, F_SETFL, O_NONBLOCK) < 0)
399 len =
sizeof (client->peer);
400 if (getpeername (client->fd, (
struct sockaddr *) &client->peer, &len) < 0)
sll srl srl sll sra u16x4 i
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
#define SOCKET_RX_END_OF_FILE
clib_error_t * clib_socket_init(clib_socket_t *s)
static clib_error_t * default_socket_write(clib_socket_t *s)
struct _socket_t clib_socket_t
#define clib_error_return(e, args...)
static word find_free_port(word sock)
#define clib_error_return_unix(e, args...)
static clib_error_t * default_socket_close(clib_socket_t *s)
static void clib_socket_tx_add_va_formatted(clib_socket_t *s, char *fmt, va_list *va)
#define vec_free(V)
Free vector's memory (no header).
static void socket_init_funcs(clib_socket_t *s)
#define clib_memcpy(a, b, c)
clib_error_t * clib_socket_accept(clib_socket_t *server, clib_socket_t *client)
#define vec_delete(V, N, M)
Delete N elements starting at element M.
static word unix_error_is_fatal(word error)
#define SOCKET_NON_BLOCKING_CONNECT
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define clib_unix_warning(format, args...)
static clib_error_t * socket_config(char *config, void *addr, socklen_t *addr_len, u32 ip4_default_address)
void clib_socket_tx_add_formatted(clib_socket_t *s, char *fmt,...)
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
static clib_error_t * default_socket_read(clib_socket_t *sock, int n_bytes)