sdns
low-level DNS library
Loading...
Searching...
No Matches
Classes | Macros | Typedefs | Enumerations | Functions
sdns.h File Reference
#include <stdint.h>
Include dependency graph for sdns.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  sdns_question
 
struct  _sdns_opt_rdata
 
struct  sdns_opt_ttl
 
struct  _sdns_rr
 
struct  sdns_header
 
struct  sdns_message
 
struct  sdns_rr_A
 
struct  sdns_rr_AAAA
 
struct  txt_data
 
struct  _sdns_rr_TXT
 
struct  sdns_rr_SOA
 
struct  sdns_rr_MX
 
struct  sdns_rr_NS
 
struct  sdns_rr_PTR
 
struct  sdns_rr_CNAME
 
struct  sdns_rr_OPT_EDE
 
struct  sdns_rr_RRSIG
 
struct  sdns_rr_SRV
 
struct  sdns_rr_URI
 
struct  sdns_rr_NID
 
struct  sdns_rr_HINFO
 
struct  sdns_rr_L32
 
struct  sdns_rr_L64
 
struct  sdns_rr_LP
 
struct  sdns_rr_CAA
 
struct  sdns_context
 

Macros

#define TO_OUTPUT(A, B)   do{if (NULL == A) {fprintf(stdout, B);}else{strcpy(A,B);}}while(0);
 
#define UDP_PAYLOAD_SIZE   1232
 Default UDP payload size for EDNS0.
 
#define DNS_HEADER_LENGTH   12
 Standard DNS Header length.
 
#define SDNS_ERROR_MEMORY_ALLOC_FAILED   -1
 memory allocation using malloc() failed
 
#define SDNS_ERROR_BUFFER_TOO_SHORT   -2
 DNS packet length is shorter than expected.
 
#define SDNS_ERROR_QNAME_IS_NULL   -3
 Qname of the question section can not be null.
 
#define SDNS_ERROR_HOSTNAME_TOO_LONG   -4
 Hostname can not be more than 255 characters.
 
#define SDNS_ERROR_WRONG_LABEL_SPECIFIED   -5
 Label name is too long or malformed.
 
#define SDNS_ERROR_BUFFER_IS_NULL   -6
 passed buffer to the function is NULL
 
#define SDNS_ERROR_BUFFER_IS_SMALL   -7
 buffer is shorter than expected
 
#define SDNS_ERROR_INVALID_DNS_PACKET   -8
 The packet is not a valid DNS packet.
 
#define SDNS_ERROR_MORE_THAN_ONE_QUESTION_FOUND   -9
 Each DNS packet must have exactly one question section.
 
#define SDNS_ERROR_RR_NULL   -10
 Resource Record section pointer is NULL.
 
#define SDNS_ERROR_ELSIMPLE   -11
 Success code for simple encoding of the name.
 
#define SDNS_ERROR_ELCOMPRESSED   -12
 Success code for compressed encoding of the name.
 
#define SDNS_ERROR_LABEL_MAX_63   -13
 each part of the name must be <= 63 characters
 
#define SDNS_ERROR_ILLEGAL_COMPRESSION   -14
 Compressed label found when it's illegal.
 
#define SDNS_ERROR_RR_SECTION_MALFORMED   -15
 Resource Record section is malformed (not enough or wrong data)
 
#define SDNS_ERROR_INVALID_HEX_VALUE   -16
 User entered a hex string that is not valid.
 
#define SDNS_ERROR_WRONG_INPUT_PARAMETER   -17
 The input parameter is not valid.
 
#define SDNS_ERROR_NSID_NOT_FOUND   -18
 There is no NSID in the DNS packet.
 
#define SDNS_ERROR_CLIENT_COOKIE_NOT_FOUND   -19
 There is no client cookie in the DNS packet.
 
#define SDNS_ERROR_CHARACTER_STRING_TOO_LONG   -20
 Maximum size of character string is 255 character.
 
#define SDNS_ERROR_NO_ANSWER_FOUND   -21
 This error shows that the DNS context has no answer section.
 
#define SDNS_ERROR_CAN_NOT_READ_SECTION   -22
 For any reason, it's not possible to decode or copy the rdata of the section.
 
#define SDNS_ERROR_INVALID_IPv6_FOUND   -23
 The code only accepts IPv6 in a format of ":" separated hex values.
 
#define SDNS_ERROR_NO_AUTHORITY_FOUND   -24
 This error shows that the DNS context has no authority section.
 
#define SDNS_ERROR_NO_ADDITIONAL_FOUND   -25
 This error shows that the DNS context has no additional section.
 
#define SDNS_ERROR_ADDITIONAL_RR_OPT   -26
 Additional section is of type OPT. Use the relevant function to fetch it.
 
#define DNS_SECTION_ANSWER   1
 Macro defining the answer section.
 
#define DNS_SECTION_AUTHORITY   2
 Macro defining the authority section.
 
#define DNS_SECTION_ADDITIONAL   3
 Macro defining the additional section.
 
#define DNS_SECTION_QUESTION   4
 Macro defining the question section.
 
#define SDNS_QR_MASK   0x8000
 
#define SDNS_OPCODE_MASK   0x7800
 
#define SDNS_AA_MASK   0x0400
 
#define SDNS_TC_MASK   0x0200
 
#define SDNS_RD_MASK   0x0100
 
#define SDNS_RA_MASK   0x0080
 
#define SDNS_Z_MASK   0x0040
 
#define SDNS_AD_MASK   0x0020
 
#define SDNS_CD_MASK   0x0010
 
#define SDNS_RCODE_MASK   0x000F
 

Typedefs

typedef struct _sdns_opt_rdata sdns_opt_rdata
 see _sdns_opt_rdata for more info
 
typedef struct _sdns_rr sdns_rr
 See _sdns_rr for more info.
 
typedef struct _sdns_rr_TXT sdns_rr_TXT
 

Enumerations

enum  sdns_opcode {
  sdns_opcode_Query =0 , sdns_opcode_IQuery =1 , sdns_opcode_Status =2 , sdns_opcode_Notify =4 ,
  sdns_opcode_Update =5 , sdns_opcode_DSO =6
}
 
enum  sdns_rcode {
  sdns_rcode_NoError =0 , sdns_rcode_FormErr =1 , sdns_rcode_ServFail =2 , sdns_rcode_NXDomain =3 ,
  sdns_rcode_NotImp =4 , sdns_rcode_Refused =5 , sdns_rcode_YXDomain =6 , sdns_rcode_YXRRSet =7 ,
  sdns_rcode_NXRRSet =8 , sdns_rcode_NotAuth =9 , sdns_rcode_NotZone =10 , sdns_rcode_DSOTYPENI =11 ,
  sdns_rcode_BADVERS =16 , sdns_rcode_BADSIG =16 , sdns_rcode_BADKEY =17 , sdns_rcode_BADTIME =18 ,
  sdns_rcode_BADMODE =19 , sdns_rcode_BADNAME =20 , sdns_rcode_BADALG =21 , sdns_rcode_BADTRUNC =22 ,
  sdns_rcode_BADCOOKIE =23 , sdns_rcode_Reserved =65535
}
 
