Exemplo n.º 1
0
static void network_handle_accept(void *opaque) {
	Socket *server_socket = opaque;
	Socket *client_socket;
	struct sockaddr_storage address;
	socklen_t length = sizeof(address);
	char hostname[NI_MAXHOST];
	char port[NI_MAXSERV];
	char buffer[NI_MAXHOST + NI_MAXSERV + 4]; // 4 == strlen("[]:") + 1
	char *name = "<unknown>";
	Client *client;

	// accept new client socket
	client_socket = socket_accept(server_socket, (struct sockaddr *)&address, &length);

	if (client_socket == NULL) {
		if (!errno_interrupted()) {
			log_error("Could not accept new client socket: %s (%d)",
			          get_errno_name(errno), errno);
		}

		return;
	}

	if (socket_address_to_hostname((struct sockaddr *)&address, length,
	                               hostname, sizeof(hostname),
	                               port, sizeof(port)) < 0) {
		log_warn("Could not get hostname and port of client (socket: %d): %s (%d)",
		         client_socket->base.handle, get_errno_name(errno), errno);
	} else {
		if (address.ss_family == AF_INET6) {
			snprintf(buffer, sizeof(buffer), "[%s]:%s", hostname, port);
		} else {
			snprintf(buffer, sizeof(buffer), "%s:%s", hostname, port);
		}

		name = buffer;
	}

	// create new client
	client = network_create_client(name, &client_socket->base);

	if (client == NULL) {
		socket_destroy(client_socket);
		free(client_socket);

		return;
	}

#ifdef BRICKD_WITH_RED_BRICK
	client_send_red_brick_enumerate(client, ENUMERATION_TYPE_CONNECTED);
#endif
}
Exemplo n.º 2
0
void mesh_handle_accept(void *opaque) {
    char port[NI_MAXSERV];
    char *name = "<unknown>";
    char hostname[NI_MAXHOST];
    Socket *mesh_client_socket;
    // Socket that is created to the root node of a mesh network.
    struct sockaddr_storage address;
    socklen_t length = sizeof(address);
    Socket *mesh_listen_socket = opaque;
    char buffer[NI_MAXHOST + NI_MAXSERV + 4];

    log_info("New connection on mesh port");

    // Accept new mesh client socket.
    mesh_client_socket = socket_accept(mesh_listen_socket,
                                       (struct sockaddr *)&address,
                                       &length);

    if (mesh_client_socket == NULL) {
        if (!errno_interrupted()) {
            log_error("Failed to accept new mesh client connection: %s (%d)",
                      get_errno_name(errno), errno);
        }

        return;
    }

    if (socket_address_to_hostname((struct sockaddr *)&address, length,
                                   hostname, sizeof(hostname),
                                   port, sizeof(port)) < 0) {
        log_warn("Could not get hostname and port of mesh client (socket: %d): %s (%d)",
                 mesh_client_socket->base.handle, get_errno_name(errno), errno);
    }
    else {
        snprintf(buffer, sizeof(buffer), "%s:%s", hostname, port);

        name = buffer;
    }

    /*
     * Allocate and initialise a new mesh stack. Note that in this stage the stack
     * is not added to brickd's central list of stacks yet.
     */
    if (mesh_stack_create(name, &mesh_client_socket->base) < 0) {
        log_error("Could not create new mesh stack");
    }
    else {
        log_info("New mesh stack created");
    }
}