int main(int argc, char const *const *argv) { char const *hostname; int port, status; char const *exchange; char const *exchangetype; amqp_socket_t *socket; amqp_connection_state_t conn; if (argc < 5) { fprintf(stderr, "Usage: amqps_exchange_declare host port exchange " "exchangetype [cacert.pem [key.pem cert.pem]]\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = argv[3]; exchangetype = argv[4]; conn = amqp_new_connection(); socket = amqp_ssl_socket_new(conn); if (!socket) { die("creating SSL/TLS socket"); } if (argc > 5) { status = amqp_ssl_socket_set_cacert(socket, argv[5]); if (status) { die("setting CA certificate"); } } if (argc > 7) { status = amqp_ssl_socket_set_key(socket, argv[7], argv[6]); if (status) { die("setting client key/cert"); } } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening SSL/TLS connection"); } die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); amqp_channel_open(conn, 1); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); amqp_exchange_declare(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(exchangetype), 0, 0, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring exchange"); die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
Channel::Channel(const std::string &host, int port, const std::string &username, const std::string &password, const std::string &vhost, int frame_max, const std::string &path_to_ca_cert, const std::string &path_to_client_key, const std::string &path_to_client_cert) : m_impl(new Detail::ChannelImpl) { m_impl->m_connection = amqp_new_connection(); if (NULL == m_impl->m_connection) { throw std::bad_alloc(); } amqp_socket_t *socket = amqp_ssl_socket_new(m_impl->m_connection); if (NULL == socket) { throw std::bad_alloc(); } try { int status = amqp_ssl_socket_set_cacert(socket, path_to_ca_cert.c_str()); if (status) { throw std::runtime_error("Error in setting CA certificate for socket"); } if (path_to_client_key != "" && path_to_client_cert != "") { status = amqp_ssl_socket_set_key(socket, path_to_client_cert.c_str(), path_to_client_key.c_str()); if (status) { throw std::runtime_error("Error in setting client certificate for socket"); } } status = amqp_socket_open(socket, host.c_str(), port); if (status) { throw std::runtime_error("Error in opening SSL/TLS connection for socket"); } m_impl->DoLogin(username, password, vhost, frame_max); } catch (...) { amqp_destroy_connection(m_impl->m_connection); throw; } m_impl->SetIsConnected(true); }
int main(int argc, char const *const *argv) { char const *hostname; int port, status; int rate_limit; int message_count; amqp_socket_t *socket; amqp_connection_state_t conn; if (argc < 5) { fprintf(stderr, "Usage: amqps_producer host port rate_limit message_count " "[cacert.pem [key.pem cert.pem]]\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); rate_limit = atoi(argv[3]); message_count = atoi(argv[4]); conn = amqp_new_connection(); socket = amqp_ssl_socket_new(conn); if (!socket) { die("creating SSL/TLS socket"); } if (argc > 5) { status = amqp_ssl_socket_set_cacert(socket, argv[5]); if (status) { die("setting CA certificate"); } } if (argc > 7) { status = amqp_ssl_socket_set_key(socket, argv[7], argv[6]); if (status) { die("setting client cert"); } } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening SSL/TLS connection"); } die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); amqp_channel_open(conn, 1); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); send_batch(conn, "test queue", rate_limit, message_count); die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
Channel::Channel(const std::string &host, int port, const std::string &username, const std::string &password, const std::string &vhost, int frame_max, const SSLConnectionParams &ssl_params) : m_impl(new Detail::ChannelImpl) { m_impl->m_connection = amqp_new_connection(); if (NULL == m_impl->m_connection) { throw std::bad_alloc(); } amqp_socket_t *socket = amqp_ssl_socket_new(m_impl->m_connection); if (NULL == socket) { throw std::bad_alloc(); } #if AMQP_VERSION >= 0x00080001 amqp_ssl_socket_set_verify_peer(socket, ssl_params.verify_hostname); amqp_ssl_socket_set_verify_hostname(socket, ssl_params.verify_hostname); #else amqp_ssl_socket_set_verify(socket, ssl_params.verify_hostname); #endif try { int status = amqp_ssl_socket_set_cacert(socket, ssl_params.path_to_ca_cert.c_str()); if (status) { throw AmqpLibraryException::CreateException( status, "Error setting CA certificate for socket"); } if (ssl_params.path_to_client_key != "" && ssl_params.path_to_client_cert != "") { status = amqp_ssl_socket_set_key(socket, ssl_params.path_to_client_cert.c_str(), ssl_params.path_to_client_key.c_str()); if (status) { throw AmqpLibraryException::CreateException( status, "Error setting client certificate for socket"); } } status = amqp_socket_open(socket, host.c_str(), port); if (status) { throw AmqpLibraryException::CreateException( status, "Error setting client certificate for socket"); } m_impl->DoLogin(username, password, vhost, frame_max); } catch (...) { amqp_destroy_connection(m_impl->m_connection); throw; } m_impl->SetIsConnected(true); }
amqp_connection_state_t make_connection(void) { int status; amqp_socket_t *socket = NULL; struct amqp_uri *uri = alloc_amqp_uri(); amqp_connection_state_t conn; init_connection_info(uri); conn = amqp_new_connection(); if (uri->ssl) { #ifdef WITH_SSL socket = amqp_ssl_socket_new(conn); if (!socket) { die("creating SSL/TLS socket"); } if (amqp_cacert) { amqp_ssl_socket_set_cacert(socket, amqp_cacert); } if (amqp_key) { amqp_ssl_socket_set_key(socket, amqp_cert, amqp_key); } #else die("librabbitmq was not built with SSL/TLS support"); #endif } else { socket = amqp_tcp_socket_new(conn); if (!socket) { die("creating TCP socket (out of memory)"); } } host_port_t * hp = ((host_port_t *)uri->host_port_array.elts); status = amqp_socket_open(socket, hp->host, hp->port); if (status) { die("opening socket to %s:%d", hp->host, hp->port); } die_rpc(amqp_login(conn, uri->vhost, 0, 131072, amqp_heartbeat, AMQP_SASL_METHOD_PLAIN, uri->user, uri->password), "logging in to AMQP server"); if (!amqp_channel_open(conn, 1)) { die_rpc(amqp_get_rpc_reply(conn), "opening channel"); } free_amqp_uri(uri); return conn; }
amqp_connection_state_t make_connection(void) { int status; amqp_socket_t *socket = NULL; struct amqp_connection_info ci; amqp_connection_state_t conn; init_connection_info(&ci); conn = amqp_new_connection(); if (ci.ssl) { #ifdef WITH_SSL socket = amqp_ssl_socket_new(); if (!socket) { die("creating SSL/TLS socket"); } if (amqp_cacert) { amqp_ssl_socket_set_cacert(socket, amqp_cacert); } if (amqp_key) { amqp_ssl_socket_set_key(socket, amqp_cert, amqp_key); } #else die("librabbitmq was not built with SSL/TLS support"); #endif } else { socket = amqp_tcp_socket_new(); if (!socket) { die("creating TCP socket (out of memory)"); } } status = amqp_socket_open(socket, ci.host, ci.port); if (status) { die("opening socket to %s:%d", ci.host, ci.port); } amqp_set_socket(conn, socket); die_rpc(amqp_login(conn, ci.vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, ci.user, ci.password), "logging in to AMQP server"); if (!amqp_channel_open(conn, 1)) { die_rpc(amqp_get_rpc_reply(conn), "opening channel"); } return conn; }
void AMQP::sockConnect() { int status; cnn = amqp_new_connection(); switch(proto) { case AMQPS_proto: { sockfd = amqp_ssl_socket_new(cnn); status = amqp_ssl_socket_set_cacert(sockfd, cacert_path.c_str()); if (status) { throw AMQPException("AMQP cannot set CA certificate"); } status = amqp_ssl_socket_set_key(sockfd, client_cert_path.c_str(), client_key_path.c_str()); if (status) { throw AMQPException("AMQP cannot set client certificate or key"); } amqp_ssl_socket_set_verify_peer(sockfd, verify_peer ? 1 : 0); amqp_ssl_socket_set_verify_hostname(sockfd, verify_hostname ? 1 : 0); } break; case AMQP_proto: default: sockfd = amqp_tcp_socket_new(cnn); break; } status = amqp_socket_open(sockfd, host.c_str(), port); if (status){ amqp_destroy_connection(cnn); throw AMQPException("AMQP cannot create socket"); } }
/** * Internal function used to initialize the connection to * the RabbitMQ server. Also used to reconnect to the server * in case the connection fails and to redeclare exchanges * and queues if they are lost * */ static int init_conn(MQ_INSTANCE *my_instance) { int rval = 0; int amqp_ok = AMQP_STATUS_OK; if(my_instance->use_ssl){ if((my_instance->sock = amqp_ssl_socket_new(my_instance->conn)) != NULL){ if((amqp_ok = amqp_ssl_socket_set_cacert(my_instance->sock,my_instance->ssl_CA_cert)) != AMQP_STATUS_OK){ skygw_log_write(LOGFILE_ERROR, "Error : Failed to set CA certificate: %s", amqp_error_string2(amqp_ok)); goto cleanup; } if((amqp_ok = amqp_ssl_socket_set_key(my_instance->sock, my_instance->ssl_client_cert, my_instance->ssl_client_key)) != AMQP_STATUS_OK){ skygw_log_write(LOGFILE_ERROR, "Error : Failed to set client certificate and key: %s", amqp_error_string2(amqp_ok)); goto cleanup; } }else{ amqp_ok = AMQP_STATUS_SSL_CONNECTION_FAILED; skygw_log_write(LOGFILE_ERROR, "Error : SSL socket creation failed."); goto cleanup; } /**SSL is not used, falling back to TCP*/ }else if((my_instance->sock = amqp_tcp_socket_new(my_instance->conn)) == NULL){ skygw_log_write(LOGFILE_ERROR, "Error : TCP socket creation failed."); goto cleanup; } /**Socket creation was successful, trying to open the socket*/ if((amqp_ok = amqp_socket_open(my_instance->sock,my_instance->hostname,my_instance->port)) != AMQP_STATUS_OK){ skygw_log_write(LOGFILE_ERROR, "Error : Failed to open socket: %s", amqp_error_string2(amqp_ok)); goto cleanup; } amqp_rpc_reply_t reply; reply = amqp_login(my_instance->conn,my_instance->vhost,0,AMQP_DEFAULT_FRAME_SIZE,0,AMQP_SASL_METHOD_PLAIN,my_instance->username,my_instance->password); if(reply.reply_type != AMQP_RESPONSE_NORMAL){ skygw_log_write(LOGFILE_ERROR, "Error : Login to RabbitMQ server failed."); goto cleanup; } amqp_channel_open(my_instance->conn,my_instance->channel); reply = amqp_get_rpc_reply(my_instance->conn); if(reply.reply_type != AMQP_RESPONSE_NORMAL){ skygw_log_write(LOGFILE_ERROR, "Error : Channel creation failed."); goto cleanup; } amqp_exchange_declare(my_instance->conn,my_instance->channel, amqp_cstring_bytes(my_instance->exchange), amqp_cstring_bytes(my_instance->exchange_type), 0, 1, amqp_empty_table); reply = amqp_get_rpc_reply(my_instance->conn); if(reply.reply_type != AMQP_RESPONSE_NORMAL){ skygw_log_write(LOGFILE_ERROR, "Error : Exchange declaration failed,trying to redeclare the exchange."); if(reply.reply_type == AMQP_RESPONSE_SERVER_EXCEPTION){ if(reply.reply.id == AMQP_CHANNEL_CLOSE_METHOD){ amqp_send_method(my_instance->conn,my_instance->channel,AMQP_CHANNEL_CLOSE_OK_METHOD,NULL); }else if(reply.reply.id == AMQP_CONNECTION_CLOSE_METHOD){ amqp_send_method(my_instance->conn,my_instance->channel,AMQP_CONNECTION_CLOSE_OK_METHOD,NULL); } my_instance->channel++; amqp_channel_open(my_instance->conn,my_instance->channel); amqp_exchange_delete(my_instance->conn,my_instance->channel,amqp_cstring_bytes(my_instance->exchange),0); amqp_exchange_declare(my_instance->conn,my_instance->channel, amqp_cstring_bytes(my_instance->exchange), amqp_cstring_bytes(my_instance->exchange_type), 0, 1, amqp_empty_table); reply = amqp_get_rpc_reply(my_instance->conn); } if(reply.reply_type != AMQP_RESPONSE_NORMAL){ skygw_log_write(LOGFILE_ERROR, "Error : Exchange redeclaration failed."); goto cleanup; } } if(my_instance->queue){ amqp_queue_declare(my_instance->conn,my_instance->channel, amqp_cstring_bytes(my_instance->queue), 0, 1, 0, 0, amqp_empty_table); reply = amqp_get_rpc_reply(my_instance->conn); if(reply.reply_type != AMQP_RESPONSE_NORMAL){ skygw_log_write(LOGFILE_ERROR, "Error : Queue declaration failed."); goto cleanup; } amqp_queue_bind(my_instance->conn,my_instance->channel, amqp_cstring_bytes(my_instance->queue), amqp_cstring_bytes(my_instance->exchange), amqp_cstring_bytes(my_instance->key), amqp_empty_table); reply = amqp_get_rpc_reply(my_instance->conn); if(reply.reply_type != AMQP_RESPONSE_NORMAL){ skygw_log_write(LOGFILE_ERROR, "Error : Failed to bind queue to exchange."); goto cleanup; } } rval = 1; cleanup: return rval; }
int main(int argc, char const *const *argv) { char const *hostname; int port, status; char const *queuename; amqp_socket_t *socket; amqp_connection_state_t conn; if (argc < 4) { fprintf(stderr, "Usage: amqps_listenq host port queuename " "[cacert.pem [key.pem cert.pem]]\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); queuename = argv[3]; conn = amqp_new_connection(); socket = amqp_ssl_socket_new(); if (!socket) { die("creating SSL/TLS socket"); } if (argc > 4) { status = amqp_ssl_socket_set_cacert(socket, argv[4]); if (status) { die("setting CA certificate"); } } if (argc > 6) { status = amqp_ssl_socket_set_key(socket, argv[6], argv[5]); if (status) { die("setting client cert"); } } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening SSL/TLS connection"); } amqp_set_socket(conn, socket); die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); amqp_channel_open(conn, 1); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); amqp_basic_consume(conn, 1, amqp_cstring_bytes(queuename), amqp_empty_bytes, 0, 0, 0, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming"); { amqp_frame_t frame; int result; amqp_basic_deliver_t *d; amqp_basic_properties_t *p; size_t body_target; size_t body_received; while (1) { amqp_maybe_release_buffers(conn); result = amqp_simple_wait_frame(conn, &frame); printf("Result %d\n", result); if (result < 0) { break; } printf("Frame type %d, channel %d\n", frame.frame_type, frame.channel); if (frame.frame_type != AMQP_FRAME_METHOD) { continue; } printf("Method %s\n", amqp_method_name(frame.payload.method.id)); if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD) { continue; } d = (amqp_basic_deliver_t *) frame.payload.method.decoded; printf("Delivery %u, exchange %.*s routingkey %.*s\n", (unsigned) d->delivery_tag, (int) d->exchange.len, (char *) d->exchange.bytes, (int) d->routing_key.len, (char *) d->routing_key.bytes); result = amqp_simple_wait_frame(conn, &frame); if (result < 0) { break; } if (frame.frame_type != AMQP_FRAME_HEADER) { fprintf(stderr, "Expected header!"); abort(); } p = (amqp_basic_properties_t *) frame.payload.properties.decoded; if (p->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) { printf("Content-type: %.*s\n", (int) p->content_type.len, (char *) p->content_type.bytes); } printf("----\n"); body_target = frame.payload.properties.body_size; body_received = 0; while (body_received < body_target) { result = amqp_simple_wait_frame(conn, &frame); if (result < 0) { break; } if (frame.frame_type != AMQP_FRAME_BODY) { fprintf(stderr, "Expected body!"); abort(); } body_received += frame.payload.body_fragment.len; assert(body_received <= body_target); amqp_dump(frame.payload.body_fragment.bytes, frame.payload.body_fragment.len); } if (body_received != body_target) { /* Can only happen when amqp_simple_wait_frame returns <= 0 */ /* We break here to close the connection */ break; } amqp_basic_ack(conn, 1, d->delivery_tag, 0); } } die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
static int init_amqp_connection(amqp_connection_state_t *conn) { int status; amqp_socket_t *socket = NULL; struct amqp_connection_info ci; amqp_rpc_reply_t ret; ci.user = "******"; ci.password = "******"; ci.host = "localhost"; ci.vhost = "/"; ci.port = 20001; ci.ssl = 0; *conn = amqp_new_connection(); if (ci.ssl) { #ifdef WITH_SSL socket = amqp_ssl_socket_new(*conn); if (!socket) { LB_SYSLOG(LOG_ERR, "creating SSL/TLS socket\n"); return LC_BUS_ERR; } if (amqp_cacert) { amqp_ssl_socket_set_cacert(socket, amqp_cacert); } if (amqp_key) { amqp_ssl_socket_set_key(socket, amqp_cert, amqp_key); } #else LB_SYSLOG(LOG_ERR, "librabbitmq was not built with SSL/TLS support\n"); return LC_BUS_ERR; #endif } else { socket = amqp_tcp_socket_new(*conn); if (!socket) { LB_SYSLOG(LOG_ERR, "creating TCP socket (out of memory)\n"); return LC_BUS_ERR; } } status = amqp_socket_open(socket, ci.host, ci.port); if (status) { LB_SYSLOG(LOG_ERR, "opening socket to %s:%d, status=%d, errno=%s.\n", ci.host, ci.port, status, strerror(errno)); return LC_BUS_ERR; } ret = amqp_login( *conn, ci.vhost, LC_BUS_MAX_CHANNEL, /* channel max */ LC_BUS_MAX_FRAME_SIZE, 0, /* heartbeat */ AMQP_SASL_METHOD_PLAIN, ci.user, ci.password ); if (ret.reply_type != AMQP_RESPONSE_NORMAL) { LB_SYSLOG(LOG_ERR, "error in logging AMQP server, %s\n", amqp_rpc_reply_string(&ret)); return LC_BUS_ERR; } return LC_BUS_OK; }
int main(int argc, char const *const *argv) { char const *hostname; int port, status; char const *exchange; char const *bindingkey; amqp_socket_t *socket; amqp_connection_state_t conn; amqp_bytes_t queuename; if (argc < 3) { fprintf(stderr, "Usage: amqps_consumer host port " "[cacert.pem [key.pem cert.pem]]\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = "amq.direct"; /* argv[3]; */ bindingkey = "test queue"; /* argv[4]; */ conn = amqp_new_connection(); socket = amqp_ssl_socket_new(conn); if (!socket) { die("creating SSL/TLS socket"); } if (argc > 3) { status = amqp_ssl_socket_set_cacert(socket, argv[3]); if (status) { die("setting CA certificate"); } } if (argc > 5) { status = amqp_ssl_socket_set_key(socket, argv[5], argv[4]); if (status) { die("setting client key"); } } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening SSL/TLS connection"); } die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); amqp_channel_open(conn, 1); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); { amqp_queue_declare_ok_t *r = amqp_queue_declare(conn, 1, amqp_empty_bytes, 0, 0, 0, 1, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring queue"); queuename = amqp_bytes_malloc_dup(r->queue); if (queuename.bytes == NULL) { fprintf(stderr, "Out of memory while copying queue name"); return 1; } } amqp_queue_bind(conn, 1, queuename, amqp_cstring_bytes(exchange), amqp_cstring_bytes(bindingkey), amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Binding queue"); amqp_basic_consume(conn, 1, queuename, amqp_empty_bytes, 0, 1, 0, amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming"); run(conn); die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
int main(int argc, char const *const *argv) { char const *hostname; int port, status; char const *exchange; char const *bindingkey; char const *queue; amqp_socket_t *socket; amqp_connection_state_t conn; if (argc < 6) { fprintf(stderr, "Usage: amqps_bind host port exchange bindingkey queue " "[cacert.pem [verifypeer] [verifyhostname] [key.pem cert.pem]]\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); exchange = argv[3]; bindingkey = argv[4]; queue = argv[5]; conn = amqp_new_connection(); socket = amqp_ssl_socket_new(conn); if (!socket) { die("creating SSL/TLS socket"); } amqp_ssl_socket_set_verify_peer(socket, 0); amqp_ssl_socket_set_verify_hostname(socket, 0); if (argc > 6) { int nextarg = 7; status = amqp_ssl_socket_set_cacert(socket, argv[6]); if (status) { die("setting CA certificate"); } if (argc > nextarg && !strcmp("verifypeer", argv[nextarg])) { amqp_ssl_socket_set_verify_peer(socket, 1); nextarg++; } if (argc > nextarg && !strcmp("verifyhostname", argv[nextarg])) { amqp_ssl_socket_set_verify_hostname(socket, 1); nextarg++; } if (argc > nextarg + 1) { status = amqp_ssl_socket_set_key(socket, argv[nextarg + 1], argv[nextarg]); if (status) { die("setting client cert"); } } } status = amqp_socket_open(socket, hostname, port); if (status) { die("opening SSL/TLS connection"); } die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); amqp_channel_open(conn, 1); die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); amqp_queue_bind(conn, 1, amqp_cstring_bytes(queue), amqp_cstring_bytes(exchange), amqp_cstring_bytes(bindingkey), amqp_empty_table); die_on_amqp_error(amqp_get_rpc_reply(conn), "Unbinding"); die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "Closing channel"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); return 0; }
int main(int argc, char const *const *argv) { char const nofile[2] = "-"; char const *hostname; int port, status; amqp_socket_t *socket; amqp_connection_state_t conn; struct timeval *tv; if (argc < 3) { fprintf(stderr, "Usage: amqps_connect_timeout host port " "[cacert.pem [key.pem cert.pem [timeout_sec [timeout_usec=0]]]]\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); if (argc > 6) { tv = malloc(sizeof(struct timeval)); tv->tv_sec = atoi(argv[6]); if (argc > 7 ) { tv->tv_usec = atoi(argv[7]); } else { tv->tv_usec = 0; } } else { tv = NULL; } conn = amqp_new_connection(); socket = amqp_ssl_socket_new(conn); if (!socket) { die("creating SSL/TLS socket"); } if (argc > 3 && strcmp(nofile, argv[3])) { die_on_error(amqp_ssl_socket_set_cacert(socket, argv[3]), "setting CA certificate"); } if (argc > 5) { if (!strcmp(nofile, argv[5]) && !strcmp(nofile, argv[4])) { status = 0; } else if (!strcmp(nofile, argv[5]) || !strcmp(nofile, argv[4])) { status = -1; } else { status = amqp_ssl_socket_set_key(socket, argv[5], argv[4]); } if (status) { die("setting client key"); } } die_on_error(amqp_socket_open_noblock(socket, hostname, port, tv), "opening SSL/TLS connection"); die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); printf ("Done\n"); return 0; }
int main(int argc, char const *const *argv) { char const *hostname; int port; int timeout; amqp_socket_t *socket; amqp_connection_state_t conn; struct timeval tval; struct timeval *tv; if (argc < 3) { fprintf( stderr, "Usage: amqps_connect_timeout host port timeout_sec " "[cacert.pem [verifypeer] [verifyhostname] [key.pem cert.pem]]\n"); return 1; } hostname = argv[1]; port = atoi(argv[2]); timeout = atoi(argv[3]); if (timeout > 0) { tv = &tval; tv->tv_sec = timeout; tv->tv_usec = 0; } else { tv = NULL; } conn = amqp_new_connection(); socket = amqp_ssl_socket_new(conn); if (!socket) { die("creating SSL/TLS socket"); } amqp_ssl_socket_set_verify_peer(socket, 0); amqp_ssl_socket_set_verify_hostname(socket, 0); if (argc > 5) { int nextarg = 6; die_on_error(amqp_ssl_socket_set_cacert(socket, argv[5]), "setting CA certificate"); if (argc > nextarg && !strcmp("verifypeer", argv[nextarg])) { amqp_ssl_socket_set_verify_peer(socket, 1); nextarg++; } if (argc > nextarg && !strcmp("verifyhostname", argv[nextarg])) { amqp_ssl_socket_set_verify_hostname(socket, 1); nextarg++; } if (argc > nextarg + 1) { die_on_error( amqp_ssl_socket_set_key(socket, argv[nextarg + 1], argv[nextarg]), "setting client key"); } } die_on_error(amqp_socket_open_noblock(socket, hostname, port, tv), "opening SSL/TLS connection"); die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in"); die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection"); die_on_error(amqp_destroy_connection(conn), "Ending connection"); printf ("Done\n"); return 0; }
/* Transport implementation */ int janus_rabbitmq_init(janus_transport_callbacks *callback, const char *config_path) { if(g_atomic_int_get(&stopping)) { /* Still stopping from before */ return -1; } if(callback == NULL || config_path == NULL) { /* Invalid arguments */ return -1; } /* This is the callback we'll need to invoke to contact the Janus core */ gateway = callback; /* Read configuration */ char filename[255]; g_snprintf(filename, 255, "%s/%s.jcfg", config_path, JANUS_RABBITMQ_PACKAGE); JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename); janus_config *config = janus_config_parse(filename); if(config == NULL) { JANUS_LOG(LOG_WARN, "Couldn't find .jcfg configuration file (%s), trying .cfg\n", JANUS_RABBITMQ_PACKAGE); g_snprintf(filename, 255, "%s/%s.cfg", config_path, JANUS_RABBITMQ_PACKAGE); JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename); config = janus_config_parse(filename); } if(config != NULL) janus_config_print(config); janus_config_category *config_general = janus_config_get_create(config, NULL, janus_config_type_category, "general"); janus_config_category *config_admin = janus_config_get_create(config, NULL, janus_config_type_category, "admin"); janus_config_item *item = janus_config_get(config, config_general, janus_config_type_item, "json"); if(item && item->value) { /* Check how we need to format/serialize the JSON output */ if(!strcasecmp(item->value, "indented")) { /* Default: indented, we use three spaces for that */ json_format = JSON_INDENT(3) | JSON_PRESERVE_ORDER; } else if(!strcasecmp(item->value, "plain")) { /* Not indented and no new lines, but still readable */ json_format = JSON_INDENT(0) | JSON_PRESERVE_ORDER; } else if(!strcasecmp(item->value, "compact")) { /* Compact, so no spaces between separators */ json_format = JSON_COMPACT | JSON_PRESERVE_ORDER; } else { JANUS_LOG(LOG_WARN, "Unsupported JSON format option '%s', using default (indented)\n", item->value); json_format = JSON_INDENT(3) | JSON_PRESERVE_ORDER; } } /* Check if we need to send events to handlers */ janus_config_item *events = janus_config_get(config, config_general, janus_config_type_item, "events"); if(events != NULL && events->value != NULL) notify_events = janus_is_true(events->value); if(!notify_events && callback->events_is_enabled()) { JANUS_LOG(LOG_WARN, "Notification of events to handlers disabled for %s\n", JANUS_RABBITMQ_NAME); } /* Handle configuration, starting from the server details */ item = janus_config_get(config, config_general, janus_config_type_item, "host"); if(item && item->value) rmqhost = g_strdup(item->value); else rmqhost = g_strdup("localhost"); int rmqport = AMQP_PROTOCOL_PORT; item = janus_config_get(config, config_general, janus_config_type_item, "port"); if(item && item->value) rmqport = atoi(item->value); /* Credentials and Virtual Host */ item = janus_config_get(config, config_general, janus_config_type_item, "vhost"); if(item && item->value) vhost = g_strdup(item->value); else vhost = g_strdup("/"); item = janus_config_get(config, config_general, janus_config_type_item, "username"); if(item && item->value) username = g_strdup(item->value); else username = g_strdup("guest"); item = janus_config_get(config, config_general, janus_config_type_item, "password"); if(item && item->value) password = g_strdup(item->value); else password = g_strdup("guest"); /* SSL config*/ gboolean ssl_enabled = FALSE; gboolean ssl_verify_peer = FALSE; gboolean ssl_verify_hostname = FALSE; item = janus_config_get(config, config_general, janus_config_type_item, "ssl_enabled"); if(item == NULL) { /* Try legacy property */ item = janus_config_get(config, config_general, janus_config_type_item, "ssl_enable"); if (item && item->value) { JANUS_LOG(LOG_WARN, "Found deprecated 'ssl_enable' property, please update it to 'ssl_enabled' instead\n"); } } if(!item || !item->value || !janus_is_true(item->value)) { JANUS_LOG(LOG_INFO, "RabbitMQ SSL support disabled\n"); } else { ssl_enabled = TRUE; item = janus_config_get(config, config_general, janus_config_type_item, "ssl_cacert"); if(item && item->value) ssl_cacert_file = g_strdup(item->value); item = janus_config_get(config, config_general, janus_config_type_item, "ssl_cert"); if(item && item->value) ssl_cert_file = g_strdup(item->value); item = janus_config_get(config, config_general, janus_config_type_item, "ssl_key"); if(item && item->value) ssl_key_file = g_strdup(item->value); item = janus_config_get(config, config_general, janus_config_type_item, "ssl_verify_peer"); if(item && item->value && janus_is_true(item->value)) ssl_verify_peer = TRUE; item = janus_config_get(config, config_general, janus_config_type_item, "ssl_verify_hostname"); if(item && item->value && janus_is_true(item->value)) ssl_verify_hostname = TRUE; } /* Now check if the Janus API must be supported */ item = janus_config_get(config, config_general, janus_config_type_item, "enabled"); if(item == NULL) { /* Try legacy property */ item = janus_config_get(config, config_general, janus_config_type_item, "enable"); if (item && item->value) { JANUS_LOG(LOG_WARN, "Found deprecated 'enable' property, please update it to 'enabled' instead\n"); } } if(!item || !item->value || !janus_is_true(item->value)) { JANUS_LOG(LOG_WARN, "RabbitMQ support disabled (Janus API)\n"); } else { /* Parse configuration */ item = janus_config_get(config, config_general, janus_config_type_item, "to_janus"); if(!item || !item->value) { JANUS_LOG(LOG_FATAL, "Missing name of incoming queue for RabbitMQ integration...\n"); goto error; } to_janus = g_strdup(item->value); item = janus_config_get(config, config_general, janus_config_type_item, "from_janus"); if(!item || !item->value) { JANUS_LOG(LOG_FATAL, "Missing name of outgoing queue for RabbitMQ integration...\n"); goto error; } from_janus = g_strdup(item->value); item = janus_config_get(config, config_general, janus_config_type_item, "janus_exchange"); if(!item || !item->value) { JANUS_LOG(LOG_INFO, "Missing name of outgoing exchange for RabbitMQ integration, using default\n"); } else { janus_exchange = g_strdup(item->value); } if (janus_exchange == NULL) { JANUS_LOG(LOG_INFO, "RabbitMQ support for Janus API enabled, %s:%d (%s/%s)\n", rmqhost, rmqport, to_janus, from_janus); } else { JANUS_LOG(LOG_INFO, "RabbitMQ support for Janus API enabled, %s:%d (%s/%s) exch: (%s)\n", rmqhost, rmqport, to_janus, from_janus, janus_exchange); } rmq_janus_api_enabled = TRUE; } /* Do the same for the admin API */ item = janus_config_get(config, config_admin, janus_config_type_item, "admin_enabled"); if(item == NULL) { /* Try legacy property */ item = janus_config_get(config, config_general, janus_config_type_item, "admin_enable"); if (item && item->value) { JANUS_LOG(LOG_WARN, "Found deprecated 'admin_enable' property, please update it to 'admin_enabled' instead\n"); } } if(!item || !item->value || !janus_is_true(item->value)) { JANUS_LOG(LOG_WARN, "RabbitMQ support disabled (Admin API)\n"); } else { /* Parse configuration */ item = janus_config_get(config, config_admin, janus_config_type_item, "to_janus_admin"); if(!item || !item->value) { JANUS_LOG(LOG_FATAL, "Missing name of incoming queue for RabbitMQ integration...\n"); goto error; } to_janus_admin = g_strdup(item->value); item = janus_config_get(config, config_admin, janus_config_type_item, "from_janus_admin"); if(!item || !item->value) { JANUS_LOG(LOG_FATAL, "Missing name of outgoing queue for RabbitMQ integration...\n"); goto error; } from_janus_admin = g_strdup(item->value); JANUS_LOG(LOG_INFO, "RabbitMQ support for Admin API enabled, %s:%d (%s/%s)\n", rmqhost, rmqport, to_janus_admin, from_janus_admin); rmq_admin_api_enabled = TRUE; } if(!rmq_janus_api_enabled && !rmq_admin_api_enabled) { JANUS_LOG(LOG_WARN, "RabbitMQ support disabled for both Janus and Admin API, giving up\n"); goto error; } else { /* FIXME We currently support a single application, create a new janus_rabbitmq_client instance */ rmq_client = g_malloc0(sizeof(janus_rabbitmq_client)); /* Connect */ rmq_client->rmq_conn = amqp_new_connection(); amqp_socket_t *socket = NULL; int status; JANUS_LOG(LOG_VERB, "Creating RabbitMQ socket...\n"); if (ssl_enabled) { socket = amqp_ssl_socket_new(rmq_client->rmq_conn); if(socket == NULL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error creating socket...\n"); goto error; } if(ssl_verify_peer) { amqp_ssl_socket_set_verify_peer(socket, 1); } else { amqp_ssl_socket_set_verify_peer(socket, 0); } if(ssl_verify_hostname) { amqp_ssl_socket_set_verify_hostname(socket, 1); } else { amqp_ssl_socket_set_verify_hostname(socket, 0); } if(ssl_cacert_file) { status = amqp_ssl_socket_set_cacert(socket, ssl_cacert_file); if(status != AMQP_STATUS_OK) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error setting CA certificate... (%s)\n", amqp_error_string2(status)); goto error; } } if(ssl_cert_file && ssl_key_file) { status = amqp_ssl_socket_set_key(socket, ssl_cert_file, ssl_key_file); if(status != AMQP_STATUS_OK) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error setting key... (%s)\n", amqp_error_string2(status)); goto error; } } } else { socket = amqp_tcp_socket_new(rmq_client->rmq_conn); if(socket == NULL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error creating socket...\n"); goto error; } } JANUS_LOG(LOG_VERB, "Connecting to RabbitMQ server...\n"); status = amqp_socket_open(socket, rmqhost, rmqport); if(status != AMQP_STATUS_OK) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error opening socket... (%s)\n", amqp_error_string2(status)); goto error; } JANUS_LOG(LOG_VERB, "Logging in...\n"); amqp_rpc_reply_t result = amqp_login(rmq_client->rmq_conn, vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, username, password); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error logging in... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } rmq_client->rmq_channel = 1; JANUS_LOG(LOG_VERB, "Opening channel...\n"); amqp_channel_open(rmq_client->rmq_conn, rmq_client->rmq_channel); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error opening channel... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } rmq_client->janus_exchange = amqp_empty_bytes; if(janus_exchange != NULL) { JANUS_LOG(LOG_VERB, "Declaring exchange...\n"); rmq_client->janus_exchange = amqp_cstring_bytes(janus_exchange); amqp_exchange_declare(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->janus_exchange, amqp_cstring_bytes(JANUS_RABBITMQ_EXCHANGE_TYPE), 0, 0, 0, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error diclaring exchange... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } } rmq_client->janus_api_enabled = FALSE; if(rmq_janus_api_enabled) { rmq_client->janus_api_enabled = TRUE; JANUS_LOG(LOG_VERB, "Declaring incoming queue... (%s)\n", to_janus); rmq_client->to_janus_queue = amqp_cstring_bytes(to_janus); amqp_queue_declare(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->to_janus_queue, 0, 0, 0, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error declaring queue... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } JANUS_LOG(LOG_VERB, "Declaring outgoing queue... (%s)\n", from_janus); rmq_client->from_janus_queue = amqp_cstring_bytes(from_janus); amqp_queue_declare(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->from_janus_queue, 0, 0, 0, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error declaring queue... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } amqp_basic_consume(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->to_janus_queue, amqp_empty_bytes, 0, 1, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error consuming... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } } rmq_client->admin_api_enabled = FALSE; if(rmq_admin_api_enabled) { rmq_client->admin_api_enabled = TRUE; JANUS_LOG(LOG_VERB, "Declaring incoming queue... (%s)\n", to_janus_admin); rmq_client->to_janus_admin_queue = amqp_cstring_bytes(to_janus_admin); amqp_queue_declare(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->to_janus_admin_queue, 0, 0, 0, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error declaring queue... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } JANUS_LOG(LOG_VERB, "Declaring outgoing queue... (%s)\n", from_janus_admin); rmq_client->from_janus_admin_queue = amqp_cstring_bytes(from_janus_admin); amqp_queue_declare(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->from_janus_admin_queue, 0, 0, 0, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error declaring queue... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } amqp_basic_consume(rmq_client->rmq_conn, rmq_client->rmq_channel, rmq_client->to_janus_admin_queue, amqp_empty_bytes, 0, 1, 0, amqp_empty_table); result = amqp_get_rpc_reply(rmq_client->rmq_conn); if(result.reply_type != AMQP_RESPONSE_NORMAL) { JANUS_LOG(LOG_FATAL, "Can't connect to RabbitMQ server: error consuming... %s, %s\n", amqp_error_string2(result.library_error), amqp_method_name(result.reply.id)); goto error; } } rmq_client->messages = g_async_queue_new(); rmq_client->destroy = 0; /* Prepare the transport session (again, just one) */ rmq_session = janus_transport_session_create(rmq_client, NULL); /* Start the threads */ GError *error = NULL; rmq_client->in_thread = g_thread_try_new("rmq_in_thread", &janus_rmq_in_thread, rmq_client, &error); if(error != NULL) { /* Something went wrong... */ JANUS_LOG(LOG_FATAL, "Got error %d (%s) trying to launch the RabbitMQ incoming thread...\n", error->code, error->message ? error->message : "??"); janus_transport_session_destroy(rmq_session); g_free(rmq_client); janus_config_destroy(config); return -1; } rmq_client->out_thread = g_thread_try_new("rmq_out_thread", &janus_rmq_out_thread, rmq_client, &error); if(error != NULL) { /* Something went wrong... */ JANUS_LOG(LOG_FATAL, "Got error %d (%s) trying to launch the RabbitMQ outgoing thread...\n", error->code, error->message ? error->message : "??"); janus_transport_session_destroy(rmq_session); g_free(rmq_client); janus_config_destroy(config); return -1; } janus_mutex_init(&rmq_client->mutex); /* Done */ JANUS_LOG(LOG_INFO, "Setup of RabbitMQ integration completed\n"); /* Notify handlers about this new transport */ if(notify_events && gateway->events_is_enabled()) { json_t *info = json_object(); json_object_set_new(info, "event", json_string("connected")); gateway->notify_event(&janus_rabbitmq_transport, rmq_session, info); } } janus_config_destroy(config); config = NULL; /* Done */ g_atomic_int_set(&initialized, 1); JANUS_LOG(LOG_INFO, "%s initialized!\n", JANUS_RABBITMQ_NAME); return 0; error: /* If we got here, something went wrong */ g_free(rmq_client); g_free(rmqhost); g_free(vhost); g_free(username); g_free(password); g_free(janus_exchange); g_free(to_janus); g_free(from_janus); g_free(to_janus_admin); g_free(from_janus_admin); g_free(ssl_cacert_file); g_free(ssl_cert_file); g_free(ssl_key_file); if(config) janus_config_destroy(config); return -1; }