NeuroMaster
Limp Gawd
- Joined
- Jun 12, 2002
- Messages
- 346
I'm working with a byte array and I'm having a devil of a time figuring out how to write in data that's more than one char (byte) long. It looks something like this:
I'd like to use the first two bytes for a vanilla 16-bit internet checksum, so say we set those to zero and then copy in the payload.
So far so good. Now say we've got a checksum function that'll return the 16-bit internet checksum of bytes bytes of data starting at addr prototyped like this:
I've more or less gotten this much working. I'm a bit uncomfortable with some of the example implementations I've found of the internet checksum because most of the stuff out there is tailored toward making things blazing fast, not beginner coders. I'm hoping that a few issues (like dealing with unsigned vs signed types) aren't the big problem.
The big problem is that once I've returned this short, I have no idea how to write it into the first two bytes. I've tried just writing it into pkt.data[0] and hoping it'd overflow/whatever you call it into the next byte nicely, I've tried bit-masking/bit-shifting the short I get out of the checksum into data[0] and data[1]... I've basically banged my head against the problem for a few hours now and I can't seem to get it to work. No matter what I try, I can't put the information I want into the byte array, then pull it back out into a short.
I shouldn't even have to - running the internet checksum algorithm on the whole array after inserting the checksum into that field should return zero, but I keep getting a nonzero result. This seems like a pretty trivial problem, but there's just something I'm missing - maybe a command or system call I've just forgotten or never learned. I can't imagine this is supposed to be so difficult.
Any thoughts? I've tried to describe my situation as best I can, but I'm a little flustered - let me know ifyou need something clarified.
Thanks.
Code:
#define RDT_PKTSIZE 64
struct packet {
char data[RDT_PKTSIZE];
};
packet pkt;
I'd like to use the first two bytes for a vanilla 16-bit internet checksum, so say we set those to zero and then copy in the payload.
Code:
memset(pkt.data, 0, 2);
memcpy(pkt.data + 2, buffer, 62);
So far so good. Now say we've got a checksum function that'll return the 16-bit internet checksum of bytes bytes of data starting at addr prototyped like this:
Code:
short checksum(char *addr, int bytes)
I've more or less gotten this much working. I'm a bit uncomfortable with some of the example implementations I've found of the internet checksum because most of the stuff out there is tailored toward making things blazing fast, not beginner coders. I'm hoping that a few issues (like dealing with unsigned vs signed types) aren't the big problem.
The big problem is that once I've returned this short, I have no idea how to write it into the first two bytes. I've tried just writing it into pkt.data[0] and hoping it'd overflow/whatever you call it into the next byte nicely, I've tried bit-masking/bit-shifting the short I get out of the checksum into data[0] and data[1]... I've basically banged my head against the problem for a few hours now and I can't seem to get it to work. No matter what I try, I can't put the information I want into the byte array, then pull it back out into a short.
I shouldn't even have to - running the internet checksum algorithm on the whole array after inserting the checksum into that field should return zero, but I keep getting a nonzero result. This seems like a pretty trivial problem, but there's just something I'm missing - maybe a command or system call I've just forgotten or never learned. I can't imagine this is supposed to be so difficult.
Any thoughts? I've tried to describe my situation as best I can, but I'm a little flustered - let me know ifyou need something clarified.
Thanks.