Browse code

core: memset 0 the struct in first line parsing; split type and flags

- initialize to 0 first line struct msg_start_t in parse_first_line()
- split field int type in short type and short flags to be able to store
more info about first line without changes in other places of existing code
- set in flags if the protocol in first line is sip or http - useful to
avoid string comparison whenever needed to get the two very used
protocols

Daniel-Constantin Mierla authored on 07/01/2015 17:04:58
Showing 2 changed files
... ...
@@ -45,6 +45,11 @@
45 45
 #include "../mem/mem.h"
46 46
 #include "../ut.h"
47 47
 
48
+/* flags for first line
49
+ * - stored on a short field (16 flags) */
50
+#define FLINE_FLAG_PROTO_SIP	(1<<0)
51
+#define FLINE_FLAG_PROTO_HTTP	(1<<1)
52
+
48 53
 int http_reply_parse = 0;
49 54
 
50 55
 /* grammar:
... ...
@@ -56,7 +61,7 @@ int http_reply_parse = 0;
56 61
 
57 62
 /* parses the first line, returns pointer to  next line  & fills fl;
58 63
    also  modifies buffer (to avoid extra copy ops) */
59
-char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
64
+char* parse_first_line(char* buffer, unsigned int len, struct msg_start* fl)
60 65
 {
61 66
 	
62 67
 	char *tmp;
... ...
@@ -77,6 +82,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
77 82
 	*/
78 83
 	
79 84
 
85
+	memset(fl, 0, sizeof(struct msg_start));
80 86
 	offset = 0;
81 87
 	end=buffer+len;
82 88
 	/* see if it's a reply (status) */
... ...
@@ -97,6 +103,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
97 103
 		strncasecmp( tmp+1, SIP_VERSION+1, SIP_VERSION_LEN-1)==0 &&
98 104
 		(*(tmp+SIP_VERSION_LEN)==' ')) {
99 105
 			fl->type=SIP_REPLY;
106
+			fl->flags|=FLINE_FLAG_PROTO_SIP;
100 107
 			fl->u.reply.version.len=SIP_VERSION_LEN;
101 108
 			tmp=buffer+SIP_VERSION_LEN;
102 109
 	} else if (http_reply_parse != 0 &&
... ...
@@ -111,6 +118,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
111 118
 			 *       - the message is marked as SIP_REPLY (ugly)
112 119
 			 */
113 120
 				fl->type=SIP_REPLY;
121
+				fl->flags|=FLINE_FLAG_PROTO_HTTP;
114 122
 				fl->u.reply.version.len=HTTP_VERSION_LEN+1 /*include last digit*/;
115 123
 				tmp=buffer+HTTP_VERSION_LEN+1 /* last digit */;
116 124
 	} else IFISMETHOD( INVITE, 'I' )
... ...
@@ -223,6 +231,22 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
223 231
 	fl->u.request.version.len=tmp-third;
224 232
 	fl->len=nl-buffer;
225 233
 
234
+	if (fl->type==SIP_REQUEST) {
235
+		if(fl->u.request.version.len >= SIP_VERSION_LEN
236
+				&& (fl->u.request.version.s[0]=='S'
237
+					|| fl->u.request.version.s[0]=='s')
238
+				&& !strncasecmp(fl->u.request.version.s+1,
239
+					SIP_VERSION+1, SIP_VERSION_LEN-1)) {
240
+			fl->flags|=FLINE_FLAG_PROTO_SIP;
241
+		} else if(fl->u.request.version.len >= HTTP_VERSION_LEN
242
+				&& (fl->u.request.version.s[0]=='H'
243
+					|| fl->u.request.version.s[0]=='h')
244
+				&& !strncasecmp(fl->u.request.version.s+1,
245
+					HTTP_VERSION+1, HTTP_VERSION_LEN-1)) {
246
+			fl->flags|=FLINE_FLAG_PROTO_HTTP;
247
+		}
248
+	}
249
+
226 250
 	return nl;
227 251
 
228 252
 error:
... ...
@@ -245,3 +269,12 @@ error1:
245 269
 	nl=eat_line(buffer,len);
246 270
 	return nl;
247 271
 }
272
+
273
+char* parse_fline(char* buffer, char* end, struct msg_start* fl)
274
+{
275
+	if(end<=buffer) {
276
+		/* make it throw error via parse_first_line() for consistency */
277
+		return parse_first_line(buffer, 0, fl);
278
+	}
279
+	return parse_first_line(buffer, (unsigned int)(end-buffer), fl);
280
+}
... ...
@@ -69,7 +69,8 @@
69 69
 #define PUBLISH_LEN 7
70 70
 
71 71
 struct msg_start {
72
-	int type;					/*!< Type of the Message - Request/Response */
72
+	short type;					/*!< Type of the message - request/response */
73
+	short flags;				/*!< First line flags */
73 74
 	int len; 					/*!< length including delimiter */
74 75
 	union {
75 76
 		struct {