Commit b97566f8 authored by AXEL JAMES's avatar AXEL JAMES Committed by Bhavesh Yadav

Adds address field parsingl

parent f518d94c
...@@ -21,7 +21,7 @@ char *toRespXML(char* msg){ ...@@ -21,7 +21,7 @@ char *toRespXML(char* msg){
} }
char * toXML(char *reqType, char *key, char * value){ char * toXML(char *reqType, char *key, char * value, char * ip, int port){
char *line1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; char *line1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
char *line21 = "<KVMessage type=\""; char *line21 = "<KVMessage type=\"";
...@@ -30,10 +30,14 @@ char * toXML(char *reqType, char *key, char * value){ ...@@ -30,10 +30,14 @@ char * toXML(char *reqType, char *key, char * value){
char *line32 = "</Key>\n"; char *line32 = "</Key>\n";
char *line41 = "<Value>"; char *line41 = "<Value>";
char *line42 = "</Value>\n"; char *line42 = "</Value>\n";
char *line61 = "<Address>";
char *line62 = "</Address>\n";
char *line5 = "</KVMessage>\n"; char *line5 = "</KVMessage>\n";
char *line6;
int len2 = strlen(line21)+strlen(line22)+strlen(reqType)+1; int len2 = strlen(line21)+strlen(line22)+strlen(reqType)+1;
int len3 = strlen(line31)+strlen(line32)+strlen(key)+1; int len3 = strlen(line31)+strlen(line32)+strlen(key)+1;
int len4 = strlen(line41)+strlen(line42)+strlen(value)+1; int len4 = strlen(line41)+strlen(line42)+strlen(value)+1;
int len6 = strlen(line61)+strlen(line62)+21;
char *line2 = (char *)malloc(sizeof(char) * len2); char *line2 = (char *)malloc(sizeof(char) * len2);
char *line3 = (char *)malloc(sizeof(char) * len3); char *line3 = (char *)malloc(sizeof(char) * len3);
...@@ -52,17 +56,37 @@ char * toXML(char *reqType, char *key, char * value){ ...@@ -52,17 +56,37 @@ char * toXML(char *reqType, char *key, char * value){
strcat(line4,line42); strcat(line4,line42);
int msglen = strlen(line1)+strlen(line2)+strlen(line3)+strlen(line4)+strlen(line5)+1; int msglen = strlen(line1)+strlen(line2)+strlen(line3)+strlen(line4)+strlen(line5)+1;
if (ip!=NULL)
{
char str[7];
sprintf(str, " %d", port);
line6 = (char *)malloc(sizeof(char) * len6);
strcpy(line6,line61);
strcat(line6,ip);
strcat(line6, str);
strcat(line6,line62);
msglen +=strlen(line6);
}
char *message = (char *)malloc(sizeof(char) * msglen); char *message = (char *)malloc(sizeof(char) * msglen);
strcpy(message,line1); strcpy(message,line1);
strcat(message,line2); strcat(message,line2);
strcat(message,line3); strcat(message,line3);
if(!strcmp(reqType,"putreq") || !strcmp(reqType,"resp")) if(!strcmp(reqType,"putreq") || !strcmp(reqType,"resp"))
strcat(message,line4); strcat(message,line4);
if (ip!=NULL)
{
strcat(message,line6);
}
strcat(message,line5); strcat(message,line5);
free(line2);free(line3);free(line4); free(line2);free(line3);free(line4);
printf("Message%s",message);
return message; return message;
} }
...@@ -111,7 +135,7 @@ extReq_t *extractXML(char *buffer){ ...@@ -111,7 +135,7 @@ extReq_t *extractXML(char *buffer){
char *p,*q; char *p,*q;
req->operation = malloc(sizeof(char)*12); req->operation = malloc(sizeof(char)*12);
req->err = malloc(sizeof(char)*170); req->err = malloc(sizeof(char)*170);
req->val = malloc(sizeof(char)*256*1024+1); req->val = malloc(sizeof(char)*256*1024+1);
req->error=true; req->error=true;
p = strstr(buffer, "type=")+ 6; p = strstr(buffer, "type=")+ 6;
q = strstr(buffer, "\">"); q = strstr(buffer, "\">");
...@@ -154,7 +178,26 @@ extReq_t *extractXML(char *buffer){ ...@@ -154,7 +178,26 @@ extReq_t *extractXML(char *buffer){
req->val[q-p] = '\0'; req->val[q-p] = '\0';
} }
} }
p = strstr(buffer, "<Address>")+9;
q = strstr(buffer, "</Address>");
if(p!=NULL && q!=NULL){
strcpy(req->ipAddr,strtok(p, " "));
int l = strlen(req->ipAddr);
char port_s[6];
memcpy(port_s,p+l+1,q-p-l-1);
port_s[q-p-l-1] = '\0';
req->port = atoi(port_s);
}
req->error = false; req->error = false;
return req; return req;
} }
\ No newline at end of file
// void main(){
// extReq_t *request = extractXML(toXML("abc","cdefg","123","111.111.111.111",12345));
// printf("Parsed msg:%s %d",request->ipAddr,request->port);
// }
\ No newline at end of file
...@@ -5,10 +5,12 @@ struct extractedReq { ...@@ -5,10 +5,12 @@ struct extractedReq {
char* val; char* val;
char* operation; char* operation;
char* err; char* err;
char ipAddr[16];
int port;
bool error; bool error;
}; };
typedef struct extractedReq extReq_t; typedef struct extractedReq extReq_t;
char *toXML(char *reqType, char *key, char * value); char *toXML(char *reqType, char *key, char * value, char * ip, int port);
extReq_t *extractXML(char *buffer); extReq_t *extractXML(char *buffer);
char *toRespXML(char* msg); char *toRespXML(char* msg);
char *extractRespXml(FILE *fp,char *buffer); char *extractRespXml(FILE *fp,char *buffer);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment