Skip to content

Commit

Permalink
core: memset 0 the struct in first line parsing; split type and flags
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
miconda committed Jan 7, 2015
1 parent b22c63a commit 9328918
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
35 changes: 34 additions & 1 deletion parser/parse_fline.c
Expand Up @@ -45,6 +45,11 @@
#include "../mem/mem.h"
#include "../ut.h"

/* flags for first line
* - stored on a short field (16 flags) */
#define FLINE_FLAG_PROTO_SIP (1<<0)
#define FLINE_FLAG_PROTO_HTTP (1<<1)

int http_reply_parse = 0;

/* grammar:
Expand All @@ -56,7 +61,7 @@ int http_reply_parse = 0;

/* parses the first line, returns pointer to next line & fills fl;
also modifies buffer (to avoid extra copy ops) */
char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
char* parse_first_line(char* buffer, unsigned int len, struct msg_start* fl)
{

char *tmp;
Expand All @@ -77,6 +82,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
*/


memset(fl, 0, sizeof(struct msg_start));
offset = 0;
end=buffer+len;
/* see if it's a reply (status) */
Expand All @@ -97,6 +103,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
strncasecmp( tmp+1, SIP_VERSION+1, SIP_VERSION_LEN-1)==0 &&
(*(tmp+SIP_VERSION_LEN)==' ')) {
fl->type=SIP_REPLY;
fl->flags|=FLINE_FLAG_PROTO_SIP;
fl->u.reply.version.len=SIP_VERSION_LEN;
tmp=buffer+SIP_VERSION_LEN;
} else if (http_reply_parse != 0 &&
Expand All @@ -111,6 +118,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
* - the message is marked as SIP_REPLY (ugly)
*/
fl->type=SIP_REPLY;
fl->flags|=FLINE_FLAG_PROTO_HTTP;
fl->u.reply.version.len=HTTP_VERSION_LEN+1 /*include last digit*/;
tmp=buffer+HTTP_VERSION_LEN+1 /* last digit */;
} else IFISMETHOD( INVITE, 'I' )
Expand Down Expand Up @@ -223,6 +231,22 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
fl->u.request.version.len=tmp-third;
fl->len=nl-buffer;

if (fl->type==SIP_REQUEST) {
if(fl->u.request.version.len >= SIP_VERSION_LEN
&& (fl->u.request.version.s[0]=='S'
|| fl->u.request.version.s[0]=='s')
&& !strncasecmp(fl->u.request.version.s+1,
SIP_VERSION+1, SIP_VERSION_LEN-1)) {
fl->flags|=FLINE_FLAG_PROTO_SIP;
} else if(fl->u.request.version.len >= HTTP_VERSION_LEN
&& (fl->u.request.version.s[0]=='H'
|| fl->u.request.version.s[0]=='h')
&& !strncasecmp(fl->u.request.version.s+1,
HTTP_VERSION+1, HTTP_VERSION_LEN-1)) {
fl->flags|=FLINE_FLAG_PROTO_HTTP;
}
}

return nl;

error:
Expand All @@ -245,3 +269,12 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
nl=eat_line(buffer,len);
return nl;
}

char* parse_fline(char* buffer, char* end, struct msg_start* fl)
{
if(end<=buffer) {
/* make it throw error via parse_first_line() for consistency */
return parse_first_line(buffer, 0, fl);
}
return parse_first_line(buffer, (unsigned int)(end-buffer), fl);
}
3 changes: 2 additions & 1 deletion parser/parse_fline.h
Expand Up @@ -69,7 +69,8 @@
#define PUBLISH_LEN 7

struct msg_start {
int type; /*!< Type of the Message - Request/Response */
short type; /*!< Type of the message - request/response */
short flags; /*!< First line flags */
int len; /*!< length including delimiter */
union {
struct {
Expand Down

0 comments on commit 9328918

Please sign in to comment.