aboutsummaryrefslogtreecommitdiffstats
path: root/src/CMakeLists.txt
blob: 27b9277b475e5cdd20c86c963022b3a0991658d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
MACRO(_AddModulesForced MLIST)
# Generate unique string for this build
	SET(MODULES_C "${CMAKE_CURRENT_BINARY_DIR}/modules.c")
	FILE(WRITE "${MODULES_C}"
		"#include \"rspamd.h\"\n")

	# Handle even old cmake
	LIST(LENGTH ${MLIST} MLIST_COUNT)
	MATH(EXPR MLIST_MAX ${MLIST_COUNT}-1)

	FOREACH(MOD_IDX RANGE ${MLIST_MAX})
		LIST(GET ${MLIST} ${MOD_IDX} MOD)
		FILE(APPEND "${MODULES_C}" "extern module_t ${MOD}_module;\n")
	ENDFOREACH(MOD_IDX RANGE ${MLIST_MAX})

	FILE(APPEND "${MODULES_C}" "\n\nmodule_t *modules[] = {\n")

	FOREACH(MOD_IDX RANGE ${MLIST_MAX})
		LIST(GET ${MLIST} ${MOD_IDX} MOD)
		FILE(APPEND "${MODULES_C}" "&${MOD}_module,\n")
	ENDFOREACH(MOD_IDX RANGE ${MLIST_MAX})

	FILE(APPEND "${MODULES_C}" "NULL\n};\n")
ENDMACRO(_AddModulesForced MLIST)

MACRO(_AddWorkersForced WLIST)
	SET(WORKERS_C "${CMAKE_CURRENT_BINARY_DIR}/workers.c")
	FILE(WRITE "${WORKERS_C}"
		"#include \"rspamd.h\"\n")

	# Handle even old cmake
	LIST(LENGTH ${WLIST} WLIST_COUNT)
	MATH(EXPR WLIST_MAX ${WLIST_COUNT}-1)
	FOREACH(MOD_IDX RANGE ${WLIST_MAX})
		LIST(GET ${WLIST} ${MOD_IDX} WRK)
		FILE(APPEND "${WORKERS_C}" "extern worker_t ${WRK}_worker;\n")
	ENDFOREACH(MOD_IDX RANGE ${WLIST_MAX})

	FILE(APPEND "${WORKERS_C}" "\n\nworker_t *workers[] = {\n")

	FOREACH(MOD_IDX RANGE ${WLIST_MAX})
		LIST(GET ${WLIST} ${MOD_IDX} WRK)
		FILE(APPEND "${WORKERS_C}" "&${WRK}_worker,\n")
	ENDFOREACH(MOD_IDX RANGE ${WLIST_MAX})
	FILE(APPEND "${WORKERS_C}" "NULL\n};\n")
ENDMACRO(_AddWorkersForced WLIST)

MACRO(AddModules MLIST WLIST)
	_AddModulesForced(${MLIST})
	_AddWorkersForced(${WLIST})
	#IF(NOT EXISTS "modules.c")
	#	_AddModulesForced(${MLIST} ${WLIST})
	#ELSE(NOT EXISTS "modules.c")
	#	FILE(STRINGS "modules.c" FILE_ID_RAW REGEX "^/.*[a-zA-Z0-9]+.*/$")
	#	STRING(REGEX MATCH "[a-zA-Z0-9]+" FILE_ID "${FILE_ID_RAW}")
	#	IF(NOT FILE_ID STREQUAL MODULES_ID)
	#		MESSAGE("Regenerate modules info")
	#		_AddModulesForced(${MLIST} ${WLIST})
	#	ENDIF(NOT FILE_ID STREQUAL MODULES_ID)
	#ENDIF(NOT EXISTS "modules.c")
ENDMACRO(AddModules MLIST WLIST)

# Rspamd core components
IF (ENABLE_CLANG_PLUGIN MATCHES "ON")
	SET(CMAKE_C_FLAGS
			"${CMAKE_C_FLAGS} -Xclang -load -Xclang ${CMAKE_CURRENT_BINARY_DIR}/../clang-plugin/librspamd-clang.so -Xclang -add-plugin -Xclang rspamd-ast")
	IF(CLANG_EXTRA_PLUGINS_LIBS)
		FOREACH(_lib ${CLANG_EXTRA_PLUGINS_LIBS})
			SET(CMAKE_C_FLAGS
					"${CMAKE_C_FLAGS} -Xclang -load -Xclang ${_lib}")
		ENDFOREACH()
	ENDIF()
	IF(CLANG_EXTRA_PLUGINS)
		FOREACH(_plug ${CLANG_EXTRA_PLUGINS})
			SET(CMAKE_C_FLAGS
					"${CMAKE_C_FLAGS} -Xclang -add-plugin -Xclang ${_plug}")
		ENDFOREACH()
	ENDIF()
ENDIF ()

