From 77825e4c00356383e69f3fc4c63eccd0a3aa103f Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Fri, 6 Jun 2008 14:18:28 +0000 Subject: Ticket #537: implemented Simple DNS Server class (for testing) in pjlib-util git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1987 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib-util/include/pjlib-util.h | 3 + pjlib-util/include/pjlib-util/dns.h | 54 ++++++++++++++ pjlib-util/include/pjlib-util/dns_server.h | 116 +++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 pjlib-util/include/pjlib-util/dns_server.h (limited to 'pjlib-util/include') diff --git a/pjlib-util/include/pjlib-util.h b/pjlib-util/include/pjlib-util.h index 728d8fee..567144a5 100644 --- a/pjlib-util/include/pjlib-util.h +++ b/pjlib-util/include/pjlib-util.h @@ -44,6 +44,9 @@ #include #include +/* Simple DNS server */ +#include + /* Text scanner */ #include diff --git a/pjlib-util/include/pjlib-util/dns.h b/pjlib-util/include/pjlib-util/dns.h index 10ba1166..a537169a 100644 --- a/pjlib-util/include/pjlib-util/dns.h +++ b/pjlib-util/include/pjlib-util/dns.h @@ -371,6 +371,60 @@ PJ_DECL(void) pj_dns_packet_dup(pj_pool_t *pool, PJ_DECL(const char *) pj_dns_get_type_name(int type); +/** + * Initialize DNS record as DNS SRV record. + * + * @param rec The DNS resource record to be initialized as DNS + * SRV record. + * @param res_name Resource name. + * @param dnsclass DNS class. + * @param ttl Resource TTL value. + * @param prio DNS SRV priority. + * @param weight DNS SRV weight. + * @param port Target port. + * @param target Target name. + */ +PJ_DECL(void) pj_dns_init_srv_rr(pj_dns_parsed_rr *rec, + const pj_str_t *res_name, + unsigned dnsclass, + unsigned ttl, + unsigned prio, + unsigned weight, + unsigned port, + const pj_str_t *target); + +/** + * Initialize DNS record as DNS CNAME record. + * + * @param rec The DNS resource record to be initialized as DNS + * CNAME record. + * @param res_name Resource name. + * @param dnsclass DNS class. + * @param ttl Resource TTL value. + * @param name Host name. + */ +PJ_DECL(void) pj_dns_init_cname_rr(pj_dns_parsed_rr *rec, + const pj_str_t *res_name, + unsigned dnsclass, + unsigned ttl, + const pj_str_t *name); + +/** + * Initialize DNS record as DNS A record. + * + * @param rec The DNS resource record to be initialized as DNS + * A record. + * @param res_name Resource name. + * @param dnsclass DNS class. + * @param ttl Resource TTL value. + * @param ip_addr Host address. + */ +PJ_DECL(void) pj_dns_init_a_rr(pj_dns_parsed_rr *rec, + const pj_str_t *res_name, + unsigned dnsclass, + unsigned ttl, + const pj_in_addr *ip_addr); + /** * Dump DNS packet to standard log. * diff --git a/pjlib-util/include/pjlib-util/dns_server.h b/pjlib-util/include/pjlib-util/dns_server.h new file mode 100644 index 00000000..742b3c8e --- /dev/null +++ b/pjlib-util/include/pjlib-util/dns_server.h @@ -0,0 +1,116 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2007 Benny Prijono + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef __PJLIB_UTIL_DNS_SERVER_H__ +#define __PJLIB_UTIL_DNS_SERVER_H__ + +/** + * @file dns_server.h + * @brief Simple DNS server + */ +#include +#include + +PJ_BEGIN_DECL + +/** + * @defgroup PJ_DNS_SERVER Simple DNS Server + * @ingroup PJLIB_UTIL + * @{ + * This contains a simple but fully working DNS server implementation, + * mostly for testing purposes. It supports serving various DNS resource + * records such as SRV, CNAME, A, and AAAA. + */ + +/** + * Opaque structure to hold DNS server instance. + */ +typedef struct pj_dns_server pj_dns_server; + +/** + * Create the DNS server instance. The instance will run immediately. + * + * @param pf The pool factory to create memory pools. + * @param ioqueue Ioqueue instance where the server socket will be + * registered to. + * @param af Address family of the server socket (valid values + * are pj_AF_INET() for IPv4 and pj_AF_INET6() for IPv6). + * @param port The UDP port to listen. + * @param flags Flags, currently must be zero. + * @param p_srv Pointer to receive the DNS server instance. + * + * @return PJ_SUCCESS if server has been created successfully, + * otherwise the function will return the appropriate + * error code. + */ +PJ_DECL(pj_status_t) pj_dns_server_create(pj_pool_factory *pf, + pj_ioqueue_t *ioqueue, + int af, + unsigned port, + unsigned flags, + pj_dns_server **p_srv); + +/** + * Destroy DNS server instance. + * + * @param srv The DNS server instance. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) pj_dns_server_destroy(pj_dns_server *srv); + + +/** + * Add generic resource record entries to the server. + * + * @param srv The DNS server instance. + * @param count Number of records to be added. + * @param rr Array of records to be added. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) pj_dns_server_add_rec(pj_dns_server *srv, + unsigned count, + const pj_dns_parsed_rr rr[]); + +/** + * Remove the specified record from the server. + * + * @param srv The DNS server instance. + * @param dns_class The resource's DNS class. Valid value is PJ_DNS_CLASS_IN. + * @param type The resource type. + * @param name The resource name to be removed. + * + * @return PJ_SUCCESS on success or the appropriate error code. + */ +PJ_DECL(pj_status_t) pj_dns_server_del_rec(pj_dns_server *srv, + int dns_class, + pj_dns_type type, + const pj_str_t *name); + + + +/** + * @} + */ + +PJ_END_DECL + + +#endif /* __PJLIB_UTIL_DNS_SERVER_H__ */ + -- cgit v1.2.3