void BufferedTransfer::initIn(pn_link_t* link, pn_delivery_t* d) { in.handle = d; //read in data data.resize(pn_delivery_pending(d)); /*ssize_t read = */pn_link_recv(link, &data[0], data.size()); pn_link_advance(link); //copy delivery tag pn_delivery_tag_t dt = pn_delivery_tag(d); tag.resize(dt.size); #ifdef NO_PROTON_DELIVERY_TAG_T ::memmove(&tag[0], dt.start, dt.size); #else ::memmove(&tag[0], dt.bytes, dt.size); #endif //set context pn_delivery_set_context(d, this); }
void client_callback(pn_connector_t *ctor) { struct client_context *ctx = pn_connector_context(ctor); if (pn_connector_closed(ctor)) { ctx->done = true; } pn_sasl_t *sasl = pn_connector_sasl(ctor); while (pn_sasl_state(sasl) != PN_SASL_PASS) { pn_sasl_state_t st = pn_sasl_state(sasl); switch (st) { case PN_SASL_IDLE: return; case PN_SASL_CONF: if (ctx->mechanism && !strcmp(ctx->mechanism, "PLAIN")) { pn_sasl_plain(sasl, ctx->username, ctx->password); } else { pn_sasl_mechanisms(sasl, ctx->mechanism); pn_sasl_client(sasl); } break; case PN_SASL_STEP: if (pn_sasl_pending(sasl)) { fprintf(stderr, "challenge failed\n"); ctx->done = true; } return; case PN_SASL_FAIL: fprintf(stderr, "authentication failed\n"); ctx->done = true; return; case PN_SASL_PASS: break; } } pn_connection_t *connection = pn_connector_connection(ctor); char tagstr[1024]; char msg[ctx->size]; char data[ctx->size + 16]; for (int i = 0; i < ctx->size; i++) { msg[i] = 'x'; } size_t ndata = pn_message_data(data, ctx->size + 16, msg, ctx->size); if (!ctx->init) { ctx->init = true; char container[1024]; if (gethostname(container, 1024)) pn_fatal("hostname lookup failed"); pn_connection_set_container(connection, container); pn_connection_set_hostname(connection, ctx->hostname); pn_session_t *ssn = pn_session(connection); pn_connection_open(connection); pn_session_open(ssn); if (ctx->send_count) { pn_link_t *snd = pn_sender(ssn, "sender"); pn_set_target(snd, ctx->address); pn_link_open(snd); char buf[16]; for (int i = 0; i < ctx->send_count; i++) { sprintf(buf, "%x", i); pn_delivery(snd, pn_dtag(buf, strlen(buf))); } } if (ctx->recv_count) { pn_link_t *rcv = pn_receiver(ssn, "receiver"); pn_set_source(rcv, ctx->address); pn_link_open(rcv); pn_flow(rcv, ctx->recv_count < ctx->high ? ctx->recv_count : ctx->high); } } pn_delivery_t *delivery = pn_work_head(connection); while (delivery) { pn_delivery_tag_t tag = pn_delivery_tag(delivery); pn_quote_data(tagstr, 1024, tag.bytes, tag.size); pn_link_t *link = pn_link(delivery); if (pn_writable(delivery)) { pn_send(link, data, ndata); if (pn_advance(link)) { if (!ctx->quiet) printf("sent delivery: %s\n", tagstr); } } else if (pn_readable(delivery)) { if (!ctx->quiet) { printf("received delivery: %s\n", tagstr); printf(" payload = \""); } while (true) { size_t n = pn_recv(link, msg, 1024); if (n == PN_EOS) { pn_advance(link); pn_disposition(delivery, PN_ACCEPTED); pn_settle(delivery); if (!--ctx->recv_count) { pn_link_close(link); } break; } else if (!ctx->quiet) { pn_print_data(msg, n); } } if (!ctx->quiet) printf("\"\n"); if (pn_credit(link) < ctx->low && pn_credit(link) < ctx->recv_count) { pn_flow(link, (ctx->recv_count < ctx->high ? ctx->recv_count : ctx->high) - pn_credit(link)); } } if (pn_updated(delivery)) { if (!ctx->quiet) printf("disposition for %s: %u\n", tagstr, pn_remote_disposition(delivery)); pn_clear(delivery); pn_settle(delivery); if (!--ctx->send_count) { pn_link_close(link); } } delivery = pn_work_next(delivery); } if (!ctx->send_count && !ctx->recv_count) { printf("closing\n"); // XXX: how do we close the session? //pn_close((pn_endpoint_t *) ssn); pn_connection_close(connection); } if (pn_connection_state(connection) == (PN_LOCAL_CLOSED | PN_REMOTE_CLOSED)) { ctx->done = true; } }
void server_callback(pn_connector_t *ctor) { pn_sasl_t *sasl = pn_connector_sasl(ctor); while (pn_sasl_state(sasl) != PN_SASL_PASS) { switch (pn_sasl_state(sasl)) { case PN_SASL_IDLE: return; case PN_SASL_CONF: pn_sasl_mechanisms(sasl, "PLAIN ANONYMOUS"); pn_sasl_server(sasl); break; case PN_SASL_STEP: { size_t n = pn_sasl_pending(sasl); char iresp[n]; pn_sasl_recv(sasl, iresp, n); printf("%s", pn_sasl_remote_mechanisms(sasl)); printf(" response = "); pn_print_data(iresp, n); printf("\n"); pn_sasl_done(sasl, PN_SASL_OK); pn_connector_set_connection(ctor, pn_connection()); } break; case PN_SASL_PASS: break; case PN_SASL_FAIL: return; } } pn_connection_t *conn = pn_connector_connection(ctor); struct server_context *ctx = pn_connector_context(ctor); char tagstr[1024]; char msg[10*1024]; char data[ctx->size + 16]; for (int i = 0; i < ctx->size; i++) { msg[i] = 'x'; } size_t ndata = pn_message_data(data, ctx->size + 16, msg, ctx->size); if (pn_connection_state(conn) == (PN_LOCAL_UNINIT | PN_REMOTE_ACTIVE)) { pn_connection_open(conn); } pn_session_t *ssn = pn_session_head(conn, PN_LOCAL_UNINIT | PN_REMOTE_ACTIVE); while (ssn) { pn_session_open(ssn); ssn = pn_session_next(ssn, PN_LOCAL_UNINIT | PN_REMOTE_ACTIVE); } pn_link_t *link = pn_link_head(conn, PN_LOCAL_UNINIT | PN_REMOTE_ACTIVE); while (link) { printf("%s, %s\n", pn_remote_source(link), pn_remote_target(link)); pn_set_source(link, pn_remote_source(link)); pn_set_target(link, pn_remote_target(link)); pn_link_open(link); if (pn_is_receiver(link)) { pn_flow(link, 100); } else { pn_delivery(link, pn_dtag("blah", 4)); } link = pn_link_next(link, PN_LOCAL_UNINIT | PN_REMOTE_ACTIVE); } pn_delivery_t *delivery = pn_work_head(conn); while (delivery) { pn_delivery_tag_t tag = pn_delivery_tag(delivery); pn_quote_data(tagstr, 1024, tag.bytes, tag.size); pn_link_t *link = pn_link(delivery); if (pn_readable(delivery)) { if (!ctx->quiet) { printf("received delivery: %s\n", tagstr); printf(" payload = \""); } while (true) { ssize_t n = pn_recv(link, msg, 1024); if (n == PN_EOS) { pn_advance(link); pn_disposition(delivery, PN_ACCEPTED); break; } else if (!ctx->quiet) { pn_print_data(msg, n); } } if (!ctx->quiet) printf("\"\n"); if (pn_credit(link) < 50) pn_flow(link, 100); } else if (pn_writable(delivery)) { pn_send(link, data, ndata); if (pn_advance(link)) { if (!ctx->quiet) printf("sent delivery: %s\n", tagstr); char tagbuf[16]; sprintf(tagbuf, "%i", ctx->count++); pn_delivery(link, pn_dtag(tagbuf, strlen(tagbuf))); } } if (pn_updated(delivery)) { if (!ctx->quiet) printf("disposition for %s: %u\n", tagstr, pn_remote_disposition(delivery)); pn_settle(delivery); } delivery = pn_work_next(delivery); } if (pn_connection_state(conn) == (PN_LOCAL_ACTIVE | PN_REMOTE_CLOSED)) { pn_connection_close(conn); } ssn = pn_session_head(conn, PN_LOCAL_ACTIVE | PN_REMOTE_CLOSED); while (ssn) { pn_session_close(ssn); ssn = pn_session_next(ssn, PN_LOCAL_ACTIVE | PN_REMOTE_CLOSED); } link = pn_link_head(conn, PN_LOCAL_ACTIVE | PN_REMOTE_CLOSED); while (link) { pn_link_close(link); link = pn_link_next(link, PN_LOCAL_ACTIVE | PN_REMOTE_CLOSED); } }
/** * Inbound Delivery Handler */ static void AMQP_rx_handler(void* context, qd_link_t *link, pn_delivery_t *pnd) { qd_router_t *router = (qd_router_t*) context; pn_link_t *pn_link = qd_link_pn(link); qdr_link_t *rlink = (qdr_link_t*) qd_link_get_context(link); qdr_delivery_t *delivery = 0; qd_message_t *msg; // // Receive the message into a local representation. If the returned message // pointer is NULL, we have not yet received a complete message. // // Note: In the link-routing case, consider cutting the message through. There's // no reason to wait for the whole message to be received before starting to // send it. // msg = qd_message_receive(pnd); if (!msg) return; // // Consume the delivery. // pn_link_advance(pn_link); // // If there's no router link, free the message and finish. It's likely that the link // is closing. // if (!rlink) { qd_message_free(msg); return; } // // Handle the link-routed case // if (qdr_link_is_routed(rlink)) { pn_delivery_tag_t dtag = pn_delivery_tag(pnd); delivery = qdr_link_deliver_to_routed_link(rlink, msg, pn_delivery_settled(pnd), (uint8_t*) dtag.start, dtag.size); if (delivery) { if (pn_delivery_settled(pnd)) pn_delivery_settle(pnd); else { pn_delivery_set_context(pnd, delivery); qdr_delivery_set_context(delivery, pnd); qdr_delivery_incref(delivery); } } return; } // // Determine if the incoming link is anonymous. If the link is addressed, // there are some optimizations we can take advantage of. // bool anonymous_link = qdr_link_is_anonymous(rlink); // // Determine if the user of this connection is allowed to proxy the // user_id of messages. A message user_id is proxied when the // property value differs from the authenticated user name of the connection. // If the user is not allowed to proxy the user_id then the message user_id // must be blank or it must be equal to the connection user name. // bool check_user = false; qd_connection_t *conn = qd_link_connection(link); if (conn->policy_settings) check_user = !conn->policy_settings->allowUserIdProxy; // // Validate the content of the delivery as an AMQP message. This is done partially, only // to validate that we can find the fields we need to route the message. // // If the link is anonymous, we must validate through the message properties to find the // 'to' field. If the link is not anonymous, we don't need the 'to' field as we will be // using the address from the link target. // qd_message_depth_t validation_depth = (anonymous_link || check_user) ? QD_DEPTH_PROPERTIES : QD_DEPTH_MESSAGE_ANNOTATIONS; bool valid_message = qd_message_check(msg, validation_depth); if (valid_message) { if (check_user) { // This connection must not allow proxied user_id qd_iterator_t *userid_iter = qd_message_field_iterator(msg, QD_FIELD_USER_ID); if (userid_iter) { // The user_id property has been specified if (qd_iterator_remaining(userid_iter) > 0) { // user_id property in message is not blank if (!qd_iterator_equal(userid_iter, (const unsigned char *)conn->user_id)) { // This message is rejected: attempted user proxy is disallowed qd_log(router->log_source, QD_LOG_DEBUG, "Message rejected due to user_id proxy violation. User:%s", conn->user_id); pn_link_flow(pn_link, 1); pn_delivery_update(pnd, PN_REJECTED); pn_delivery_settle(pnd); qd_message_free(msg); qd_iterator_free(userid_iter); return; } } qd_iterator_free(userid_iter); } } qd_parsed_field_t *in_ma = qd_message_message_annotations(msg); qd_bitmask_t *link_exclusions; bool strip = qdr_link_strip_annotations_in(rlink); qd_iterator_t *ingress_iter = router_annotate_message(router, in_ma, msg, &link_exclusions, strip); if (anonymous_link) { qd_iterator_t *addr_iter = 0; int phase = 0; // // If the message has delivery annotations, get the to-override field from the annotations. // if (in_ma) { qd_parsed_field_t *ma_to = qd_parse_value_by_key(in_ma, QD_MA_TO); if (ma_to) { addr_iter = qd_iterator_dup(qd_parse_raw(ma_to)); phase = qd_message_get_phase_annotation(msg); } } // // Still no destination address? Use the TO field from the message properties. // if (!addr_iter) addr_iter = qd_message_field_iterator(msg, QD_FIELD_TO); if (addr_iter) { qd_iterator_reset_view(addr_iter, ITER_VIEW_ADDRESS_HASH); if (phase > 0) qd_iterator_annotate_phase(addr_iter, '0' + (char) phase); delivery = qdr_link_deliver_to(rlink, msg, ingress_iter, addr_iter, pn_delivery_settled(pnd), link_exclusions); } } else { const char *term_addr = pn_terminus_get_address(qd_link_remote_target(link)); if (!term_addr) term_addr = pn_terminus_get_address(qd_link_source(link)); if (term_addr) { qd_composed_field_t *to_override = qd_compose_subfield(0); qd_compose_insert_string(to_override, term_addr); qd_message_set_to_override_annotation(msg, to_override); int phase = qdr_link_phase(rlink); if (phase != 0) qd_message_set_phase_annotation(msg, phase); } delivery = qdr_link_deliver(rlink, msg, ingress_iter, pn_delivery_settled(pnd), link_exclusions); } if (delivery) { if (pn_delivery_settled(pnd)) pn_delivery_settle(pnd); else { pn_delivery_set_context(pnd, delivery); qdr_delivery_set_context(delivery, pnd); qdr_delivery_incref(delivery); } } else { // // The message is now and will always be unroutable because there is no address. // pn_link_flow(pn_link, 1); pn_delivery_update(pnd, PN_REJECTED); pn_delivery_settle(pnd); qd_message_free(msg); } // // Rules for delivering messages: // // For addressed (non-anonymous) links: // to-override must be set (done in the core?) // uses qdr_link_deliver to hand over to the core // // For anonymous links: // If there's a to-override in the annotations, use that address // Or, use the 'to' field in the message properties // } else { // // Message is invalid. Reject the message and don't involve the router core. // pn_link_flow(pn_link, 1); pn_delivery_update(pnd, PN_REJECTED); pn_delivery_settle(pnd); qd_message_free(msg); } }