2 * Copyright (c) 2017 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
22 #include <vppinfra/format.h>
24 #include <sys/ucontext.h>
26 volatile int signal_received;
29 unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
35 setup_signal_handler (void)
40 for (i = 1; i < 32; i++)
42 memset (&sa, 0, sizeof (sa));
43 sa.sa_sigaction = (void *) unix_signal_handler;
44 sa.sa_flags = SA_SIGINFO;
48 /* these signals take the default action */
56 /* ignore SIGPIPE, SIGCHLD */
59 sa.sa_sigaction = (void *) SIG_IGN;
62 /* catch and handle all other signals */
67 if (sigaction (i, &sa, 0) < 0)
68 clib_unix_warning ("sigaction %U", format_signal, i);
74 main (int argc, char *argv[])
76 int sockfd, portno, n, sent, accfd, reuse;
77 socklen_t client_addr_len;
78 struct sockaddr_in serv_addr;
79 struct sockaddr_in client;
80 struct hostent *server;
83 if (argc > 1 && argc < 3)
85 fformat (stderr, "usage %s host port\n", argv[0]);
91 portno = atoi (argv[2]);
92 server = gethostbyname (argv[1]);
95 clib_unix_warning ("gethostbyname");
103 server = gethostbyname ("6.0.1.1");
106 clib_unix_warning ("gethostbyname");
112 setup_signal_handler ();
114 sockfd = socket (AF_INET, SOCK_STREAM, 0);
117 clib_unix_error ("socket");
122 if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuse,
125 clib_unix_error ("setsockopt(SO_REUSEADDR) failed");
129 bzero ((char *) &serv_addr, sizeof (serv_addr));
130 serv_addr.sin_family = AF_INET;
131 bcopy ((char *) server->h_addr,
132 (char *) &serv_addr.sin_addr.s_addr, server->h_length);
133 serv_addr.sin_port = htons (portno);
134 if (bind (sockfd, (const void *) &serv_addr, sizeof (serv_addr)) < 0)
136 clib_unix_warning ("bind");
140 vec_validate (rx_buffer, 8999 /* jumbo mtu */ );
142 if (listen (sockfd, 5 /* backlog */ ) < 0)
144 clib_unix_warning ("listen");
154 client_addr_len = sizeof (struct sockaddr);
155 accfd = accept (sockfd, (struct sockaddr *) &client, &client_addr_len);
158 clib_unix_warning ("accept");
161 fformat (stderr, "Accepted connection from: %s : %d\n",
162 inet_ntoa (client.sin_addr), client.sin_port);
165 n = recv (accfd, rx_buffer, vec_len (rx_buffer), 0 /* flags */ );
174 clib_unix_warning ("recv");
182 sent = send (accfd, rx_buffer, n, 0 /* flags */ );
185 clib_unix_warning ("send");
192 clib_warning ("sent %d not %d", sent, n);
207 * fd.io coding-style-patch-verification: ON
210 * eval: (c-set-style "gnu")