Exemplo n.º 1
0
int custom_hdrs_add(struct list *hdrs, const char *name,
		    const char *fmt, ...)
{
	struct pl temp_pl = { NULL, 0 };
	struct sip_hdr *hdr;
	char *value = NULL;
	va_list ap;
	int err = 0;

	va_start(ap, fmt);
	err = re_vsdprintf(&value, fmt, ap);
	va_end(ap);

	hdr = mem_zalloc(sizeof(*hdr), hdr_destructor);
	if (!hdr || !value)
		goto error;

	pl_set_str(&temp_pl, name);
	err = pl_dup(&hdr->name, &temp_pl);
	if (err)
		goto error;

	pl_set_str(&hdr->val, value);

	hdr->id = SIP_HDR_NONE;

	list_append(hdrs, &hdr->le, hdr);

	return 0;

error:
	mem_deref(hdr);
	return err;
}
Exemplo n.º 2
0
/**
 * Print a formatted string to a buffer
 *
 * @param strp Buffer pointer for output string
 * @param fmt  Formatted string
 *
 * @return 0 if success, otherwise errorcode
 */
int re_sdprintf(char **strp, const char *fmt, ...)
{
	va_list ap;
	int err;

	va_start(ap, fmt);
	err = re_vsdprintf(strp, fmt, ap);
	va_end(ap);

	return err;
}
Exemplo n.º 3
0
int sip_req_send(struct ua *ua, const char *method, const char *uri,
                 sip_resp_h *resph, void *arg, const char *fmt, ...)
{
    const char *routev[1];
    struct sip_req *sr;
    int err;

    info("baresip.sipreq.sip_req_send()\n");

    if (!ua || !method || !uri || !fmt)
        return EINVAL;

    routev[0] = ua_outbound(ua);

    sr = mem_zalloc(sizeof(*sr), destructor);
    if (!sr)
        return ENOMEM;

    sr->resph = resph;
    sr->arg   = arg;

    err = str_dup(&sr->method, method);

    if (fmt) {
        va_list ap;

        va_start(ap, fmt);
        err |= re_vsdprintf(&sr->fmt, fmt, ap);
        va_end(ap);
    }

    if (err)
        goto out;

    err = sip_dialog_alloc(&sr->dlg, uri, uri, NULL, ua_aor(ua),
                           routev[0] ? routev : NULL,
                           routev[0] ? 1 : 0);
    if (err)
        goto out;

    err = sip_auth_alloc(&sr->auth, auth_handler, ua_prm(ua), true);
    if (err)
        goto out;

    err = request(sr);

out:
    if (err)
        mem_deref(sr);

    return err;
}
Exemplo n.º 4
0
int sdp_format_set_params(struct sdp_format *fmt, const char *params, ...)
{
	int err = 0;

	if (!fmt)
		return EINVAL;

	fmt->params = mem_deref(fmt->params);

	if (params) {
		va_list ap;

		va_start(ap, params);
		err = re_vsdprintf(&fmt->params, params, ap);
		va_end(ap);
	}

	return err;
}
Exemplo n.º 5
0
void vlog(enum log_level level, const char *fmt, va_list ap)
{
	struct le *le;
	char *msg;
	int err;

	err = re_vsdprintf(&msg, fmt, ap);
	if (err)
		return;

	if (lg.stder) {

		bool color = level == LOG_LEVEL_WARN
			  || level == LOG_LEVEL_ERROR;

		if (color)
			(void)re_fprintf(stderr, "\x1b[31m"); /* Red */

		(void)re_fprintf(stderr, "%s", msg);

		if (color)
			(void)re_fprintf(stderr, "\x1b[;m");
	}

	le = lg.logl.head;

	while (le) {

		struct log *log = le->data;
		le = le->next;

		if (log->h)
			log->h(level, msg);
	}

	mem_deref(msg);
}
Exemplo n.º 6
0
int mqtt_publish_message(struct mqtt *mqtt, const char *topic,
			 const char *fmt, ...)
{
	char *message;
	va_list ap;
	int ret;
	int err = 0;

	if (!mqtt || !topic || !fmt)
		return EINVAL;

	va_start(ap, fmt);
	err = re_vsdprintf(&message, fmt, ap);
	va_end(ap);

	if (err)
		return err;

	ret = mosquitto_publish(mqtt->mosq,
				NULL,
				topic,
				(int)str_len(message),
				message,
				0,
				false);
	if (ret != MOSQ_ERR_SUCCESS) {
		warning("mqtt: failed to publish (%s)\n",
			mosquitto_strerror(ret));
		err = EINVAL;
		goto out;
	}

 out:
	mem_deref(message);
	return err;
}
Exemplo n.º 7
0
int sdp_format_add(struct sdp_format **fmtp, struct sdp_media *m,
		   bool prepend, const char *id, const char *name,
		   uint32_t srate, uint8_t ch,
		   sdp_fmtp_cmp_h *cmph, void *data, bool ref,
		   const char *params, ...)
{
	struct sdp_format *fmt;
	int err;

	if (!m)
		return EINVAL;

	if (!id && (m->dynpt > RTP_DYNPT_END))
		return ERANGE;

	fmt = mem_zalloc(sizeof(*fmt), destructor);
	if (!fmt)
		return ENOMEM;

	if (prepend)
		list_prepend(&m->lfmtl, &fmt->le, fmt);
	else
		list_append(&m->lfmtl, &fmt->le, fmt);

	if (id)
		err = str_dup(&fmt->id, id);
	else
		err = re_sdprintf(&fmt->id, "%i", m->dynpt++);
	if (err)
		goto out;

	if (name) {
		err = str_dup(&fmt->name, name);
		if (err)
			goto out;
	}

	if (params) {
		va_list ap;

		va_start(ap, params);
		err = re_vsdprintf(&fmt->params, params, ap);
		va_end(ap);

		if (err)
			goto out;
	}

	fmt->pt    = atoi(fmt->id);
	fmt->srate = srate;
	fmt->ch    = ch;
	fmt->cmph  = cmph;
	fmt->data  = ref ? mem_ref(data) : data;
	fmt->ref   = ref;
	fmt->sup   = true;

 out:
	if (err)
		mem_deref(fmt);
	else if (fmtp)
		*fmtp = fmt;

	return err;
}
Exemplo n.º 8
0
int sipevent_accept(struct sipnot **notp, struct sipevent_sock *sock,
		    const struct sip_msg *msg, struct sip_dialog *dlg,
		    const struct sipevent_event *event,
		    uint16_t scode, const char *reason, uint32_t expires_min,
		    uint32_t expires_dfl, uint32_t expires_max,
		    const char *cuser, const char *ctype,
		    sip_auth_h *authh, void *aarg, bool aref,
		    sipnot_close_h *closeh, void *arg, const char *fmt, ...)
{
	struct sipnot *not;
	uint32_t expires;
	int err;

	if (!notp || !sock || !msg || !scode || !reason || !expires_dfl ||
	    !expires_max || !cuser || !ctype || expires_dfl < expires_min)
		return EINVAL;

	not = mem_zalloc(sizeof(*not), destructor);
	if (!not)
		return ENOMEM;

	if (!pl_strcmp(&msg->met, "REFER")) {

		err = str_dup(&not->event, "refer");
		if (err)
			goto out;

		err = re_sdprintf(&not->id, "%u", msg->cseq.num);
		if (err)
			goto out;
	}
	else {
		if (!event) {
			err = EINVAL;
			goto out;
		}

		err = pl_strdup(&not->event, &event->event);
		if (err)
			goto out;

		if (pl_isset(&event->id)) {

			err = pl_strdup(&not->id, &event->id);
			if (err)
				goto out;
		}
	}

	if (dlg) {
		not->dlg = mem_ref(dlg);
	}
	else {
		err = sip_dialog_accept(&not->dlg, msg);
		if (err)
			goto out;
	}

	hash_append(sock->ht_not,
		    hash_joaat_str(sip_dialog_callid(not->dlg)),
		    &not->he, not);

	err = sip_auth_alloc(&not->auth, authh, aarg, aref);
	if (err)
		goto out;

	err = str_dup(&not->cuser, cuser);
	if (err)
		goto out;

	err = str_dup(&not->ctype, ctype);
	if (err)
		goto out;

	if (fmt) {
		va_list ap;

		va_start(ap, fmt);
		err = re_vsdprintf(&not->hdrs, fmt, ap);
		va_end(ap);
		if (err)
			goto out;
	}

	not->expires_min = expires_min;
	not->expires_dfl = expires_dfl;
	not->expires_max = expires_max;
	not->substate = SIPEVENT_PENDING;
	not->sock   = mem_ref(sock);
	not->sip    = mem_ref(sock->sip);
	not->closeh = closeh ? closeh : internal_close_handler;
	not->arg    = arg;

	if (pl_isset(&msg->expires))
		expires = pl_u32(&msg->expires);
	else
		expires = not->expires_dfl;

	sipnot_refresh(not, expires);

	err = sipnot_reply(not, msg, scode, reason);
	if (err)
		goto out;

	not->subscribed = true;

 out:
	if (err)
		mem_deref(not);
	else
		*notp = not;

	return err;
}