UTC timestamp in 8bytes size instead of 25bytes

790 views
Skip to first unread message

Rex Stefan

unread,
Oct 2, 2016, 9:15:02 PM10/2/16
to ns-3-users
Hello NS-3 users,

Thank you so much for your replies on my previous posts which helped me to fix my issues. Currently I am trying to add a UTC time into my header which I am working on. The expected size of the timestamp should be 8 bytes but it takes 25bytes for the same. Could someone please help me with this issue. 

The UTC date and time is:Sun Oct 02 21:00:28 2016. I want this timestamp within 8 bytes instead of 25bytes. Thanks a lot in advance.

Regards,
Rex 

Konstantinos

unread,
Oct 3, 2016, 4:10:01 AM10/3/16
to ns-3-users
Hi Rex,

Why does it take 25byes? How do you write it?
The epoc timestamp is a long integer stored in only 4byes.
You might need to convert the human readable string representation to the integer timestamp

Regards
K

Rex Stefan

unread,
Oct 17, 2016, 11:13:53 AM10/17/16
to ns-3-users
Dear Konstantinos,

Thank you so much for your reply and extremely sorry for getting back very late. As you suggested, I have converted the string into time_t datatype. Since time_t is a 8bytes datatype, I am serializing and deserializing time_t value using WriteU64 and ReadU64() functions respectively. But by doing so, I couldn't able to deserialize it as it shows an error message that I am trying to read beyond the available buffer space.

Is there any other way that I can use to serialize and deserialize a time_t datatype in NS-3?

P.S : For the above case which explained, the getSerializedSize() function returns the expected number of bytes but serializing and deserializing is not happening. 

Regards,
Rex

pdbarnes

unread,
Oct 18, 2016, 10:44:38 AM10/18/16
to ns-3-users
Please share the code and the error.
Peter

Rex

unread,
Oct 23, 2016, 8:29:45 PM10/23/16
to ns-3-users
Dear Peter,

Please find the code below. I am not sure whether my approach is correct or not. Please do suggest me the correct way if I am wrong. Thanks a lot in advance.

void
GoosePduHeader::Settimestamp()
{

time_t now = std::time(0);
  // convert now to tm struct for UTC
     tm *gmtm = gmtime(&now);
time_t     result = mktime(gmtm);
}
time_t
GoosePduHeader::Gettimestamp()
{
return result;
}

I try to serialize and desrialize the same as below. Since time_t is of 8 byte size, i use WriteU64 and ReadU64() functions respectively.

Serialize : i.WriteU64 = result;
Deserialize : result = i.ReadU64();


Regards,
Rex

pdbarnes

unread,
Oct 24, 2016, 12:43:19 AM10/24/16
to ns-3-users
Please show the code for all three functions: GetSerializedSize(), Serialize(), Deserialize()

and the complete error message.

Peter

Rex

unread,
Oct 24, 2016, 7:37:59 PM10/24/16
to ns-3-users
Dear Peter,

I posted the code and the error message. Here I have tried a different way to serialize and deserialize the same but ended up getting the same error message as I got last time when i tried WriteU64 & ReadU64.

void
GoosePduHeader::Settimestamp ()
{
//uint16_t k;
char m_tag='\x84';
// current date/time based on current system
time_t now = std::time(0);

    
  // convert now to tm struct for UTC
     tm *gmtm = gmtime(&now);
 
     time_t ret = mktime(gmtm);
 
     long long result =(long long) ret;


  char m_length = '\x08';
  timestamp = new char(10);
  timestamp[0]=m_tag;
  timestamp[1]=m_length;
  timestamp[2]=(result >> 56) & 0xFF;
  timestamp[3]=(result >> 48) & 0xFF;
  timestamp[4]=(result >> 40) & 0xFF;
  timestamp[5]=(result >> 32) & 0xFF;
  timestamp[6]=(result >> 24) & 0xFF;
  timestamp[7]=(result >> 16) & 0xFF;
  timestamp[8]=(result >> 8) & 0xFF;
  timestamp[9]= result  & 0xFF;

char*
GoosePduHeader::Gettimestamp (void)
{

return timestamp;
}
uint32_t
GoosePduHeader::GetSerializedSize (void) const
 {

return 10;
 }

void
GoosePduHeader::Serialize (Buffer::Iterator start)const
{

uint8_t x;
Buffer::Iterator i = start;
 for (x=0;x<10;x++)
   {
  i.WriteU8 (timestamp[x]);
   }
 
}
uint32_t
GoosePduHeader::Deserialize (Buffer::Iterator start)
{

uint8_t x;
   Buffer::Iterator it = start;
   
timestamp = new char[10];
for  (x=0;x<10;x++)
{
timestamp[x]=it.ReadU8();
}
return GetSerializedSize ();
}


}

