Print this page
ioctl & FIONREAD need more includes on Solarish systems.
| Split |
Close |
| Expand all |
| Collapse all |
--- old/bin/echo.c
+++ new/bin/echo.c
1 1 /*
2 2 * Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 3 *
4 4 * Licensed under the Apache License, Version 2.0 (the "License").
5 5 * You may not use this file except in compliance with the License.
6 6 * A copy of the License is located at
7 7 *
8 8 * http://aws.amazon.com/apache2.0
|
↓ open down ↓ |
8 lines elided |
↑ open up ↑ |
9 9 *
10 10 * or in the "license" file accompanying this file. This file is distributed
11 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 12 * express or implied. See the License for the specific language governing
13 13 * permissions and limitations under the License.
14 14 */
15 15
16 16 #include <sys/types.h>
17 17 #include <sys/socket.h>
18 18 #include <sys/ioctl.h>
19 +#include <sys/filio.h>
19 20 #include <sys/poll.h>
20 21 #include <netdb.h>
21 22
22 23 #include <stdlib.h>
23 24 #include <unistd.h>
24 25 #include <string.h>
26 +#include <stropts.h>
25 27 #include <stdio.h>
26 28
27 29 #include <errno.h>
28 30
29 31 #include <s2n.h>
30 32
31 33 int echo(struct s2n_connection *conn, int sockfd)
32 34 {
33 35 struct pollfd readers[2];
34 36
35 37 readers[0].fd = sockfd;
36 38 readers[0].events = POLLIN;
37 39 readers[1].fd = STDIN_FILENO;
38 40 readers[1].events = POLLIN;
39 41
40 42 int more;
41 43 do {
42 44 if (s2n_negotiate(conn, &more) < 0) {
43 45 fprintf(stderr, "Failed to negotiate: '%s' %d\n", s2n_strerror(s2n_errno, "EN"), s2n_connection_get_alert(conn));
44 46 exit(1);
45 47 }
46 48 } while (more);
47 49
48 50 /* Now that we've negotiated, print some parameters */
49 51 int client_hello_version;
50 52 int client_protocol_version;
51 53 int server_protocol_version;
52 54 int actual_protocol_version;
53 55
54 56 if ((client_hello_version = s2n_connection_get_client_hello_version(conn)) < 0) {
55 57 fprintf(stderr, "Could not get client hello version\n");
56 58 exit(1);
57 59 }
58 60 if ((client_protocol_version = s2n_connection_get_client_protocol_version(conn)) < 0) {
59 61 fprintf(stderr, "Could not get client protocol version\n");
60 62 exit(1);
61 63 }
62 64 if ((server_protocol_version = s2n_connection_get_server_protocol_version(conn)) < 0) {
63 65 fprintf(stderr, "Could not get server protocol version\n");
64 66 exit(1);
65 67 }
66 68 if ((actual_protocol_version = s2n_connection_get_actual_protocol_version(conn)) < 0) {
67 69 fprintf(stderr, "Could not get actual protocol version\n");
68 70 exit(1);
69 71 }
70 72 printf("Client hello version: %d\n", client_hello_version);
71 73 printf("Client protocol version: %d\n", client_protocol_version);
72 74 printf("Server protocol version: %d\n", server_protocol_version);
73 75 printf("Actual protocol version: %d\n", actual_protocol_version);
74 76
75 77 if (s2n_get_server_name(conn)) {
76 78 printf("Server name: %s\n", s2n_get_server_name(conn));
77 79 }
78 80 if (s2n_get_application_protocol(conn)) {
79 81 printf("Application protocol: %s\n",
80 82 s2n_get_application_protocol(conn));
81 83 }
82 84 uint32_t length;
83 85 const uint8_t *status = s2n_connection_get_ocsp_response(conn, &length);
84 86 if (status && length > 0) {
85 87 fprintf(stderr, "OCSP response received, length %d\n", length);
86 88 }
87 89
88 90 printf("Cipher negotiated: %s\n", s2n_connection_get_cipher(conn));
89 91
90 92 /* Act as a simple proxy between stdin and the SSL connection */
91 93 while (poll(readers, 2, -1) > 0) {
92 94 char buffer[10240];
93 95 int bytes_read, bytes_written;
94 96
95 97 if (readers[0].revents & POLLIN) {
96 98 do {
97 99 bytes_read = s2n_recv(conn, buffer, 10240, &more);
98 100 if (bytes_read == 0) {
99 101 /* Connection has been closed */
100 102 s2n_connection_wipe(conn);
101 103 return 0;
102 104 }
103 105 if (bytes_read < 0) {
104 106 fprintf(stderr, "Error reading from connection: '%s' %d\n", s2n_strerror(s2n_errno, "EN"), s2n_connection_get_alert(conn));
105 107 exit(1);
106 108 }
107 109 bytes_written = write(STDOUT_FILENO, buffer, bytes_read);
108 110 if (bytes_written <= 0) {
109 111 fprintf(stderr, "Error writing to stdout\n");
110 112 exit(1);
111 113 }
112 114 } while (more);
113 115 }
114 116 if (readers[1].revents & POLLIN) {
115 117 int bytes_available;
116 118 if (ioctl(STDIN_FILENO, FIONREAD, &bytes_available) < 0) {
117 119 bytes_available = 1;
118 120 }
119 121 if (bytes_available > sizeof(buffer)) {
120 122 bytes_available = sizeof(buffer);
121 123 }
122 124
123 125 /* Read as many bytes as we think we can */
124 126 bytes_read = read(STDIN_FILENO, buffer, bytes_available);
125 127 if (bytes_read < 0) {
126 128 fprintf(stderr, "Error reading from stdin\n");
127 129 exit(1);
128 130 }
129 131 if (bytes_read == 0) {
130 132 /* Exit on EOF */
131 133 return 0;
132 134 }
133 135
134 136 char *buf_ptr = buffer;
135 137 do {
136 138 bytes_written = s2n_send(conn, buf_ptr, bytes_available, &more);
137 139 if (bytes_written < 0) {
138 140 fprintf(stderr, "Error writing to connection: '%s'\n", s2n_strerror(s2n_errno, "EN"));
139 141 exit(1);
140 142 }
141 143
142 144 bytes_available -= bytes_written;
143 145 buf_ptr += bytes_written;
144 146 } while (bytes_available || more);
145 147 }
146 148 }
147 149
148 150 return 0;
149 151 }
|
↓ open down ↓ |
115 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX