Пример #1
0
/*
  create a temporary mailslot handler for a reply mailslot, allocating
  a new mailslot name using the given base name and a random integer extension
*/
struct dgram_mailslot_handler *dgram_mailslot_temp(struct nbt_dgram_socket *dgmsock,
        const char *mailslot_name,
        dgram_mailslot_handler_t handler,
        void *private_data)
{
    char *name;
    int i;
    struct dgram_mailslot_handler *dgmslot;

    /* try a 100 times at most */
    for (i=0; i<100; i++) {
        name = talloc_asprintf(dgmsock, "%s%03u",
                               mailslot_name,
                               generate_random() % 1000);
        if (name == NULL) return NULL;
        if (dgram_mailslot_find(dgmsock, name)) {
            talloc_free(name);
            return NULL;
        }
        dgmslot = dgram_mailslot_listen(dgmsock, name, handler, private_data);
        talloc_free(name);
        if (dgmslot != NULL) {
            return dgmslot;
        }
    }
    DEBUG(2,("Unable to create temporary mailslot from %s\n", mailslot_name));
    return NULL;
}
Пример #2
0
/*
  setup the port 138 datagram listener for a given interface
*/
NTSTATUS nbtd_dgram_setup(struct nbtd_interface *iface, const char *bind_address)
{
	struct nbt_dgram_socket *bcast_dgmsock = NULL;
	struct nbtd_server *nbtsrv = iface->nbtsrv;
	struct socket_address *bcast_addr, *bind_addr;
	NTSTATUS status;
	TALLOC_CTX *tmp_ctx = talloc_new(iface);
	/* the list of mailslots that we are interested in */
	int i;

	if (!tmp_ctx) {
		return NT_STATUS_NO_MEMORY;
	}

	if (strcmp("0.0.0.0", iface->netmask) != 0) {
		/* listen for broadcasts on port 138 */
		bcast_dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
		if (!bcast_dgmsock) {
			talloc_free(tmp_ctx);
			return NT_STATUS_NO_MEMORY;
		}
	
		bcast_addr = socket_address_from_strings(tmp_ctx, bcast_dgmsock->sock->backend_name, 
							 iface->bcast_address, lp_dgram_port());
		if (!bcast_addr) {
			talloc_free(tmp_ctx);
			return NT_STATUS_NO_MEMORY;
		}

		status = socket_listen(bcast_dgmsock->sock, bcast_addr, 0, 0);
		if (!NT_STATUS_IS_OK(status)) {
			talloc_free(tmp_ctx);
			DEBUG(0,("Failed to bind to %s:%d - %s\n", 
				 iface->bcast_address, lp_dgram_port(), nt_errstr(status)));
			return status;
		}
	
		dgram_set_incoming_handler(bcast_dgmsock, dgram_request_handler, iface);
	}

	/* listen for unicasts on port 138 */
	iface->dgmsock = nbt_dgram_socket_init(iface, nbtsrv->task->event_ctx);
	if (!iface->dgmsock) {
		talloc_free(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	bind_addr = socket_address_from_strings(tmp_ctx, iface->dgmsock->sock->backend_name, 
						bind_address, lp_dgram_port());
	if (!bind_addr) {
		talloc_free(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	status = socket_listen(iface->dgmsock->sock, bind_addr, 0, 0);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(tmp_ctx);
		DEBUG(0,("Failed to bind to %s:%d - %s\n", 
			 bind_address, lp_dgram_port(), nt_errstr(status)));
		return status;
	}

	dgram_set_incoming_handler(iface->dgmsock, dgram_request_handler, iface);

	talloc_free(tmp_ctx);

	for (i=0;i<ARRAY_SIZE(mailslot_handlers);i++) {
		/* note that we don't need to keep the pointer
		   to the dgmslot around - the callback is all
		   we need */
		struct dgram_mailslot_handler *dgmslot;

		if (bcast_dgmsock) {
			dgmslot = dgram_mailslot_listen(bcast_dgmsock, 
						mailslot_handlers[i].mailslot_name,
						mailslot_handlers[i].handler, iface);
			NT_STATUS_HAVE_NO_MEMORY(dgmslot);
		}

		dgmslot = dgram_mailslot_listen(iface->dgmsock, 
						mailslot_handlers[i].mailslot_name,
						mailslot_handlers[i].handler, iface);
		NT_STATUS_HAVE_NO_MEMORY(dgmslot);
	}

	return NT_STATUS_OK;
}