ADD_SUBDIRECTORY(lua)
ADD_SUBDIRECTORY(libcryptobox)
ADD_SUBDIRECTORY(libutil)
ADD_SUBDIRECTORY(libserver)
ADD_SUBDIRECTORY(libmime)
ADD_SUBDIRECTORY(libstat)
ADD_SUBDIRECTORY(client)
ADD_SUBDIRECTORY(rspamadm)

SET(RSPAMDSRC	controller.c
				fuzzy_storage.c
				lua_worker.c
				rspamd.c
				worker.c
				rspamd_proxy.c
				log_helper.c)

SET(PLUGINSSRC	plugins/surbl.c
				plugins/regexp.c
				plugins/chartable.c
				plugins/fuzzy_check.c
				plugins/spf.c
				plugins/dkim_check.c
				libserver/rspamd_control.c
				lua/lua_fann.c)

SET(MODULES_LIST surbl regexp chartable fuzzy_check spf dkim)
SET(WORKERS_LIST normal controller fuzzy lua rspamd_proxy log_helper)
IF (ENABLE_HYPERSCAN MATCHES "ON")
	LIST(APPEND WORKERS_LIST "hs_helper")
	LIST(APPEND RSPAMDSRC "hs_helper.c")
ENDIF()

AddModules(MODULES_LIST WORKERS_LIST)
LIST(LENGTH PLUGINSSRC RSPAMD_MODULES_NUM)

SET(RAGEL_DEPENDS "${CMAKE_SOURCE_DIR}/src/ragel/smtp_address.rl"
	"${CMAKE_SOURCE_DIR}/src/ragel/smtp_date.rl"
	"${CMAKE_SOURCE_DIR}/src/ragel/smtp_ip.rl"
	"${CMAKE_SOURCE_DIR}/src/ragel/smtp_base.rl"
	"${CMAKE_SOURCE_DIR}/src/ragel/content_disposition.rl")
RAGEL_TARGET(ragel_smtp_addr
	INPUTS ${CMAKE_SOURCE_DIR}/src/ragel/smtp_addr_parser.rl
	DEPENDS ${RAGEL_DEPENDS}
	COMPILE_FLAGS -T1
	OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/smtp_addr_parser.rl.c)
RAGEL_TARGET(ragel_content_disposition
	INPUTS ${CMAKE_SOURCE_DIR}/src/ragel/content_disposition_parser.rl
	DEPENDS ${RAGEL_DEPENDS}
	COMPILE_FLAGS -G2
	OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/content_disposition.rl.c)
RAGEL_TARGET(ragel_rfc2047
	INPUTS ${CMAKE_SOURCE_DIR}/src/ragel/rfc2047_parser.rl
	DEPENDS ${RAGEL_DEPENDS}
	COMPILE_FLAGS -G2
	OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/rfc2047.rl.c)
RAGEL_TARGET(ragel_smtp_date
	INPUTS ${CMAKE_SOURCE_DIR}/src/ragel/smtp_date_parser.rl
	DEPENDS ${RAGEL_DEPENDS}
	COMPILE_FLAGS -G2
	OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/date_parser.rl.c)
RAGEL_TARGET(ragel_smtp_ip
	INPUTS ${CMAKE_SOURCE_DIR}/src/ragel/smtp_ip_parser.rl
	DEPENDS ${RAGEL_DEPENDS}
	COMPILE_FLAGS -G2
	OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ip_parser.rl.c)
######################### LINK SECTION ###############################

IF(ENABLE_STATIC MATCHES "ON")
	ADD_LIBRARY(rspamd-server STATIC
			${RSPAMD_CRYPTOBOX}
			${RSPAMD_UTIL}
			${RSPAMD_LUA}
			${RSPAMD_SERVER}
			${RSPAMD_STAT}
			${RSPAMD_MIME}
			${CMAKE_CURRENT_BINARY_DIR}/modules.c
			${PLUGINSSRC}
			"${RAGEL_ragel_smtp_addr_OUTPUTS}"
			"${RAGEL_ragel_newlines_strip_OUTPUTS}"
			"${RAGEL_ragel_content_type_OUTPUTS}"
			"${RAGEL_ragel_content_disposition_OUTPUTS}"
			"${RAGEL_ragel_rfc2047_OUTPUTS}"
			"${RAGEL_ragel_smtp_date_OUTPUTS}"
			"${RAGEL_ragel_smtp_ip_OUTPUTS}")
ELSE()
	ADD_LIBRARY(rspamd-server SHARED
			${RSPAMD_CRYPTOBOX}
			${RSPAMD_UTIL}
			${RSPAMD_LUA}
			${RSPAMD_SERVER}
			${RSPAMD_STAT}
			${RSPAMD_MIME}
			${CMAKE_CURRENT_BINARY_DIR}/modules.c
			${PLUGINSSRC}
			"${RAGEL_ragel_smtp_addr_OUTPUTS}"
			"${RAGEL_ragel_newlines_strip_OUTPUTS}"
			"${RAGEL_ragel_content_type_OUTPUTS}"
			"${RAGEL_ragel_content_disposition_OUTPUTS}"
			"${RAGEL_ragel_rfc2047_OUTPUTS}"
			"${RAGEL_ragel_smtp_date_OUTPUTS}"
			"${RAGEL_ragel_smtp_ip_OUTPUTS}")
