Debian Codegraph @

c/examples/send-ssl.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include <proton/connection.h>
23 #include <proton/condition.h>
24 #include <proton/delivery.h>
25 #include <proton/link.h>
26 #include <proton/message.h>
27 #include <proton/proactor.h>
28 #include <proton/session.h>
29 #include <proton/sasl.h>
30 #include <proton/ssl.h>
31 #include <proton/transport.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 typedef struct app_data_t {
37 const char *host, *port;
38 const char *amqp_address;
39 const char *container_id;
40 const char *user;
41 const char *pass;
42 int message_count;
43
44 pn_proactor_t *proactor;
45 pn_rwbytes_t message_buffer;
46 int sent;
47 int acknowledged;
48 } app_data_t;
49
50 static int exit_code = 0;
51 static void check_condition(pn_event_t *e, pn_condition_t *cond) {
52 if (pn_condition_is_set(cond)) {
53 fprintf(stderr, "%s: %s: %s\n", pn_event_type_name(pn_event_type(e)),
54 pn_condition_get_name(cond), pn_condition_get_description(cond));
55 pn_connection_close(pn_event_connection(e));
56 exit_code = 1;
57 }
58 }
59
60 /* Create a message with a map { "sequence" : number } encode it and return the encoded buffer. */
61 static pn_bytes_t encode_message(app_data_t* app) {
62 /* Construct a message with the map { "sequence": app.sent } */
63 pn_message_t* message = pn_message();
64 pn_data_t* body = pn_message_body(message);
65 pn_message_set_id(message, (pn_atom_t){.type=PN_ULONG, .u.as_ulong=app->sent});
66 pn_data_put_map(body);
67 pn_data_enter(body);
68 pn_data_put_string(body, pn_bytes(sizeof("sequence")-1, "sequence"));
69 pn_data_put_int(body, app->sent); /* The sequence number */
70 pn_data_exit(body);
71
72 /* encode the message, expanding the encode buffer as needed */
73 if (app->message_buffer.start == NULL) {
74 static const size_t initial_size = 128;
75 app->message_buffer = pn_rwbytes(initial_size, (char*)malloc(initial_size));
76 }
77 /* app->message_buffer is the total buffer space available. */
78 /* mbuf wil point at just the portion used by the encoded message */
79 {
80 pn_rwbytes_t mbuf = pn_rwbytes(app->message_buffer.size, app->message_buffer.start);
81 int status = 0;
82 while ((status = pn_message_encode(message, mbuf.start, &mbuf.size)) == PN_OVERFLOW) {
83 app->message_buffer.size *= 2;
84 app->message_buffer.start = (char*)realloc(app->message_buffer.start, app->message_buffer.size);
85 mbuf.size = app->message_buffer.size;
86 mbuf.start = app->message_buffer.start;
87 }
88 if (status != 0) {
89 fprintf(stderr, "error encoding message: %s\n", pn_error_text(pn_message_error(message)));
90 exit(1);
91 }
92 pn_message_free(message);
93 return pn_bytes(mbuf.size, mbuf.start);
94 }
95 }
96
97 /* Returns true to continue, false if finished */
98 static bool handle(app_data_t* app, pn_event_t* event) {
99 switch (pn_event_type(event)) {
100
101 case PN_CONNECTION_INIT: {
102 pn_connection_t* c = pn_event_connection(event);
103 pn_session_t* s = pn_session(pn_event_connection(event));
104 pn_connection_set_container(c, app->container_id);
105 pn_connection_set_hostname(c, app->host);
106 pn_connection_set_user(c, app->user);
107 pn_connection_set_password(c, app->pass);
108 pn_connection_open(c);
109 pn_session_open(s);
110 {
111 pn_link_t* l = pn_sender(s, "my_sender");
112 pn_terminus_set_address(pn_link_target(l), app->amqp_address);
113 pn_link_open(l);
114 break;
115 }
116 }
117
118 case PN_CONNECTION_REMOTE_OPEN: {
119 pn_ssl_t *ssl = pn_ssl(pn_event_transport(event));
120 if (ssl) {
121 char name[1024];
122 pn_ssl_get_protocol_name(ssl, name, sizeof(name));
123 {
124 const char *subject = pn_ssl_get_remote_subject(ssl);
125 if (subject) {
126 printf("secure connection: to %s using %s\n", subject, name);
127 } else {
128 printf("anonymous connection: using %s\n", name);
129 }
130 fflush(stdout);
131 }
132 }
133 break;
134 }
135
136 case PN_LINK_FLOW: {
137 /* The peer has given us some credit, now we can send messages */
138 pn_link_t *sender = pn_event_link(event);
139 while (pn_link_credit(sender) > 0 && app->sent < app->message_count) {
140 ++app->sent;
141 /* Use sent counter as unique delivery tag. */
142 pn_delivery(sender, pn_dtag((const char *)&app->sent, sizeof(app->sent)));
143 {
144 pn_bytes_t msgbuf = encode_message(app);
145 pn_link_send(sender, msgbuf.start, msgbuf.size);
146 }
147 pn_link_advance(sender);
148 }
149 break;
150 }
151
152 case PN_DELIVERY: {
153 /* We received acknowledgement from the peer that a message was delivered. */
154 pn_delivery_t* d = pn_event_delivery(event);
155 if (pn_delivery_remote_state(d) == PN_ACCEPTED) {
156 if (++app->acknowledged == app->message_count) {
157 printf("%d messages sent and acknowledged\n", app->acknowledged);
158 fflush(stdout);
159 pn_connection_close(pn_event_connection(event));
160 /* Continue handling events till we receive TRANSPORT_CLOSED */
161 }
162 } else {
163 fprintf(stderr, "unexpected delivery state %d\n", (int)pn_delivery_remote_state(d));
164 pn_connection_close(pn_event_connection(event));
165 exit_code=1;
166 }
167 break;
168 }
169
170 case PN_TRANSPORT_CLOSED:
171 check_condition(event, pn_transport_condition(pn_event_transport(event)));
172 break;
173
174 case PN_CONNECTION_REMOTE_CLOSE:
175 check_condition(event, pn_connection_remote_condition(pn_event_connection(event)));
176 pn_connection_close(pn_event_connection(event));
177 break;
178
179 case PN_SESSION_REMOTE_CLOSE:
180 check_condition(event, pn_session_remote_condition(pn_event_session(event)));
181 pn_connection_close(pn_event_connection(event));
182 break;
183
184 case PN_LINK_REMOTE_CLOSE:
185 case PN_LINK_REMOTE_DETACH:
186 check_condition(event, pn_link_remote_condition(pn_event_link(event)));
187 pn_connection_close(pn_event_connection(event));
188 break;
189
190 case PN_PROACTOR_INACTIVE:
191 return false;
192
193 default: break;
194 }
195 return true;
196 }
197
198 void run(app_data_t *app) {
199 /* Loop and handle events */
200 do {
201 pn_event_batch_t *events = pn_proactor_wait(app->proactor);
202 pn_event_t *e;
203 for (e = pn_event_batch_next(events); e; e = pn_event_batch_next(events)) {
204 if (!handle(app, e)) {
205 return;
206 }
207 }
208 pn_proactor_done(app->proactor, events);
209 } while(true);
210 }
211
212 int main(int argc, char **argv) {
213 struct app_data_t app = {0};
214 char addr[PN_MAX_ADDR];
215 pn_transport_t *t = NULL;
216 int err;
217
218 app.container_id = argv[0]; /* Should be unique */
219 app.host = (argc > 1) ? argv[1] : "";
220 app.port = (argc > 2) ? argv[2] : "amqp";
221 app.amqp_address = (argc > 3) ? argv[3] : "examples";
222 app.message_count = (argc > 4) ? atoi(argv[4]) : 10;
223 app.user = (argc > 5) ? argv[5] : 0 ;
224 app.pass = (argc > 6) ? argv[6] : 0 ;
225
226 app.proactor = pn_proactor();
227 pn_proactor_addr(addr, sizeof(addr), app.host, app.port);
228
229 /* Configure a transport for SSL. The transport will be freed by the proactor. */
230 t = pn_transport();
231 /* If we got a username/password on the command line set up for authentication, else allow anonymous ssl */
232 if (app.user && app.pass) {
233 err = pn_ssl_init(pn_ssl(t), NULL, NULL);
234 pn_sasl_allowed_mechs(pn_sasl(t), "PLAIN");
235 } else {
236 pn_ssl_domain_t *domain = pn_ssl_domain(PN_SSL_MODE_CLIENT);
237 err = pn_ssl_domain_set_peer_authentication(domain, PN_SSL_ANONYMOUS_PEER, NULL);
238 if (!err) {
239 err = pn_ssl_init(pn_ssl(t), domain, NULL);
240 }
241 pn_ssl_domain_free(domain);
242 }
243 if (err) {
244 fprintf(stderr, "error initializing SSL: %s\n", pn_code(err));
245 return 1;
246 }
247 pn_proactor_connect2(app.proactor, NULL, t, addr);
248
249 run(&app);
250
251 pn_proactor_free(app.proactor);
252 free(app.message_buffer.start);
253 return exit_code;
254 }