Changeset 1355


Ignore:
Timestamp:
Jun 11, 2007 4:43:29 PM (12 years ago)
Author:
bennylp
Message:

Ticket #328: Possible alignment error in DNS

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib-util/src/pjlib-util/dns.c

    r1239 r1355  
    4242 
    4343 
     44static void write16(pj_uint8_t *p, pj_uint16_t val) 
     45{ 
     46    p[0] = (pj_uint8_t)(val >> 8); 
     47    p[1] = (pj_uint8_t)(val & 0xFF); 
     48} 
     49 
     50 
    4451/** 
    4552 * Initialize a DNS query transaction. 
     
    5158                                       const pj_str_t *name) 
    5259{ 
    53     pj_dns_hdr *hdr; 
    54     char *query, *p; 
     60    pj_uint8_t *query, *p = packet; 
    5561    const char *startlabel, *endlabel, *endname; 
    56     pj_uint16_t tmp; 
    5762    unsigned d; 
    5863 
     
    6772 
    6873    /* Initialize header */ 
    69     hdr = (pj_dns_hdr*) packet; 
    70     pj_bzero(hdr, sizeof(struct pj_dns_hdr)); 
    71     hdr->id = pj_htons(id); 
    72     hdr->flags = pj_htons(PJ_DNS_SET_RD(1)); 
    73     hdr->qdcount = pj_htons(1); 
     74    pj_assert(sizeof(pj_dns_hdr)==12); 
     75    pj_bzero(p, sizeof(struct pj_dns_hdr)); 
     76    write16(p+0, id); 
     77    write16(p+2, (pj_uint16_t)PJ_DNS_SET_RD(1)); 
     78    write16(p+4, (pj_uint16_t)1); 
    7479 
    7580    /* Initialize query */ 
    76     query = p = (char*)(hdr+1); 
     81    query = p = ((pj_uint8_t*)packet)+sizeof(pj_dns_hdr); 
    7782 
    7883    /* Tokenize name */ 
     
    8287        while (endlabel != endname && *endlabel != '.') 
    8388            ++endlabel; 
    84         *p++ = (char)(endlabel - startlabel); 
     89        *p++ = (pj_uint8_t)(endlabel - startlabel); 
    8590        pj_memcpy(p, startlabel, endlabel-startlabel); 
    8691        p += (endlabel-startlabel); 
     
    9297 
    9398    /* Set type */ 
    94     tmp = pj_htons((pj_uint16_t)(qtype)); 
    95     pj_memcpy(p, &tmp, 2); 
     99    write16(p, (pj_uint16_t)qtype); 
    96100    p += 2; 
    97101 
    98102    /* Set class (IN=1) */ 
    99     tmp = pj_htons(1); 
    100     pj_memcpy(p, &tmp, 2); 
     103    write16(p, 1); 
    101104    p += 2; 
    102105 
    103106    /* Done, calculate length */ 
    104     *size = p - (char*)packet; 
     107    *size = p - (pj_uint8_t*)packet; 
    105108 
    106109    return 0; 
     
    112115 */ 
    113116static pj_status_t get_name_len(int rec_counter, const pj_uint8_t *pkt,  
    114                                 const pj_uint8_t *start, const pj_uint8_t *max, 
     117                                const pj_uint8_t *start, const pj_uint8_t *max,  
    115118                                int *parsed_len, int *name_len) 
    116119{ 
     
    240243 
    241244 
    242 /* Skip query records. */ 
     245/* Parse query records. */ 
    243246static pj_status_t parse_query(pj_dns_parsed_query *q, pj_pool_t *pool, 
    244247                               const pj_uint8_t *pkt, const pj_uint8_t *start, 
     
    327330    /* Get TTL */ 
    328331    pj_memcpy(&rr->ttl, p, 4); 
    329     rr->ttl = pj_htonl(rr->ttl); 
     332    rr->ttl = pj_ntohl(rr->ttl); 
    330333    p += 4; 
    331334 
    332335    /* Get rdlength */ 
    333336    pj_memcpy(&rr->rdlength, p, 2); 
    334     rr->rdlength = pj_htons(rr->rdlength); 
     337    rr->rdlength = pj_ntohs(rr->rdlength); 
    335338    p += 2; 
    336339 
Note: See TracChangeset for help on using the changeset viewer.