ENDIF()

TARGET_LINK_LIBRARIES(rspamd-server rspamd-http-parser)
TARGET_LINK_LIBRARIES(rspamd-server rspamd-cdb)
TARGET_LINK_LIBRARIES(rspamd-server rspamd-lpeg)
TARGET_LINK_LIBRARIES(rspamd-server lcbtrie)
TARGET_LINK_LIBRARIES(rspamd-server rspamd-zstd)

IF (ENABLE_CLANG_PLUGIN MATCHES "ON")
	ADD_DEPENDENCIES(rspamd-server rspamd-clang)
ENDIF()

IF (ENABLE_SNOWBALL MATCHES "ON")
	TARGET_LINK_LIBRARIES(rspamd-server stemmer)
ENDIF()
TARGET_LINK_LIBRARIES(rspamd-server rspamd-hiredis)

IF (ENABLE_FANN MATCHES "ON")
	TARGET_LINK_LIBRARIES(rspamd-server fann)
ENDIF ()

IF (ENABLE_HYPERSCAN MATCHES "ON")
	TARGET_LINK_LIBRARIES(rspamd-server hs)
ENDIF()

TARGET_LINK_LIBRARIES(rspamd-server rspamd-linenoise)

IF(USE_CXX_LINKER)
	SET_TARGET_PROPERTIES(rspamd PROPERTIES LINKER_LANGUAGE CXX)
	SET_TARGET_PROPERTIES(rspamd-server PROPERTIES LINKER_LANGUAGE CXX)
ENDIF()


TARGET_LINK_LIBRARIES(rspamd-server ${RSPAMD_REQUIRED_LIBRARIES})

ADD_EXECUTABLE(rspamd ${RSPAMDSRC} ${CMAKE_CURRENT_BINARY_DIR}/workers.c)
SET_TARGET_PROPERTIES(rspamd PROPERTIES LINKER_LANGUAGE C)
SET_TARGET_PROPERTIES(rspamd PROPERTIES COMPILE_FLAGS "-DRSPAMD_MAIN")
IF(NOT DEBIAN_BUILD)
	SET_TARGET_PROPERTIES(rspamd PROPERTIES VERSION ${RSPAMD_VERSION})
ENDIF(NOT DEBIAN_BUILD)

TARGET_LINK_LIBRARIES(rspamd rspamd-server)

INSTALL(TARGETS rspamd RUNTIME DESTINATION bin)
INSTALL(TARGETS rspamd-server LIBRARY DESTINATION ${RSPAMD_LIBDIR})
0 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
/* Copyright (c) 2014, Vsevolod Stakhov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *       * Redistributions of source code must retain the above copyright
 *         notice, this list of conditions and the following disclaimer.
 *       * Redistributions in binary form must reproduce the above copyright
 *         notice, this list of conditions and the following disclaimer in the
 *         documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include <ctype.h>

#include "ottery.h"
#include "util.h"
#include "logger.h"
#include "rdns.h"

inline void
rdns_request_remove_from_hash (struct rdns_request *req)
{
	/* Remove from id hashes */
	if (req->io) {
		khiter_t k;

		k = kh_get(rdns_requests_hash, req->io->requests, req->id);

		if (k != kh_end(req->io->requests)) {
			kh_del(rdns_requests_hash, req->io->requests, k);
		}
	}
}

static int
rdns_make_socket_nonblocking (int fd)
{
	int                            ofl;

	ofl = fcntl (fd, F_GETFL, 0);

	if (fcntl (fd, F_SETFL, ofl | O_NONBLOCK) == -1) {
		return -1;
	}
	return 0;
}

static int
rdns_make_inet_socket (int type, struct addrinfo *addr, struct sockaddr **psockaddr,
		socklen_t *psocklen)
{
	int fd = -1;
	struct addrinfo *cur;

	cur = addr;
	while (cur) {
		/* Create socket */
		fd = socket (cur->ai_family, type, 0);
		if (fd == -1) {
			goto out;
		}

		if (rdns_make_socket_nonblocking (fd) < 0) {
			goto out;
		}

		/* Set close on exec */
		if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) {
			goto out;
		}

		if (psockaddr) {
			*psockaddr = cur->ai_addr;
			*psocklen = cur->ai_addrlen;
		}
		break;
out:
		if (fd != -1) {
			close (fd);
		}
		fd = -1;
		cur = cur->ai_next;
	}

	return (fd);
}

