00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "../dprint.h"
00030 #include "msg_parser.h"
00031 #include "parser_f.h"
00032 #include "parse_methods.h"
00033 #include "../mem/mem.h"
00034 #include "../ut.h"
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 enum { START,
00045 INVITE1, INVITE2, INVITE3, INVITE4, INVITE5,
00046 ACK1, ACK2,
00047 CANCEL1, CANCEL2, CANCEL3, CANCEL4, CANCEL5,
00048 BYE1, BYE2,
00049 SIP1, SIP2, SIP3, SIP4, SIP5, SIP6,
00050 FIN_INVITE = 100, FIN_ACK, FIN_CANCEL, FIN_BYE, FIN_SIP,
00051 P_METHOD = 200, L_URI, P_URI, L_VER,
00052 VER1, VER2, VER3, VER4, VER5, VER6, FIN_VER,
00053 L_STATUS, P_STATUS, L_REASON, P_REASON,
00054 L_LF, F_CR, F_LF
00055 };
00056
00057
00058
00059 char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
00060 {
00061
00062 char *tmp;
00063 char* second;
00064 char* third;
00065 char* nl;
00066 unsigned int offset;
00067
00068 char* end;
00069 char s1,s2,s3;
00070 char *prn;
00071 unsigned int t;
00072
00073
00074
00075
00076
00077
00078
00079
00080 end=buffer+len;
00081
00082
00083
00084
00085
00086
00087
00088
00089 if (len <=16 ) {
00090 LM_INFO("message too short: %d\n", len);
00091 goto error1;
00092 }
00093
00094 tmp=buffer;
00095
00096 if ( (*tmp=='S' || *tmp=='s') &&
00097 strncasecmp( tmp+1, SIP_VERSION+1, SIP_VERSION_LEN-1)==0 &&
00098 (*(tmp+SIP_VERSION_LEN)==' ')) {
00099 fl->type=SIP_REPLY;
00100 fl->u.reply.version.len=SIP_VERSION_LEN;
00101 tmp=buffer+SIP_VERSION_LEN;
00102 } else IFISMETHOD( INVITE, 'I' )
00103 else IFISMETHOD( CANCEL, 'C')
00104 else IFISMETHOD( ACK, 'A' )
00105 else IFISMETHOD( BYE, 'B' )
00106 else IFISMETHOD( INFO, 'I' )
00107
00108
00109
00110
00111
00112
00113 else {
00114
00115
00116
00117 tmp=eat_token_end(buffer,buffer+len);
00118 if ((tmp==buffer)||(tmp>=end)){
00119 LM_INFO("empty or bad first line\n");
00120 goto error1;
00121 }
00122 if (*tmp!=' ') {
00123 LM_INFO("method not followed by SP\n");
00124 goto error1;
00125 }
00126 fl->type=SIP_REQUEST;
00127
00128
00129 if(parse_method(buffer, tmp,
00130 (unsigned int*)&fl->u.request.method_value)==0)
00131 {
00132 LM_INFO("failed to parse the method\n");
00133 goto error1;
00134 }
00135 fl->u.request.method.len=tmp-buffer;
00136 }
00137
00138
00139
00140
00141
00142 fl->u.request.method.s=buffer;
00143 second=tmp+1;
00144 offset=second-buffer;
00145
00146
00147
00148
00149 tmp=eat_token_end(second, second+len-offset);
00150 if (tmp>=end){
00151 goto error;
00152 }
00153 offset+=tmp-second;
00154 third=eat_space_end(tmp, tmp+len-offset);
00155 offset+=third-tmp;
00156 if ((third==tmp)||(tmp>=end)){
00157 goto error;
00158 }
00159 fl->u.request.uri.s=second;
00160 fl->u.request.uri.len=tmp-second;
00161
00162
00163 if (fl->type==SIP_REPLY) {
00164 if (fl->u.request.uri.len!=3) {
00165 LM_INFO("len(status code)!=3: %.*s\n",
00166 fl->u.request.uri.len, ZSW(second) );
00167 goto error;
00168 }
00169 s1=*second; s2=*(second+1);s3=*(second+2);
00170 if (s1>='0' && s1<='9' &&
00171 s2>='0' && s2<='9' &&
00172 s3>='0' && s3<='9' ) {
00173 fl->u.reply.statuscode=(s1-'0')*100+10*(s2-'0')+(s3-'0');
00174 } else {
00175 LM_INFO("status_code non-numerical: %.*s\n",
00176 fl->u.request.uri.len, ZSW(second) );
00177 goto error;
00178 }
00179 }
00180
00181
00182
00183
00184
00185 if (fl->type==SIP_REQUEST){
00186 tmp=eat_token_end(third,third+len-offset);
00187 offset+=tmp-third;
00188 if ((tmp==third)||(tmp>=end)){
00189 goto error;
00190 }
00191 if (! is_empty_end(tmp, tmp+len-offset)){
00192 goto error;
00193 }
00194 }else{
00195 tmp=eat_token2_end(third,third+len-offset,'\r');
00196
00197 if (tmp>=end){
00198 goto error;
00199 }
00200 offset+=tmp-third;
00201 }
00202 nl=eat_line(tmp,len-offset);
00203 if (nl>=end){
00204 goto error;
00205 }
00206 fl->u.request.version.s=third;
00207 fl->u.request.version.len=tmp-third;
00208 fl->len=nl-buffer;
00209
00210 return nl;
00211
00212 error:
00213 LM_ERR("bad %s first line\n",
00214 (fl->type==SIP_REPLY)?"reply(status)":"request");
00215
00216 LM_ERR("at line 0 char %d: \n", offset );
00217 prn=pkg_malloc( offset );
00218 if (prn) {
00219 for (t=0; t<offset; t++)
00220 if (*(buffer+t)) *(prn+t)=*(buffer+t);
00221 else *(prn+t)='°';
00222 LM_ERR("parsed so far: %.*s\n", offset, ZSW(prn) );
00223 pkg_free( prn );
00224 };
00225 error1:
00226 fl->type=SIP_INVALID;
00227 LM_INFO("bad message\n");
00228
00229 nl=eat_line(buffer,len);
00230 return nl;
00231 }