enum  sdns_rr_type {
  sdns_rr_type_A =1 , sdns_rr_type_NS =2 , sdns_rr_type_MD =3 , sdns_rr_type_MF =4 ,
  sdns_rr_type_CNAME =5 , sdns_rr_type_SOA =6 , sdns_rr_type_MB =7 , sdns_rr_type_MG =8 ,
  sdns_rr_type_MR =9 , sdns_rr_type_NULL =10 , sdns_rr_type_WKS =11 , sdns_rr_type_PTR =12 ,
  sdns_rr_type_HINFO =13 , sdns_rr_type_MINFO =14 , sdns_rr_type_MX =15 , sdns_rr_type_TXT =16 ,
  sdns_rr_type_RP =17 , sdns_rr_type_AFSDB =18 , sdns_rr_type_X25 =19 , sdns_rr_type_ISDN =20 ,
  sdns_rr_type_RT =21 , sdns_rr_type_NSAP =22 , sdns_rr_type_NSAP_PTR =23 , sdns_rr_type_SIG =24 ,
  sdns_rr_type_KEY =25 , sdns_rr_type_PX =26 , sdns_rr_type_GPOS =27 , sdns_rr_type_AAAA =28 ,
  sdns_rr_type_LOC =29 , sdns_rr_type_NXT =30 , sdns_rr_type_EID =31 , sdns_rr_type_NIMLOC =32 ,
  sdns_rr_type_SRV =33 , sdns_rr_type_ATMA =34 , sdns_rr_type_NAPTR =35 , sdns_rr_type_KX =36 ,
  sdns_rr_type_CERT =37 , sdns_rr_type_A6 =38 , sdns_rr_type_DNAME =39 , sdns_rr_type_SINK =40 ,
  sdns_rr_type_OPT =41 , sdns_rr_type_APL =42 , sdns_rr_type_DS =43 , sdns_rr_type_SSHFP =44 ,
  sdns_rr_type_IPSECKEY =45 , sdns_rr_type_RRSIG =46 , sdns_rr_type_NSEC =47 , sdns_rr_type_DNSKEY =48 ,
  sdns_rr_type_DHCID =49 , sdns_rr_type_NSEC3 =50 , sdns_rr_type_NSEC3PARAM =51 , sdns_rr_type_TLSA =52 ,
  sdns_rr_type_SMIMEA =53 , sdns_rr_type_HIP =55 , sdns_rr_type_NINFO =56 , sdns_rr_type_RKEY =57 ,
  sdns_rr_type_TALINK =58 , sdns_rr_type_CDS =59 , sdns_rr_type_CDNSKEY =60 , sdns_rr_type_OPENPGPKEY =61 ,
  sdns_rr_type_CSYNC =62 , sdns_rr_type_ZONEMD =63 , sdns_rr_type_SVCB =64 , sdns_rr_type_HTTPS =65 ,
  sdns_rr_type_SPF =99 , sdns_rr_type_UINFO =100 , sdns_rr_type_UID =101 , sdns_rr_type_GID =102 ,
  sdns_rr_type_UNSPEC =103 , sdns_rr_type_NID =104 , sdns_rr_type_L32 =105 , sdns_rr_type_L64 =106 ,
  sdns_rr_type_LP =107 , sdns_rr_type_EUI48 =108 , sdns_rr_type_EUI64 =109 , sdns_rr_type_TKEY =249 ,
  sdns_rr_type_TSIG =250 , sdns_rr_type_IXFR =251 , sdns_rr_type_AXFR =252 , sdns_rr_type_MAILB =253 ,
  sdns_rr_type_MAILA =254 , sdns_rr_type_star =255 , sdns_rr_type_URI =256 , sdns_rr_type_CAA =257 ,
  sdns_rr_type_AVC =258 , sdns_rr_type_DOA =259 , sdns_rr_type_AMTRELAY =260 , sdns_rr_type_RESINFO =261 ,
  sdns_rr_type_WALLET =262 , sdns_rr_type_TA =32768 , sdns_rr_type_DLV =32769
}
 
enum  sdns_edns0_option_code {
  sdns_edns0_option_code_Reserved0 =0 , sdns_edns0_option_code_LLQ =1 , sdns_edns0_option_code_Update_Lease =2 , sdns_edns0_option_code_NSID =3 ,
  sdns_edns0_option_code_Reserved4 =4 , sdns_edns0_option_code_DAU =5 , sdns_edns0_option_code_DHU =6 , sdns_edns0_option_code_N3U =7 ,
  sdns_edns0_option_code_edns_client_subnet =8 , sdns_edns0_option_code_EDNS_EXPIRE =9 , sdns_edns0_option_code_COOKIE =10 , sdns_edns0_option_code_edns_tcp_keepalive =11 ,
  sdns_edns0_option_code_Padding =12 , sdns_edns0_option_code_CHAIN =13 , sdns_edns0_option_code_edns_key_tag =14 , sdns_edns0_option_code_Extended_DNS_Error =15 ,
  sdns_edns0_option_code_EDNS_Client_Tag =16 , sdns_edns0_option_code_EDNS_Server_Tag =17 , sdns_edns0_option_code_Report_Channel =18 , sdns_edns0_option_code_Umbrella_Ident =20292 ,
  sdns_edns0_option_code_DeviceID =26946
}
 
enum  sdns_rr_class { sdns_rr_class_IN =1 , sdns_rr_class_CS =2 , sdns_rr_class_CH =3 , sdns_rr_class_HS =4 }
 
enum  sdns_q_class {
  sdns_q_class_IN =1 , sdns_q_class_CS =2 , sdns_q_class_CH =3 , sdns_q_class_HS =4 ,
  sdns_q_class_STAR =255
}
 
enum  sdns_ede_code {
  sdns_ede_code_Other_Error =0 , sdns_ede_code_Unsupported_DNSKEY_Algorithm =1 , sdns_ede_code_Unsupported_DS_Digest_Type =2 , sdns_ede_code_Stale_Answer =3 ,
  sdns_ede_code_Forged_Answer =4 , sdns_ede_code_DNSSEC_Indeterminate =5 , sdns_ede_code_DNSSEC_Bogus =6 , sdns_ede_code_Signature_Expired =7 ,
  sdns_ede_code_Signature_Not_Yet_Valid =8 , sdns_ede_code_DNSKEY_Missing =9 , sdns_ede_code_RRSIGs_Missing =10 , sdns_ede_code_No_Zone_Key_Bit_Set =11 ,
  sdns_ede_code_NSEC_Missing =12 , sdns_ede_code_Cached_Error =13 , sdns_ede_code_Not_Ready =14 , sdns_ede_code_Blocked =15 ,
  sdns_ede_code_Censored =16 , sdns_ede_code_Filtered =17 , sdns_ede_code_Prohibited =18 , sdns_ede_code_Stale_NXDomain_Answer =19 ,
  sdns_ede_code_Not_Authoritative =20 , sdns_ede_code_Not_Supported =21 , sdns_ede_code_No_Reachable_Authority =22 , sdns_ede_code_Network_Error =23 ,
  sdns_ede_code_Invalid_Data =24 , sdns_ede_code_Signature_Expired_before_Valid =25 , sdns_ede_code_Too_Early =26 , sdns_ede_code_Unsupported_NSEC3_Iterations_Value =27 ,
  sdns_ede_code_Unable_to_conform_to_policy =28 , sdns_ede_code_Synthesized =29
}
 

Functions

int sdns_make_query (sdns_context *ctx, sdns_rr_type qtype, sdns_q_class cls, char *qname, int enable_edns0)
 Creates a DNS question query.
 
int sdns_to_wire (sdns_context *ctx)
 Coverts a DNS context to binary format.
 
int sdns_from_wire (sdns_context *ctx)
 Converts the raw data received from socket (bytes) to a DNS packet.
 
void sdns_error_string (int err, char **err_buff)
 Coverts error codes to a string.
 
sdns_contextsdns_init_context (void)
 Initialize and create a new DNS context.
 
void sdns_free_context (sdns_context *ctx)
 Frees the context allocated by sdns_init_context().
 
int sdns_add_answer_section (sdns_context *ctx, sdns_rr *rr)
 Creates an answer section for the DNS packet.
 
int sdns_add_authority_section (sdns_context *ctx, sdns_rr *rr)
 Adds a new RR to the authority section of the DNS packet.
 
int sdns_add_additional_section (sdns_context *ctx, sdns_rr *rr)
 Adds a new RR to the additional section of the DNS packet.
 
int check_if_qtype_is_valid (uint16_t qtype)
 This function checks if the given qtype code is valid or not.
 
int check_if_rrtype_is_valid (uint16_t qtype)
 This function checks if the given RR type is valid or not.
 
int check_if_qclass_is_valid (uint16_t qclass)
 This function checks if the given Question class is valid or not.
 
int check_if_rrclass_is_valid (uint16_t qclass)
 This function checks if the given RR class is valid or not.
 