static int
rdns_make_unix_socket (const char *path, struct sockaddr_un *addr, int type)
{
	int fd = -1, serrno;

	if (path == NULL) {
		return -1;
	}

	addr->sun_family = AF_UNIX;

	memset (addr->sun_path, 0, sizeof (addr->sun_path));
	memccpy (addr->sun_path, path, 0, sizeof (addr->sun_path) - 1);
#ifdef FREEBSD
	addr->sun_len = SUN_LEN (addr);
#endif

	fd = socket (PF_LOCAL, type, 0);

	if (fd == -1) {
		return -1;
	}

	if (rdns_make_socket_nonblocking (fd) < 0) {
		goto out;
	}

	/* Set close on exec */
	if (fcntl (fd, F_SETFD, FD_CLOEXEC) == -1) {
		goto out;
	}

	return (fd);

  out:
	serrno = errno;
	if (fd != -1) {
		close (fd);
	}
	errno = serrno;
	return (-1);
}

/**
 * Make a universal socket
 * @param credits host, ip or path to unix socket
 * @param port port (used for network sockets)
 * @param async make this socket asynced
 * @param is_server make this socket as server socket
 * @param try_resolve try name resolution for a socket (BLOCKING)
 */
int
rdns_make_client_socket (const char *credits,
						 uint16_t port,
						 int type,
						 struct sockaddr **psockaddr,
						 socklen_t *psocklen)
{
	struct sockaddr_un              un;
	struct stat                     st;
	struct addrinfo                 hints, *res;
	int                             r;
	char                            portbuf[8];

	if (*credits == '/') {
		r = stat (credits, &st);
		if (r == -1) {
			/* Unix socket doesn't exists it must be created first */
			errno = ENOENT;
			return -1;
		}
		else {
			if ((st.st_mode & S_IFSOCK) == 0) {
				/* Path is not valid socket */
				errno = EINVAL;
				return -1;
			}
			else {
				r = rdns_make_unix_socket (credits, &un, type);

				if (r != -1 && psockaddr) {
					struct sockaddr *cpy;

					cpy = calloc (1, sizeof (un));
					*psocklen = sizeof (un);

					if (cpy == NULL) {
						close (r);

						return -1;
					}

					memcpy (cpy, &un, *psocklen);
					*psockaddr = cpy;
				}

				return r;
			}
		}
	}
	else {
		/* TCP related part */
		memset (&hints, 0, sizeof (hints));
		hints.ai_family = AF_UNSPEC;     /* Allow IPv4 or IPv6 */
		hints.ai_socktype = type; /* Type of the socket */
		hints.ai_flags = 0;
		hints.ai_protocol = 0;           /* Any protocol */
		hints.ai_canonname = NULL;
		hints.ai_addr = NULL;
		hints.ai_next = NULL;

		hints.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV;

		snprintf (portbuf, sizeof (portbuf), "%d", (int)port);
		if (getaddrinfo (credits, portbuf, &hints, &res) == 0) {
			r = rdns_make_inet_socket (type, res, psockaddr, psocklen);

			if (r != -1 && psockaddr) {
				struct sockaddr *cpy;

				cpy = calloc (1, *psocklen);

				if (cpy == NULL) {
					close (r);
					freeaddrinfo (res);

					return -1;
				}

				memcpy (cpy, *psockaddr, *psocklen);
				*psockaddr = cpy;
			}

			freeaddrinfo (res);
			return r;
		}
		else {
			return -1;
		}
	}

	/* Not reached */
	return -1;
}

const char *
rdns_strerror (enum dns_rcode rcode)
{
	rcode &= 0xf;
	static char numbuf[16];

	if ('\0' == dns_rcodes[rcode][0]) {
		snprintf (numbuf, sizeof (numbuf), "UNKNOWN: %d", (int)rcode);
		return numbuf;
	}
	return dns_rcodes[rcode];
}

const char *
rdns_strtype (enum rdns_request_type type)
{
	return dns_types[type];
}

enum rdns_request_type
rdns_type_fromstr (const char *str)
{
	if (str) {
		if (strcmp (str, "a") == 0) {
			return RDNS_REQUEST_A;
		}
		else if (strcmp (str, "ns") == 0) {
			return RDNS_REQUEST_NS;
		}
		else if (strcmp (str, "soa") == 0) {
			return RDNS_REQUEST_SOA;
		}
		else if (strcmp (str, "ptr") == 0) {
			return RDNS_REQUEST_PTR;
		}
		else if (strcmp (str, "mx") == 0) {
			return RDNS_REQUEST_MX;
		}
		else if (strcmp (str, "srv") == 0) {
			return RDNS_REQUEST_SRV;
		}
		else if (strcmp (str, "txt") == 0) {
			return RDNS_REQUEST_TXT;
		}
		else if (strcmp (str, "spf") == 0) {
			return RDNS_REQUEST_SPF;
		}
		else if (strcmp (str, "aaaa") == 0) {
			return RDNS_REQUEST_AAAA;
		}
		else if (strcmp (str, "tlsa") == 0) {
			return RDNS_REQUEST_TLSA;
		}
		else if (strcmp (str, "any") == 0) {
			return RDNS_REQUEST_ANY;
		}
	}

	return RDNS_REQUEST_INVALID;
}

