/*
 *	Check for an Accounting-Stop
 *	If we find one and we have allocated an IP to this nas/port
 *	combination, then deallocate it.
 */
static rlm_rcode_t CC_HINT(nonnull) mod_accounting(void *instance, REQUEST *request)
{
	int			rcode = RLM_MODULE_NOOP;
	VALUE_PAIR		*vp;

	int			acct_status_type;

	rlm_sqlippool_t		*inst = (rlm_sqlippool_t *) instance;
	rlm_sql_handle_t	*handle;

	vp = fr_pair_find_by_num(request->packet->vps, PW_ACCT_STATUS_TYPE, 0, TAG_ANY);
	if (!vp) {
		RDEBUG("Could not find account status type in packet");
		return RLM_MODULE_NOOP;
	}
	acct_status_type = vp->vp_integer;

	switch (acct_status_type) {
	case PW_STATUS_START:
	case PW_STATUS_ALIVE:
	case PW_STATUS_STOP:
	case PW_STATUS_ACCOUNTING_ON:
	case PW_STATUS_ACCOUNTING_OFF:
		break;		/* continue through to the next section */

	default:
		/* We don't care about any other accounting packet */
		return RLM_MODULE_NOOP;
	}

	handle = fr_connection_get(inst->sql_inst->pool);
	if (!handle) {
		RDEBUG("Failed reserving SQL connection");
		return RLM_MODULE_FAIL;
	}

	if (inst->sql_inst->sql_set_user(inst->sql_inst, request, NULL) < 0) return RLM_MODULE_FAIL;

	switch (acct_status_type) {
	case PW_STATUS_START:
		rcode = mod_accounting_start(&handle, inst, request);
		break;

	case PW_STATUS_ALIVE:
		rcode = mod_accounting_alive(&handle, inst, request);
		break;

	case PW_STATUS_STOP:
		rcode = mod_accounting_stop(&handle, inst, request);
		break;

	case PW_STATUS_ACCOUNTING_ON:
		rcode = mod_accounting_on(&handle, inst, request);
		break;

	case PW_STATUS_ACCOUNTING_OFF:
		rcode = mod_accounting_off(&handle, inst, request);
		break;
	}

	fr_connection_release(inst->sql_inst->pool, handle);

	return rcode;
}
/*
 *	Check for an Accounting-Stop
 *	If we find one and we have allocated an IP to this nas/port
 *	combination, then deallocate it.
 */
static rlm_rcode_t mod_accounting(void *instance, REQUEST *request)
{
    int rcode = RLM_MODULE_NOOP;
    VALUE_PAIR *vp;
    int acct_status_type;
    rlm_sqlippool_t *inst = (rlm_sqlippool_t *) instance;
    rlm_sql_handle_t *handle;

    vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE, 0, TAG_ANY);
    if (!vp) {
        RDEBUG("Could not find account status type in packet.");
        return RLM_MODULE_NOOP;
    }
    acct_status_type = vp->vp_integer;

    switch (acct_status_type) {
    case PW_STATUS_START:
    case PW_STATUS_ALIVE:
    case PW_STATUS_STOP:
    case PW_STATUS_ACCOUNTING_ON:
    case PW_STATUS_ACCOUNTING_OFF:
        break;		/* continue through to the next section */

    default:
        /* We don't care about any other accounting packet */
        return RLM_MODULE_NOOP;
    }

    handle = inst->sql_inst->sql_get_socket(inst->sql_inst);
    if (!handle) {
        RDEBUG("Cannot allocate sql connection");
        return RLM_MODULE_FAIL;
    }

    if (inst->sql_inst->sql_set_user(inst->sql_inst, request, NULL) < 0) {
        return RLM_MODULE_FAIL;
    }

    switch (acct_status_type) {
    case PW_STATUS_START:
        rcode = mod_accounting_start(handle, inst, request);
        break;

    case PW_STATUS_ALIVE:
        rcode = mod_accounting_alive(handle, inst, request);
        break;

    case PW_STATUS_STOP:
        rcode = mod_accounting_stop(handle, inst, request);
        break;

    case PW_STATUS_ACCOUNTING_ON:
        rcode = mod_accounting_on(handle, inst, request);
        break;

    case PW_STATUS_ACCOUNTING_OFF:
        rcode = mod_accounting_off(handle, inst, request);
        break;
    }

    inst->sql_inst->sql_release_socket(inst->sql_inst, handle);

    return rcode;
}