1

I read a lot of posts on stackoverflow about endianness for network.
For example, if I do something like this :

struct timeval start_t
gettimeofday(&start_t, NULL);

Now , how can I be sure this kind of cast will work ?

uint32_t receivedTime = someGetterSomewhere();
struct timeval result = (struct timeval) receivedTime;
struct timeval diffTime;
timersub(&start_t,&result,&diffTime);


// do stuff like timersub, etc to get ms elapsed time 

int calculatedTime =  (int) floor( ((diffTime.tv_sec) * 1000 + (diffTime.tv_usec) / 1000) + 0.5);

Thanks for answering

PS:

My Gcc says this is not allowed

error: aggregate value used where an integer was expected
         pkt_set_timestamp(emptyPacket, (const uint32_t) start_t);
jy95
  • 773
  • 13
  • 36
  • 1
    Question: why do you think endianness is going to influence your calculations here? – fvu Oct 21 '17 at 23:34
  • That is all the question. I read many stuff about uint16_t and uint32_t that can be problematic when transfered into the network – jy95 Oct 21 '17 at 23:37
  • 1
    But there's no network anywhere in this question? Plus, Also, endianness problems can only arise when you transfer binary data between systems with different byte orders. Unless both are true you should not be worried. – fvu Oct 21 '17 at 23:46
  • Here (see my PS) , I created a packet that should provide uint32_t value of timestamp and my gcc doesn't allow that – jy95 Oct 22 '17 at 00:03
  • Your lines: `uint32_t receivedTime = someGetterSomewhere(); struct timeval result = (struct timeval) receivedTime;` can't work because you can't simply cast a `uint32_t` to a structure type. What you might do is `struct timeval result = (struct timeval) { .tv_sec = receivedTime, .tv_usec = 0 };` which uses a compound literal (and designated initializers, both from C99) to initialize the structure. There are still problems here. Typically, `time_t` is a 64-bit value (to avoid the Y2038 problem), so you're at risk of losing information (though if you're dealing with current time, you're 'OK'). – Jonathan Leffler Oct 22 '17 at 00:03
  • Note that a [`struct timeval`](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_time.h.html) has (at least) two components in some order: `time_t tv_sec;` and `suseconds_t tv_usec;`. You can't simply treat structures as if they're integers or vice versa. – Jonathan Leffler Oct 22 '17 at 00:06
  • Thanks. What I need is the time collapsed (in milliseconds) between the two hosts and that is why I compare the two timeval – jy95 Oct 22 '17 at 00:07
  • 1
    You, my friend, need to show a more nearly complete MCVE ([MCVE]). This is the first time that 2 hosts have been mentioned. You need to show what data you are sending over the wire. If you're not sending the time information in an ASCII format, you might have endianness issues to deal with after all, but you've just changed the landscape completely. You might need to look at [NTP v4](https://tools.ietf.org/html/rfc5905) and/or [SNTP v4](https://tools.ietf.org/html/rfc4330). – Jonathan Leffler Oct 22 '17 at 00:11
  • Considered that all packets are correctly checked. The problem is now to correctly cast to uint32_t (with minimal loss) and vis versa – jy95 Oct 22 '17 at 00:13
  • Conversion to `uint32_t` is probably wrong in the first place — that's worrying. What have you converted to `uint32_t`? How have you converted it to `uint32_t`? Without that information, we can't make any guesses about how to unconvert it — and with that information, the inverse process should be obvious (unless you've lost information, in which case it's impossible instead). – Jonathan Leffler Oct 22 '17 at 00:15
  • I try to convert the result of gettimeofday() (because time(null) doesn't provide a correct value for this case) – jy95 Oct 22 '17 at 00:17
  • 1
    If you don't know a value's endianness, how are you going to cast it? I'm starting to wonder if what you are looking for is just [htons](https://linux.die.net/man/3/htons) and friends: just exchange data in network byte order and done. – fvu Oct 22 '17 at 00:17
  • After reading again context; it is written : not specialy endianess : feel free to use as you wish XD – jy95 Oct 22 '17 at 00:20
  • If you want to compute elapsed time by using `struct timeval` and `gettimeofday` (which is not a bad way of doing it), what you need to do is perform arithmetic on `struct timeval`, as discussed in [this question](https://stackoverflow.com/questions/15846762/timeval-subtract-explanation). (The answer has *nothing* to do with casting to `uint32_t`, btw.) – Steve Summit Oct 22 '17 at 00:37
  • Seem it is time for me to sleep ...I will see this tomorrow – jy95 Oct 22 '17 at 01:01

0 Answers0