39 #include <sys/types.h> 40 #include <sys/socket.h> 42 #include <netinet/in.h> 43 #include <arpa/inet.h> 71 for (port = IPPORT_USERRESERVED; port < 1 << 16; port++)
75 memset (&a, 0,
sizeof (a));
77 a.sin_family = PF_INET;
78 a.sin_addr.s_addr = INADDR_ANY;
79 a.sin_port = htons (port);
81 if (bind (sock, (
struct sockaddr *) &a,
sizeof (a)) >= 0)
85 return port < 1 << 16 ? port : -1;
92 void *
addr, socklen_t * addr_len,
u32 ip4_default_address)
100 if (config[0] ==
'/')
102 struct sockaddr_un *su =
addr;
103 su->sun_family = PF_LOCAL;
105 clib_min (
sizeof (su->sun_path), 1 + strlen (config)));
106 *addr_len =
sizeof (su[0]);
114 struct sockaddr_in *sa =
addr;
123 if (
unformat (&i,
"%s:%d", &host_name, &port)
124 ||
unformat (&i,
"%s:0x%x", &host_name, &port))
126 else if (
unformat (&i,
"%s", &host_name))
137 sa->sin_family = PF_INET;
138 *addr_len =
sizeof (sa[0]);
140 sa->sin_port = htons (port);
146 struct in_addr host_addr;
149 if (!strcmp (host_name,
"localhost"))
150 sa->sin_addr.s_addr = htonl (INADDR_LOOPBACK);
152 else if (inet_aton (host_name, &host_addr))
153 sa->sin_addr = host_addr;
155 else if (host_name && strlen (host_name) > 0)
157 struct hostent *host = gethostbyname (host_name);
161 clib_memcpy (&sa->sin_addr.s_addr, host->h_addr_list[0],
166 sa->sin_addr.s_addr = htonl (ip4_default_address);
193 tx_len =
vec_len (s->tx_buffer);
194 written = write (fd, s->tx_buffer, tx_len);
209 else if (written > 0)
211 if (written == tx_len)
212 _vec_len (s->tx_buffer) = 0;
219 else if (written == 0 && tx_len > 64 * 1024)
241 vec_add2 (sock->rx_buffer, buf, n_bytes);
243 if ((n_read = read (fd, buf, n_bytes)) < 0)
259 _vec_len (sock->rx_buffer) += n_read - n_bytes;
267 if (close (s->fd) < 0)
289 struct sockaddr_un su;
291 socklen_t addr_len = 0;
297 ? INADDR_LOOPBACK : INADDR_ANY));
303 s->fd = socket (addr.sa.sa_family, SOCK_STREAM, 0);
311 if (addr.sa.sa_family == PF_INET)
312 port = ((
struct sockaddr_in *) &addr)->sin_port;
318 if (addr.sa.sa_family == PF_INET)
331 if (addr.sa.sa_family == PF_LOCAL)
332 unlink (((
struct sockaddr_un *) &addr)->sun_path);
337 if (setsockopt (s->fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof (v)) < 0)
341 if (need_bind && bind (s->fd, &addr.sa, addr_len) < 0)
347 if (listen (s->fd, 5) < 0)
352 if (addr.sa.sa_family == PF_LOCAL
355 struct stat st = { 0 };
356 stat (((
struct sockaddr_un *) &addr)->sun_path, &st);
357 st.st_mode |= S_IWGRP;
358 chmod (((
struct sockaddr_un *) &addr)->sun_path, st.st_mode);
364 && fcntl (s->fd, F_SETFL, O_NONBLOCK) < 0)
370 if (connect (s->fd, &addr.sa, addr_len) < 0
372 errno == EINPROGRESS))
393 memset (client, 0,
sizeof (client[0]));
396 client->fd = accept (server->fd, 0, 0);
401 if (fcntl (client->fd, F_SETFL, O_NONBLOCK) < 0)
408 len =
sizeof (client->peer);
409 if (getpeername (client->fd, (
struct sockaddr *) &client->peer, &len) < 0)
static word find_free_port(word sock)
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.
static clib_error_t * default_socket_close(clib_socket_t *s)
#define SOCKET_RX_END_OF_FILE
static clib_error_t * default_socket_write(clib_socket_t *s)
struct _socket_t clib_socket_t
clib_error_t * clib_socket_accept(clib_socket_t *server, clib_socket_t *client)
#define clib_error_return(e, args...)
clib_error_t * clib_socket_init(clib_socket_t *s)
static void socket_init_funcs(clib_socket_t *s)
#define clib_error_return_unix(e, args...)
static clib_error_t * socket_config(char *config, void *addr, socklen_t *addr_len, u32 ip4_default_address)
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 clib_error_t * default_socket_read(clib_socket_t *sock, int n_bytes)
#define clib_memcpy(a, b, c)
#define vec_delete(V, N, M)
Delete N elements starting at element M.
static word unix_error_is_fatal(word error)
#define SOCKET_ALLOW_GROUP_WRITE
void clib_socket_tx_add_formatted(clib_socket_t *s, char *fmt,...)
#define SOCKET_NON_BLOCKING_CONNECT
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define clib_unix_warning(format, args...)
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".