thilinaur thilinaur -4 years ago 82
C Question

gSOAP incorrect response data

I'm trying to setup a SOAP server on an embedded system running on linux for sending commands to it and getting response. I chose to use gSOAP since I am coding in C. I wrote a simple gsoap server to run on my embedded system and client application to run on my linux PC. For now I just implemented the server as it copies command data in the request to the response and send.
But the data I receive at my PC in the response is different from the data I sent. Please help.
I have attached my server and client codes

Header

struct data_packet
{
uint8_t *data;
int8_t data_length;
};

int ns__send_command(struct data_packet cmd_packet, struct data_packet *resp_packet);


Server

#include "soapH.h" // include the generated declarations
#include "ns.nsmap" // include the XML namespace mappings
int main()
{
struct soap soap;
int m, s, i; // master and slave sockets with loop variable
soap_init(&soap);
m = soap_bind(&soap, "10.0.0.10", 2012, 100);
if (m < 0)
soap_print_fault(&soap, stderr);
else
{
fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
for (i = 1; ; i++)
{
s = soap_accept(&soap);
if (s < 0)
{
soap_print_fault(&soap, stderr);
break;
}
fprintf(stderr, "%d: accepted connection from IP=%d.%d.%d.%d socket=%d", i,
(int)(soap.ip >> 24)&0xFF, (int)(soap.ip >> 16)&0xFF, (int)(soap.ip >> 8)&0xFF, (int)soap.ip&0xFF, s);
if (soap_serve(&soap) != SOAP_OK) // process RPC request
soap_print_fault(&soap, stderr); // print error
fprintf(stderr, "request served\n");
soap_destroy(&soap); // clean up class instances
soap_end(&soap); // clean up everything and close socket
}
}
soap_done(&soap); // close master socket and detach context
}

int ns__send_command(struct soap *soap, struct data_packet cmd_packet, struct data_packet *resp_packet)
{
resp_packet->data_length = cmd_packet.data_length;
resp_packet->data = (uint8_t*)soap_malloc(soap, sizeof(uint8_t*) * resp_packet->data_length);
memcpy(&resp_packet->data[0], &cmd_packet.data[0], resp_packet->data_length);
return SOAP_OK;
}


Client

#include "soapH.h"
#include "ns.nsmap"

int main()
{
struct soap soap;
uint8_t cmd_ping[16] = {0x56, 0x69, 0x56, 0x4F, 0x70, 0x61, 0x79, 0x56, 0x33, 0x00, 0x18, 0x01, 0x00, 0x00, 0x04, 0x7E};

struct data_packet cmd_packet;
struct data_packet resp_packet;

cmd_packet.data_length = 16;
cmd_packet.data = (uint8_t*)soap_malloc(&soap, sizeof(uint8_t*) * cmd_packet.data_length);
memcpy(&cmd_packet.data[0], &cmd_ping[0], cmd_packet.data_length);

soap_init1(&soap, SOAP_XML_INDENT);

soap_call_ns__send_command(&soap, "http://10.0.0.10:2012", "", cmd_packet, &resp_packet);
if (soap.error)
{
soap_print_fault(&soap, stderr);
exit(1);
}
else
{
int j;
printf("Command Data, length = %d\n", cmd_packet.data_length);
for(j = 0; j < cmd_packet.data_length; j++)
printf("%02X ", cmd_packet.data[j]);
printf("\n");
printf("Response Data, length = %d\n", resp_packet.data_length);
for(j = 0; j < resp_packet.data_length; j++)
printf("%02X ", resp_packet.data[j]);
printf("\n");
}

soap_destroy(&soap);
soap_end(&soap);
soap_done(&soap);
return 0;
}


Output from client

Command Data, length = 16
56 69 56 4F 70 61 79 56 33 00 18 01 00 00 04 7E
Response Data, length = 16
56 00 DE C0 F4 C8 45 09 04 00 00 00 F9 06 02 00

Answer Source

Using soap_memcpy instead of memcpy in the soap server solved the problem

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download