parse.c 2.89 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (c) 2012-2019 Belledonne Communications SARL.
 *
 * This file is part of belle-sip.
 *
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (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/>.
 */
Simon Morlat's avatar
Simon Morlat committed
19 20 21 22 23 24 25


#include <stdio.h>

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

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

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

45 46
	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
47 48
		return -1;
	}
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	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
66 67
		return -1;
	}
68

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

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