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:
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.
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
25 #define SOCKCLNT_SERVER_PORT 32741 /* whatever */
27 typedef signed char i8;
28 typedef signed short i16;
29 typedef signed int i32;
30 typedef signed long long i64;
31 typedef unsigned char u8;
32 typedef unsigned short u16;
33 typedef unsigned int u32;
34 typedef unsigned long long u64;
35 typedef unsigned long uword;
37 #define VL_API_PACKED(x) x __attribute__ ((packed))
39 typedef VL_API_PACKED (struct _vl_api_sockclnt_create
41 u16 _vl_msg_id; u8 name[64];
43 }) vl_api_sockclnt_create_t;
45 typedef VL_API_PACKED (struct _vl_api_sockclnt_create_reply
48 i32 response; u64 handle; u32 index; u32 context;
49 }) vl_api_sockclnt_create_reply_t;
51 typedef VL_API_PACKED (struct _vl_api_sockclnt_delete
53 u16 _vl_msg_id; u32 index;
55 }) vl_api_sockclnt_delete_t;
57 typedef VL_API_PACKED (struct _vl_api_sockclnt_delete_reply
59 u16 _vl_msg_id; i32 response; u64 handle;
60 }) vl_api_sockclnt_delete_reply_t;
70 main (int argc, char *argv[])
72 int sockfd, portno, n;
73 struct sockaddr_in serv_addr;
74 struct hostent *server;
78 vl_api_sockclnt_create_t *mp;
79 vl_api_sockclnt_create_reply_t *rp;
83 for (i = 0; i < 1; i++)
85 portno = SOCKCLNT_SERVER_PORT;
86 sockfd = socket (AF_INET, SOCK_STREAM, 0);
88 error ("ERROR opening socket");
89 server = gethostbyname ("localhost");
92 fprintf (stderr, "ERROR, no such host\n");
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");
103 memset (buffer, 0, sizeof (buffer));
105 mp = (vl_api_sockclnt_create_t *) buffer;
106 mp->_vl_msg_id = ntohs (8); /* VL_API_SOCKCLNT_CREATE */
107 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));
115 error ("ERROR writing len to socket");
116 n = write (sockfd, mp, sizeof (*mp));
118 error ("ERROR writing msg to socket");
120 memset (buffer, 0, sizeof (buffer));
126 n = read (sockfd, rdptr, sizeof (buffer) - (rdptr - buffer));
128 error ("ERROR reading from socket");
129 printf ("read %d bytes\n", n);
133 while (total_bytes < sizeof (vl_api_sockclnt_create_reply_t) + 4);
135 rp = (vl_api_sockclnt_create_reply_t *) (buffer + 4);
136 /* VL_API_SOCKCLNT_CREATE_REPLY */
137 if (ntohs (rp->_vl_msg_id) != 9)
139 printf ("WARNING: msg id %d\n", ntohs (rp->_vl_msg_id));
142 printf ("response %d, handle 0x%llx, index %d, context 0x%x\n",
143 ntohl (rp->response), rp->handle, rp->index, rp->context);
150 * fd.io coding-style-patch-verification: ON
153 * eval: (c-set-style "gnu")