blob: 08e22e7eb61a0560985ce2b0eddc75225ea88548 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Dave Barachc3799992016-08-15 11:12:27 -040049typedef struct _socket_t
50{
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 /* File descriptor. */
52 i32 fd;
53
54 /* Config string for socket HOST:PORT or just HOST. */
Dave Barachc3799992016-08-15 11:12:27 -040055 char *config;
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
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
62 /* Read returned end-of-file. */
63#define SOCKET_RX_END_OF_FILE (1 << 2)
64
65 /* Transmit buffer. Holds data waiting to be written. */
Dave Barachc3799992016-08-15 11:12:27 -040066 u8 *tx_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -070067
68 /* Receive buffer. Holds data read from socket. */
Dave Barachc3799992016-08-15 11:12:27 -040069 u8 *rx_buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
71 /* Peer socket we are connected to. */
72 struct sockaddr_in peer;
73
Dave Barachc3799992016-08-15 11:12:27 -040074 clib_error_t *(*write_func) (struct _socket_t * sock);
75 clib_error_t *(*read_func) (struct _socket_t * sock, int min_bytes);
76 clib_error_t *(*close_func) (struct _socket_t * sock);
77 void *private_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078} clib_socket_t;
79
80/* socket config format is host:port.
81 Unspecified port causes a free one to be chosen starting
82 from IPPORT_USERRESERVED (5000). */
Dave Barachc3799992016-08-15 11:12:27 -040083clib_error_t *clib_socket_init (clib_socket_t * socket);
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
Dave Barachc3799992016-08-15 11:12:27 -040085clib_error_t *clib_socket_accept (clib_socket_t * server,
86 clib_socket_t * client);
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
Dave Barachc3799992016-08-15 11:12:27 -040088always_inline uword
89clib_socket_is_server (clib_socket_t * sock)
90{
91 return (sock->flags & SOCKET_IS_SERVER) != 0;
92}
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Dave Barachc3799992016-08-15 11:12:27 -040094always_inline uword
95clib_socket_is_client (clib_socket_t * s)
96{
97 return !clib_socket_is_server (s);
98}
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
Dave Barachc3799992016-08-15 11:12:27 -0400100always_inline int
101clib_socket_rx_end_of_file (clib_socket_t * s)
102{
103 return s->flags & SOCKET_RX_END_OF_FILE;
104}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105
106always_inline void *
107clib_socket_tx_add (clib_socket_t * s, int n_bytes)
108{
Dave Barachc3799992016-08-15 11:12:27 -0400109 u8 *result;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 vec_add2 (s->tx_buffer, result, n_bytes);
111 return result;
112}
113
114always_inline void
Dave Barachc3799992016-08-15 11:12:27 -0400115clib_socket_tx_add_va_formatted (clib_socket_t * s, char *fmt, va_list * va)
116{
117 s->tx_buffer = va_format (s->tx_buffer, fmt, va);
118}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119
120always_inline clib_error_t *
121clib_socket_tx (clib_socket_t * s)
Dave Barachc3799992016-08-15 11:12:27 -0400122{
123 return s->write_func (s);
124}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
126always_inline clib_error_t *
127clib_socket_rx (clib_socket_t * s, int n_bytes)
Dave Barachc3799992016-08-15 11:12:27 -0400128{
129 return s->read_func (s, n_bytes);
130}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
132always_inline void
Dave Barachc3799992016-08-15 11:12:27 -0400133clib_socket_free (clib_socket_t * s)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134{
135 vec_free (s->tx_buffer);
136 vec_free (s->rx_buffer);
137 if (clib_mem_is_heap_object (s->config))
138 vec_free (s->config);
139 memset (s, 0, sizeof (s[0]));
140}
141
142always_inline clib_error_t *
Dave Barachc3799992016-08-15 11:12:27 -0400143clib_socket_close (clib_socket_t * sock)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144{
Dave Barachc3799992016-08-15 11:12:27 -0400145 clib_error_t *err;
146 err = (*sock->close_func) (sock);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 return err;
148}
149
Dave Barachc3799992016-08-15 11:12:27 -0400150void clib_socket_tx_add_formatted (clib_socket_t * s, char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151
152#endif /* _clib_included_socket_h */
Dave Barachc3799992016-08-15 11:12:27 -0400153
154/*
155 * fd.io coding-style-patch-verification: ON
156 *
157 * Local Variables:
158 * eval: (c-set-style "gnu")
159 * End:
160 */