1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/*
* Copyright (c) 2018 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <vcl/vppcom.h>
#include <vcl/vcl_event.h>
#include <unistd.h>
char MESSAGE[] = "Hello, World!\n";
static const int PORT = 9995;
void
listener_cb (uint32_t new_session_index, vppcom_endpt_t *ep, void *stuff)
{
vppcom_session_write (new_session_index, &MESSAGE, sizeof (MESSAGE));
printf ("\n Heard from port: %d\n", ep->port);
}
typedef struct vppcomm_listener_main_
{
int new_fd;
struct event *event;
} vppcomm_listener_main_t;
vppcomm_listener_main_t _vlm_main;
vppcomm_listener_main_t *vlm = &_vlm_main;
int
main (int argc, char **argv)
{
int rv;
struct sockaddr_in sin;
uint32_t listen_fd;
vppcom_endpt_t endpt;
clib_mem_init_thread_safe (0, 64<<20);
//Address stuff
memset (&sin, 0, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (PORT);
//sin.sin_addr.s_addr = inet_addr("127.0.0.1");
endpt.is_ip4 = (sin.sin_family == AF_INET);
endpt.ip = (uint8_t *) & sin.sin_addr;
endpt.port = (uint16_t) sin.sin_port;
//VCL stuff
rv = vppcom_app_create ("test_vcl_listener_server");
if (rv) return rv;
listen_fd = vppcom_session_create (VPPCOM_PROTO_TCP,
0 /* is_nonblocking */ );
rv = vppcom_session_bind (listen_fd, &endpt);
//Make a listener and dispatch
rv = vppcom_session_register_listener (listen_fd, listener_cb, 0,
0, 0, &MESSAGE);
if (rv)
{
fprintf (stderr, "Could not create a listener!\n");
return 1;
}
while (1)
{
sleep (3);
}
printf ("done\n");
return 0;
}
|