Changeset dc95342 in mainline


Ignore:
Timestamp:
2013-04-22T06:45:49Z (11 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
31e9fe0
Parents:
7262f89
Message:

Memory freeing, error paths.

Location:
uspace/srv/net/dnsres
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/dnsres/dns_msg.c

    r7262f89 rdc95342  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2013 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    489489                dns_question_t *q = list_get_instance(link, dns_question_t, msg);
    490490                rc = dns_question_encode(q, data + di, size - di, &q_size);
    491                 assert(rc == EOK);
     491                if (rc != EOK) {
     492                        assert(rc == ENOMEM || rc == EINVAL);
     493                        free(data);
     494                        return rc;
     495                }
    492496
    493497                di += q_size;
     
    578582        return EOK;
    579583error:
    580         /* XXX Destroy message */
     584        dns_message_destroy(msg);
    581585        return rc;
     586}
     587
     588static void dns_question_destroy(dns_question_t *question)
     589{
     590        free(question->qname);
     591        free(question);
     592}
     593
     594static void dns_rr_destroy(dns_rr_t *rr)
     595{
     596        free(rr->name);
     597        free(rr->rdata);
     598        free(rr);
     599}
     600
     601void dns_message_destroy(dns_message_t *msg)
     602{
     603        link_t *link;
     604        dns_question_t *question;
     605        dns_rr_t *rr;
     606
     607        while (!list_empty(&msg->question)) {
     608                link = list_first(&msg->question);
     609                question = list_get_instance(link, dns_question_t, msg);
     610                list_remove(&question->msg);
     611                dns_question_destroy(question);
     612        }
     613
     614        while (!list_empty(&msg->answer)) {
     615                link = list_first(&msg->answer);
     616                rr = list_get_instance(link, dns_rr_t, msg);
     617                list_remove(&rr->msg);
     618                dns_rr_destroy(rr);
     619        }
     620
     621        while (!list_empty(&msg->authority)) {
     622                link = list_first(&msg->authority);
     623                rr = list_get_instance(link, dns_rr_t, msg);
     624                list_remove(&rr->msg);
     625                dns_rr_destroy(rr);
     626        }
     627
     628        while (!list_empty(&msg->additional)) {
     629                link = list_first(&msg->additional);
     630                rr = list_get_instance(link, dns_rr_t, msg);
     631                list_remove(&rr->msg);
     632                dns_rr_destroy(rr);
     633        }
     634
     635        free(msg);
    582636}
    583637
  • uspace/srv/net/dnsres/dns_msg.h

    r7262f89 rdc95342  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2013 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4545extern int dns_message_encode(dns_message_t *, void **, size_t *);
    4646extern int dns_message_decode(void *, size_t, dns_message_t **);
     47extern void dns_message_destroy(dns_message_t *);
    4748extern uint32_t dns_uint32_t_decode(uint8_t *, size_t);
    4849
  • uspace/srv/net/dnsres/dnsres.c

    r7262f89 rdc95342  
    6060int main(int argc, char *argv[])
    6161{
    62         dns_host_info_t hinfo;
     62        dns_host_info_t *hinfo;
    6363        char *astr;
    6464        int rc;
     
    6969
    7070        if (rc == EOK) {
    71                 rc = addr_format(&hinfo.addr, &astr);
     71                rc = addr_format(&hinfo->addr, &astr);
    7272                if (rc != EOK) {
     73                        dns_hostinfo_destroy(hinfo);
    7374                        printf("Out of memory\n");
    7475                        return ENOMEM;
    7576                }
    7677
    77                 printf("hostname: %s\n", hinfo.name);
     78                printf("hostname: %s\n", hinfo->name);
    7879                printf("IPv4 address: %s\n", astr);
    7980                free(astr);
     81                dns_hostinfo_destroy(hinfo);
    8082        }
    8183
  • uspace/srv/net/dnsres/query.c

    r7262f89 rdc95342  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2013 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#include <errno.h>
    3737#include <mem.h>
     38#include <stdlib.h>
    3839#include <str.h>
    3940
     
    4748
    4849#include <stdio.h>
    49 int dns_name2host(const char *name, dns_host_info_t *info)
     50int dns_name2host(const char *name, dns_host_info_t **rinfo)
    5051{
    5152        dns_message_t msg;
    5253        dns_message_t *amsg;
    5354        dns_question_t question;
     55        dns_host_info_t *info;
    5456        int rc;
    5557
     
    8183                        rr->name, rr->rtype, rr->rclass, rr->rdata_size);
    8284
    83                 if (rr->rtype == DTYPE_A && rr->rclass == DC_IN) {
    84                         if (rr->rdata_size != sizeof(uint32_t)) {
    85                                 printf("rdata_size = %u - fail\n", rr->rdata_size);
    86                                 return EIO;
     85                if (rr->rtype == DTYPE_A && rr->rclass == DC_IN &&
     86                        rr->rdata_size == sizeof(uint32_t)) {
     87
     88                        info = calloc(1, sizeof(dns_host_info_t));
     89                        if (info == NULL) {
     90                                dns_message_destroy(amsg);
     91                                return ENOMEM;
    8792                        }
    8893
     
    9095                        info->addr.ipv4 = dns_uint32_t_decode(rr->rdata, rr->rdata_size);
    9196                        printf("info->addr = %x\n", info->addr.ipv4);
     97
     98                        dns_message_destroy(amsg);
     99                        *rinfo = info;
    92100                        return EOK;
    93101                }
    94102        }
    95103
     104        dns_message_destroy(amsg);
    96105        printf("no A/IN found, fail\n");
    97106
     
    99108}
    100109
     110void dns_hostinfo_destroy(dns_host_info_t *info)
     111{
     112        free(info->name);
     113        free(info);
     114}
     115
    101116/** @}
    102117 */
  • uspace/srv/net/dnsres/query.h

    r7262f89 rdc95342  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2013 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3939#include "dns_type.h"
    4040
    41 extern int dns_name2host(const char *, dns_host_info_t *);
     41extern int dns_name2host(const char *, dns_host_info_t **);
     42extern void dns_hostinfo_destroy(dns_host_info_t *);
    4243
    4344#endif
  • uspace/srv/net/dnsres/transport.c

    r7262f89 rdc95342  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2013 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    120120
    121121        rc = dns_message_decode(recv_buf, recv_size, &resp);
    122         if (rc != EOK)
    123                 return EIO;
     122        if (rc != EOK) {
     123                rc = EIO;
     124                goto error;
     125        }
    124126
    125127        *rresp = resp;
Note: See TracChangeset for help on using the changeset viewer.