📄 dns_client_resource.c
字号:
/* ############################################################################ (c) Copyright Virata Limited 2001 ## Virata Limited Confidential and Proprietary## The following software source code ("Software") is strictly confidential and# is proprietary to Virata Limited ("Virata"). It may only be read, used,# copied, adapted, modified or otherwise dealt with by you if you have# entered into a confidentiality agreement with Virata and then subject to the# terms of that confidentiality agreement and any other applicable agreement# between you and Virata. If you are in any doubt as to whether you are # entitled to access, read, use, copy, adapt, modify or otherwise deal with# the Software or whether you are entitled to disclose the Software to any# other person you should contact Virata. If you have not entered into a# confidentiality agreement with Virata granting access to this Software you# should forthwith return all media, copies and printed listings containing# the Software to Virata. ## Virata reserves the right to take legal action against you should you breach# the above provisions.## If you are unsure, or to report violations, please contact # support@virata.com# ##########################################################################*//* * file: dns_client_resource.c * * description: this file contains typdefs, declarations and routines that * provide control for resources needed by the dns client * application (and also DNS relay). Moved some common utility * functions here rather than having them static inline in * dns_client.h, to save space. *//* * List of New Routines: * dns_ip6_alloc() */#include <stdlib.h>#include "dns_resolv.h"#include "dns_client_resource.h"#undef DEBUG_LEVEL#define DEBUG_LEVEL (0x01)#ifdef DNS_DEBUGextern void print_cache_resource(void);#endiftypedef struct { /* structure used to allocate space for IP addresses */ long used; struct sockaddr_storage address;} ip_t;typedef struct { /* structure used to allocate space for domain names */ long used; U8 name[NAMEMAX+1];} name_t;/* * dns_ip_alloc(), dns_ip_free(), dns_name_alloc(), dns_name_free() * and dns_ip6_alloc() * * these routines find space that will be used in the data portion of the * DNS resource records, RR. currently we only handle RR types that deal * with IP addresses and names. *//* * dns_ip6_alloc -- * Allocate resources for IPv6 address. * * PARAMETERS * n/a * * RETURNS * pointer to allocated area for IPv6 address or * NULL on failure */U8 *dns_ip6_alloc(){ U8 *ip6 = (U8*)malloc(sizeof(struct sockaddr_in6)); if( ip6 ) memset(ip6, 0, sizeof(struct sockaddr_in6)); return ip6;} /* end dns_ip6_alloc() *//* * dns_ip_alloc() and dns_ip_free() * * these routines allocate and free elements of the iplist. these are used * for storing IP addresses in the DNS messages. * * return: dns_ip_alloc() returns a pointer to the address element of the * structure. */U8 *dns_ip_alloc(){ U8 *ip4 = (U8*)malloc(sizeof(struct sockaddr_in)); if( ip4 ) memset(ip4, 0, sizeof(struct sockaddr_in)); return ip4;} /* end dns_ip_alloc() */voiddns_ip_free(U8 *ip){ assert(ip); free(ip);} /* end dns_ip_free() *//* * dns_name_alloc() and dns_name_free() * * these routines allocate and free charater arrays used to store domain names. * they are used in constructing and parsing the DNS messages. * * return: dns_name_alloc() returns a pointer to the array element of the * structure. */U8 *dns_name_alloc(){ /* For now, we resign ourselves to pretty much guaranteeing * to waste space by assuming the worst string size possible! */ U8 *name = (U8*)malloc(NAMEMAX+1); if( name ) memset(name, 0, NAMEMAX+1); return name;} /* end dns_name_alloc() */voiddns_name_free(U8 *name){ assert(name); free(name);} /* end dns_name_free() *//* * dns_get_question() * * this routine will return a pointer to the first unused element in the list * DnsQuestions. these are used in the construction and parsing of the DNS * messages. * * return: pointer to the first unused question in the list, or * NULL if there are no unused elements. */DnsQuestion *dns_get_question(){ DnsQuestion *q = (DnsQuestion *)malloc(sizeof(DnsQuestion)); if( q ) memset( q, 0, sizeof(DnsQuestion)); return q;} /* end dns_get_question() *//* * dns_free_question() * * this routine will mark the DnsQuestion element as unused. */voiddns_free_question(DnsQuestion *dq){ assert(dq); free(dq);} /* end dns_free_question() *//* * dns_get_RR() * * this routine will find the first unused DnsRRec structure in the list. These * are used in the construction and parsing of DNS messages. * * return: pointer to the first unused element of the list, or * NULL to indicate they are all used. */DnsRRec *dns_get_RR(){ DnsRRec *rr = (DnsRRec *)malloc(sizeof(DnsRRec)); if (rr) memset(rr, 0, sizeof(DnsRRec)); return rr;} /* end dns_get_RR() *//* * dns_free_RR() * * this routine marks the given DnsRRec are unused. * note this does not free the r_data memory, it is expected to have already * been freed. */voiddns_free_RR(DnsRRec *RR){ assert(RR); free(RR);} /* end dns_free_RR() *//* * dns_get_SOA() * * this routine will find the first unused SOArec structure and return a pointer * to it. note that there is only a small number of these since we don't expect * to receive a lot for the name servers. we should typically only get one in an * individual message. they are typically used for negative caching, see RFC 2308. * * return: pointer to the found SOArec structure, or * NULL if none available. */SOArec *dns_get_SOA(){ SOArec *soa = (SOArec *)malloc(sizeof(SOArec)); if (soa) memset(soa, 0, sizeof(SOArec)); return soa;} /* end dns_get_SOA() */ /* * dns_free_SOA() * * this routine will just mark the indicated SOArec structure as unused. */voiddns_free_SOA(SOArec *soa){ assert(soa); free(soa);} /* end dns_free_SOA() *//* dns_freehostent -- * This routine releases memory occupied by 'hostent' structure. * This structure in its turn consists of pointers which also * need to be released. However, we always check that the pointers * being released are not NULL. This fact is extensively used * in dns_getipnodeby() functions. * * PARAMETERS * p_hent - host entry to release * * RETURNS * N/A */voiddns_freehostent(IN OUT struct hostent *p_hent){ if (p_hent == NULL) return; /* release h_name */ if (p_hent->h_name) { free((void *)(p_hent->h_name)); } /* release aliases */ if (p_hent->h_aliases) { char **tmp; char *h_alias; for (tmp = p_hent->h_aliases, h_alias = *tmp; h_alias != NULL; tmp++, h_alias = *tmp) { free(h_alias); } free(p_hent->h_aliases); } /* release address list */ if (p_hent->h_addr_list) { char **tmp; char *p_addr; for (tmp = p_hent->h_addr_list, p_addr = *tmp; p_addr != NULL; tmp++, p_addr = *tmp) { free(p_addr); } free(p_hent->h_addr_list); } free(p_hent); return;} /* dns_freehostent */#if 0#ifdef DNS_DEBUG/* * print_resource_pool() * * this routine prints out information about the node_t pool and the * lists. */voidprint_resource_pool(){ dprintf("\n"); dprintf("QUERY STATE POOL\n"); dprintf("pool size: %d in use: %d failures: %d\n\n", MAXQS, ncnt, nfail); dprintf("IP ADDRESS BUFFERS FOR DNS MESSAGES\n"); dprintf("pool size: %d in use: %d failures: %d\n\n", MAXIPS, ipcnt, ipfail); dprintf("NAME SPACE BUFFERS FOR DNS MESSAGES\n"); dprintf("pool size: %d in use: %d failures: %d\n\n", MAXNAMES, nlcnt, nlfail); dprintf("DNS QUESTION STRUCTURES\n"); dprintf("pool size: %d in use: %d failures: %d\n\n", MAXQUESTS, dqcnt, dqfail); dprintf("DNS RESOURCE RECORD STRUCTURES\n"); dprintf("pool size: %d in use: %d failures: %d\n\n", MAXRRS, rrcnt, rrfail); dprintf("SOA RECORDS\n"); dprintf("pool size: %d in use: %d failures: %d\n\n", MAXSOA, soacnt, soafail); dprintf("DNS HOSTENT POOL\n"); dprintf("pool size: %d in use: %d failures: %d\n\n", MAXMSGHOSTENT, hpcnt, hpfail); dprintf("NSLOOKUP POOL\n"); dprintf("pool size: %d in use: %d failures: %d\n\n", MAXNLP, nlpcnt, nlpfail);/* print_cache_resource(); */} /* end print_resource_pool() */#endif#endif //0/* Utility functions. * These were formally static inline in dns_client.h. * Moved here, no longer inlined to save space. *//* * long2sock -- * This routine converts U32 IPv4 address into sockaddr_in structure * * PARAMETERS * sock - storage for result * ip - IPv4 address to convert * * RETURNS * n/a */voidlong2sock(OUT void *sock, IN U32 ip)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -