parse.c 2.89 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation, either version 2 of the License, or
Simon Morlat's avatar
Simon Morlat committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    (at your option) any later version.

    This program 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, see <http://www.gnu.org/licenses/>.
*/


#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
25
#ifdef _WIN32
26 27
#define strcasecmp _stricmp
#else
Simon Morlat's avatar
Simon Morlat committed
28
#include <unistd.h>
29
#endif
Simon Morlat's avatar
Simon Morlat committed
30 31 32
#include <string.h>

#include "belle-sip/belle-sip.h"
33
#include "belle-sip/belle-sdp.h"
Ghislain MARY's avatar
Ghislain MARY committed
34
#include <bctoolbox/vfs.h>
Simon Morlat's avatar
Simon Morlat committed
35 36 37 38 39 40

int main(int argc, char *argv[]){
	char *str;
	struct stat st;
	int fd;
	int i;
41 42
	const char *filename=NULL;
	const char *protocol="sip";
43

44 45
	if (argc<2){
		fprintf(stderr,"Usage:\n%s [--protocol sip|http|sdp] <text file containing messages>\n",argv[0]);
Simon Morlat's avatar
Simon Morlat committed
46 47
		return -1;
	}
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	for(i=1;i<argc;++i){
		if (strcmp(argv[i],"--protocol")==0){
			i++;
			if (i<argc){
				protocol=argv[i];
			}else{
				fprintf(stderr,"Missing argument for --protocol\n");
				return -1;
			}
		}else filename=argv[i];
	}
	if (!filename){
		fprintf(stderr,"No filename specified\n");
				return -1;
	}
	if (stat(filename,&st)==-1){
		fprintf(stderr,"Could not stat %s: %s\n",filename,strerror(errno));
Simon Morlat's avatar
Simon Morlat committed
65 66
		return -1;
	}
67

68
	fd=open(filename,O_RDONLY);
Simon Morlat's avatar
Simon Morlat committed
69
	if (fd==-1){
70
		fprintf(stderr,"Could not open %s: %s\n",filename,strerror(errno));
Simon Morlat's avatar
Simon Morlat committed
71 72 73 74
		return -1;
	}
	str=belle_sip_malloc0(st.st_size+1);
	if (read(fd,str,st.st_size)==-1){
75
		fprintf(stderr,"Could not read %s: %s\n",filename,strerror(errno));
Simon Morlat's avatar
Simon Morlat committed
76 77 78 79 80
		belle_sip_free(str);
		close(fd);
		return -1;
	}
	close(fd);
81
	belle_sip_set_log_level(BELLE_SIP_LOG_DEBUG);
82

Ghislain MARY's avatar
Ghislain MARY committed
83 84
	for (i=0;i<(int)st.st_size;){
		size_t nbread;
85
		if (strcasecmp(protocol,"sip")==0 || strcasecmp(protocol,"http")==0){
Simon Morlat's avatar
Simon Morlat committed
86 87 88
			belle_sip_message_t *msg;
			uint64_t begin,end;
			begin=belle_sip_time_ms();
Ghislain MARY's avatar
Ghislain MARY committed
89
			msg=belle_sip_message_parse_raw(str+i,st.st_size-i,&nbread);
Simon Morlat's avatar
Simon Morlat committed
90
			end=belle_sip_time_ms();
91
			if (msg){
Ghislain MARY's avatar
Ghislain MARY committed
92
				printf("Successfully parsed %s message of %i bytes in %i ms.\n",protocol,(int)nbread, (int)(end-begin));
93
			}else{
94
				fprintf(stderr,"Failed to parse message.\n");
95 96
				break;
			}
Ghislain MARY's avatar
Ghislain MARY committed
97
			i+=(int)nbread;
98 99 100
		}else if (strcasecmp(protocol,"sdp")==0){
			belle_sdp_session_description_t *sdp=belle_sdp_session_description_parse(str);
			if (sdp){
101
				printf("Successfully parsed %s message of %i bytes.\n",protocol,(int)strlen(str));
102
			}else{
103
				fprintf(stderr,"Failed to parse SDP message.\n");
104
			}
Simon Morlat's avatar
Simon Morlat committed
105 106 107 108 109 110
			break;
		}
	}
	belle_sip_free(str);
	return 0;
}