sdns_messagesdns_init_message (void)
 Initialize a DNS message structure (sdns_message).
 
sdns_rrsdns_init_rr (char *name, uint16_t type, uint16_t class, uint32_t ttl, uint16_t rdlength, uint8_t decoded, void *rdata)
 Initialize an RR data structure.
 
int sdns_create_edns_option (uint16_t, uint16_t, char *, sdns_opt_rdata **)
 
int sdns_add_edns (sdns_context *ctx, sdns_opt_rdata *opt)
 
sdns_opt_rdatasdns_create_edns0_ede (uint16_t info_code, char *extra_text, uint16_t extra_text_len)
 Creates EDNS0 extended DNS error option.
 
sdns_opt_rdatasdns_create_edns0_nsid (char *nsid, uint16_t nsid_len)
 
int sdns_ends0_option_code_to_text (sdns_edns0_option_code oc, char *buffer)
 Copies the text description of the EDNS0 option code to the buffer.
 
void * sdns_copy_rr_section (sdns_context *, sdns_rr *)
 
void * sdns_decode_rr_section (sdns_context *, sdns_rr *)
 
sdns_rr_Asdns_decode_rr_A (sdns_context *, sdns_rr *)
 
sdns_rr_AAAAsdns_decode_rr_AAAA (sdns_context *, sdns_rr *)
 
sdns_rr_TXTsdns_decode_rr_TXT (sdns_context *, sdns_rr *)
 
sdns_rr_SOAsdns_decode_rr_SOA (sdns_context *, sdns_rr *)
 
sdns_rr_MXsdns_decode_rr_MX (sdns_context *, sdns_rr *)
 
sdns_rr_NSsdns_decode_rr_NS (sdns_context *ctx, sdns_rr *rr)
 
sdns_rr_PTRsdns_decode_rr_PTR (sdns_context *ctx, sdns_rr *rr)
 
sdns_rr_CNAMEsdns_decode_rr_CNAME (sdns_context *ctx, sdns_rr *rr)
 
sdns_rr_RRSIGsdns_decode_rr_RRSIG (sdns_context *ctx, sdns_rr *rr)
 
sdns_rr_SRVsdns_decode_rr_SRV (sdns_context *ctx, sdns_rr *rr)
 
sdns_rr_HINFOsdns_decode_rr_HINFO (sdns_context *ctx, sdns_rr *rr)
 
sdns_rr_URIsdns_decode_rr_URI (sdns_context *ctx, sdns_rr *rr)
 
sdns_rr_NIDsdns_decode_rr_NID (sdns_context *ctx, sdns_rr *rr)
 
sdns_rr_L32sdns_decode_rr_L32 (sdns_context *ctx, sdns_rr *rr)
 
sdns_rr_L64sdns_decode_rr_L64 (sdns_context *ctx, sdns_rr *rr)
 
sdns_rr_LPsdns_decode_rr_LP (sdns_context *ctx, sdns_rr *rr)
 
sdns_rr_CAAsdns_decode_rr_CAA (sdns_context *ctx, sdns_rr *rr)
 
sdns_opt_rdatasdns_decode_rr_OPT (sdns_context *ctx, sdns_rr *rr)
 
sdns_rr_Asdns_init_rr_A (uint32_t ipaddress)
 Initialize a structure of sdns_rr_A.
 
sdns_rr_AAAAsdns_init_rr_AAAA (char *aaaa)
 Initialize a structure of sdns_rr_AAAA.
 
sdns_rr_TXTsdns_init_rr_TXT (char *data, uint16_t data_len)
 Initialize a structure of sdns_rr_TXT.
 
sdns_rr_SRVsdns_init_rr_SRV (uint16_t Priority, uint16_t Weight, uint16_t Port, char *target)
 Initialize a structure of sdns_rr_SRV.
 
sdns_rr_MXsdns_init_rr_MX (uint16_t preference, char *exchange)
 Initialize a structure of sdns_rr_MX.
 
sdns_rr_NSsdns_init_rr_NS (char *nsdname)
 Initialize a structure of sdns_rr_NS.
 
sdns_rr_PTRsdns_init_rr_PTR (char *ptrdname)
 Initialize a structure of sdns_rr_PTR.
 
sdns_rr_CNAMEsdns_init_rr_CNAME (char *cname)
 Initialize a structure of sdns_rr_CNAME.
 
sdns_rr_NIDsdns_init_rr_NID (uint16_t preference, char *nodid)
 Initialize a structure of sdns_rr_NID.
 
sdns_rr_L32sdns_init_rr_L32 (uint16_t preference, uint32_t locator32)
 Initialize a structure of sdns_rr_L32.
 
sdns_rr_L64sdns_init_rr_L64 (uint16_t preference, char *locator64)
 Initialize a structure of sdns_rr_L64.
 
sdns_rr_LPsdns_init_rr_LP (uint16_t preference, char *fqdn)
 Initialize a structure of sdns_rr_LP.
 
sdns_rr_CAAsdns_init_rr_CAA (uint8_t flag, char *tag, uint8_t tag_len, char *value, uint16_t value_len)
 Initialize a structure of sdns_rr_CAA.
 
void sdns_free_opt_rdata (sdns_opt_rdata *opt)
 
sdns_opt_rdatasdns_init_opt_rdata (void)
 
sdns_rr_SRVsdns_init_SRV (uint16_t, uint16_t, uint16_t, char *)
 
sdns_rr_URIsdns_init_rr_URI (uint16_t, uint16_t, char *, uint16_t)
 
sdns_rr_RRSIGsdns_init_rr_RRSIG (uint16_t, uint8_t, uint8_t, uint32_t, uint32_t, uint32_t, uint8_t, char *, char *, uint16_t)
 
sdns_rr_HINFOsdns_init_rr_HINFO (uint8_t cpu_len, char *cpu, uint8_t os_len, char *os)
 
sdns_rr_SOAsdns_init_rr_SOA (char *mname, char *rname, uint32_t expire, uint32_t minimum, uint32_t refresh, uint32_t retry, uint32_t serial)
 
void sdns_free_rr_A (sdns_rr_A *a)
 Free the memory of the structure of type sdns_rr_A.
 
void sdns_free_rr_MX (sdns_rr_MX *mx)
 Free the memory of the structure of type sdns_rr_MX.
 
void sdns_free_rr_TXT (sdns_rr_TXT *txt)
 Free the memory of the structure of type sdns_rr_TXT.
 
void sdns_free_rr_AAAA (sdns_rr_AAAA *aaaa)
 Free the memory of the structure of type sdns_rr_AAAA.
 
void sdns_free_rr_SOA (sdns_rr_SOA *soa)
 Free the memory of the structure of type sdns_rr_SOA.
 
void sdns_free_rr_NS (sdns_rr_NS *ns)
 Free the memory of the structure of type sdns_rr_NS.
 
void sdns_free_rr_PTR (sdns_rr_PTR *ptr)
 Free the memory of the structure of type sdns_rr_PTR.
 
void sdns_free_rr_CNAME (sdns_rr_CNAME *cname)
 Free the memory of the structure of type sdns_rr_CNAME.
 
void sdns_free_rr_RRSIG (sdns_rr_RRSIG *rrsig)
 Free the memory of the structure of type sdns_rr_RRSIG.
 
void sdns_free_rr_SRV (sdns_rr_SRV *srv)
 Free the memory of the structure of type sdns_rr_SRV.
 
void sdns_free_rr_HINFO (sdns_rr_HINFO *hinfo)
 Free the memory of the structure of type sdns_rr_HINFO.
 
void sdns_free_rr_URI (sdns_rr_URI *uri)
 Free the memory of the structure of type sdns_rr_URI.
 
void sdns_free_rr_NID (sdns_rr_NID *nid)
 Free the memory of the structure of type sdns_rr_NID.
 
void sdns_free_rr_L32 (sdns_rr_L32 *l32)
 Free the memory of the structure of type sdns_rr_L32.
 
void sdns_free_rr_L64 (sdns_rr_L64 *l64)
 Free the memory of the structure of type sdns_rr_L64.
 
