blob: 0b05467af80c65b75f81242de64144959321d79f [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";
53 s->flags = SOCKET_IS_CLIENT;
54
55 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
56 {
57 if (unformat (input, "server %s %=", &config,
58 &s->flags, SOCKET_IS_SERVER))
59 ;
60 else if (unformat (input, "client %s %=", &config,
61 &s->flags, SOCKET_IS_CLIENT))
62 ;
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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 _vec_len (s->rx_buffer) = 0;
103 }
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
120 verbose = (argc > 1);
121 unformat_init_command_line (&i, argv);
122 r = test_socket_main (&i);
123 unformat_free (&i);
124 return r;
125}
126#endif
Dave Barachc3799992016-08-15 11:12:27 -0400127
128/*
129 * fd.io coding-style-patch-verification: ON
130 *
131 * Local Variables:
132 * eval: (c-set-style "gnu")
133 * End:
134 */