Make sure that readlong/putlong uses 32 bits

This commit is contained in:
Erik Ekman 2006-11-07 22:54:29 +00:00
parent 3f0dbf5ce5
commit 51f3d75d2d
4 changed files with 23 additions and 19 deletions

3
dns.c
View File

@ -26,6 +26,7 @@
#include <time.h> #include <time.h>
#include <err.h> #include <err.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <unistd.h> #include <unistd.h>
@ -286,7 +287,7 @@ int
dns_parse_reply(char *outbuf, int buflen, char *packet, int packetlen) dns_parse_reply(char *outbuf, int buflen, char *packet, int packetlen)
{ {
int rv; int rv;
long ttl; uint32_t ttl;
short rlen; short rlen;
short type; short type;
short class; short class;

22
read.c
View File

@ -15,7 +15,7 @@
*/ */
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdint.h>
static int static int
readname_loop(char *packet, int packetlen, char **src, char *dst, size_t length, size_t loop) readname_loop(char *packet, int packetlen, char **src, char *dst, size_t length, size_t loop)
@ -95,19 +95,20 @@ readshort(char *packet, char **src, short *dst)
} }
int int
readlong(char *packet, char **src, long *dst) readlong(char *packet, char **src, uint32_t *dst)
{ {
// A long as described in dns protocol is always 32 bits
unsigned char *p; unsigned char *p;
p = *src; p = *src;
*dst = ((long)p[0] << 24) *dst = ((uint32_t)p[0] << 24)
| ((long)p[1] << 16) | ((uint32_t)p[1] << 16)
| ((long)p[2] << 8) | ((uint32_t)p[2] << 8)
| ((long)p[3]); | ((uint32_t)p[3]);
(*src) += sizeof(long); (*src) += sizeof(uint32_t);
return sizeof(long); return sizeof(uint32_t);
} }
int int
@ -147,8 +148,9 @@ putshort(char **dst, short value)
} }
int int
putlong(char **dst, long value) putlong(char **dst, uint32_t value)
{ {
// A long as described in dns protocol is always 32 bits
unsigned char *p; unsigned char *p;
p = *dst; p = *dst;
@ -159,7 +161,7 @@ putlong(char **dst, long value)
*p++ = (value); *p++ = (value);
(*dst) = p; (*dst) = p;
return sizeof(long); return sizeof(uint32_t);
} }
int int

4
read.h
View File

@ -19,12 +19,12 @@
int readname(char *, int, char **, char *, size_t); int readname(char *, int, char **, char *, size_t);
int readshort(char *, char **, short *); int readshort(char *, char **, short *);
int readlong(char *, char **, long *); int readlong(char *, char **, uint32_t *);
int readdata(char *, char **, char *, size_t); int readdata(char *, char **, char *, size_t);
int putbyte(char **, char); int putbyte(char **, char);
int putshort(char **, short); int putshort(char **, short);
int putlong(char **, long); int putlong(char **, uint32_t);
int putdata(char **, char *, size_t); int putdata(char **, char *, size_t);
#endif #endif

13
test.c
View File

@ -23,6 +23,7 @@
#include <arpa/nameser8_compat.h> #include <arpa/nameser8_compat.h>
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
@ -73,10 +74,10 @@ static void
test_readputlong() test_readputlong()
{ {
char buf[4]; char buf[4];
long putint; uint32_t putint;
long tempi; uint32_t tempi;
long tint; uint32_t tint;
long *l; uint32_t *l;
char* p; char* p;
int i; int i;
@ -89,13 +90,13 @@ test_readputlong()
p = buf; p = buf;
putlong(&p, tint); putlong(&p, tint);
l = &putint; l = &putint;
memcpy(l, buf, sizeof(int)); memcpy(l, buf, sizeof(uint32_t));
if (putint != tempi) { if (putint != tempi) {
printf("Bad value on putlong for %d\n", i); printf("Bad value on putlong for %d\n", i);
exit(2); exit(2);
} }
l = &tempi; l = &tempi;
memcpy(buf, l, sizeof(int)); memcpy(buf, l, sizeof(uint32_t));
p = buf; p = buf;
readlong(NULL, &p, &tempi); readlong(NULL, &p, &tempi);
if (tempi != tint) { if (tempi != tint) {