int print_encoded_route(FILE *fd,char *hdr,int hdrlen,unsigned char* payload,int paylen,char *prefix) { int i=2;/* flags + urilength */ unsigned char flags=0; flags=payload[0]; fprintf(fd,"%s",prefix); for(i=0;i<paylen;i++) fprintf(fd,"%s%d%s",i==0?"ENCODED ROUTE=[":":",payload[i],i==paylen-1?"]\n":""); i=2; if(flags & HAS_NAME_F){ fprintf(fd,"%sNAME=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(print_encoded_uri(fd,&payload[i],payload[1],hdr,hdrlen,strcat(prefix," "))<0){ prefix[strlen(prefix)-2]=0; fprintf(fd,"Error parsing URI\n"); return -1; } prefix[strlen(prefix)-2]=0; print_encoded_parameters(fd,&payload[i+payload[1]],hdr,paylen-i-payload[1],prefix); /* for(i+=payload[1];i<paylen-1;i+=2){ fprintf(fd,"%s[PARAMETER[%.*s]",prefix,payload[i+1]-payload[i]-1,&hdr[payload[i]]); fprintf(fd,"VALUE[%.*s]]\n",(payload[i+2]-payload[i+1])==0?0:(payload[i+2]-payload[i+1]-1),&hdr[payload[i+1]]); }*/ return 0; }
int print_encoded_to_body(FILE *fp,char *hdr,int hdrlen,unsigned char* payload,int paylen,char *prefix) { int i=2;/* flags + urilength */ unsigned char flags=0; flags=payload[0]; fprintf(fp,"%s",prefix); for(i=0;i<paylen;i++) fprintf(fp,"%s%d%s",i==0?"BODY CODE=[":":",payload[i],i==paylen-1?"]\n":""); i=2; if(flags & HAS_DISPLAY_F){ fprintf(fp,"%sDISPLAY NAME=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags & HAS_TAG_F){ fprintf(fp,"%sTAG=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(print_encoded_uri(fp,&payload[i],payload[1],hdr,hdrlen,strcat(prefix," "))<0){ fprintf(fp,"Error parsing URI\n"); prefix[strlen(prefix)-2]=0; return -1; } prefix[strlen(prefix)-2]=0; print_encoded_parameters(fp,&payload[i+payload[1]],hdr,paylen-i-payload[1],prefix); return 0; }
int print_encoded_digest(FILE *fd,char *hdr,int hdrlen,unsigned char* payload,int paylen,char *prefix) { int i=2;/* flags + flags1 */ unsigned char flags1,flags2; flags1=payload[0]; flags2=payload[1]; fprintf(fd,"%s",prefix); for(i=0;i<paylen;i++) fprintf(fd,"%s%d%s",i==0?"ENCODED DIGEST=[":":",payload[i],i==paylen-1?"]\n":""); i=2; if(flags1 & HAS_NAME_F){ fprintf(fd,"%sDIGEST NAME=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags1& HAS_REALM_F){ fprintf(fd,"%sDIGEST REALM=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags1& HAS_NONCE_F){ fprintf(fd,"%sDIGEST NONCE=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags1& HAS_URI_F){ if(print_encoded_uri(fd,&payload[i+1],payload[i],hdr,hdrlen,strcat(prefix," "))<0){ prefix[strlen(prefix)-2]=0; fprintf(fd,"Error parsing encoded URI\n"); return -1; } i+=payload[i]+1; } if(flags1& HAS_RESPONSE_F){ fprintf(fd,"%sDIGEST RESPONSE=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags1& HAS_ALG_F){ fprintf(fd,"%sDIGEST ALGORITHM=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags1& HAS_CNONCE_F){ fprintf(fd,"%sDIGEST CNONCE=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags1& HAS_OPAQUE_F){ fprintf(fd,"%sDIGEST OPAQUE=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags2& HAS_QoP_F){ fprintf(fd,"%sDIGEST QualityOfProtection=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags2& HAS_NC_F){ fprintf(fd,"%sDIGEST NonceCount=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } return 0; }
int print_encoded_msg(int fd,char *code,char *prefix) { unsigned short int i,j,k,l,m,msglen; char r,*msg; unsigned char *payload; FILE *fp; fp = fdopen(fd, "w*"); if(fp == NULL) return -1; payload=(unsigned char*)code; memcpy(&i,code,2); memcpy(&j,&code[MSG_START_IDX],2); memcpy(&msglen,&code[MSG_LEN_IDX],2); i=ntohs(i); j=ntohs(j); msglen=ntohs(msglen); for(k=0; k<j; k++) fprintf(fp,"%s%d%s",k==0?"ENCODED-MSG:[":":",payload[k],k==j-1?"]\n":""); msg=(char*)&payload[j]; fprintf(fp,"MESSAGE:\n[%.*s]\n",msglen,msg); r=(i<100)?1:0; if(r) { fprintf(fp,"%sREQUEST CODE=%d==%.*s,URI=%.*s,VERSION=%*.s\n",prefix,i, payload[METHOD_CODE_IDX+1],&msg[payload[METHOD_CODE_IDX]], payload[URI_REASON_IDX+1],&msg[payload[URI_REASON_IDX]], payload[VERSION_IDX+1],&msg[payload[VERSION_IDX]]); print_encoded_uri(fp,&payload[REQUEST_URI_IDX+1],payload[REQUEST_URI_IDX],msg,50,strcat(prefix," ")); prefix[strlen(prefix)-2]=0; i=REQUEST_URI_IDX+1+payload[REQUEST_URI_IDX]; } else { fprintf(fp,"%sRESPONSE CODE=%d==%.*s,REASON=%.*s,VERSION=%.*s\n",prefix,i, payload[METHOD_CODE_IDX+1],&msg[payload[METHOD_CODE_IDX]], payload[URI_REASON_IDX+1],&msg[payload[URI_REASON_IDX]], payload[VERSION_IDX+1],&msg[payload[VERSION_IDX]]); i=REQUEST_URI_IDX; } k=((payload[CONTENT_IDX]<<8)|payload[CONTENT_IDX+1]); j=msglen-k; fprintf(fp,"%sMESSAGE CONTENT:%.*s\n",prefix,j,&msg[k]); j=payload[i]; fprintf(fp,"%sHEADERS PRESENT(%d):",prefix,j); i++; for(k=i; k<i+(j*3); k+=3) fprintf(fp,"%c%d%c",k==i?'[':',',payload[k],k==(i+3*j-3)?']':' '); fprintf(fp,"\n"); for(k=i; k<i+(j*3); k+=3) { memcpy(&l,&payload[k+1],2); memcpy(&m,&payload[k+4],2); l=ntohs(l); m=ntohs(m); print_encoded_header(fp,msg,msglen,&payload[l],m-l,payload[k],prefix); } return 1; }
int print_encoded_contact(FILE *fp,char *hdr,int hdrlen,unsigned char* payload,int paylen,char *prefix) { int i=2;/* flags + urilength */ unsigned char flags=0; flags=payload[0]; fprintf(fp,"%s",prefix); for(i=0;i<paylen;i++) fprintf(fp,"%s%d%s",i==0?"ENCODED CONTACT=[":":",payload[i],i==paylen-1?"]\n":""); i=2; if(flags & HAS_NAME_F){ fprintf(fp,"%sCONTACT NAME=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags & HAS_Q_F){ fprintf(fp,"%sCONTACT Q=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags & HAS_EXPIRES_F){ fprintf(fp,"%sCONTACT EXPIRES=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags & HAS_RECEIVED_F){ fprintf(fp,"%sCONTACT RECEIVED=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(flags & HAS_METHOD_F){ fprintf(fp,"%sCONTACT METHOD=[%.*s]\n",prefix,payload[i+1],&hdr[payload[i]]); i+=2; } if(print_encoded_uri(fp,&payload[i],payload[1],hdr,hdrlen,strcat(prefix," "))<0){ prefix[strlen(prefix)-2]=0; fprintf(fp,"Error parsing URI\n"); return -1; } prefix[strlen(prefix)-2]=0; print_encoded_parameters(fp,(unsigned char*)&payload[i+payload[1]],hdr,paylen-i-payload[1],prefix); return 0; }