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> |
| 23 | #include <netdb.h> |
| 24 | |
| 25 | #define SOCKCLNT_SERVER_PORT 32741 /* whatever */ |
| 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 | |
| 39 | typedef VL_API_PACKED(struct _vl_api_sockclnt_create { |
| 40 | u16 _vl_msg_id; |
| 41 | u8 name[64]; |
| 42 | u32 context; |
| 43 | }) vl_api_sockclnt_create_t; |
| 44 | |
| 45 | typedef VL_API_PACKED(struct _vl_api_sockclnt_create_reply { |
| 46 | u16 _vl_msg_id; |
| 47 | i32 response; |
| 48 | u64 handle; |
| 49 | u32 index; |
| 50 | u32 context; |
| 51 | }) vl_api_sockclnt_create_reply_t; |
| 52 | |
| 53 | typedef VL_API_PACKED(struct _vl_api_sockclnt_delete { |
| 54 | u16 _vl_msg_id; |
| 55 | u32 index; |
| 56 | u64 handle; |
| 57 | }) vl_api_sockclnt_delete_t; |
| 58 | |
| 59 | typedef VL_API_PACKED(struct _vl_api_sockclnt_delete_reply { |
| 60 | u16 _vl_msg_id; |
| 61 | i32 response; |
| 62 | u64 handle; |
| 63 | }) vl_api_sockclnt_delete_reply_t; |
| 64 | |
| 65 | void error(char *msg) |
| 66 | { |
| 67 | perror(msg); |
| 68 | exit(0); |
| 69 | } |
| 70 | |
| 71 | int main(int argc, char *argv[]) |
| 72 | { |
| 73 | int sockfd, portno, n; |
| 74 | struct sockaddr_in serv_addr; |
| 75 | struct hostent *server; |
| 76 | char buffer[256]; |
| 77 | int i; |
| 78 | u32 nbytes; |
| 79 | vl_api_sockclnt_create_t *mp; |
| 80 | vl_api_sockclnt_create_reply_t *rp; |
| 81 | char *rdptr; |
| 82 | int total_bytes; |
| 83 | |
| 84 | for (i = 0; i < 1; i++) { |
| 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 | fprintf(stderr,"ERROR, no such host\n"); |
| 92 | exit(0); |
| 93 | } |
| 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, |
| 98 | 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"); |
| 102 | |
| 103 | memset(buffer, 0, sizeof(buffer)); |
| 104 | |
| 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"); |
| 119 | |
| 120 | memset(buffer, 0, sizeof (buffer)); |
| 121 | |
| 122 | total_bytes = 0; |
| 123 | rdptr = buffer; |
| 124 | do { |
| 125 | n = read(sockfd,rdptr,sizeof(buffer) - (rdptr - buffer)); |
| 126 | if (n < 0) |
| 127 | error("ERROR reading from socket"); |
| 128 | printf ("read %d bytes\n", n); |
| 129 | total_bytes += n; |
| 130 | rdptr += n; |
| 131 | } while (total_bytes < sizeof (vl_api_sockclnt_create_reply_t) + 4); |
| 132 | |
| 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)); |
| 137 | } |
| 138 | |
| 139 | printf ("response %d, handle 0x%llx, index %d, context 0x%x\n", |
| 140 | ntohl(rp->response), rp->handle, rp->index, rp->context); |
| 141 | close(sockfd); |
| 142 | } |
| 143 | return 0; |
| 144 | } |