Initial commit of vpp code.
[vpp.git] / vppinfra / vppinfra / test_socket.c
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) 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
41 static int verbose;
42 #define if_verbose(format,args...) \
43   if (verbose) { clib_warning(format, ## args); }
44
45 int test_socket_main (unformat_input_t * input)
46 {
47   clib_socket_t _s = {0}, * s = &_s;
48   char * config;
49   clib_error_t * error;
50
51   s->config = "localhost:22";
52   s->flags = SOCKET_IS_CLIENT;
53
54   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
55     {
56       if (unformat (input, "server %s %=", &config,
57                     &s->flags, SOCKET_IS_SERVER))
58         ;
59       else if (unformat (input, "client %s %=", &config,
60                          &s->flags, SOCKET_IS_CLIENT))
61         ;
62       else
63         {
64           error = clib_error_create ("unknown input `%U'\n",
65                                      format_unformat_error, input);
66           goto done;
67         }
68     }
69
70   error = clib_socket_init (s);
71   if (error)
72     goto done;
73
74   if (0)
75     {
76       struct { int a, b; } * msg;
77       msg = clib_socket_tx_add (s, sizeof (msg[0]));
78       msg->a = 99;
79       msg->b = 100;
80     }
81   else
82     clib_socket_tx_add_formatted (s, "hello there mr server %d\n", 99);
83
84   error = clib_socket_tx (s);
85   if (error)
86     goto done;
87
88   while (1)
89     {
90       error = clib_socket_rx (s, 100);
91       if (error)
92         break;
93
94       if (clib_socket_rx_end_of_file (s))
95         break;
96
97       if_verbose   ("%v", s->rx_buffer);
98       _vec_len (s->rx_buffer) = 0;
99     }
100
101   error = clib_socket_close (s);
102
103  done:
104   if (error)
105     clib_error_report (error);
106   return 0;
107 }
108
109 #ifdef CLIB_UNIX
110 int main (int argc, char * argv [])
111 {
112   unformat_input_t i;
113   int r;
114
115   verbose = (argc > 1);
116   unformat_init_command_line (&i, argv);
117   r = test_socket_main (&i);
118   unformat_free (&i);
119   return r;
120 }
121 #endif