view libsip/grok_from.c @ 16:4f81b959a5f5

sipout-test-voice: implement PCMU GSM uplink catcher
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 13 May 2024 22:10:25 -0800
parents 35c0d9f03c0a
children
line wrap: on
line source

/*
 * Here we implement further parsing of the From header,
 * beyond the preliminary step of uas_get_basic_headers().
 */

#include <ctype.h>
#include <string.h>
#include <strings.h>
#include "grok_from.h"

grok_from_header(from_hdr, gfr)
	char *from_hdr;
	struct grok_from *gfr;
{
	char *cp = from_hdr, *bp;
	int bracketed;

	if (*cp == '<' || !strncasecmp(cp, "sip:", 4)) {
		gfr->cnam = 0;
		gfr->cnam_len = 0;
		gfr->cnam_quoted = 0;
	} else {
		if (*cp == '"') {
			cp++;
			gfr->cnam = cp;
			for (;;) {
				if (!*cp)
					return(-1);
				if (*cp == '"')
					break;
				if (*cp++ == '\\') {
					if (!*cp)
						return(-1);
					cp++;
				}
			}
			gfr->cnam_len = cp - gfr->cnam;
			gfr->cnam_quoted = 1;
			cp++;
			while (isspace(*cp))
				cp++;
			if (*cp != '<')
				return(-1);
		} else {
			gfr->cnam = cp;
			for (;;) {
				if (!*cp)
					return(-1);
				if (*cp == '<')
					break;
				cp++;
			}
			for (bp = cp; bp > gfr->cnam && isspace(bp[-1]); bp--)
				;
			gfr->cnam_len = bp - gfr->cnam;
			gfr->cnam_quoted = 0;
		}
	}
	if (*cp == '<') {
		cp++;
		bracketed = 1;
	} else
		bracketed = 0;
	gfr->uri = cp;
	if (strncasecmp(cp, "sip:", 4))
		return(-1);
	cp += 4;
	gfr->user = cp;
	for (;;) {
		if (!*cp || *cp == '<' || *cp == '>')
			return(-1);
		if (*cp == '@')
			break;
		cp++;
	}
	if (cp == gfr->user)
		return(-1);
	gfr->user_len = cp - gfr->user;
	cp++;
	for (;;) {
		switch (*cp) {
		case '\0':
		case ';':
			if (bracketed)
				return(-1);
			else
				break;
		case '>':
			if (bracketed)
				break;
			else
				return(-1);
		case '<':
			return(-1);
		default:
			cp++;
			continue;
		}
		break;
	}
	gfr->uri_len = cp - gfr->uri;
	return(0);
}