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 {
43 }) vl_api_sockclnt_create_t;
45 typedef VL_API_PACKED(struct _vl_api_sockclnt_create_reply {
51 }) vl_api_sockclnt_create_reply_t;
53 typedef VL_API_PACKED(struct _vl_api_sockclnt_delete {
57 }) vl_api_sockclnt_delete_t;
59 typedef VL_API_PACKED(struct _vl_api_sockclnt_delete_reply {
63 }) vl_api_sockclnt_delete_reply_t;
71 int main(int argc, char *argv[])
73 int sockfd, portno, n;
74 struct sockaddr_in serv_addr;
75 struct hostent *server;
79 vl_api_sockclnt_create_t *mp;
80 vl_api_sockclnt_create_reply_t *rp;
84 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");
91 fprintf(stderr,"ERROR, no such host\n");
94 bzero((char *) &serv_addr, sizeof(serv_addr));
95 serv_addr.sin_family = AF_INET;
96 bcopy((char *)server->h_addr,
97 (char *)&serv_addr.sin_addr.s_addr,
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));
125 n = read(sockfd,rdptr,sizeof(buffer) - (rdptr - buffer));
127 error("ERROR reading from socket");
128 printf ("read %d bytes\n", n);
131 } while (total_bytes < sizeof (vl_api_sockclnt_create_reply_t) + 4);
133 rp = (vl_api_sockclnt_create_reply_t *)(buffer + 4);
134 /* VL_API_SOCKCLNT_CREATE_REPLY */
135 if (ntohs(rp->_vl_msg_id) != 9) {
136 printf ("WARNING: msg id %d\n", ntohs(rp->_vl_msg_id));
139 printf ("response %d, handle 0x%llx, index %d, context 0x%x\n",
140 ntohl(rp->response), rp->handle, rp->index, rp->context);