const char *
rdns_str_from_type (enum rdns_request_type rcode)
{
	switch (rcode) {
		case RDNS_REQUEST_INVALID:
			return "(invalid)";
		case RDNS_REQUEST_A:
			return "a";
		case RDNS_REQUEST_NS:
			return "ns";
		case RDNS_REQUEST_SOA:
			return "soa";
		case RDNS_REQUEST_PTR:
			return "ptr";
		case RDNS_REQUEST_MX:
			return "mx";
		case RDNS_REQUEST_TXT:
			return "txt";
		case RDNS_REQUEST_SRV:
			return "srv";
		case RDNS_REQUEST_SPF:
			return "spf";
		case RDNS_REQUEST_AAAA:
			return "aaaa";
		case RDNS_REQUEST_TLSA:
			return "tlsa";
		case RDNS_REQUEST_ANY:
			return "any";
		default:
			return "(unknown)";
	}

}

enum dns_rcode
rdns_rcode_fromstr (const char *str)
{
	if (str) {
		if (strcmp (str, "noerror") == 0) {
			return RDNS_RC_NOERROR;
		}
		else if (strcmp (str, "formerr") == 0) {
			return RDNS_RC_FORMERR;
		}
		else if (strcmp (str, "servfail") == 0) {
			return RDNS_RC_SERVFAIL;
		}
		else if (strcmp (str, "nxdomain") == 0) {
			return RDNS_RC_NXDOMAIN;
		}
		else if (strcmp (str, "notimp") == 0) {
			return RDNS_RC_NOTIMP;
		}
		else if (strcmp (str, "yxdomain") == 0) {
			return RDNS_RC_YXDOMAIN;
		}
		else if (strcmp (str, "yxrrset") == 0) {
			return RDNS_RC_YXRRSET;
		}
		else if (strcmp (str, "nxrrset") == 0) {
			return RDNS_RC_NXRRSET;
		}
		else if (strcmp (str, "notauth") == 0) {
			return RDNS_RC_NOTAUTH;
		}
		else if (strcmp (str, "notzone") == 0) {
			return RDNS_RC_NOTZONE;
		}
		else if (strcmp (str, "timeout") == 0) {
			return RDNS_RC_TIMEOUT;
		}
		else if (strcmp (str, "neterr") == 0) {
			return RDNS_RC_NETERR;
		}
		else if (strcmp (str, "norec") == 0) {
			return RDNS_RC_NOREC;
		}
	}

	return RDNS_RC_INVALID;
}

uint16_t
rdns_permutor_generate_id (void)
{
	uint16_t id;

	id = ottery_rand_unsigned ();

	return id;
}

struct rdns_reply *
rdns_make_reply (struct rdns_request *req, enum dns_rcode rcode)
{
	struct rdns_reply *rep;

	rep = malloc (sizeof (struct rdns_reply));
	if (rep != NULL) {
		rep->request = req;
		rep->resolver = req->resolver;
		rep->entries = NULL;
		rep->code = rcode;
		req->reply = rep;
		rep->flags = 0;
		rep->requested_name = req->requested_names[0].name;
	}

	return rep;
}

void
rdns_reply_free (struct rdns_reply *rep)
{
	struct rdns_reply_entry *entry, *tmp;

	/* We don't need to free data for faked replies */
	if (!rep->request || rep->request->state != RDNS_REQUEST_FAKE) {
		LL_FOREACH_SAFE (rep->entries, entry, tmp) {
			switch (entry->type) {
			case RDNS_REQUEST_PTR:
				free (entry->content.ptr.name);
				break;
			case RDNS_REQUEST_NS:
				free (entry->content.ns.name);
				break;
			case RDNS_REQUEST_MX:
				free (entry->content.mx.name);
				break;
			case RDNS_REQUEST_TXT:
			case RDNS_REQUEST_SPF:
				free (entry->content.txt.data);
				break;
			case RDNS_REQUEST_SRV:
				free (entry->content.srv.target);
				break;
			case RDNS_REQUEST_TLSA:
				free (entry->content.tlsa.data);
				break;
			case RDNS_REQUEST_SOA:
				free (entry->content.soa.mname);
				free (entry->content.soa.admin);
				break;
			default:
				break;
			}
			free (entry);
		}
	}

	free (rep);
}

