blob: e563b9a8d6162f20b7047777820db7e18a99f549 [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#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <strings.h>
19#include <unistd.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
Dave Barach371e4e12016-07-08 09:38:52 -040023#include <netdb.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070024
Dave Barach59b25652017-09-10 15:04:27 -040025#define SOCKCLNT_SERVER_PORT 1234 /* whatever */
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
27typedef signed char i8;
28typedef signed short i16;
29typedef signed int i32;
30typedef signed long long i64;
31typedef unsigned char u8;
32typedef unsigned short u16;
33typedef unsigned int u32;
34typedef unsigned long long u64;
35typedef unsigned long uword;
36
37#define VL_API_PACKED(x) x __attribute__ ((packed))
38
Dave Barach371e4e12016-07-08 09:38:52 -040039typedef VL_API_PACKED (struct _vl_api_sockclnt_create
40 {
41 u16 _vl_msg_id; u8 name[64];
42 u32 context;
43 }) vl_api_sockclnt_create_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070044
Dave Barach371e4e12016-07-08 09:38:52 -040045typedef VL_API_PACKED (struct _vl_api_sockclnt_create_reply
46 {
47 u16 _vl_msg_id;
48 i32 response; u64 handle; u32 index; u32 context;
49 }) vl_api_sockclnt_create_reply_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
Dave Barach371e4e12016-07-08 09:38:52 -040051typedef VL_API_PACKED (struct _vl_api_sockclnt_delete
52 {
53 u16 _vl_msg_id; u32 index;
54 u64 handle;
55 }) vl_api_sockclnt_delete_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
Dave Barach371e4e12016-07-08 09:38:52 -040057typedef VL_API_PACKED (struct _vl_api_sockclnt_delete_reply
58 {
59 u16 _vl_msg_id; i32 response; u64 handle;
60 }) vl_api_sockclnt_delete_reply_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -070061
Dave Barach371e4e12016-07-08 09:38:52 -040062void
63error (char *msg)
Ed Warnickecb9cada2015-12-08 15:45:58 -070064{
Dave Barach371e4e12016-07-08 09:38:52 -040065 perror (msg);
66 exit (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070067}
68
Dave Barach371e4e12016-07-08 09:38:52 -040069int
70main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -070071{
Dave Barach371e4e12016-07-08 09:38:52 -040072 int sockfd, portno, n;
73 struct sockaddr_in serv_addr;
74 struct hostent *server;
75 char buffer[256];
76 int i;
77 u32 nbytes;
78 vl_api_sockclnt_create_t *mp;
79 vl_api_sockclnt_create_reply_t *rp;
80 char *rdptr;
81 int total_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
Dave Barach371e4e12016-07-08 09:38:52 -040083 for (i = 0; i < 1; i++)
84 {
85 portno = SOCKCLNT_SERVER_PORT;
86 sockfd = socket (AF_INET, SOCK_STREAM, 0);
87 if (sockfd < 0)
88 error ("ERROR opening socket");
89 server = gethostbyname ("localhost");
90 if (server == NULL)
91 {
92 fprintf (stderr, "ERROR, no such host\n");
93 exit (0);
94 }
95 bzero ((char *) &serv_addr, sizeof (serv_addr));
96 serv_addr.sin_family = AF_INET;
97 bcopy ((char *) server->h_addr,
98 (char *) &serv_addr.sin_addr.s_addr, server->h_length);
99 serv_addr.sin_port = htons (portno);
100 if (connect (sockfd, (const void *) &serv_addr, sizeof (serv_addr)) < 0)
101 error ("ERROR connecting");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
Dave Barach371e4e12016-07-08 09:38:52 -0400103 memset (buffer, 0, sizeof (buffer));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
Dave Barach371e4e12016-07-08 09:38:52 -0400105 mp = (vl_api_sockclnt_create_t *) buffer;
Dave Barach59b25652017-09-10 15:04:27 -0400106 mp->_vl_msg_id = ntohs (13); /* VL_API_SOCKCLNT_CREATE */
Dave Barach371e4e12016-07-08 09:38:52 -0400107 strncpy ((char *) mp->name, "socket-test", sizeof (mp->name) - 1);
108 mp->name[sizeof (mp->name) - 1] = 0;
109 mp->context = 0xfeedface;
110 /* length of the message, including the length itself */
111 nbytes = sizeof (*mp) + sizeof (nbytes);
112 nbytes = ntohl (nbytes);
113 n = write (sockfd, &nbytes, sizeof (nbytes));
114 if (n < 0)
115 error ("ERROR writing len to socket");
116 n = write (sockfd, mp, sizeof (*mp));
117 if (n < 0)
118 error ("ERROR writing msg to socket");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119
Dave Barach371e4e12016-07-08 09:38:52 -0400120 memset (buffer, 0, sizeof (buffer));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
Dave Barach371e4e12016-07-08 09:38:52 -0400122 total_bytes = 0;
123 rdptr = buffer;
124 do
125 {
126 n = read (sockfd, rdptr, sizeof (buffer) - (rdptr - buffer));
127 if (n < 0)
128 error ("ERROR reading from socket");
129 printf ("read %d bytes\n", n);
130 total_bytes += n;
131 rdptr += n;
132 }
133 while (total_bytes < sizeof (vl_api_sockclnt_create_reply_t) + 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
Dave Barach371e4e12016-07-08 09:38:52 -0400135 rp = (vl_api_sockclnt_create_reply_t *) (buffer + 4);
136 /* VL_API_SOCKCLNT_CREATE_REPLY */
Dave Barach59b25652017-09-10 15:04:27 -0400137 if (ntohs (rp->_vl_msg_id) != 14)
Dave Barach371e4e12016-07-08 09:38:52 -0400138 {
139 printf ("WARNING: msg id %d\n", ntohs (rp->_vl_msg_id));
140 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
Dave Barach371e4e12016-07-08 09:38:52 -0400142 printf ("response %d, handle 0x%llx, index %d, context 0x%x\n",
143 ntohl (rp->response), rp->handle, rp->index, rp->context);
144 close (sockfd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 }
Dave Barach371e4e12016-07-08 09:38:52 -0400146 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147}
Dave Barach371e4e12016-07-08 09:38:52 -0400148
149/*
150 * fd.io coding-style-patch-verification: ON
151 *
152 * Local Variables:
153 * eval: (c-set-style "gnu")
154 * End:
155 */