void sdns_free_rr_LP (sdns_rr_LP *lp)
 Free the memory of the structure of type sdns_rr_LP.
 
void sdns_free_rr_CAA (sdns_rr_CAA *caa)
 Free the memory of the structure of type sdns_rr_CAA.
 
sdns_opt_rdatasdns_create_edns0_cookie (char *client_cookie, char *server_cookie, uint8_t server_cookie_len)
 Creates and adds a cookie in the DNS packet.
 
void sdns_rr_type_to_string (uint16_t t, char *buff)
 Gets the string representation of the TYPE tp in to buffer
 
void sdns_class_to_string (uint16_t cls, char *buff)
 Gets the string representation of the CLASS cls in to buffer
 
int sdns_convert_type_to_int (char *type)
 Converts text type to its numerical value.
 
int sdns_convert_class_to_int (char *cls)
 Converts text class to its numerical value.
 
void sdns_free_section (sdns_rr *rr)
 free a DNS resource record no matter of the type
 

Macro Definition Documentation

◆ SDNS_ERROR_ILLEGAL_COMPRESSION

#define SDNS_ERROR_ILLEGAL_COMPRESSION   -14

Compressed label found when it's illegal.

In some cases like RRSIG, the name can not be compressed (e.g., signer's name in RRSIG). So if we found a compressed name in those sections, we return this error

Typedef Documentation

◆ sdns_opt_rdata

see _sdns_opt_rdata for more info

See _sdns_opt_rdata structure for detail.

◆ sdns_rr

typedef struct _sdns_rr sdns_rr

See _sdns_rr for more info.

See _sdns_rr for more information

◆ sdns_rr_TXT

typedef struct _sdns_rr_TXT sdns_rr_TXT

check the definition of _sdns_rr_TXT.

Enumeration Type Documentation

◆ sdns_ede_code

List of known value for Extended DNS Errors (EDE). The full list is available in RFC 8914

Enumerator
sdns_ede_code_Other_Error 

Other.

sdns_ede_code_Unsupported_DNSKEY_Algorithm 

Unsupported DNSKEY Algorithm.

sdns_ede_code_Unsupported_DS_Digest_Type 

Unsupported DS Digest Type.

sdns_ede_code_Stale_Answer 

Stale Answer.

sdns_ede_code_Forged_Answer 

For policy reasons (legal obligation or malware filtering, for instance), an answer was forged.

sdns_ede_code_DNSSEC_Indeterminate 

DNSSEC Indeterminate.

sdns_ede_code_DNSSEC_Bogus 

The resolver attempted to perform DNSSEC validation, but validation ended in the Bogus state.

sdns_ede_code_Signature_Expired 

Signature Expired.

sdns_ede_code_Signature_Not_Yet_Valid 

Signature Not Yet Valid.

sdns_ede_code_DNSKEY_Missing 

A DS record existed at a parent, but no supported matching DNSKEY record could be found for the child.

sdns_ede_code_RRSIGs_Missing 

RRSIGs Missing.

sdns_ede_code_No_Zone_Key_Bit_Set 

The resolver attempted to perform DNSSEC validation, but no Zone Key Bit was set in a DNSKEY.

sdns_ede_code_NSEC_Missing 

NSEC Missing.

sdns_ede_code_Cached_Error 

Cached Error.

sdns_ede_code_Not_Ready 

The server is unable to answer the query, as it was not fully functional when the query was received.

sdns_ede_code_Blocked 

Blocked.

sdns_ede_code_Censored 

Censored.

sdns_ede_code_Filtered 

Filtered.

sdns_ede_code_Prohibited 

Prohibited.

sdns_ede_code_Stale_NXDomain_Answer 

Stale NXDOMAIN Answer.

sdns_ede_code_Not_Authoritative 

Not Authoritative.

sdns_ede_code_Not_Supported 

Not Supported.

sdns_ede_code_No_Reachable_Authority 

No Reachable Authority.

sdns_ede_code_Network_Error 

Network Error.

sdns_ede_code_Invalid_Data 

Invalid Data.

sdns_ede_code_Signature_Expired_before_Valid 

Signature Expired before Valid.

sdns_ede_code_Too_Early 

Too Early

sdns_ede_code_Unsupported_NSEC3_Iterations_Value 

Unsupported NSEC3 Iterations Value.

sdns_ede_code_Unable_to_conform_to_policy 

Unable to conform to policy.

sdns_ede_code_Synthesized 

Synthesized.

◆ sdns_edns0_option_code

Possible valid values for the option code of EDNS0 https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-11

Enumerator
sdns_edns0_option_code_Reserved0 

Reserved RFC6891.

sdns_edns0_option_code_LLQ 

LLQ RFC8764.

sdns_edns0_option_code_Update_Lease 

Update Lease.

sdns_edns0_option_code_NSID 

NSID RFC5001.

sdns_edns0_option_code_Reserved4 

Reserved.

sdns_edns0_option_code_DAU 

DAU RFC6975.

sdns_edns0_option_code_DHU 

DHU RFC6975.

sdns_edns0_option_code_N3U 

N3U RFC6975.

sdns_edns0_option_code_edns_client_subnet 

edns-client-subnet RFC7871

sdns_edns0_option_code_EDNS_EXPIRE 

EDNS EXPIRE RFC7314.

sdns_edns0_option_code_COOKIE 

COOKIE RFC7873.

sdns_edns0_option_code_edns_tcp_keepalive 

edns-tcp-keepalive RFC7828

sdns_edns0_option_code_Padding 

Padding RFC7830.

sdns_edns0_option_code_CHAIN 

CHAIN RFC7901.

sdns_edns0_option_code_edns_key_tag 

edns-key-tag RFC8145

sdns_edns0_option_code_Extended_DNS_Error 

Extended DNS Error RFC8914.

sdns_edns0_option_code_EDNS_Client_Tag 

EDNS-Client-Tag.

sdns_edns0_option_code_EDNS_Server_Tag 

EDNS-Server-Tag.

sdns_edns0_option_code_Report_Channel 

Report-Channel RFC9567.

sdns_edns0_option_code_Umbrella_Ident 

Umbrella Ident.

sdns_edns0_option_code_DeviceID 

DeviceID.

◆ sdns_opcode

The list of opcodes can be found at: http://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-5

Enumerator
sdns_opcode_Query 

RFC1035.

sdns_opcode_IQuery 

RFC1035.

sdns_opcode_Status 

RFC1035.

sdns_opcode_Notify 

RFC1996.

sdns_opcode_Update 

RFC2136.

sdns_opcode_DSO 

RFC8490.

◆ sdns_q_class

Possible values for the CLASS field of a question section

Enumerator
sdns_q_class_IN 

RFC1035 - the Internet.

sdns_q_class_CS 

RFC1035 - the CSNET.

sdns_q_class_CH 

RFC1035 - the CHAOS class.

sdns_q_class_HS 

RFC1035 - Hesiod.

sdns_q_class_STAR 

RFC1035 - any class.

◆ sdns_rcode

enum sdns_rcode

The list of standard error codes for DNS can be found at: http://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6

Enumerator
sdns_rcode_NoError 

RFC1035.

sdns_rcode_FormErr 

RFC1035.

sdns_rcode_ServFail 

RFC1035.

sdns_rcode_NXDomain 

RFC1035.

sdns_rcode_NotImp 

RFC1035.

sdns_rcode_Refused 

RFC1035.

sdns_rcode_YXDomain 

RFC2136.

sdns_rcode_YXRRSet 

RFC2136.

sdns_rcode_NXRRSet 

RFC2136.

sdns_rcode_NotAuth 

RFC2136, RFC8945.

sdns_rcode_NotZone 

RFC2136.

sdns_rcode_DSOTYPENI 

RFC8490.

sdns_rcode_BADVERS 

Bad OPT Version RFC6891.

sdns_rcode_BADSIG 

TSIG Signature Failure RFC8945.

sdns_rcode_BADKEY 

Key not recognized RFC8945.

sdns_rcode_BADTIME 

Signature out of time window RFC8945.

sdns_rcode_BADMODE 

Bad TKEY Mode RFC2930.

sdns_rcode_BADNAME 

Duplicate key name RFC2930.

sdns_rcode_BADALG 

Algorithm not supported RFC2930.

sdns_rcode_BADTRUNC 

Bad Truncation RFC8945.

sdns_rcode_BADCOOKIE 

Bad/missing Server Cookie RFC7873.

sdns_rcode_Reserved 

Reserved, can be allocated by Standards Action RFC6895.

◆ sdns_rr_class

Possible values for the CLASS field of a Resource Record

Enumerator
sdns_rr_class_IN 

RFC1035 - the Internet.

sdns_rr_class_CS 

RFC1035 - the CSNET.

sdns_rr_class_CH 

RFC1035 - the CHAOS class.

sdns_rr_class_HS 

RFC1035 - Hesiod.

◆ sdns_rr_type

Here is the list of possible RR types reitrieved from: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4

Enumerator
sdns_rr_type_A 

RFC1035 - A record.

sdns_rr_type_NS 

RFC1035 - NS record.

sdns_rr_type_MD 

RFC1035 - mail destination record.

sdns_rr_type_MF 

RFC1035 - mail forwarder record.

sdns_rr_type_CNAME 

RFC1035 - canonical name.

sdns_rr_type_SOA 

RFC1035 - start of a zone of authority.

sdns_rr_type_MB 

RFC1035 - mailbox domain name.

sdns_rr_type_MG 

RFC1035 - mail group member.

sdns_rr_type_MR 

RFC1035 - mail rename domain name.

sdns_rr_type_NULL 

RFC1035 - a null RR.

sdns_rr_type_WKS 

RFC1035 - a well known service description.

sdns_rr_type_PTR 

RFC1035 - a domain name pointer.

sdns_rr_type_HINFO 

RFC1035 - host information.

sdns_rr_type_MINFO 

RFC1035 - mailbox or mail list information.

sdns_rr_type_MX 

RFC1035 - mail exchange.

sdns_rr_type_TXT 

RFC1035 - text strings.

sdns_rr_type_RP 

for Responsible Person RFC1183

sdns_rr_type_AFSDB 

for AFS Data Base location RFC1183, RFC5864

sdns_rr_type_X25 

for X.25 PSDN address RFC1183

sdns_rr_type_ISDN 

for ISDN address RFC1183

sdns_rr_type_RT 

for Route Through RFC1183

sdns_rr_type_NSAP 

for NSAP address, NSAP style A record (deprecated)

sdns_rr_type_NSAP_PTR 

for domain name pointer, NSAP style

sdns_rr_type_SIG 

for security signature RFC2536, RFC2931, RFC3110, RFC4034

sdns_rr_type_KEY 

for security key RFC2536, RFC2539, RFC3110, RFC4034

sdns_rr_type_PX 

X.400 mail mapping information FC2163.

sdns_rr_type_GPOS 

Geographical Position RFC1712.

sdns_rr_type_AAAA 

RFC3596 - AAAA record.

sdns_rr_type_LOC 

Location Information RFC1876.

sdns_rr_type_NXT 

Next Domain (OBSOLETE) RFC2535, RFC3755.

sdns_rr_type_EID 

Endpoint Identifier.

sdns_rr_type_NIMLOC 

Nimrod Locator.

sdns_rr_type_SRV 

Server Selection.

sdns_rr_type_ATMA 

ATM Address.

sdns_rr_type_NAPTR 

Naming Authority Pointer.

sdns_rr_type_KX 

Key Exchanger.

sdns_rr_type_CERT 

CERT.

sdns_rr_type_A6 

A6 (OBSOLETE - use AAAA)

sdns_rr_type_DNAME 

DNAME.

sdns_rr_type_SINK 

SINK.

sdns_rr_type_OPT 

RFC6891 - option RR for ends.

sdns_rr_type_APL 

APL.

sdns_rr_type_DS 

Delegation Signer RFC4034.

sdns_rr_type_SSHFP 

SSH Key Fingerprint RFC4255.

sdns_rr_type_IPSECKEY 

IPSECKEY RFC4025.

sdns_rr_type_RRSIG 

RRSIG RFC4034.

sdns_rr_type_NSEC 

NSEC RFC4034, RFC9077.

sdns_rr_type_DNSKEY 

DNSKEY RFC4034.

sdns_rr_type_DHCID 

DHCID RFC4701.

sdns_rr_type_NSEC3 

NSEC3 RFC5155, RFC9077.

sdns_rr_type_NSEC3PARAM 

NSEC3PARAM [RFC5155.

sdns_rr_type_TLSA 

TLSA [RFC6698.

sdns_rr_type_SMIMEA 

S/MIME cert association RFC8162.

sdns_rr_type_HIP 

Host Identity Protocol RFC8005.

sdns_rr_type_NINFO 

NINFO.

sdns_rr_type_RKEY 

RKEY.

sdns_rr_type_TALINK 

Trust Anchor LINK.

sdns_rr_type_CDS 

Child DS.

sdns_rr_type_CDNSKEY 

DNSKEY(s) the Child wants reflected in DS RFC7344.

sdns_rr_type_OPENPGPKEY 

OpenPGP Key RFC7929.

sdns_rr_type_CSYNC 

Child-To-Parent Synchronization RFC7477.

sdns_rr_type_ZONEMD 

Message Digest Over Zone Data RFC8976.

sdns_rr_type_SVCB 

General-purpose service binding RFC9460.

sdns_rr_type_HTTPS 

SVCB-compatible type for use with HTTP RFC9460.

sdns_rr_type_SPF 

RFC7208.

sdns_rr_type_UINFO 

IANA-Reserved.

sdns_rr_type_UID 

IANA-Reserved.

sdns_rr_type_GID 

IANA-Reserved.

sdns_rr_type_UNSPEC 

IANA-Reserved.

sdns_rr_type_NID 

RFC6742.

sdns_rr_type_L32 

RFC6742.

sdns_rr_type_L64 

RFC6742.

sdns_rr_type_LP 

RFC6742.

sdns_rr_type_EUI48 

an EUI-48 address RFC7043

sdns_rr_type_EUI64 

an EUI-64 address RFC7043

sdns_rr_type_TKEY 

Transaction Key RFC2930.

sdns_rr_type_TSIG 

Transaction Signature RFC8945.

sdns_rr_type_IXFR 

incremental transfer RFC1995

sdns_rr_type_AXFR 

RFC1035 - request for transfer of entire zone.

sdns_rr_type_MAILB 

RFC1035 - request for mailbox related records.

sdns_rr_type_MAILA 

RFC1035 - request for mail agent RRs.

sdns_rr_type_star 

RFC1035 - request for all records.

sdns_rr_type_URI 

URI RFC7553.

sdns_rr_type_CAA 

Certification Authority Restriction [RFC8659].

sdns_rr_type_AVC 

Application Visibility and Control.

sdns_rr_type_DOA 

Digital Object Architecture.

sdns_rr_type_AMTRELAY 

Automatic Multicast Tunneling Relay [RFC8777].

sdns_rr_type_RESINFO 

Resolver Information as Key/Value Pairs.

sdns_rr_type_WALLET 

Public wallet address.

sdns_rr_type_TA 

DNSSEC Trust Authorities.

sdns_rr_type_DLV 

DNSSEC Lookaside Validation (OBSOLETE)

Function Documentation

◆ check_if_qclass_is_valid()

int check_if_qclass_is_valid ( uint16_t  qclass)

This function checks if the given Question class is valid or not.

Parameters
qclassinput Resource record class
Returns
0 if the given value is valid else 1

◆ check_if_qtype_is_valid()

int check_if_qtype_is_valid ( uint16_t  qtype)

This function checks if the given qtype code is valid or not.

Parameters
qtype16-bit unsigned integer
Returns
1 if given qtype is valid on 0 otherwise

◆ check_if_rrclass_is_valid()

int check_if_rrclass_is_valid ( uint16_t  qclass)

This function checks if the given RR class is valid or not.

Parameters
qclassinput Resource record class
Returns
0 if the given value is valid else 1

◆ check_if_rrtype_is_valid()

int check_if_rrtype_is_valid ( uint16_t  qtype)

This function checks if the given RR type is valid or not.

Parameters
qtypeinput Resource record type
Returns
0 if the given value is valid else 1

◆ sdns_add_additional_section()

int sdns_add_additional_section ( sdns_context ctx,
sdns_rr rr 
)

Adds a new RR to the additional section of the DNS packet.

Parameters
ctxA pointer to DNS context created by sdns_init_context()
rrA pointer to the RR we want to add.
Returns
0 on success and other values on failure (call sdns_error_string() to get the error string)

This function is exactly the same as sdns_add_answer_section() but adds the new record to additional section of the DNS packet instead of the answer section.

For more info, check the provided example in sdns_add_answer_section() function.

NOTE: We don't use this function to add EDNS0 options. Instead, we use sdns_add_edns() function.

◆ sdns_add_answer_section()

int sdns_add_answer_section ( sdns_context ctx,
sdns_rr rr 
)

Creates an answer section for the DNS packet.

Parameters
ctxA pointer to the DNS context created by sdns_init_context().
rrA pointer to the sdns_rr structure we want to add.
Returns
0 on success and other values on failure (call sdns_error_string() to get the error string)
// let's create a new context
if (NULL == dns_ctx){
fprintf(stderr, "Can not initialize the DNS context...\n");
return 1;
}
// let's make a query (google.com. IN TXT with EDNS0 enabled)
int res = sdns_make_query(dns_ctx, sdns_rr_type_TXT, sdns_q_class_IN, strdup("google.com"), 1);
if (res != 0){
// handle errors and return
}
// let's add an answer section for TXT record
// first we have to create a TXT structure using sdns_init_rr_TXT() function.
sdns_rr_TXT * txt = sdns_init_rr_TXT(strdup("This_is_a_txt_record"), 16);
// then we create a new section by calling sdns_init_rr() function.
sdns_rr * rr = sdns_init_rr(strdup("google.com"), sdns_rr_type_TXT, sdns_rr_class_IN, 86400, 0, 1, (void*)txt);
if (rr == NULL){
// handle errors and exit
}
// finally, we add the newly created section to our context.
res = sdns_add_answer_section(dns_ctx, rr);
if (res != 0){
// handle errors and exit
}
// let's print the final packet to see if works.
@ sdns_q_class_IN
RFC1035 - the Internet.
Definition sdns.h:395
void sdns_free_context(sdns_context *ctx)
Frees the context allocated by sdns_init_context().
sdns_rr_TXT * sdns_init_rr_TXT(char *data, uint16_t data_len)
Initialize a structure of sdns_rr_TXT.
int sdns_add_answer_section(sdns_context *ctx, sdns_rr *rr)
Creates an answer section for the DNS packet.
int sdns_make_query(sdns_context *ctx, sdns_rr_type qtype, sdns_q_class cls, char *qname, int enable_edns0)
Creates a DNS question query.
@ sdns_rr_class_IN
RFC1035 - the Internet.
Definition sdns.h:384
sdns_context * sdns_init_context(void)
Initialize and create a new DNS context.
@ sdns_rr_type_TXT
RFC1035 - text strings.
Definition sdns.h:273
sdns_rr * sdns_init_rr(char *name, uint16_t type, uint16_t class, uint32_t ttl, uint16_t rdlength, uint8_t decoded, void *rdata)
Initialize an RR data structure.
void sdns_neat_print_dns(sdns_context *ctx)
Prints a DNS packet in a human-readable format (like Dig)
Definition sdns.h:516
Definition sdns.h:229
Definition sdns.h:688

◆ sdns_add_authority_section()

int sdns_add_authority_section ( sdns_context ctx,
sdns_rr rr 
)

Adds a new RR to the authority section of the DNS packet.

Parameters
ctxA pointer to DNS context created by sdns_init_context()
rrA pointer to the RR we want to add.
Returns
0 on success and other values on failure (call sdns_error_string() to get the error string)

This function is exactly the same as sdns_add_answer_section() but adds the new record to authority section of the DNS packet instead of the answer section.

For more info, check the provided example in sdns_add_answer_section() function.

◆ sdns_class_to_string()

void sdns_class_to_string ( uint16_t  cls,
char *  buff 
)

Gets the string representation of the CLASS cls in to buffer

Parameters
clsA 16bit integer, one of the possible values of sdns_rr_class or sdns_q_class
buffA pointer to the user-provided buffer

The buff must be long enough to cover all classes.

Currently, a buffer of length 20 is enough.

◆ sdns_convert_class_to_int()

int sdns_convert_class_to_int ( char *  cls)

Converts text class to its numerical value.

Parameters
clsa pointer to the class string (e.g., "IN" or "ch")
Returns
A positive integer on success showing the DNS class or negative value in case of unknown class

◆ sdns_convert_type_to_int()

int sdns_convert_type_to_int ( char *  type)

Converts text type to its numerical value.

Parameters
typea pointer to the type string (e.g., "AAAA")
Returns
A positive integer on success showing the type or negative value in case of unknown type

◆ sdns_create_edns0_cookie()

sdns_opt_rdata * sdns_create_edns0_cookie ( char *  client_cookie,
char *  server_cookie,
uint8_t  server_cookie_len 
)

Creates and adds a cookie in the DNS packet.

Parameters
client_cookieThis parameter can not be NULL. It must be provided and the length is exactly 8 bytes.
server_cookiethis is an optional parameter which can be NULL or a pointer to a memory which stores the cookie
server_cookie_lenThe length of the server cookie which can be between 8 and 32 (both valued included).

the caller can free() the memory of 'client_cookie' and 'server_cookie' params after calling the method. The library creates an internal copy of the data.

Returns
a pointer to an instance of type sdns_opt_rdata on success, NULL on failure.

On success, you can add the returned structure to the DNS packet by calling sdns_create_edns_option() function.

◆ sdns_create_edns0_ede()

sdns_opt_rdata * sdns_create_edns0_ede ( uint16_t  info_code,
char *  extra_text,
uint16_t  extra_text_len 
)

Creates EDNS0 extended DNS error option.

Parameters
info_codeOne of the possible values of sdns_ede_code
extra_textA text description of the error or whatever you want to send
extra_text_lenthe length of the extra_text filed (as it's not necessarily nul-terminated)

This function creates a new copy of 'extra_text' field. So the caller is responsible to free the memory of 'extra_text' if it's a heap-allocated memory.

Returns
returns a pointer to a sdns_opt_rdata structure on success, NULL on fail.

◆ sdns_ends0_option_code_to_text()

int sdns_ends0_option_code_to_text ( sdns_edns0_option_code  oc,
char *  buffer 
)

Copies the text description of the EDNS0 option code to the buffer.

Parameters
octhe numerical value of the edns0 option code, one of the possible values of sdns_edns0_option_code
bufferThe user provided buffer to receive the string.

The user provided buffer must be large enough to cover the longest string. You can pass a buffer of length 100 to make sure it goes well.

Returns
0 on success, other values on failure.

if the return value is 0, it means the given 'oc' value is a valid EDNS option code. If the return value is 1, it means the given 'oc' value is not a valid EDNS option code.

This means you can also use this function as a validator to check of 'oc' is valid or not.

◆ sdns_error_string()

void sdns_error_string ( int  err,
char **  err_buff 
)

Coverts error codes to a string.

Parameters
errthe error code you received from one of the functions or any DNS defined error.
err_buffA double pointer to the buffer to receive the string. if it's NULL, we will allocate memory.

The err_buff parameter must be large enough to cover all possible error codes. Currently, no error string is larger than 256. Hence, 256 bytes of buffer is large enough and safe.

check the source code example in sdns_make_query() for more information.

◆ sdns_free_context()

void sdns_free_context ( sdns_context ctx)

Frees the context allocated by sdns_init_context().

Parameters
ctxA pointer to the context.

To prevent memory leak, you must free the context all the time.

This function will call all other sdns_free_*() functions recursively to free the whole memory.

◆ sdns_free_rr_A()

void sdns_free_rr_A ( sdns_rr_A a)

Free the memory of the structure of type sdns_rr_A.

Parameters
aA pointer to the structure

◆ sdns_free_rr_AAAA()

void sdns_free_rr_AAAA ( sdns_rr_AAAA aaaa)

Free the memory of the structure of type sdns_rr_AAAA.

Parameters
aaaaA pointer to the structure

◆ sdns_free_rr_CAA()

void sdns_free_rr_CAA ( sdns_rr_CAA caa)

Free the memory of the structure of type sdns_rr_CAA.

Parameters
caaA pointer to the structure

◆ sdns_free_rr_CNAME()

void sdns_free_rr_CNAME ( sdns_rr_CNAME cname)

Free the memory of the structure of type sdns_rr_CNAME.

Parameters
cnameA pointer to the structure

◆ sdns_free_rr_HINFO()

void sdns_free_rr_HINFO ( sdns_rr_HINFO hinfo)

Free the memory of the structure of type sdns_rr_HINFO.

Parameters
hinfoA pointer to the structure

◆ sdns_free_rr_L32()

void sdns_free_rr_L32 ( sdns_rr_L32 l32)

Free the memory of the structure of type sdns_rr_L32.

Parameters
l32A pointer to the structure

◆ sdns_free_rr_L64()

void sdns_free_rr_L64 ( sdns_rr_L64 l64)

Free the memory of the structure of type sdns_rr_L64.

Parameters
l64A pointer to the structure

◆ sdns_free_rr_LP()

void sdns_free_rr_LP ( sdns_rr_LP lp)

Free the memory of the structure of type sdns_rr_LP.

Parameters
lpA pointer to the structure

◆ sdns_free_rr_MX()

void sdns_free_rr_MX ( sdns_rr_MX mx)

Free the memory of the structure of type sdns_rr_MX.

Parameters
mxA pointer to the structure

◆ sdns_free_rr_NID()

void sdns_free_rr_NID ( sdns_rr_NID nid)

Free the memory of the structure of type sdns_rr_NID.

Parameters
nidA pointer to the structure

◆ sdns_free_rr_NS()

void sdns_free_rr_NS ( sdns_rr_NS ns)

Free the memory of the structure of type sdns_rr_NS.

Parameters
nsA pointer to the structure

◆ sdns_free_rr_PTR()

void sdns_free_rr_PTR ( sdns_rr_PTR ptr)

Free the memory of the structure of type sdns_rr_PTR.

Parameters
ptrA pointer to the structure

◆ sdns_free_rr_RRSIG()

void sdns_free_rr_RRSIG ( sdns_rr_RRSIG rrsig)

Free the memory of the structure of type sdns_rr_RRSIG.

Parameters
rrsigA pointer to the structure

◆ sdns_free_rr_SOA()

void sdns_free_rr_SOA ( sdns_rr_SOA soa)

Free the memory of the structure of type sdns_rr_SOA.

Parameters
soaA pointer to the structure

◆ sdns_free_rr_SRV()

void sdns_free_rr_SRV ( sdns_rr_SRV srv)

Free the memory of the structure of type sdns_rr_SRV.

Parameters
srvA pointer to the structure

◆ sdns_free_rr_TXT()

void sdns_free_rr_TXT ( sdns_rr_TXT txt)

Free the memory of the structure of type sdns_rr_TXT.

Parameters
txtA pointer to the structure

◆ sdns_free_rr_URI()

void sdns_free_rr_URI ( sdns_rr_URI uri)

Free the memory of the structure of type sdns_rr_URI.

Parameters
uriA pointer to the structure

◆ sdns_free_section()

void sdns_free_section ( sdns_rr rr)

free a DNS resource record no matter of the type

Parameters
rra pointer to the memory of sdns_rr which holds the resource record section to be freed
Returns
nothing

◆ sdns_from_wire()

int sdns_from_wire ( sdns_context ctx)

Converts the raw data received from socket (bytes) to a DNS packet.

Parameters
ctxA pointer to DNS context created by sdns_init_context().

A description of all error codes returned by this method can be retrieved by calling sdns_error_string() function.

Returns
0 on success, other values in case of failure.
char * buff = (char*)malloc(1024);
ssize_t received = recvfrom(socketfd, buff, 1024, ......)
// assume that we received "received" bytes from socket stored in "buff"
// here is how we can convert it to DNS packet.
// I am not doing error checks but you must always make sure ctx is not NULL
// After you are done with error checks....
ctx->raw = buff;
ctx->raw_len = received;
int res = sdns_from_wire(ctx);
if (res != 0){
// we need to handle the error, we can call sdns_error_string
}
// we have a parsed DNS packet in ctx->msg
int sdns_from_wire(sdns_context *ctx)
Converts the raw data received from socket (bytes) to a DNS packet.

◆ sdns_init_context()

sdns_context * sdns_init_context ( void  )

Initialize and create a new DNS context.

This function is responsible for creating a new context for DNS.

For each DNS packet, you must create a new context. This function does nothing more than creating a structure and allocating the necessary memories.

◆ sdns_init_message()

sdns_message * sdns_init_message ( void  )

Initialize a DNS message structure (sdns_message).

Users don't need to this method directly. We use this method internally when we call sdns_init_context().

◆ sdns_init_rr()

sdns_rr * sdns_init_rr ( char *  name,
uint16_t  type,
uint16_t  class,
uint32_t  ttl,
uint16_t  rdlength,
uint8_t  decoded,
void *  rdata 
)

Initialize an RR data structure.

Parameters
namea pointer to the name of the resource record
typethe type of the resource record
classthe class of the resource record
ttlthe ttl of the resource record
rdlengththe length of the rdata section of this resource record
decodedif resource record is decoded (1) or not (0).
rdataa pointer to the raw data (if decode = 0) or a structure (decoded =1)

One can use this method to add a resource record to a DNS packet that is not supported by this library. For all RRs that this library support, we have a sdns_init_rr_*() function. If you have a new type to add, you can use this method to create it.

Returns
an instance of sdns_rr structure on success. NULL on fail.

◆ sdns_init_rr_A()

sdns_rr_A * sdns_init_rr_A ( uint32_t  ipaddress)

Initialize a structure of sdns_rr_A.

Parameters
ipaddressan unsigned 32bit integer representing the IPv4

The address is an unsigned integer value for example 2130706433 means 127.0.0.1

Returns
a pointer to sdns_rr_A structure on success and NULL on fail

◆ sdns_init_rr_AAAA()

sdns_rr_AAAA * sdns_init_rr_AAAA ( char *  aaaa)

Initialize a structure of sdns_rr_AAAA.

Parameters
aaaapointer to the memory address that keeps the 16 bytes of IPv6

NOTE: do not free the 'aaaa' pointer after calling this method. This function does not copy the memory but use the pointer.

Returns
a pointer to sdns_rr_AAAA structure on success and NULL on fail

◆ sdns_init_rr_CAA()

sdns_rr_CAA * sdns_init_rr_CAA ( uint8_t  flag,
char *  tag,
uint8_t  tag_len,
char *  value,
uint16_t  value_len 
)

Initialize a structure of sdns_rr_CAA.

Parameters
flagone byte flag specified in Section 3 of RFC 6844
taga pointer to tag value specified in RFC 6844
tag_lenthe length of the tag filed. max value can be 255.
valuea pointer to 'value' value specified in RFC 6844 @pram NOTE: Do not free the 'tag' and 'value' after calling this method. This function does not copy the memory but use a pointer to the memory you allocated. Make sure it's a heap-allocated memory to avoid memory leak.
Returns
a pointer to sdns_rr_CAA structure on success and NULL on fail

◆ sdns_init_rr_CNAME()

sdns_rr_CNAME * sdns_init_rr_CNAME ( char *  cname)

Initialize a structure of sdns_rr_CNAME.

Parameters
cnamepointer to the memory address that keeps the cname data

NOTE: do not free the 'cname' pointer after calling this method. This function does not copy the memory but use the pointer. Make sure it's a heap-allocated memory to avoid memory leak.

Returns
a pointer to sdns_rr_CNAME structure on success and NULL on fail

◆ sdns_init_rr_L32()

sdns_rr_L32 * sdns_init_rr_L32 ( uint16_t  preference,
uint32_t  locator32 
)

Initialize a structure of sdns_rr_L32.

Parameters
preference16bit unsigned integer showing the preference value of the L32 record
locator3232bit unsigned integer for Locator32
Returns
a pointer to sdns_rr_L32 structure on success and NULL on fail

◆ sdns_init_rr_L64()

sdns_rr_L64 * sdns_init_rr_L64 ( uint16_t  preference,
char *  locator64 
)

Initialize a structure of sdns_rr_L64.

Parameters
preference16bit unsigned integer showing the preference value of the L64 record
locator64pointer to the memory address that keeps the Locator64 data

NOTE: do not free the 'locator64' pointer after calling this method. This function does not copy the memory but use the pointer. Make sure it's a heap-allocated memory to avoid memory leak.

NOTE: locator64 is exactly 8 bytes based on RFC 6742

Returns
a pointer to sdns_rr_L64 structure on success and NULL on fail

◆ sdns_init_rr_LP()

sdns_rr_LP * sdns_init_rr_LP ( uint16_t  preference,
char *  fqdn 
)

Initialize a structure of sdns_rr_LP.

Parameters
preference16bit unsigned integer showing the preference value of the LP record
fqdnpointer to the memory address that keeps the fully-qualified-domain-name data

NOTE: do not free the 'fqdn' pointer after calling this method. This function does not copy the memory but use the pointer. Make sure it's a heap-allocated memory to avoid memory leak.

Returns
a pointer to sdns_rr_LP structure on success and NULL on fail

◆ sdns_init_rr_MX()

sdns_rr_MX * sdns_init_rr_MX ( uint16_t  preference,
char *  exchange 
)

Initialize a structure of sdns_rr_MX.

Parameters
preferencea 16-bit integer for the prereference of the MX record
exchangepointer to the memory address that keeps the 'exchange' data

NOTE: do not free the 'exchange' pointer after calling this method. This function does not copy the memory but use the pointer. Make sure it's a heap-allocated memory to avoid memory leak.

Returns
a pointer to sdns_rr_MX structure on success and NULL on fail

◆ sdns_init_rr_NID()

sdns_rr_NID * sdns_init_rr_NID ( uint16_t  preference,
char *  nodid 
)

Initialize a structure of sdns_rr_NID.

Parameters
preference16bit unsigned integer showing the preference value of the NID record
nodidpointer to the memory address that keeps the NodeId data

NOTE: do not free the 'nodeid' pointer after calling this method. This function does not copy the memory but use the pointer. Make sure it's a heap-allocated memory to avoid memory leak.

NOTE: nodeid is exactly 8 bytes based on RFC 6742

Returns
a pointer to sdns_rr_NID structure on success and NULL on fail

◆ sdns_init_rr_NS()

sdns_rr_NS * sdns_init_rr_NS ( char *  nsdname)

Initialize a structure of sdns_rr_NS.

Parameters
nsdnamepointer to the memory address that keeps the nsdname data (e.g., ns1.google.com)

NOTE: do not free the 'nsdname' pointer after calling this method. This function does not copy the memory but use the pointer. Make sure it's a heap-allocated memory to avoid memory leak.

Returns
a pointer to sdns_rr_NS structure on success and NULL on fail

◆ sdns_init_rr_PTR()

sdns_rr_PTR * sdns_init_rr_PTR ( char *  ptrdname)

Initialize a structure of sdns_rr_PTR.

Parameters
ptrdnamepointer to the memory address that keeps the ptrdname data

NOTE: do not free the 'ptrdname' pointer after calling this method. This function does not copy the memory but use the pointer. Make sure it's a heap-allocated memory to avoid memory leak.

Returns
a pointer to sdns_rr_PTR structure on success and NULL on fail

◆ sdns_init_rr_SRV()

sdns_rr_SRV * sdns_init_rr_SRV ( uint16_t  Priority,
uint16_t  Weight,
uint16_t  Port,
char *  target 
)

Initialize a structure of sdns_rr_SRV.

Parameters
Priority16-bit unsigned value for the priority of the SRV record
Weight16-bit unsigned value for the weight of the SRV record
Port16-bit unsigned value for the port of the SRV record
targetA pointer to the target value of the SRV record

NOTE: do not free the 'target' pointer after calling this method. This function does not copy the memory but use the pointer. Make sure it's a heap-allocated memory to avoid memory leak.

Returns
a pointer to sdns_rr_SRV structure on success and NULL on fail

◆ sdns_init_rr_TXT()

sdns_rr_TXT * sdns_init_rr_TXT ( char *  data,
uint16_t  data_len 
)

Initialize a structure of sdns_rr_TXT.

Parameters
datapointer to the memory address that keeps the data
data_lenlength of the data pointed by 'data' pointer

NOTE: the 'data' pointer is not necessarily a nul-terminated string. That's why we need to know the length of the data.

NOTE: do not free the 'data' pointer after calling this method. This function does not copy the memory but use the pointer. Make sure it's a heap-allocated memory to avoid memory leak.

Returns
a pointer to sdns_rr_TXT structure on success and NULL on fail

◆ sdns_make_query()

int sdns_make_query ( sdns_context ctx,
sdns_rr_type  qtype,
sdns_q_class  cls,
char *  qname,
int  enable_edns0 
)

Creates a DNS question query.

Parameters
ctxA pointer to the sdns_context structure created by sdns_init_context()
qtypeone of the possible values of sdns_rr_type
clsone of the possible values of sdns_q_class
qnamea pointer to the question name.
enable_edns0a value of 0 or 1 specifying if the packet is EDNS0 aware or not.

qname pointer must be a pointer to char* and not a local buffer. when you call sdns_free_context(), the function try to free() the qname.

In case the function returns a value other than 0, you can call sdns_error_string() to get the description of the error code.

Returns
A successful call to this function will return 0 and other values indicate errors.
char *error = NULL;
if (NULL == ctx)
return 1;
// Let's make the most trivial and basic DNS packet. (google.com IN A)
// notice how I use strdup to pass a heap-allocated pointer to the function.
int res = sdns_make_query(ctx, sdns_rr_type_A, sdns_q_class_IN, strdup("google.com"), 1);
if (res != 0){
sdns_error_string(res, &error);
fprintf(stderr, "%s\n", error);
free(error);
return 1;
}
// the packet has been created successfully. We can convert it to wire format and send it.
res = sdns_to_wire(ctx);
if (res != sdns_rcode_NoError){
sdns_error_string(res, &error);
fprintf(stderr, "%s\n", error);
free(error);
return 1;
}
// ctx->raw is ready for the socket and ctx->raw_len is its length.
// ctx->raw and ctx->raw_len can be used in sendto() function.
// after we are done, we need to free the context.
int sdns_to_wire(sdns_context *ctx)
Coverts a DNS context to binary format.
@ sdns_rcode_NoError
RFC1035.
Definition sdns.h:82
@ sdns_rr_type_A
RFC1035 - A record.
Definition sdns.h:258
void sdns_error_string(int err, char **err_buff)
Coverts error codes to a string.

◆ sdns_rr_type_to_string()

void sdns_rr_type_to_string ( uint16_t  t,
char *  buff 
)

Gets the string representation of the TYPE tp in to buffer

Parameters
tA 16bit integer, one of the possible values of sdns_rr_type
buffA pointer to the user-provided buffer

The buffer must be long enough to cover all types.

Currently, a buffer of length 20 is enough.

◆ sdns_to_wire()

int sdns_to_wire ( sdns_context ctx)

Coverts a DNS context to binary format.

Parameters
ctxA pointer to DNS context created by sdns_init_context().
Returns
0 on success other values for failure.

This is one of the main APIs provided by this library. Whenever you want to send data to the socket, you must call this function first to create the binary data.

Look at the provided example for sdns_make_query() for more info.