void
rdns_request_free (struct rdns_request *req)
{
	unsigned int i;

	if (req != NULL) {
		if (req->packet != NULL) {
			free (req->packet);
		}
		for (i = 0; i < req->qcount; i ++) {
			free (req->requested_names[i].name);
		}
		if (req->requested_names != NULL) {
			free (req->requested_names);
		}
		if (req->reply != NULL) {
			rdns_reply_free (req->reply);
		}
		if (req->async_event) {
			if (req->state == RDNS_REQUEST_WAIT_REPLY) {
				/* Remove timer */
				req->async->del_timer (req->async->data,
						req->async_event);
				rdns_request_remove_from_hash(req);
				req->async_event = NULL;
			}
			else if (req->state == RDNS_REQUEST_WAIT_SEND) {
				/* Remove retransmit event */
				req->async->del_write (req->async->data,
						req->async_event);
				rdns_request_remove_from_hash(req);
				req->async_event = NULL;
			}
			else if (req->state == RDNS_REQUEST_FAKE) {
				req->async->del_write (req->async->data,
						req->async_event);
				req->async_event = NULL;
			}
		}
		if (req->state == RDNS_REQUEST_TCP) {
			if (req->async_event) {
				req->async->del_timer (req->async->data,
						req->async_event);
			}

			rdns_request_remove_from_hash(req);
		}
#ifdef TWEETNACL
		if (req->curve_plugin_data != NULL) {
			req->resolver->curve_plugin->cb.curve_plugin.finish_cb (
					req, req->resolver->curve_plugin->data);
		}
#endif
		if (req->io != NULL && req->state > RDNS_REQUEST_NEW) {
			REF_RELEASE (req->io);
			REF_RELEASE (req->resolver);
		}

		free (req);
	}
}

void
rdns_ioc_free (struct rdns_io_channel *ioc)
{
	struct rdns_request *req;

	if (IS_CHANNEL_TCP(ioc)) {
		rdns_ioc_tcp_reset(ioc);
	}

	kh_foreach_value(ioc->requests, req, {
		REF_RELEASE (req);
	});

	if (ioc->async_io) {
		ioc->resolver->async->del_read(ioc->resolver->async->data,
				ioc->async_io);
	}
	kh_destroy(rdns_requests_hash, ioc->requests);

	if (ioc->sock != -1) {
		close(ioc->sock);
	}

	if (ioc->saddr != NULL) {
		free(ioc->saddr);
	}

	free (ioc);
}

struct rdns_io_channel *
rdns_ioc_new (struct rdns_server *serv,
			  struct rdns_resolver *resolver,
			  bool is_tcp)
{
	struct rdns_io_channel *nioc;

	if (is_tcp) {
		nioc = calloc (1, sizeof (struct rdns_io_channel)
				+ sizeof (struct rdns_tcp_channel));
	}
	else {
		nioc = calloc (1, sizeof (struct rdns_io_channel));
	}

	if (nioc == NULL) {
		rdns_err ("calloc fails to allocate rdns_io_channel");
		return NULL;
	}

	nioc->struct_magic = RDNS_IO_CHANNEL_TAG;
	nioc->srv = serv;
	nioc->resolver = resolver;

	nioc->sock = rdns_make_client_socket (serv->name, serv->port,
			is_tcp ? SOCK_STREAM : SOCK_DGRAM, &nioc->saddr, &nioc->slen);
	if (nioc->sock == -1) {
		rdns_err ("cannot open socket to %s: %s", serv->name,
				strerror (errno));
		free (nioc);
		return NULL;
	}

	if (is_tcp) {
		/* We also need to connect a TCP channel and set a TCP buffer */
		nioc->tcp = (struct rdns_tcp_channel *)(((unsigned char *)nioc) + sizeof(*nioc));

		if (!rdns_ioc_tcp_connect(nioc)) {
			rdns_err ("cannot connect TCP socket to %s: %s", serv->name,
					strerror (errno));
			close (nioc->sock);
			free (nioc);

			return NULL;
		}

		nioc->flags |= RDNS_CHANNEL_TCP;
	}
	else {
		nioc->flags |= RDNS_CHANNEL_ACTIVE;
		nioc->async_io = resolver->async->add_read(resolver->async->data,
				nioc->sock, nioc);
	}

	nioc->requests = kh_init(rdns_requests_hash);
	REF_INIT_RETAIN (nioc, rdns_ioc_free);

	return nioc;
}

void
rdns_resolver_release (struct rdns_resolver *resolver)
{
	REF_RELEASE (resolver);
}

struct rdns_request*
rdns_request_retain (struct rdns_request *req)
{
	REF_RETAIN (req);
	return req;
}

void
rdns_request_unschedule (struct rdns_request *req, bool remove_from_hash)
{
	if (req->async_event) {
		if (req->state == RDNS_REQUEST_WAIT_REPLY) {
			req->async->del_timer (req->async->data,
					req->async_event);
			if (remove_from_hash) {
				rdns_request_remove_from_hash(req);
			}
			req->async_event = NULL;
		}
		else if (req->state == RDNS_REQUEST_WAIT_SEND) {
			req->async->del_write (req->async->data,
					req->async_event);
			/* Remove from id hashes */
			if (remove_from_hash) {
				rdns_request_remove_from_hash(req);
			}
			req->async_event = NULL;
		}
	}
	else if (req->state == RDNS_REQUEST_TCP) {
		if (remove_from_hash) {
			rdns_request_remove_from_hash(req);
		}

		req->async->del_timer(req->async->data,
				req->async_event);

		req->async_event = NULL;
	}
}

void
rdns_request_release (struct rdns_request *req)
{
	rdns_request_unschedule (req, true);
	REF_RELEASE (req);
}

