39 #include <sys/types.h> 40 #include <sys/socket.h> 41 #include <netinet/in.h> 42 #include <arpa/inet.h> 68 for (port = IPPORT_USERRESERVED; port < 1 << 16; port++)
72 memset(&a, 0,
sizeof (a));
74 a.sin_family = PF_INET;
75 a.sin_addr.s_addr = INADDR_ANY;
76 a.sin_port = htons (port);
78 if (bind (sock, (
struct sockaddr *) &a,
sizeof (a)) >= 0)
82 return port < 1 << 16 ? port : -1;
91 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], host->h_length);
164 sa->sin_addr.s_addr = htonl (ip4_default_address);
191 tx_len =
vec_len (s->tx_buffer);
192 written = write (fd, s->tx_buffer, tx_len);
207 else if (written > 0)
209 if (written == tx_len)
210 _vec_len (s->tx_buffer) = 0;
217 else if (written == 0 && tx_len > 64*1024)
239 vec_add2 (sock->rx_buffer, buf, n_bytes);
241 if ((n_read = read (fd, buf, n_bytes)) < 0)
257 _vec_len (sock->rx_buffer) += n_read - n_bytes;
264 if (close (s->fd) < 0)
284 struct sockaddr_un su;
286 socklen_t addr_len = 0;
299 s->fd = socket (addr.sa.sa_family, SOCK_STREAM, 0);
307 if (addr.sa.sa_family == PF_INET)
308 port = ((
struct sockaddr_in *) &addr)->sin_port;
314 if (addr.sa.sa_family == PF_INET)
327 if (addr.sa.sa_family == PF_LOCAL)
328 unlink (((
struct sockaddr_un *) &addr)->sun_path);
333 if (setsockopt (s->fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof (v)) < 0)
338 && bind (s->fd, &addr.sa, addr_len) < 0)
344 if (listen (s->fd, 5) < 0)
353 && fcntl (s->fd, F_SETFL, O_NONBLOCK) < 0)
359 if (connect (s->fd, &addr.sa, addr_len) < 0
361 errno == EINPROGRESS))
381 memset (client, 0,
sizeof (client[0]));
384 client->fd = accept (server->fd, 0, 0);
389 if (fcntl (client->fd, F_SETFL, O_NONBLOCK) < 0)
396 len =
sizeof (client->peer);
397 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)
always_inline void clib_socket_tx_add_va_formatted(clib_socket_t *s, char *fmt, va_list *va)
struct _socket_t clib_socket_t
static word find_free_port(word sock)
#define clib_error_return_unix(e, args...)
always_inline word unix_error_is_fatal(word error)
static clib_error_t * default_socket_close(clib_socket_t *s)
#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 clib_unix_warning(format, args...)
#define vec_delete(V, N, M)
Delete N elements starting at element M.
#define SOCKET_NON_BLOCKING_CONNECT
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
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,...)
#define clib_error_return(e, args...)
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)