瀏覽代碼

name-addr (see RFC3261) parser

Jan Janak 22 年之前
父節點
當前提交
5717778e5b
共有 3 個文件被更改,包括 164 次插入0 次删除
  1. 86 0
      parser/parse_nameaddr.c
  2. 59 0
      parser/parse_nameaddr.h
  3. 19 0
      parser/parser_f.h

+ 86 - 0
parser/parse_nameaddr.c

@@ -0,0 +1,86 @@
+/* 
+ * $Id$
+ *
+ * Copyright (C) 2001-2003 Fhg Fokus
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * History
+ * --------
+ * 2003-03-24 Created by janakj
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include "../dprint.h"
+#include "parse_nameaddr.h"
+#include "parser_f.h"
+
+/*
+ * Parse name-addr part, the given string can be longer,
+ * it will be updated to point right behind the name-addr part
+ */
+int parse_nameaddr(str* _s, name_addr_t* _a)
+{
+	char* uri_end;
+
+	if (!_s || !_a) {
+		LOG(L_ERR, "parse_nameaddr(): Invalid parameter value\n");
+		return -1;
+	}
+
+	memset(_a, 0, sizeof(name_addr_t));
+
+	_a->name.s = _s->s;
+
+	_a->uri.s = find_not_quoted(_s, '<'); 
+	if (_a->uri.s) {
+		_a->name.len = _a->uri.s - _a->name.s;
+		_a->uri.s++; /* We will skip < character */
+	} else {
+		LOG(L_ERR, "parse_nameaddr(): No < found\n");
+		return -3;
+	}
+	
+	_a->uri.len = _s->len - _a->name.len - 1;
+	uri_end = find_not_quoted(&_a->uri, '>');
+	
+	if (!uri_end) {
+		LOG(L_ERR, "parse_nameaddr(): No > found\n");
+		return -4;
+	}
+	
+	_a->uri.len = uri_end - _a->uri.s;
+	return 0;
+}
+
+
+/*
+ * Print a name-addr structure, just for debugging
+ */
+void print_nameaddr(name_addr_t* _a)
+{
+	printf("---name-addr---\n");
+	printf("name: '%.*s'\n", _a->name.len, _a->name.s);
+	printf("uri : '%.*s'\n", _a->uri.len, _a->uri.s);
+	printf("---/name-addr---\n");
+}

+ 59 - 0
parser/parse_nameaddr.h

@@ -0,0 +1,59 @@
+/* 
+ * $Id$
+ *
+ * Copyright (C) 2001-2003 Fhg Fokus
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * History
+ * --------
+ * 2003-03-24 Created by janakj
+ */
+
+#ifndef PARSE_NAMEADDR_H
+#define PARSE_NAMEADDR_H
+
+#include "../str.h"
+
+/*
+ * Name-addr structure, see RFC3261 for more details
+ */
+typedef struct name_addr {
+	str name;   /* Display name part */
+	str uri;    /* Uri part without surrounding <> */
+} name_addr_t;
+
+
+/*
+ * Parse name-addr part, the given string can be longer,
+ * it will be updated to point right behind the name-addr part
+ */
+int parse_nameaddr(str* _s, name_addr_t* _a);
+
+
+/*
+ * Print a name-addr structure, just for debugging
+ */
+void print_nameaddr(name_addr_t* _a);
+
+
+#endif /* PARSE_NAMEADDR_H */

+ 19 - 0
parser/parser_f.h

@@ -34,6 +34,7 @@
 #define parser_f_h
 #define parser_f_h
 
 
 #include "../comp_defs.h"
 #include "../comp_defs.h"
+#include "../str.h"
 
 
 char* eat_line(char* buffer, unsigned int len);
 char* eat_line(char* buffer, unsigned int len);
 
 
@@ -83,4 +84,22 @@ inline static int is_empty_end(char* p, char* pend )
 }
 }
 
 
 
 
+/*
+ * Find a character occurence that is not quoted
+ */
+inline static char* find_not_quoted(str* _s, char _c)
+{
+	int quoted = 0, i;
+	
+	for(i = 0; i < _s->len; i++) {
+		if (!quoted) {
+			if (_s->s[i] == '\"') quoted = 1;
+			else if (_s->s[i] == _c) return _s->s + i;
+		} else {
+			if ((_s->s[i] == '\"') && (_s->s[i - 1] != '\\')) quoted = 0;
+		}
+	}
+	return 0;
+}
+
 #endif /* parser_f_h */
 #endif /* parser_f_h */