| 1 | /* |
| 2 | Copyright (c) 2012, Broadcom Europe Ltd |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the copyright holder nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <limits.h> |
| 30 | #include <string.h> |
| 31 | #include <stdio.h> |
| 32 | |
| 33 | #include "containers/net/net_sockets.h" |
| 34 | |
| 35 | #include "nb_io.h" |
| 36 | |
| 37 | #define MAX_BUFFER_LEN 1024 |
| 38 | /* Time in milliseconds to yield when nothing is happening */ |
| 39 | #define YIELD_PERIOD_MS 33 |
| 40 | |
| 41 | |
| 42 | static vc_container_net_status_t local_net_control(VC_CONTAINER_NET_T *sock, |
| 43 | vc_container_net_control_t operation, ...) |
| 44 | { |
| 45 | vc_container_net_status_t result; |
| 46 | va_list args; |
| 47 | |
| 48 | va_start(args, operation); |
| 49 | result = vc_container_net_control(sock, operation, args); |
| 50 | va_end(args); |
| 51 | |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | int main(int argc, char **argv) |
| 56 | { |
| 57 | VC_CONTAINER_NET_T *sock; |
| 58 | vc_container_net_status_t status; |
| 59 | char send_buffer[MAX_BUFFER_LEN]; |
| 60 | char recv_buffer[MAX_BUFFER_LEN]; |
| 61 | int ready = 1; |
| 62 | int to_send = 0; |
| 63 | size_t received; |
| 64 | |
| 65 | if (argc < 3) |
| 66 | { |
| 67 | printf("Usage:\n%s <address> <port>\n" , argv[0]); |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | sock = vc_container_net_open(argv[1], argv[2], VC_CONTAINER_NET_OPEN_FLAG_STREAM, &status); |
| 72 | if (!sock) |
| 73 | { |
| 74 | printf("vc_container_net_open failed: %d\n" , status); |
| 75 | return 2; |
| 76 | } |
| 77 | |
| 78 | /* Use non-blocking I/O for both network and console */ |
| 79 | local_net_control(sock, VC_CONTAINER_NET_CONTROL_SET_READ_TIMEOUT_MS, YIELD_PERIOD_MS); |
| 80 | nb_set_nonblocking_input(1); |
| 81 | |
| 82 | while (ready) |
| 83 | { |
| 84 | if (nb_char_available()) |
| 85 | { |
| 86 | char c = nb_get_char(); |
| 87 | |
| 88 | if (c == 26) /* CTRL+Z */ |
| 89 | break; |
| 90 | |
| 91 | send_buffer[to_send++] = c; |
| 92 | |
| 93 | if (c == '\r') /* Translate CR into CRLF */ |
| 94 | { |
| 95 | c = '\n'; |
| 96 | nb_put_char(c); |
| 97 | send_buffer[to_send++] = c; |
| 98 | } |
| 99 | |
| 100 | if (c == '\n' || to_send == sizeof(send_buffer) - 1) /* Allow for next character needing translation */ |
| 101 | { |
| 102 | int already_sent = 0, sent; |
| 103 | |
| 104 | /* Send a line at a time */ |
| 105 | while (to_send) |
| 106 | { |
| 107 | sent = vc_container_net_write(sock, send_buffer + already_sent, to_send); |
| 108 | if (!sent) |
| 109 | { |
| 110 | printf("vc_container_net_write failed: %d\n" , vc_container_net_status(sock)); |
| 111 | to_send = 0; |
| 112 | ready = 0; |
| 113 | break; |
| 114 | } |
| 115 | to_send -= sent; |
| 116 | already_sent += sent; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* Read back and print any data from the server */ |
| 122 | received = vc_container_net_read(sock, recv_buffer, sizeof(recv_buffer) - 1); |
| 123 | if (received) |
| 124 | { |
| 125 | char *ptr = recv_buffer; |
| 126 | |
| 127 | while (received--) |
| 128 | nb_put_char(*ptr++); |
| 129 | } else { |
| 130 | vc_container_net_status_t net_status = vc_container_net_status(sock); |
| 131 | |
| 132 | if (net_status != VC_CONTAINER_NET_ERROR_TIMED_OUT && net_status != VC_CONTAINER_NET_SUCCESS) |
| 133 | { |
| 134 | printf("vc_container_net_read failed: %d\n" , net_status); |
| 135 | ready = 0; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | nb_set_nonblocking_input(0); |
| 141 | |
| 142 | vc_container_net_close(sock); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |