blob: 3a0e6b29ce66eea2e3b040918b4244d8061f91c5 [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) 2005 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#include <vppinfra/format.h>
39#include <vppinfra/socket.h>
40
41static int verbose;
42#define if_verbose(format,args...) \
43 if (verbose) { clib_warning(format, ## args); }
44
Dave Barachc3799992016-08-15 11:12:27 -040045int
46test_socket_main (unformat_input_t * input)
Ed Warnickecb9cada2015-12-08 15:45:58 -070047{
Dave Barachc3799992016-08-15 11:12:27 -040048 clib_socket_t _s = { 0 }, *s = &_s;
49 char *config;
50 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
52 s->config = "localhost:22";
Damjan Marionf49921f2017-09-11 16:52:11 +020053 s->flags = CLIB_SOCKET_F_IS_CLIENT;
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
55 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
56 {
57 if (unformat (input, "server %s %=", &config,
Damjan Marionf49921f2017-09-11 16:52:11 +020058 &s->flags, CLIB_SOCKET_F_IS_SERVER))
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 ;
60 else if (unformat (input, "client %s %=", &config,
Damjan Marionf49921f2017-09-11 16:52:11 +020061 &s->flags, CLIB_SOCKET_F_IS_CLIENT))
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 ;
63 else
64 {
65 error = clib_error_create ("unknown input `%U'\n",
66 format_unformat_error, input);
67 goto done;
68 }
69 }
70
71 error = clib_socket_init (s);
72 if (error)
73 goto done;
74
75 if (0)
76 {
Dave Barachc3799992016-08-15 11:12:27 -040077 struct
78 {
79 int a, b;
80 } *msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 msg = clib_socket_tx_add (s, sizeof (msg[0]));
82 msg->a = 99;
83 msg->b = 100;
84 }
85 else
86 clib_socket_tx_add_formatted (s, "hello there mr server %d\n", 99);
87
88 error = clib_socket_tx (s);
89 if (error)
90 goto done;
91
92 while (1)
93 {
94 error = clib_socket_rx (s, 100);
95 if (error)
96 break;
97
98 if (clib_socket_rx_end_of_file (s))
99 break;
100
Dave Barachc3799992016-08-15 11:12:27 -0400101 if_verbose ("%v", s->rx_buffer);
Damjan Marion8bea5892022-04-04 22:40:45 +0200102 vec_set_len (s->rx_buffer, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 }
104
105 error = clib_socket_close (s);
106
Dave Barachc3799992016-08-15 11:12:27 -0400107done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 if (error)
109 clib_error_report (error);
110 return 0;
111}
112
113#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400114int
115main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116{
117 unformat_input_t i;
118 int r;
119
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200120 clib_mem_init (0, 64ULL << 20);
121
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 verbose = (argc > 1);
123 unformat_init_command_line (&i, argv);
124 r = test_socket_main (&i);
125 unformat_free (&i);
126 return r;
127}
128#endif
Dave Barachc3799992016-08-15 11:12:27 -0400129
130/*
131 * fd.io coding-style-patch-verification: ON
132 *
133 * Local Variables:
134 * eval: (c-set-style "gnu")
135 * End:
136 */