avoid structure padding

P

pmm

hi I am repeating my post here plz excuse

i am trying out a UDP packet
transfer between a windows machine and a linux I created a structure on
both sides (ie on linux and on windows) and I sent using UDP
but on the receiving side when I am trying to reform the packet back
into structure tis giving me different outputs I assume it is the
problem of structure padding I even used pragma pack (1) but still it
is not working out for me kindly do suggest u r view to execute this
correctly I am palcing structure all well capturing code on recving
side and also print statement of both the machines

kindly suggest me what to modify

my structure
<code>
typedef struct
{
INT32S connId;
INT32S seqNo;

}addrData_t;

typedef struct
{
INT16S typeId;
INT16S length;
addrData_t addrData;

}addrItem_t;

typedef struct
{
INT16S typeId;
INT16S length;
INT8U data[1];

}dataItem_t;

typedef struct
{
INT16S itemCount;
addrItem_t addrItem;
dataItem_t dataItem;

} udpPacket_t;

sending side code lines
INT8U *buff=GetMemBuff(MAX_DATA_LEN);
<code>
udpPacket_t *udpPacket=(udpPacket_t*)buff ;
addrItem_t *ai=&udpPacket->addrItem;
dataItem_t *di=&udpPacket->dataItem;
addrData_t *ad=&ai->addrData;
static int sockFd; int len,thrdId; int siz; int
kkk; extern int
udp_port_no;
extern int connId;
/*static hostent* h;*/
// sockHdl_t *sock = GetNewSockObj();
static sockAddr_t srv;
wVersionRequested = MAKEWORD( 2, 0 );
memset(buff,0,MAX_DATA_LEN);

udpPacket->itemCount=2;

ai->typeId=0x8002;
ai->length=8;
ad->connId=connId;
ad->seqNo=0;
di->typeId=0x00b1;
di->length=1;
di->data[0]=0x12;

siz=sizeof(udpPacket);
printf("\n udpacket size %d\n",siz);

printf("\n typeid %x",ai->typeId);
printf("\nlen %x",ai->length);
printf("\nconn %x",ad->connId);
printf("\nseq %x",ad->seqNo);
printf("\ndtype %x",di->typeId);
printf("\ndlen %x",di->length);
printf("\n data %x",di->data[0]);

srv.sin_family=AF_INET;
sock->sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sock->sfd <0)
{
FreeSockObj(sock);
perror("Unable to get UDP_SOCKET \n");
return FALSE;
}

/* 1.Create socket 2.send data on socket 3 create UDP thrd
4. recv data on Thrd */

if (send(sock->sfd,(const char
*)&udpPacket,sizeof(udpPacket_t),0)
<0)
{
closesocket(sock->sfd);
FreeSockObj(sock);
printf("Unable to send UDP DATA PACKET \n");
return FALSE;
}
else
{
printf("UDP PACKET SENT\n");
}

</code>

this is receving side code

void* IODataRecv(void* sfd)
{
int nsfd=*((int *)sfd);
int len ,i;
char *buff=GetMemBuff(128);

struct in_addr adr;

struct sockaddr_in dst;
socklen_t adrLen;
memset(buff,0,128);
int count = 0;
sleep(2);
inet_aton("192.168.16.25",&adr);
dst.sin_addr=adr;
dst.sin_port=htons(CONNECTED_UDP_PORT);
dst.sin_family= AF_INET;
while(1)
{
printf("\nIO data function\n ");
/*len= recvfrom(nsfd,buff,1024,0,(struct sockaddr*)
&dst,sizeof(dst)); */
len= recv(nsfd,buff,1024,0);
printf("\n data function\n ");
if (len>0)
{
for (i=0;i<len;i++)
{
printf(" %x ",buff);
}
udpPacket_t *udppkt= (udpPacket_t *)buff;
addrItem_t ai=udppkt->addrItem;
dataItem_t di=udppkt->dataItem;
addrData_t ad=ai.addrData;

printf("Type Id %x \n",ai.typeId);
printf("length %x\n",ai.length);
printf("ConnId %x \n",ad.connId);
printf("Seq no %x \n",ad.seqNo);
printf("Data typeid %x\n",di.typeId);
printf("Data length %x\n",di.length);
printf("Item Count %x\n",udppkt->itemCount);

}

}

outputs on both the machines
sending side prints

Itemcnt 2
adr type 8002
adr len 8
connid b5c06921
seq 0
data type id b1
data len 1
data 1

2 0 0 0 2 80 0 0 8 0 0 0 21 69 c0 b5 0 0 0 0 b1 0 0 0 1 0 0 0 1 UDP
PACKET SENT

this is recving side prints

recv data function

2 0 0 0 2 80 0 0 8 0 0 0 21 69 c0 b5 0 0 0 0 b1 0
0 0
1 0 0 0 1
<+==========================>
plz see here the recving data is changed even though the packet
captured is fine in order
<+======================>
Item Count 2
Type Id 0
length 8002
ConnId 80000
Seq no 69210000
Data typeid b5c0
Data length 0

IO data function
kindly suggest u r views to correct my errors
thanks and regards
PMM
 
F

Flash Gordon

pmm said:
hi I am repeating my post here plz excuse

i am trying out a UDP packet
transfer between a windows machine and a linux I created a structure on
both sides (ie on linux and on windows) and I sent using UDP
but on the receiving side when I am trying to reform the packet back
into structure tis giving me different outputs I assume it is the
problem of structure padding I even used pragma pack (1) but still it
is not working out for me kindly do suggest u r view to execute this

Please don't use stupid contractions like u, r, plz. They make it a
*lot* harder to read your posts. Your use of such things may well be one
reason why some people don't bother responding.
correctly I am palcing structure all well capturing code on recving
side and also print statement of both the machines

kindly suggest me what to modify

my structure
<code>
typedef struct
{
INT32S connId;

You are still not providing complete compilable examples, something I'm
sure you have been advised to do. This is obvious because I've yet to
see a definition of INT32S.
INT32S seqNo;

}addrData_t;

typedef struct
{
INT16S typeId;
INT16S length;
addrData_t addrData;

}addrItem_t;

typedef struct
{
INT16S typeId;
INT16S length;
INT8U data[1];

}dataItem_t;

typedef struct
{
INT16S itemCount;
addrItem_t addrItem;
dataItem_t dataItem;

} udpPacket_t;

sending side code lines
INT8U *buff=GetMemBuff(MAX_DATA_LEN);
<code>
udpPacket_t *udpPacket=(udpPacket_t*)buff ;

I'm sure you have been advised that this is not a good way to do this.
You should manually build up the packets in to the buffer since the
compiler can insert padding between fields. Such padding may be
dependant on the compiler options you use and might change on the next
build. Also, there is absolutely no guarantee that the buffer returned
by GetMemBuff (something else we have seen no definition of) is
correctly aligned for your structure.
addrItem_t *ai=&udpPacket->addrItem;

</code>

this is receving side code

void* IODataRecv(void* sfd)
{
int nsfd=*((int *)sfd);
int len ,i;
char *buff=GetMemBuff(128);

<snip>

I know for a fact I advised you to use an unsigned char buffer, yet you
are using a char buffer.

I'm not going to read the rest of your code since you have yet to deal
with the issues already raised with it.

Also, please don't email me code to comment on. I deal with C questions
on this group so that others get a chance to correct the mistakes that I
make. My private email address is only provided for when people have
reason to contact me about things not topical here, which occasionally
does happen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top