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 | #include <ctype.h> |
33 | |
34 | #include "containers/net/net_sockets.h" |
35 | |
36 | #define MAX_BUFFER_LEN 1024 |
37 | #define MAX_NAME_LEN 256 |
38 | #define MAX_PORT_LEN 32 |
39 | |
40 | int main(int argc, char **argv) |
41 | { |
42 | VC_CONTAINER_NET_T *server_sock, *sock; |
43 | vc_container_net_status_t status; |
44 | char buffer[MAX_BUFFER_LEN]; |
45 | char name[MAX_NAME_LEN]; |
46 | unsigned short port; |
47 | int ii, connections = 1; |
48 | size_t received; |
49 | |
50 | if (argc < 2) |
51 | { |
52 | printf("Usage:\n%s <port> [<connections>]\n" , argv[0]); |
53 | return 1; |
54 | } |
55 | |
56 | server_sock = vc_container_net_open(NULL, argv[1], VC_CONTAINER_NET_OPEN_FLAG_STREAM, &status); |
57 | if (!server_sock) |
58 | { |
59 | printf("vc_container_net_open failed: %d\n" , status); |
60 | return 2; |
61 | } |
62 | |
63 | if (argc > 2) |
64 | { |
65 | sscanf(argv[2], "%d" , &connections); |
66 | } |
67 | |
68 | status = vc_container_net_listen(server_sock, connections); |
69 | if (status != VC_CONTAINER_NET_SUCCESS) |
70 | { |
71 | printf("vc_container_net_listen failed: %d\n" , status); |
72 | vc_container_net_close(server_sock); |
73 | return 3; |
74 | } |
75 | |
76 | for (ii = 0; ii < connections; ii++) |
77 | { |
78 | status = vc_container_net_accept(server_sock, &sock); |
79 | if (status != VC_CONTAINER_NET_SUCCESS) |
80 | { |
81 | printf("vc_container_net_accept failed: %d\n" , status); |
82 | vc_container_net_close(server_sock); |
83 | return 3; |
84 | } |
85 | |
86 | strcpy(name, "<unknown>" ); |
87 | vc_container_net_get_client_name(sock, name, sizeof(name)); |
88 | vc_container_net_get_client_port(sock, &port); |
89 | printf("Connection from %s:%hu\n" , name, port); |
90 | |
91 | while ((received = vc_container_net_read(sock, buffer, sizeof(buffer) - 1)) != 0) |
92 | { |
93 | char *ptr = buffer; |
94 | size_t jj; |
95 | |
96 | printf("Rx:" ); |
97 | |
98 | /* Flip case and echo data back to client */ |
99 | for (jj = 0; jj < received; jj++, ptr++) |
100 | { |
101 | char c = *ptr; |
102 | |
103 | putchar(c); |
104 | if (isalpha((unsigned char)c)) |
105 | *ptr ^= 0x20; /* Swaps case of ASCII alphabetic characters */ |
106 | } |
107 | |
108 | ptr = buffer; |
109 | |
110 | printf("Tx:" ); |
111 | while (received) |
112 | { |
113 | size_t sent; |
114 | |
115 | sent = vc_container_net_write(sock, ptr, received); |
116 | if (!sent) |
117 | { |
118 | status = vc_container_net_status(sock); |
119 | printf("vc_container_net_write failed: %d\n" , status); |
120 | break; |
121 | } |
122 | |
123 | /* Print out bytes actually sent */ |
124 | while (sent--) |
125 | { |
126 | received--; |
127 | putchar(*ptr++); |
128 | } |
129 | } |
130 | } |
131 | |
132 | status = vc_container_net_status(sock); |
133 | |
134 | if (status != VC_CONTAINER_NET_SUCCESS && status != VC_CONTAINER_NET_ERROR_CONNECTION_LOST) |
135 | break; |
136 | |
137 | vc_container_net_close(sock); |
138 | } |
139 | |
140 | if (status != VC_CONTAINER_NET_SUCCESS) |
141 | { |
142 | printf("vc_container_net_read failed: %d\n" , status); |
143 | } |
144 | |
145 | vc_container_net_close(server_sock); |
146 | |
147 | return 0; |
148 | } |
149 | |