void
rdns_ioc_tcp_reset (struct rdns_io_channel *ioc)
{
	struct rdns_resolver *resolver = ioc->resolver;

	if (IS_CHANNEL_CONNECTED(ioc)) {
		if (ioc->tcp->async_write) {
			resolver->async->del_write (resolver->async->data, ioc->tcp->async_write);
			ioc->tcp->async_write = NULL;
		}
		if (ioc->tcp->async_read) {
			resolver->async->del_read (resolver->async->data, ioc->tcp->async_read);
			ioc->tcp->async_read = NULL;
		}

		/* Clean all buffers and temporaries */
		if (ioc->tcp->cur_read_buf) {
			free (ioc->tcp->cur_read_buf);
			ioc->tcp->read_buf_allocated = 0;
			ioc->tcp->next_read_size = 0;
			ioc->tcp->cur_read = 0;
			ioc->tcp->cur_read_buf = NULL;
		}

		struct rdns_tcp_output_chain *oc, *tmp;
		DL_FOREACH_SAFE(ioc->tcp->output_chain, oc, tmp) {
			DL_DELETE (ioc->tcp->output_chain, oc);
			free (oc);
		}

		ioc->tcp->cur_output_chains = 0;
		ioc->tcp->output_chain = NULL;

		ioc->flags &= ~RDNS_CHANNEL_CONNECTED;
	}

	/* Remove all requests pending as we are unable to complete them */
	struct rdns_request *req;
	kh_foreach_value(ioc->requests, req, {
		struct rdns_reply *rep = rdns_make_reply (req, RDNS_RC_NETERR);
		/*
		 * Unschedule request explicitly as we set state to RDNS_REQUEST_REPLIED
		 * that will prevent timer from being removed on req dtor.
		 *
		 * We skip hash removal here, as the hash will be cleared as a single
		 * operation afterwards.
		 */
		rdns_request_unschedule(req, false);
		req->state = RDNS_REQUEST_REPLIED;
		req->func (rep, req->arg);
		REF_RELEASE (req);
	});

	if (ioc->sock != -1) {
		close (ioc->sock);
		ioc->sock = -1;
	}
	if (ioc->saddr) {
		free (ioc->saddr);
		ioc->saddr = NULL;
	}

	kh_clear(rdns_requests_hash, ioc->requests);
}

bool
rdns_ioc_tcp_connect (struct rdns_io_channel *ioc)
{
	struct rdns_resolver *resolver = ioc->resolver;

	if (IS_CHANNEL_CONNECTED(ioc)) {
		rdns_err ("trying to connect already connected IO channel!");
		return false;
	}

	if (ioc->flags & RDNS_CHANNEL_TCP_CONNECTING) {
		/* Already connecting channel, ignore connect request */

		return true;
	}

	if (ioc->sock == -1) {
		ioc->sock = rdns_make_client_socket (ioc->srv->name, ioc->srv->port,
				SOCK_STREAM, &ioc->saddr, &ioc->slen);
		if (ioc->sock == -1) {
			rdns_err ("cannot open socket to %s: %s", ioc->srv->name,
					strerror (errno));

			if (ioc->saddr) {
				free (ioc->saddr);
				ioc->saddr = NULL;
			}

			return false;
		}
	}

	int r = connect (ioc->sock, ioc->saddr, ioc->slen);

	if (r == -1) {
		if (errno != EAGAIN && errno != EINTR && errno != EINPROGRESS) {
			rdns_err ("cannot connect a TCP socket: %s for server %s",
					strerror(errno), ioc->srv->name);
			close (ioc->sock);

			if (ioc->saddr) {
				free (ioc->saddr);
				ioc->saddr = NULL;
			}

			ioc->sock = -1;

			return false;
		}
		else {
			/* We need to wait for write readiness here */
			if (ioc->tcp->async_write != NULL) {
				rdns_err("internal rdns error: write event is already registered on connect");
			}
			else {
				ioc->tcp->async_write = resolver->async->add_write(resolver->async->data,
						ioc->sock, ioc);
			}
			/* Prevent double connect attempts */
			ioc->flags |= RDNS_CHANNEL_TCP_CONNECTING;
		}
	}
	else {
		/* Always be ready to read from a TCP socket */
		ioc->flags |= RDNS_CHANNEL_CONNECTED|RDNS_CHANNEL_ACTIVE;
		ioc->flags &= ~RDNS_CHANNEL_TCP_CONNECTING;
		ioc->tcp->async_read = resolver->async->add_read(resolver->async->data,
				ioc->sock, ioc);
	}

	return true;
}