Error : assert failed. cond="m_current >= m_dataStart && m_current < m_dataEnd", msg="You have attempted to read beyond the bounds of the available buffer space. This usually indicates that a Header::Deserialize or Trailer::Deserialize method is trying to read data which was not written by a Header::Serialize or Trailer::Serialize method. In short: check the code of your Serialize and Deserialize methods.", file=./ns3/buffer.h, line=996
terminate called without an active exception.

Awaiting your valuable suggestions.

Regards,
Rex

Rex

unread,
Oct 27, 2016, 2:57:17 PM10/27/16
to ns-3-users
Dear Peter,

Awaiting your valuable suggestions for me to proceed further. I couldn't get rid of this error still.

Regards,
Rex

pdbarnes

unread,
Oct 27, 2016, 5:12:59 PM10/27/16
to ns-3-users
Hmm. When everything looks right but there is still a problem, something you're sure of is incorrect.

Are you sure the assert is from your GooseHeader?

Print the amount the BufferIterator advances when Serializing.

Print the remaining size in the Buffer before you start to Deserialize

Print the bytes as you Serialize/Deserialize. Do the values you get look right?

To make it easier for us to understand your code, follow the coding guidelines. From your usage I assume your time stamp variable is a class member. If so, name it m_timestamp to make that clear.

As written your code has a memory leak. Why does timestamp have to be char * instead of just char[10]?

HTH,
Peter

Tommaso Pecorella

unread,
Oct 28, 2016, 4:18:28 PM10/28/16
to ns-3-users
I'm 99% sure that he's trying to deserialize a GooseHeader where there's no GooseHeader.

T.

Rex

unread,
Oct 28, 2016, 6:56:18 PM10/28/16
to ns-3-users
Thank you so much Peter and Tommaso for your replies. 

In my GooseHeader, I have 11 other functions which I can serialize and deserialize successfully. But only when I try to include the timestamp(), I am getting the error message. 

As Peter suggested, I have tried timestamp as char[10] instead of char* which yields the same error as well. 

Could you please suggest me how to debug the buffer to know what values are stored in the particular BufferIterator while serializing & deserializing timestamp value? While debugging, I verified the size in GetSerializedSize() and the number of bytes I am trying to serialize & deserilaize and everything matches. But not sure why I am still getting it. 

If you want to have a look at my complete code, I can provide it.

Regards,
Rex

pdbarnes

unread,
Oct 31, 2016, 5:21:23 PM10/31/16
to ns-3-users
I gave you several other suggestions to make progress on debugging.  Have you tried those?  Can you show the results?

Tommaso gave another suggestion:  convince yourself there really is a GooseHeader there when you expect it.  Perhaps turn on packet metadata and print the packet?  (Of course, that assumes the buffer underflow is occurring where you think it is, one of the suggestions I made.)


On Friday, October 28, 2016 at 3:56:18 PM UTC-7, Rex wrote:
As Peter suggested, I have tried timestamp as char[10] instead of char* which yields the same error as well. 

Could you please suggest me how to debug the buffer to know what values are stored in the particular BufferIterator while serializing & deserializing timestamp value? While debugging, I verified the size in GetSerializedSize() and the number of bytes I am trying to serialize & deserilaize and everything matches. But not sure why I am still getting it. 

If you want to have a look at my complete code, I can provide it.

As always, the complete code for a minimal example can be very helpful, but don't expect others to wade through a complicated scenario for you.
Reply all
Reply to author
Forward
0 new messages