FD.io VPP  v17.07-30-g839fa73
Vector Packet Processing
socket.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
17 
18  Permission is hereby granted, free of charge, to any person obtaining
19  a copy of this software and associated documentation files (the
20  "Software"), to deal in the Software without restriction, including
21  without limitation the rights to use, copy, modify, merge, publish,
22  distribute, sublicense, and/or sell copies of the Software, and to
23  permit persons to whom the Software is furnished to do so, subject to
24  the following conditions:
25 
26  The above copyright notice and this permission notice shall be
27  included in all copies or substantial portions of the Software.
28 
29  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37 
38 #ifndef _clib_included_socket_h
39 #define _clib_included_socket_h
40 
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 
45 #include <vppinfra/clib.h>
46 #include <vppinfra/error.h>
47 #include <vppinfra/format.h>
48 
49 typedef struct _socket_t
50 {
51  /* File descriptor. */
52  i32 fd;
53 
54  /* Config string for socket HOST:PORT or just HOST. */
55  char *config;
56 
57  u32 flags;
58 #define SOCKET_IS_SERVER (1 << 0)
59 #define SOCKET_IS_CLIENT (0 << 0)
60 #define SOCKET_NON_BLOCKING_CONNECT (1 << 1)
61 #define SOCKET_ALLOW_GROUP_WRITE (1 << 2)
62 
63  /* Read returned end-of-file. */
64 #define SOCKET_RX_END_OF_FILE (1 << 2)
65 
66  /* Transmit buffer. Holds data waiting to be written. */
67  u8 *tx_buffer;
68 
69  /* Receive buffer. Holds data read from socket. */
70  u8 *rx_buffer;
71 
72  /* Peer socket we are connected to. */
73  struct sockaddr_in peer;
74 
75  clib_error_t *(*write_func) (struct _socket_t * sock);
76  clib_error_t *(*read_func) (struct _socket_t * sock, int min_bytes);
77  clib_error_t *(*close_func) (struct _socket_t * sock);
78  void *private_data;
80 
81 /* socket config format is host:port.
82  Unspecified port causes a free one to be chosen starting
83  from IPPORT_USERRESERVED (5000). */
85 
87  clib_socket_t * client);
88 
91 {
92  return (sock->flags & SOCKET_IS_SERVER) != 0;
93 }
94 
97 {
98  return !clib_socket_is_server (s);
99 }
100 
101 always_inline int
103 {
104  return s->flags & SOCKET_RX_END_OF_FILE;
105 }
106 
107 always_inline void *
109 {
110  u8 *result;
111  vec_add2 (s->tx_buffer, result, n_bytes);
112  return result;
113 }
114 
115 always_inline void
116 clib_socket_tx_add_va_formatted (clib_socket_t * s, char *fmt, va_list * va)
117 {
118  s->tx_buffer = va_format (s->tx_buffer, fmt, va);
119 }
120 
123 {
124  return s->write_func (s);
125 }
126 
128 clib_socket_rx (clib_socket_t * s, int n_bytes)
129 {
130  return s->read_func (s, n_bytes);
131 }
132 
133 always_inline void
135 {
136  vec_free (s->tx_buffer);
137  vec_free (s->rx_buffer);
138  if (clib_mem_is_heap_object (s->config))
139  vec_free (s->config);
140  memset (s, 0, sizeof (s[0]));
141 }
142 
145 {
146  clib_error_t *err;
147  err = (*sock->close_func) (sock);
148  return err;
149 }
150 
151 void clib_socket_tx_add_formatted (clib_socket_t * s, char *fmt, ...);
152 
153 #endif /* _clib_included_socket_h */
154 
155 /*
156  * fd.io coding-style-patch-verification: ON
157  *
158  * Local Variables:
159  * eval: (c-set-style "gnu")
160  * End:
161  */
static uword clib_socket_is_server(clib_socket_t *sock)
Definition: socket.h:90
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:561
static clib_error_t * clib_socket_close(clib_socket_t *sock)
Definition: socket.h:144
u8 * va_format(u8 *s, const char *fmt, va_list *va)
Definition: format.c:387
#define SOCKET_RX_END_OF_FILE
Definition: socket.h:64
static clib_error_t * clib_socket_rx(clib_socket_t *s, int n_bytes)
Definition: socket.h:128
#define SOCKET_IS_SERVER
Definition: socket.h:58
static clib_error_t * tx_buffer(void *transport, mc_transport_type_t type, u32 buffer_index)
Definition: mc_socket.c:129
struct _socket_t clib_socket_t
#define always_inline
Definition: clib.h:84
int i32
Definition: types.h:81
static void clib_socket_free(clib_socket_t *s)
Definition: socket.h:134
static clib_error_t * clib_socket_tx(clib_socket_t *s)
Definition: socket.h:122
static void clib_socket_tx_add_va_formatted(clib_socket_t *s, char *fmt, va_list *va)
Definition: socket.h:116
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
clib_error_t * clib_socket_accept(clib_socket_t *server, clib_socket_t *client)
Definition: socket.c:388
static uword clib_socket_is_client(clib_socket_t *s)
Definition: socket.h:96
unsigned int u32
Definition: types.h:88
static uword clib_mem_is_heap_object(void *p)
Definition: mem.h:159
u64 uword
Definition: types.h:112
unsigned char u8
Definition: types.h:56
clib_error_t * clib_socket_init(clib_socket_t *socket)
Definition: socket.c:284
static int clib_socket_rx_end_of_file(clib_socket_t *s)
Definition: socket.h:102
static void * clib_socket_tx_add(clib_socket_t *s, int n_bytes)
Definition: socket.h:108
u32 flags
Definition: vhost-user.h:76
void clib_socket_tx_add_formatted(clib_socket_t *s, char *fmt,...)
Definition: socket.c:57