Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 23 | #include <netdb.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 25 | #define SOCKCLNT_SERVER_PORT 32741 /* whatever */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | |
| 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; |
| 36 | |
| 37 | #define VL_API_PACKED(x) x __attribute__ ((packed)) |
| 38 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 39 | typedef 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 45 | typedef 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 51 | typedef 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 57 | typedef 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 62 | void |
| 63 | error (char *msg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 65 | perror (msg); |
| 66 | exit (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 69 | int |
| 70 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 72 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 83 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 103 | memset (buffer, 0, sizeof (buffer)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 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)); |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 120 | memset (buffer, 0, sizeof (buffer)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 122 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 135 | rp = (vl_api_sockclnt_create_reply_t *) (buffer + 4); |
| 136 | /* VL_API_SOCKCLNT_CREATE_REPLY */ |
| 137 | if (ntohs (rp->_vl_msg_id) != 9) |
| 138 | { |
| 139 | printf ("WARNING: msg id %d\n", ntohs (rp->_vl_msg_id)); |
| 140 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 142 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 146 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * fd.io coding-style-patch-verification: ON |
| 151 | * |
| 152 | * Local Variables: |
| 153 | * eval: (c-set-style "gnu") |
| 154 | * End: |
| 155 | */ |