diff options
author | Eric Faurot <eric@cvs.openbsd.org> | 2012-11-24 13:59:54 +0000 |
---|---|---|
committer | Eric Faurot <eric@cvs.openbsd.org> | 2012-11-24 13:59:54 +0000 |
commit | 9b10ff15ab3d534cff190b2b288e825cbb0be4af (patch) | |
tree | 64c2d46fdfea517202ae9f11e6f3c76faad6c215 /lib/libc | |
parent | ac43f6af9b3c40b76a32e810f6b523cbd4344271 (diff) |
make separate structures for pack and unpack
Diffstat (limited to 'lib/libc')
-rw-r--r-- | lib/libc/asr/asr_debug.c | 6 | ||||
-rw-r--r-- | lib/libc/asr/asr_private.h | 27 | ||||
-rw-r--r-- | lib/libc/asr/asr_utils.c | 69 | ||||
-rw-r--r-- | lib/libc/asr/getaddrinfo_async.c | 6 | ||||
-rw-r--r-- | lib/libc/asr/gethostnamadr_async.c | 6 | ||||
-rw-r--r-- | lib/libc/asr/getnetnamadr_async.c | 6 | ||||
-rw-r--r-- | lib/libc/asr/res_mkquery.c | 6 | ||||
-rw-r--r-- | lib/libc/asr/res_send_async.c | 14 |
8 files changed, 79 insertions, 61 deletions
diff --git a/lib/libc/asr/asr_debug.c b/lib/libc/asr/asr_debug.c index 6c6331afa34..617aed64649 100644 --- a/lib/libc/asr/asr_debug.c +++ b/lib/libc/asr/asr_debug.c @@ -1,4 +1,4 @@ -/* $OpenBSD: asr_debug.c,v 1.8 2012/09/09 12:15:32 eric Exp $ */ +/* $OpenBSD: asr_debug.c,v 1.9 2012/11/24 13:59:53 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> * @@ -164,7 +164,7 @@ void asr_dump_packet(FILE *f, const void *data, size_t len) { char buf[1024]; - struct packed p; + struct unpack p; struct header h; struct query q; struct rr rr; @@ -173,7 +173,7 @@ asr_dump_packet(FILE *f, const void *data, size_t len) if (f == NULL) return; - packed_init(&p, (char *)data, len); + unpack_init(&p, data, len); if (unpack_header(&p, &h) == -1) { fprintf(f, ";; BAD PACKET: %s\n", p.err); diff --git a/lib/libc/asr/asr_private.h b/lib/libc/asr/asr_private.h index 16b46243f49..c03e9200450 100644 --- a/lib/libc/asr/asr_private.h +++ b/lib/libc/asr/asr_private.h @@ -1,4 +1,4 @@ -/* $OpenBSD: asr_private.h,v 1.8 2012/09/09 12:15:32 eric Exp $ */ +/* $OpenBSD: asr_private.h,v 1.9 2012/11/24 13:59:53 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> * @@ -33,8 +33,15 @@ #define RCODE(v) ((v) & RCODE_MASK) -struct packed { - char *data; +struct pack { + char *buf; + size_t len; + size_t offset; + const char *err; +}; + +struct unpack { + const char *buf; size_t len; size_t offset; const char *err; @@ -297,12 +304,14 @@ enum asr_state { /* asr_utils.c */ -void packed_init(struct packed*, char*, size_t); -int pack_header(struct packed*, const struct header*); -int pack_query(struct packed*, uint16_t, uint16_t, const char*); -int unpack_header(struct packed*, struct header*); -int unpack_query(struct packed*, struct query*); -int unpack_rr(struct packed*, struct rr*); +void pack_init(struct pack *, char *, size_t); +int pack_header(struct pack*, const struct header*); +int pack_query(struct pack*, uint16_t, uint16_t, const char*); + +void unpack_init(struct unpack *, const char *, size_t); +int unpack_header(struct unpack*, struct header*); +int unpack_query(struct unpack*, struct query*); +int unpack_rr(struct unpack*, struct rr*); int sockaddr_from_str(struct sockaddr *, int, const char *); ssize_t dname_from_fqdn(const char*, char*, size_t); diff --git a/lib/libc/asr/asr_utils.c b/lib/libc/asr/asr_utils.c index 2329de71850..b7f1a38f609 100644 --- a/lib/libc/asr/asr_utils.c +++ b/lib/libc/asr/asr_utils.c @@ -1,4 +1,4 @@ -/* $OpenBSD: asr_utils.c,v 1.1 2012/04/14 09:24:18 eric Exp $ */ +/* $OpenBSD: asr_utils.c,v 1.2 2012/11/24 13:59:53 eric Exp $ */ /* * Copyright (c) 2009-2012 Eric Faurot <eric@faurot.net> * @@ -35,16 +35,16 @@ static int dname_check_label(const char*, size_t); static ssize_t dname_expand(const unsigned char*, size_t, size_t, size_t*, char *, size_t); -static int unpack_data(struct packed*, void*, size_t); -static int unpack_u16(struct packed*, uint16_t*); -static int unpack_u32(struct packed*, uint32_t*); -static int unpack_inaddr(struct packed*, struct in_addr*); -static int unpack_in6addr(struct packed*, struct in6_addr*); -static int unpack_dname(struct packed*, char*, size_t); +static int unpack_data(struct unpack*, void*, size_t); +static int unpack_u16(struct unpack*, uint16_t*); +static int unpack_u32(struct unpack*, uint32_t*); +static int unpack_inaddr(struct unpack*, struct in_addr*); +static int unpack_in6addr(struct unpack*, struct in6_addr*); +static int unpack_dname(struct unpack*, char*, size_t); -static int pack_data(struct packed*, const void*, size_t); -static int pack_u16(struct packed*, uint16_t); -static int pack_dname(struct packed*, const char*); +static int pack_data(struct pack*, const void*, size_t); +static int pack_u16(struct pack*, uint16_t); +static int pack_dname(struct pack*, const char*); static int dname_check_label(const char *s, size_t l) @@ -163,16 +163,25 @@ dname_expand(const unsigned char *data, size_t len, size_t offset, } void -packed_init(struct packed *pack, char *data, size_t len) +pack_init(struct pack *pack, char *buf, size_t len) { - pack->data = data; + pack->buf = buf; pack->len = len; pack->offset = 0; pack->err = NULL; } +void +unpack_init(struct unpack *unpack, const char *buf, size_t len) +{ + unpack->buf = buf; + unpack->len = len; + unpack->offset = 0; + unpack->err = NULL; +} + static int -unpack_data(struct packed *p, void *data, size_t len) +unpack_data(struct unpack *p, void *data, size_t len) { if (p->err) return (-1); @@ -182,14 +191,14 @@ unpack_data(struct packed *p, void *data, size_t len) return (-1); } - memmove(data, p->data + p->offset, len); + memmove(data, p->buf + p->offset, len); p->offset += len; return (0); } static int -unpack_u16(struct packed *p, uint16_t *u16) +unpack_u16(struct unpack *p, uint16_t *u16) { if (unpack_data(p, u16, 2) == -1) return (-1); @@ -200,7 +209,7 @@ unpack_u16(struct packed *p, uint16_t *u16) } static int -unpack_u32(struct packed *p, uint32_t *u32) +unpack_u32(struct unpack *p, uint32_t *u32) { if (unpack_data(p, u32, 4) == -1) return (-1); @@ -211,26 +220,26 @@ unpack_u32(struct packed *p, uint32_t *u32) } static int -unpack_inaddr(struct packed *p, struct in_addr *a) +unpack_inaddr(struct unpack *p, struct in_addr *a) { return (unpack_data(p, a, 4)); } static int -unpack_in6addr(struct packed *p, struct in6_addr *a6) +unpack_in6addr(struct unpack *p, struct in6_addr *a6) { return (unpack_data(p, a6, 16)); } static int -unpack_dname(struct packed *p, char *dst, size_t max) +unpack_dname(struct unpack *p, char *dst, size_t max) { ssize_t e; if (p->err) return (-1); - e = dname_expand(p->data, p->len, p->offset, &p->offset, dst, max); + e = dname_expand(p->buf, p->len, p->offset, &p->offset, dst, max); if (e == -1) { p->err = "bad domain name"; return (-1); @@ -244,7 +253,7 @@ unpack_dname(struct packed *p, char *dst, size_t max) } int -unpack_header(struct packed *p, struct header *h) +unpack_header(struct unpack *p, struct header *h) { if (unpack_data(p, h, HFIXEDSZ) == -1) return (-1); @@ -259,7 +268,7 @@ unpack_header(struct packed *p, struct header *h) } int -unpack_query(struct packed *p, struct query *q) +unpack_query(struct unpack *p, struct query *q) { unpack_dname(p, q->q_dname, sizeof(q->q_dname)); unpack_u16(p, &q->q_type); @@ -269,7 +278,7 @@ unpack_query(struct packed *p, struct query *q) } int -unpack_rr(struct packed *p, struct rr *rr) +unpack_rr(struct unpack *p, struct rr *rr) { uint16_t rdlen; size_t save_offset; @@ -332,7 +341,7 @@ unpack_rr(struct packed *p, struct rr *rr) break; default: other: - rr->rr.other.rdata = p->data + p->offset; + rr->rr.other.rdata = p->buf + p->offset; rr->rr.other.rdlen = rdlen; p->offset += rdlen; } @@ -348,7 +357,7 @@ unpack_rr(struct packed *p, struct rr *rr) } static int -pack_data(struct packed *p, const void *data, size_t len) +pack_data(struct pack *p, const void *data, size_t len) { if (p->err) return (-1); @@ -358,14 +367,14 @@ pack_data(struct packed *p, const void *data, size_t len) return (-1); } - memmove(p->data + p->offset, data, len); + memmove(p->buf + p->offset, data, len); p->offset += len; return (0); } static int -pack_u16(struct packed *p, uint16_t v) +pack_u16(struct pack *p, uint16_t v) { v = htons(v); @@ -373,7 +382,7 @@ pack_u16(struct packed *p, uint16_t v) } static int -pack_dname(struct packed *p, const char *dname) +pack_dname(struct pack *p, const char *dname) { /* dname compression would be nice to have here. * need additionnal context. @@ -382,7 +391,7 @@ pack_dname(struct packed *p, const char *dname) } int -pack_header(struct packed *p, const struct header *h) +pack_header(struct pack *p, const struct header *h) { struct header c; @@ -397,7 +406,7 @@ pack_header(struct packed *p, const struct header *h) } int -pack_query(struct packed *p, uint16_t type, uint16_t class, const char *dname) +pack_query(struct pack *p, uint16_t type, uint16_t class, const char *dname) { pack_dname(p, dname); pack_u16(p, type); diff --git a/lib/libc/asr/getaddrinfo_async.c b/lib/libc/asr/getaddrinfo_async.c index bc327c19eea..6b1fb63b13c 100644 --- a/lib/libc/asr/getaddrinfo_async.c +++ b/lib/libc/asr/getaddrinfo_async.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getaddrinfo_async.c,v 1.8 2012/09/06 15:05:16 eric Exp $ */ +/* $OpenBSD: getaddrinfo_async.c,v 1.9 2012/11/24 13:59:53 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> * @@ -608,7 +608,7 @@ addrinfo_from_file(struct async *as, int family, FILE *f) static int addrinfo_from_pkt(struct async *as, char *pkt, size_t pktlen) { - struct packed p; + struct unpack p; struct header h; struct query q; struct rr rr; @@ -620,7 +620,7 @@ addrinfo_from_pkt(struct async *as, char *pkt, size_t pktlen) } u; char buf[MAXDNAME], *c; - packed_init(&p, pkt, pktlen); + unpack_init(&p, pkt, pktlen); unpack_header(&p, &h); for(; h.qdcount; h.qdcount--) unpack_query(&p, &q); diff --git a/lib/libc/asr/gethostnamadr_async.c b/lib/libc/asr/gethostnamadr_async.c index 66b46dec3ed..8fc4d48385a 100644 --- a/lib/libc/asr/gethostnamadr_async.c +++ b/lib/libc/asr/gethostnamadr_async.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gethostnamadr_async.c,v 1.8 2012/09/06 15:05:16 eric Exp $ */ +/* $OpenBSD: gethostnamadr_async.c,v 1.9 2012/11/24 13:59:53 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> * @@ -431,7 +431,7 @@ static struct hostent * hostent_from_packet(int reqtype, int family, char *pkt, size_t pktlen) { struct hostent *h; - struct packed p; + struct unpack p; struct header hdr; struct query q; struct rr rr; @@ -439,7 +439,7 @@ hostent_from_packet(int reqtype, int family, char *pkt, size_t pktlen) if ((h = hostent_alloc(family)) == NULL) return (NULL); - packed_init(&p, pkt, pktlen); + unpack_init(&p, pkt, pktlen); unpack_header(&p, &hdr); for(; hdr.qdcount; hdr.qdcount--) unpack_query(&p, &q); diff --git a/lib/libc/asr/getnetnamadr_async.c b/lib/libc/asr/getnetnamadr_async.c index 33085f84f1e..eaea8f5f537 100644 --- a/lib/libc/asr/getnetnamadr_async.c +++ b/lib/libc/asr/getnetnamadr_async.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getnetnamadr_async.c,v 1.4 2012/09/07 13:21:34 eric Exp $ */ +/* $OpenBSD: getnetnamadr_async.c,v 1.5 2012/11/24 13:59:53 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> * @@ -316,7 +316,7 @@ static struct netent * netent_from_packet(int reqtype, char *pkt, size_t pktlen) { struct netent *n; - struct packed p; + struct unpack p; struct header hdr; struct query q; struct rr rr; @@ -324,7 +324,7 @@ netent_from_packet(int reqtype, char *pkt, size_t pktlen) if ((n = netent_alloc(AF_INET)) == NULL) return (NULL); - packed_init(&p, pkt, pktlen); + unpack_init(&p, pkt, pktlen); unpack_header(&p, &hdr); for(; hdr.qdcount; hdr.qdcount--) unpack_query(&p, &q); diff --git a/lib/libc/asr/res_mkquery.c b/lib/libc/asr/res_mkquery.c index dedf558cb48..6e2371c868d 100644 --- a/lib/libc/asr/res_mkquery.c +++ b/lib/libc/asr/res_mkquery.c @@ -1,4 +1,4 @@ -/* $OpenBSD: res_mkquery.c,v 1.1 2012/09/08 11:08:21 eric Exp $ */ +/* $OpenBSD: res_mkquery.c,v 1.2 2012/11/24 13:59:53 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> * @@ -33,7 +33,7 @@ res_mkquery(int op, const char *dname, int class, int type, unsigned char *buf, int buflen) { struct asr_ctx *ac; - struct packed p; + struct pack p; struct header h; char fqdn[MAXDNAME]; char dn[MAXDNAME]; @@ -60,7 +60,7 @@ res_mkquery(int op, const char *dname, int class, int type, h.flags |= RD_MASK; h.qdcount = 1; - packed_init(&p, buf, buflen); + pack_init(&p, buf, buflen); pack_header(&p, &h); pack_query(&p, type, class, dn); diff --git a/lib/libc/asr/res_send_async.c b/lib/libc/asr/res_send_async.c index 1f37d67742b..8e5b96e0a27 100644 --- a/lib/libc/asr/res_send_async.c +++ b/lib/libc/asr/res_send_async.c @@ -1,4 +1,4 @@ -/* $OpenBSD: res_send_async.c,v 1.4 2012/09/09 12:15:32 eric Exp $ */ +/* $OpenBSD: res_send_async.c,v 1.5 2012/11/24 13:59:53 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> * @@ -54,7 +54,7 @@ res_send_async(const unsigned char *buf, int buflen, unsigned char *ans, { struct asr_ctx *ac; struct async *as; - struct packed p; + struct unpack p; struct header h; struct query q; @@ -83,7 +83,7 @@ res_send_async(const unsigned char *buf, int buflen, unsigned char *ans, as->as.dns.obuflen = buflen; as->as.dns.obufsize = buflen; - packed_init(&p, (char*)buf, buflen); + unpack_init(&p, buf, buflen); unpack_header(&p, &h); unpack_query(&p, &q); if (p.err) { @@ -365,7 +365,7 @@ static int setup_query(struct async *as, const char *name, const char *dom, int class, int type) { - struct packed p; + struct pack p; struct header h; char fqdn[MAXDNAME]; char dname[MAXDNAME]; @@ -402,7 +402,7 @@ setup_query(struct async *as, const char *name, const char *dom, h.flags |= RD_MASK; h.qdcount = 1; - packed_init(&p, as->as.dns.obuf, as->as.dns.obufsize); + pack_init(&p, as->as.dns.obuf, as->as.dns.obufsize); pack_header(&p, &h); pack_query(&p, type, class, dname); if (p.err) { @@ -702,13 +702,13 @@ ensure_ibuf(struct async *as, size_t n) static int validate_packet(struct async *as) { - struct packed p; + struct unpack p; struct header h; struct query q; struct rr rr; int r; - packed_init(&p, as->as.dns.ibuf, as->as.dns.ibuflen); + unpack_init(&p, as->as.dns.ibuf, as->as.dns.ibuflen); unpack_header(&p, &h); if (p.err) |