static bool
rdns_resolver_conf_process_line (struct rdns_resolver *resolver,
		const char *line, rdns_resolv_conf_cb cb, void *ud)
{
	const char *p, *c, *end;
	bool has_obrace = false, ret;
	unsigned int port = dns_port;
	char *cpy_buf;

	end = line + strlen (line);

	if (end - line > sizeof ("nameserver") - 1 &&
			strncmp (line, "nameserver", sizeof ("nameserver") - 1) == 0) {
		p = line + sizeof ("nameserver") - 1;
		/* Skip spaces */
		while (isspace (*p)) {
			p ++;
		}

		if (*p == '[') {
			has_obrace = true;
			p ++;
		}

		if (isxdigit (*p) || *p == ':') {
			c = p;
			while (isxdigit (*p) || *p == ':' || *p == '.') {
				p ++;
			}
			if (has_obrace && *p != ']') {
				return false;
			}
			else if (*p != '\0' && !isspace (*p) && *p != '#') {
				return false;
			}

			if (has_obrace) {
				p ++;
				if (*p == ':') {
					/* Maybe we have a port definition */
					port = strtoul (p + 1, NULL, 10);
					if (port == 0 || port > UINT16_MAX) {
						return false;
					}
				}
			}

			cpy_buf = malloc (p - c + 1);
			assert (cpy_buf != NULL);
			memcpy (cpy_buf, c, p - c);
			cpy_buf[p - c] = '\0';

			if (cb == NULL) {
				ret = rdns_resolver_add_server (resolver, cpy_buf, port, 0,
						default_io_cnt) != NULL;
			}
			else {
				ret = cb (resolver, cpy_buf, port, 0,
						default_io_cnt, ud);
			}

			free (cpy_buf);

			return ret;
		}
		else {
			return false;
		}
	}
	/* XXX: skip unknown resolv.conf lines */

	return false;
}

bool
rdns_resolver_parse_resolv_conf_cb (struct rdns_resolver *resolver,
		const char *path, rdns_resolv_conf_cb cb, void *ud)
{
	FILE *in;
	char buf[BUFSIZ];
	char *p;
	bool processed = false;

	in = fopen (path, "r");

	if (in == NULL) {
		return false;
	}

	while (!feof (in)) {
		if (fgets (buf, sizeof (buf) - 1, in) == NULL) {
			break;
		}

		/* Strip trailing spaces */
		p = buf + strlen (buf) - 1;
		while (p > buf &&
				(*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')) {
			*p-- = '\0';
		}

		if (rdns_resolver_conf_process_line (resolver, buf, cb, ud)) {
			processed = true;
		}
	}

	fclose (in);

	return processed;
}

bool
rdns_resolver_parse_resolv_conf (struct rdns_resolver *resolver, const char *path)
{
	return rdns_resolver_parse_resolv_conf_cb (resolver, path, NULL, NULL);
}

bool
rdns_request_has_type (struct rdns_request *req, enum rdns_request_type type)
{
	unsigned int i;

	for (i = 0; i < req->qcount; i ++) {
		if (req->requested_names[i].type == type) {
			return true;
		}
	}

	return false;
}

const struct rdns_request_name *
rdns_request_get_name (struct rdns_request *req, unsigned int *count)
{

	if (count != NULL) {
		*count = req->qcount;
	}
	return req->requested_names;
}

const char*
rdns_request_get_server (struct rdns_request *req)
{
	if (req && req->io) {
		return req->io->srv->name;
	}

	return NULL;
}

char *
rdns_generate_ptr_from_str (const char *str)
{
	union {
		struct in_addr v4;
		struct in6_addr v6;
	} addr;
	char *res = NULL;
	unsigned char *bytes;
	size_t len;

	if (inet_pton (AF_INET, str, &addr.v4) == 1) {
		bytes = (unsigned char *)&addr.v4;

		len = 4 * 4 + sizeof ("in-addr.arpa");
		res = malloc (len);
		if (res) {
			snprintf (res, len, "%u.%u.%u.%u.in-addr.arpa",
					(unsigned)bytes[3]&0xFF,
					(unsigned)bytes[2]&0xFF,
					(unsigned)bytes[1]&0xFF,
					(unsigned)bytes[0]&0xFF);
		}
	}
	else if (inet_pton (AF_INET6, str, &addr.v6) == 1) {
		bytes = (unsigned char *)&addr.v6;

		len = 2*32 + sizeof ("ip6.arpa");
		res = malloc (len);
		if (res) {
			snprintf(res, len,
					"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
					"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa",
					bytes[15]&0xF, bytes[15] >> 4, bytes[14]&0xF, bytes[14] >> 4,
					bytes[13]&0xF, bytes[13] >> 4, bytes[12]&0xF, bytes[12] >> 4,
					bytes[11]&0xF, bytes[11] >> 4, bytes[10]&0xF, bytes[10] >> 4,
					bytes[9]&0xF, bytes[9] >> 4, bytes[8]&0xF, bytes[8] >> 4,
					bytes[7]&0xF, bytes[7] >> 4, bytes[6]&0xF, bytes[6] >> 4,
					bytes[5]&0xF, bytes[5] >> 4, bytes[4]&0xF, bytes[4] >> 4,
					bytes[3]&0xF, bytes[3] >> 4, bytes[2]&0xF, bytes[2] >> 4,
					bytes[1]&0xF, bytes[1] >> 4, bytes[0]&0xF, bytes[0] >> 4);
		}
	}

	return res;
}