VCL API for external callback for listener/connect event
[vpp.git] / src / vcl / test_vcl_listener_server.c
1 /*
2  * Copyright (c) 2018 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
17 #include <string.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <signal.h>
21 #include <stdlib.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <sys/socket.h>
25
26
27 #include <vcl/vppcom.h>
28 #include <unistd.h>
29
30 char MESSAGE[] = "Hello, World!\n";
31
32 static const int PORT = 9995;
33
34 void
35 listener_cb (uint32_t new_session_index, vppcom_endpt_t *ep, void *stuff)
36 {
37
38   vppcom_session_write (new_session_index, &MESSAGE, sizeof (MESSAGE));
39   printf ("\n Heard from port: %d\n", ep->port);
40 }
41
42
43 typedef struct vppcomm_listener_main_
44 {
45   int new_fd;
46
47   struct event *event;
48
49 } vppcomm_listener_main_t;
50
51 vppcomm_listener_main_t _vlm_main;
52 vppcomm_listener_main_t *vlm = &_vlm_main;
53
54
55 int
56 main (int argc, char **argv)
57 {
58
59   int rv;
60   struct sockaddr_in sin;
61   uint32_t listen_fd;
62   vppcom_endpt_t endpt;
63
64   //Address stuff
65   memset (&sin, 0, sizeof (sin));
66   sin.sin_family = AF_INET;
67   sin.sin_port = htons (PORT);
68   //sin.sin_addr.s_addr = inet_addr("127.0.0.1");
69
70   endpt.is_ip4 = (sin.sin_family == AF_INET);
71   endpt.ip = (uint8_t *) & sin.sin_addr;
72   endpt.port = (uint16_t) sin.sin_port;
73
74   //VCL stuff
75   rv = vppcom_app_create ("test_vcl_listener_server");
76   if (rv) return rv;
77
78   listen_fd = vppcom_session_create (VPPCOM_PROTO_TCP,
79                                           0 /* is_nonblocking */ );
80
81   rv = vppcom_session_bind (listen_fd, &endpt);
82
83   //Make a listener and dispatch
84   rv = vppcom_session_register_listener (listen_fd, listener_cb, 0,
85                                             0, 0, &MESSAGE);
86
87   if (rv)
88     {
89       fprintf (stderr, "Could not create a listener!\n");
90       return 1;
91     }
92
93   while (1)
94     {
95       sleep (3);
96     }
97
98   printf ("done\n");
99   return 0;
100 }
101
102