int main(int argc, char const *const *argv) { char const *hostname; int port; amqp_socket_t *socket; amqp_connection_state_t conn; struct timeval tval; struct timeval *tv; if (argc < 3) { fprintf(stderr, "Usage: amqp_connect_timeout host port [timeout_sec [timeout_usec=0]]\n"); return 1; } if (argc > 3) { tv = &tval; tv->tv_sec = atoi(argv[3]); if (argc > 4 ) { tv->tv_usec = atoi(argv[4]); } else { tv->tv_usec = 0; } } else { tv = NULL; } hostname = argv[1]; port = atoi(argv[2]); conn = amqp_new_connection(); socket = amqp_tcp_socket_new(conn); if (!socket) { die("creating TCP socket"); } die_on_error(amqp_socket_open_noblock(socket, hostname, port, tv), "opening TCP socket"); 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; }
static gboolean afamqp_dd_connect(AMQPDestDriver *self, gboolean reconnect) { int sockfd_ret; amqp_rpc_reply_t ret; if (reconnect && self->conn) { ret = amqp_get_rpc_reply(self->conn); if (ret.reply_type == AMQP_RESPONSE_NORMAL) { return TRUE; } else { _amqp_connection_disconnect(self); } } self->conn = amqp_new_connection(); if (self->conn == NULL) { msg_error("Error allocating AMQP connection.", NULL); goto exception_amqp_dd_connect_failed_init; } self->sockfd = amqp_tcp_socket_new(self->conn); struct timeval delay; delay.tv_sec = 1; delay.tv_usec = 0; sockfd_ret = amqp_socket_open_noblock(self->sockfd, self->host, self->port, &delay); if (sockfd_ret != AMQP_STATUS_OK) { msg_error("Error connecting to AMQP server", evt_tag_str("driver", self->super.super.super.id), evt_tag_str("error", amqp_error_string2(-sockfd_ret)), evt_tag_int("time_reopen", self->super.time_reopen), NULL); goto exception_amqp_dd_connect_failed_init; } ret = amqp_login(self->conn, self->vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, self->user, self->password); if (!afamqp_is_ok(self, "Error during AMQP login", ret)) { goto exception_amqp_dd_connect_failed_init; } amqp_channel_open(self->conn, 1); ret = amqp_get_rpc_reply(self->conn); if (!afamqp_is_ok(self, "Error during AMQP channel open", ret)) { goto exception_amqp_dd_connect_failed_channel; } if (self->declare) { amqp_exchange_declare(self->conn, 1, amqp_cstring_bytes(self->exchange), amqp_cstring_bytes(self->exchange_type), 0, 0, 0, 0, amqp_empty_table); ret = amqp_get_rpc_reply(self->conn); if (!afamqp_is_ok(self, "Error during AMQP exchange declaration", ret)) { goto exception_amqp_dd_connect_failed_exchange; } } msg_debug ("Connecting to AMQP succeeded", evt_tag_str("driver", self->super.super.super.id), NULL); return TRUE; /* Exceptions */ exception_amqp_dd_connect_failed_exchange: amqp_channel_close(self->conn, 1, AMQP_REPLY_SUCCESS); exception_amqp_dd_connect_failed_channel: amqp_connection_close(self->conn, AMQP_REPLY_SUCCESS); exception_amqp_dd_connect_failed_init: _amqp_connection_deinit(self); return FALSE; }
amqp_connection_resource *connection_resource_constructor(amqp_connection_params *params, zend_bool persistent TSRMLS_DC) { struct timeval tv = {0}; struct timeval *tv_ptr = &tv; char *std_datetime; amqp_table_entry_t client_properties_entries[5]; amqp_table_t client_properties_table; amqp_table_entry_t custom_properties_entries[1]; amqp_table_t custom_properties_table; amqp_connection_resource *resource; /* Allocate space for the connection resource */ resource = (amqp_connection_resource *)pecalloc(1, sizeof(amqp_connection_resource), persistent); /* Create the connection */ resource->connection_state = amqp_new_connection(); /* Create socket object */ resource->socket = amqp_tcp_socket_new(resource->connection_state); if (params->connect_timeout > 0) { tv.tv_sec = (long int) params->connect_timeout; tv.tv_usec = (long int) ((params->connect_timeout - tv.tv_sec) * 1000000); } else { tv_ptr = NULL; } /* Try to connect and verify that no error occurred */ if (amqp_socket_open_noblock(resource->socket, params->host, params->port, tv_ptr)) { zend_throw_exception(amqp_connection_exception_class_entry, "Socket error: could not connect to host.", 0 TSRMLS_CC); connection_resource_destructor(resource, persistent TSRMLS_CC); return NULL; } if (!php_amqp_set_resource_read_timeout(resource, params->read_timeout TSRMLS_CC)) { connection_resource_destructor(resource, persistent TSRMLS_CC); return NULL; } if (!php_amqp_set_resource_write_timeout(resource, params->write_timeout TSRMLS_CC)) { connection_resource_destructor(resource, persistent TSRMLS_CC); return NULL; } std_datetime = php_std_date(time(NULL) TSRMLS_CC); client_properties_entries[0].key = amqp_cstring_bytes("type"); client_properties_entries[0].value.kind = AMQP_FIELD_KIND_UTF8; client_properties_entries[0].value.value.bytes = amqp_cstring_bytes("php-amqp extension"); client_properties_entries[1].key = amqp_cstring_bytes("version"); client_properties_entries[1].value.kind = AMQP_FIELD_KIND_UTF8; client_properties_entries[1].value.value.bytes = amqp_cstring_bytes(PHP_AMQP_VERSION); client_properties_entries[2].key = amqp_cstring_bytes("revision"); client_properties_entries[2].value.kind = AMQP_FIELD_KIND_UTF8; client_properties_entries[2].value.value.bytes = amqp_cstring_bytes(PHP_AMQP_REVISION); client_properties_entries[3].key = amqp_cstring_bytes("connection type"); client_properties_entries[3].value.kind = AMQP_FIELD_KIND_UTF8; client_properties_entries[3].value.value.bytes = amqp_cstring_bytes(persistent ? "persistent" : "transient"); client_properties_entries[4].key = amqp_cstring_bytes("connection started"); client_properties_entries[4].value.kind = AMQP_FIELD_KIND_UTF8; client_properties_entries[4].value.value.bytes = amqp_cstring_bytes(std_datetime); client_properties_table.entries = client_properties_entries; client_properties_table.num_entries = sizeof(client_properties_entries) / sizeof(amqp_table_entry_t); custom_properties_entries[0].key = amqp_cstring_bytes("client"); custom_properties_entries[0].value.kind = AMQP_FIELD_KIND_TABLE; custom_properties_entries[0].value.value.table = client_properties_table; custom_properties_table.entries = custom_properties_entries; custom_properties_table.num_entries = sizeof(custom_properties_entries) / sizeof(amqp_table_entry_t); /* We can assume that connection established here but it is not true, real handshake goes during login */ assert(params->frame_max > 0); amqp_rpc_reply_t res = amqp_login_with_properties( resource->connection_state, params->vhost, params->channel_max, params->frame_max, params->heartbeat, &custom_properties_table, AMQP_SASL_METHOD_PLAIN, params->login, params->password ); efree(std_datetime); if (res.reply_type != AMQP_RESPONSE_NORMAL) { char *message, *long_message; php_amqp_connection_resource_error(res, &message, resource, 0 TSRMLS_CC); spprintf(&long_message, 0, "%s - Potential login failure.", message); zend_throw_exception(amqp_connection_exception_class_entry, long_message, 0 TSRMLS_CC); efree(message); efree(long_message); /* https://www.rabbitmq.com/resources/specs/amqp0-9-1.pdf * * 2.2.4 The Connection Class: * ... a peer that detects an error MUST close the socket without sending any further data. * * 4.10.2 Denial of Service Attacks: * ... The general response to any exceptional condition in the connection negotiation is to pause that connection * (presumably a thread) for a period of several seconds and then to close the network connection. This * includes syntax errors, over-sized data, and failed attempts to authenticate. */ connection_resource_destructor(resource, persistent TSRMLS_CC); return NULL; } /* Allocate space for the channel slots in the ring buffer */ resource->max_slots = (amqp_channel_t) amqp_get_channel_max(resource->connection_state); assert(resource->max_slots > 0); resource->slots = (amqp_channel_resource **)pecalloc(resource->max_slots + 1, sizeof(amqp_channel_object*), persistent); resource->is_connected = '\1'; return resource; }
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; }