mod_netconf: mod_netconf only receives requests.

Since now, requests are send to the mod_netconf daemon via UNIX socket
using JSON format. There is new testing application (not completed yet)
to provide examples of sending messages to the daemon and receiving the
answers. In future, this will be done by php/python/perl/... scripts
providing web-based NETCONF GUI.
diff --git a/src/Makefile b/src/Makefile
index a817e39..af7dbd8 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,17 +1,21 @@
 NAME=mod_netconf
-SOURCES=$(NAME).c
 
 APXS=/usr/sbin/apxs2
 
-CFLAGS=-I/usr/include/libxml2 
-LIBS=-lnetconf -lxml2 -lz -lm 
+CFLAGS=-I/usr/include/libxml2  -I/usr/include/json
+LIBS=-lnetconf -lxml2 -lz -lm -ljson 
 
-all: $(SOURCES)
-	$(APXS) $(CPPFLAGS) $(CFLAGS) $(LIBS) -c $(SOURCES)
+all: module client
+
+module: mod_netconf.c
+	$(APXS) $(CPPFLAGS) $(CFLAGS) $(LIBS) -c mod_netconf.c
+
+client: test-client.c
+	gcc -std=gnu99 -Wall -g -ljson -I/usr/include/json test-client.c -o test-client
 
 install: $(NAME).la
 	$(APXS) -i $(NAME).la
 
 clean:
-	rm -rf .libs *.la *.lo *.slo
+	rm -rf .libs *.la *.lo *.slo test-client
 
diff --git a/src/README b/src/README
index c62e3ba..2b538c2 100644
--- a/src/README
+++ b/src/README
@@ -1,6 +1,6 @@
 How To Setup Apache Server
 --------------------------
-- mod_netconf compilation requires apache2-devel package.
+- mod_netconf compilation requires apache2-devel and libjson-devel packages.
 
 - run following commands in this directory
 $ make
@@ -8,10 +8,9 @@
 
 - Add following lines into the (appropriate parts of the ) Apache configuration:
 LoadModule netconf_module                 /usr/lib64/apache2/mod_netconf.so
-<Location /netconf>
-        SetHandler netconf
-</Location>
 
-- copy index.php and login.php into Apache DocumentRoot.
-- create empty directory 'netconf' in Apache DocumentRoot
-- open index.php in the browser and follow instructions
+- Start Apache
+
+- For testing, you can run test-client
+  - run under the same user as Apache, or change access rights of the
+    communication socket (/tmp/pconn.sock) to allow writing.
diff --git a/src/index.php b/src/index.php
deleted file mode 100644
index 44c0089..0000000
--- a/src/index.php
+++ /dev/null
@@ -1,40 +0,0 @@
-<?php 
-

-if (isset($_POST['action']) && $_POST['action'] == "disconnect") {
-	

-	apache_setenv("NETCONF_OP", "disconnect");

-	apache_setenv("NETCONF_NSID", $_POST["nsid"]);

-	virtual("/netconf/");

-	

-	echo "OK";
-}
-
-if (isset($_POST['action']) && $_POST['action'] == "login") {
-	apache_setenv("NETCONF_OP", "connect");
-	apache_setenv("NETCONF_HOST", "localhost");
-	apache_setenv("NETCONF_PORT", "830");
-	apache_setenv("NETCONF_USER", $_POST["login"]);
-	apache_setenv("NETCONF_PASS", $_POST["password"]);

-	virtual("/netconf/");
-	
-	echo "<html><body>";

-	$out = apache_getenv("NETCONF_NSID");

-	echo "NSID: ".$out;
-	

-	echo "<form method=\"post\" action=\"./index.php\" name=\"disconnect\">";

-	echo "<input type=\"hidden\" name=\"action\" value=\"disconnect\">";
-	echo "<input type=\"hidden\" name=\"nsid\" value=\"$out\">";

-	echo "<input type=\"submit\" value=\"Disconnect\">";
-	echo "</form>";
-	echo "</body></html>";
-	
-	return ;

-}
-
-if (!isset($_POST['nsid'])) {
-	/* no session related information known - redirect to the login page */
-	header('Location: ./login.php');
-	die();
-}
-
-?>
diff --git a/src/login.php b/src/login.php
deleted file mode 100644
index a633146..0000000
--- a/src/login.php
+++ /dev/null
@@ -1,26 +0,0 @@
-<html>
-<body>
-
-<center>
-  <table cellpadding='2' cellspacing='0' border='1' id='login_table'>
-  <tr><td align=center style="padding:2;padding-bottom:4">
-    <b>Enter your login and password</b>
-  </td></tr>
-  <tr><td style="padding:5"><br>
-	<form method="post" action="./index.php" name="login">
-	  <input type="hidden" name="action" value="login">
-      <input type="hidden" name="hide" value="">
-	  <center>
-	    <table>
-		<tr><td>Login:</td><td><input type="text" name="login"></td></tr>
-		<tr><td>Password:</td><td><input type="password" name="password"></td></tr>
-		<tr><td>&nbsp;</td><td><input type="submit" value="Enter"></td></tr>
-		</table>
-      </center>
-	</form>
-  </td></tr>
-  </table>
-</center>
-
-</body>
-</html>
diff --git a/src/mod_netconf.c b/src/mod_netconf.c
index d56a9f3..23c3251 100644
--- a/src/mod_netconf.c
+++ b/src/mod_netconf.c
@@ -73,6 +73,8 @@
 #include <string.h>
 #include <errno.h>
 
+#include <json/json.h>
+
 #include <libnetconf.h>
 #include <libxml/tree.h>
 #include <libxml/parser.h>
@@ -91,6 +93,22 @@
 
 struct timeval timeout = { 1, 0 };
 
+typedef enum MSG_TYPE {
+	REPLY_OK,
+	REPLY_DATA,
+	REPLY_ERROR,
+	MSG_CONNECT,
+	MSG_DISCONNECT,
+	MSG_GET,
+	MSG_GETCONFIG,
+	MSG_EDITCONFIG,
+	MSG_COPYCONFIG,
+	MSG_DELETECONFIG,
+	MSG_LOCK,
+	MSG_UNLOCK,
+	MSG_KILL
+} MSG_TYPE;
+
 #define MSG_OK 0
 #define MSG_OPEN  1
 #define MSG_DATA  2
@@ -101,8 +119,9 @@
 module AP_MODULE_DECLARE_DATA netconf_module;
 
 typedef struct {
-	apr_proc_t *forkproc;
 	apr_pool_t *pool;
+	apr_proc_t *forkproc;
+	char* sockname;
 } mod_netconf_cfg;
 
 volatile int isterminated = 0;
@@ -115,20 +134,15 @@
 	switch (sign) {
 	case SIGTERM:
 		isterminated = 1;
-		fprintf(stderr, "got TERM signal... %s\n",
-			apr_signal_description_get(sign));
-		break;
-	default:
-		printf("%s\n", apr_signal_description_get(sign));
 		break;
 	}
 }
 
-server_rec *gserver;
-static int gen_ncsession_hash(char** hash, const char* hostname, const char* port, const char* sid)
+static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid)
 {
 	unsigned char hash_raw[APR_SHA1_DIGESTSIZE];
 	int i;
+	char* hash;
 
 	apr_sha1_ctx_t sha1_ctx;
 	apr_sha1_init(&sha1_ctx);
@@ -138,13 +152,13 @@
 	apr_sha1_final(hash_raw, &sha1_ctx);
 
 	/* convert binary hash into hex string, which is printable */
-	*hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
+	hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1));
 	for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) {
-		snprintf(*hash + (2*i), 3, "%02x", hash_raw[i]);
+		snprintf(hash + (2*i), 3, "%02x", hash_raw[i]);
 	}
 	//hash[2*APR_SHA1_DIGESTSIZE] = 0;
 
-	return (APR_SUCCESS);
+	return (hash);
 }
 
 int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint)
@@ -183,63 +197,35 @@
 	return;
 }
 
-static void handle_msg_open(server_rec* server, apr_pool_t* pool, int client, apr_hash_t* conns, char** address)
+static char* netconf_connect(server_rec* server, apr_pool_t* pool, apr_hash_t* conns, const char* host, const char* port, const char* user, const char* pass)
 {
 	struct nc_session* session;
-	char *hostname, *port, *username, *sid;
+	char *sid;
 	char *session_key;
-	char *reply;
-
-	hostname = address[0];
-	port = address[1];
-	username = address[2];
 
 	/* connect to the requested NETCONF server */
-	password = address[3];
-	session = nc_session_connect(hostname, (unsigned short) atoi (port), username, NULL);
-	password = NULL;
-
-	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", hostname, port, username);
-
-	/* clear plaintext password */
-	size_t size = strlen(address[3]);
-	memset(address[3], 0, size + 1);
+	password = (char*)pass;
+	session = nc_session_connect(host, (unsigned short) atoi (port), user, NULL);
 
 	/* if connected successful, add session to the list */
 	if (session != NULL) {
 		/* generate hash for the session */
 		sid = nc_session_get_id(session);
-		gserver = server;
-		gen_ncsession_hash(&session_key, hostname, port, sid);
+		session_key = gen_ncsession_hash(host, port, sid);
 		free(sid);
-		ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
-
-		ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to add: %s", session_key);
 		apr_hash_set(conns, apr_pstrdup(pool, session_key), APR_HASH_KEY_STRING, (void *) session);
-		reply = malloc(strlen(session_key) + 2);
-		reply[0] = MSG_OK;
-		strcpy(&(reply[1]), session_key);
-		ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "reply: %x%s", reply[0], &(reply[1]));
-		send(client, reply, strlen(session_key) + 2, 0);
 		ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established");
-		free(session_key);
-		free(reply);
-		return;
+		return (session_key);
 	} else {
 		ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established");
+		return (NULL);
 	}
 
-	reply = malloc(2);
-	reply[0] = MSG_ERROR;
-	reply[1] = 0;
-	send(client, reply, 2, 0);
-	free(reply);
 }
 
-static void handle_msg_close(server_rec* server, int client, apr_hash_t* conns, char* session_key)
+static int netconf_close(server_rec* server, apr_hash_t* conns, char* session_key)
 {
 	struct nc_session *ns = NULL;
-	char* reply;
 
 	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Key in hash to get: %s", session_key);
 	ns = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
@@ -252,109 +238,126 @@
 		apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL);
 		ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed");
 
-		reply = malloc(2);
-		reply[0] = MSG_OK;
-		reply[1] = 0;
-		send(client, reply, 2, 0);
-		free(reply);
+		return (EXIT_SUCCESS);
 	} else {
 		ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
-
-		reply = malloc(2);
-		reply[0] = MSG_ERROR;
-		reply[1] = 0;
-		send(client, reply, 2, 0);
-		free(reply);
+		return (EXIT_FAILURE);
 	}
 }
 
-static void handle_netconf_request(server_rec* server, int client, apr_hash_t* conns, char* session_key)
+static char* netconf_getconfig(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE source, const char* filter)
 {
 	struct nc_session *session = NULL;
-	NC_DATASTORE target = NC_DATASTORE_RUNNING;
 	nc_rpc* rpc;
 	nc_reply* reply;
-	char* data, *client_reply_data;
+	char* data;
+	struct nc_filter *f = NULL;
 
 	session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
 	if (session != NULL) {
+		/* create filter if set */
+		if (filter != NULL) {
+			f = nc_filter_new(NC_FILTER_SUBTREE, filter);
+		}
+
 		/* create requests */
-		rpc = nc_rpc_getconfig (target, NULL);
+		rpc = nc_rpc_getconfig (source, f);
+		nc_filter_free(f);
 		if (rpc == NULL) {
 			ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
-			goto error_reply;
+			return (NULL);
 		}
+
 		/* send the request and get the reply */
 		nc_session_send_rpc (session, rpc);
 		if (nc_session_recv_reply (session, &reply) == 0) {
 			nc_rpc_free (rpc);
 			if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
 				ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
-				handle_msg_close(server, client, conns, session_key);
-				goto error_reply;
+				netconf_close(server, conns, session_key);
+				return (NULL);
 			}
+
 			/* there is error handled by callback */
-			return;
+			return (NULL);
 		}
 		nc_rpc_free (rpc);
 
 		switch (nc_reply_get_type (reply)) {
 		case NC_REPLY_DATA:
-		case NC_REPLY_ERROR:
-			data = nc_reply_dump(reply);
+			if ((data = nc_reply_get_data (reply)) == NULL) {
+				ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
+				return (NULL);
+			}
 			break;
 		default:
 			ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
-			goto error_reply;
+			return (NULL);
 		}
 		nc_reply_free(reply);
 
-		if (!data) {
-			ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
-			goto error_reply;
-		}
-
-		ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF get-config is done");
-		client_reply_data = malloc(sizeof(char) * (1 + strlen(data) + 1));
-		if (client_reply_data == NULL) {
-			ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: allocating memory for client reply failed");
-			goto error_reply;
-		}
-		client_reply_data[0] = MSG_DATA;
-		apr_cpystrn(&client_reply_data[1], data, strlen(data) + 1);
-
-		client_reply_data[0] = MSG_OK;
-		send(client, client_reply_data, strlen(client_reply_data) + 2, 0);
-		ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "sending message from daemon: %s", client_reply_data);
-		free(client_reply_data);
+		return (data);
 	} else {
-		ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close");
-		goto error_reply;
+		ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
+		return (NULL);
 	}
-
-	return;
-
-error_reply:
-	client_reply_data = malloc(2);
-	client_reply_data[0] = MSG_ERROR;
-	client_reply_data[1] = 0;
-	send(client, client_reply_data, 2, 0);
-	free(client_reply_data);
 }
 
-static void get_target_address(char *address[4], char *cred)
+static char* netconf_get(server_rec* server, apr_hash_t* conns, char* session_key, const char* filter)
 {
-	int i;
+	struct nc_session *session = NULL;
+	nc_rpc* rpc;
+	nc_reply* reply;
+	char* data;
+	struct nc_filter *f = NULL;
 
-	/* <MSG_TYPE> <host> \0 <port> \0 <user> \0 <pass> \0 */
+	session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING);
+	if (session != NULL) {
+		/* create filter if set */
+		if (filter != NULL) {
+			f = nc_filter_new(NC_FILTER_SUBTREE, filter);
+		}
 
-	/* hostname */
-	address[0] = &cred[0];
+		/* create requests */
+		rpc = nc_rpc_get (f);
+		nc_filter_free(f);
+		if (rpc == NULL) {
+			ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed");
+			return (NULL);
+		}
 
-	/* process the rest in loop */
-	for (i = 1; i < 4; i++) {
-		/* port, user, password */
-		address[i] = address[i-1] + strlen(address[i-1]) + 1;
+		/* send the request and get the reply */
+		nc_session_send_rpc (session, rpc);
+		if (nc_session_recv_reply (session, &reply) == 0) {
+			nc_rpc_free (rpc);
+			if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
+				ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed");
+				netconf_close(server, conns, session_key);
+				return (NULL);
+			}
+
+			/* there is error handled by callback */
+			return (NULL);
+		}
+		nc_rpc_free (rpc);
+
+		switch (nc_reply_get_type (reply)) {
+		case NC_REPLY_DATA:
+			if ((data = nc_reply_get_data (reply)) == NULL) {
+				ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply");
+				return (NULL);
+			}
+			break;
+		default:
+			ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply");
+			return (NULL);
+		}
+		nc_reply_free(reply);
+
+		return (data);
+	} else {
+		ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process.");
+		return (NULL);
 	}
 }
 
@@ -380,23 +383,35 @@
 	socklen_t len, len2;
 	struct pollfd fds;
 	int status;
+	mod_netconf_cfg *config;
+	json_object *request, *reply;
+	int operation;
+	char* session_key, *data;
+	const char *msgtext;
+	const char *host, *port, *user, *pass;
+	const char *target, *source, *filter;
+	NC_DATASTORE ds_type1, ds_type2;
 
 	apr_hash_t *netconf_sessions_list;
 	char buffer[BUFFER_SIZE];
-	char *address[4];
 
 	/* change uid and gid of process for security reasons */
 	unixd_setup_child();
 
-	/* create listening UNIX socket to accept incoming connections */
+	config = ap_get_module_config(server->module_config, &netconf_module);
+	if (config == NULL) {
+		ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed");
+		return;
+	}
 
+	/* create listening UNIX socket to accept incoming connections */
 	if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
 		ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno));
 		return;
 	}
 
 	local.sun_family = AF_UNIX;
-	strncpy(local.sun_path, SOCKET_FILENAME, sizeof(local.sun_path));
+	strncpy(local.sun_path, config->sockname, sizeof(local.sun_path));
 	unlink(local.sun_path);
 	len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path);
 
@@ -456,17 +471,19 @@
 			fds.events = POLLIN;
 			fds.revents = 0;
 
-			status = poll(&fds, 1, 100);
+			status = poll(&fds, 1, 1000);
 
-			if (status == -1 && errno == EINTR && isterminated == 0) {
+			if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
 				/* poll was interrupted - check if the isterminated is set and if not, try poll again */
+				//ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted");
 				continue;
-			} else if (status <= 0) {
+			} else if (status < 0) {
 				/* 0:  poll time outed
 				 *     close socket and ignore this request from the client, it can try it again
 				 * -1: poll failed
 				 *     something wrong happend, close this socket and wait for another request
 				 */
+				//ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll failed, status %d(%d: %s)", status, errno, strerror(errno));
 				close(client);
 				break;
 			}
@@ -477,6 +494,7 @@
 			/* if nothing to read and POLLHUP (EOF) or POLLERR set */
 			if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
 				/* close client's socket (it's probably already closed by client */
+				//ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "socket error (%d)", fds.revents);
 				close(client);
 				break;
 			}
@@ -485,27 +503,120 @@
 				ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "receiving failed %d (%s)", errno, strerror(errno));
 				continue;
 			} else {
-				/* got message from client */
-				buffer[len2] = 0;
-				ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "received from client: %s", buffer);
+				request = json_tokener_parse(buffer);
+				operation = json_object_get_int(json_object_object_get(request, "type"));
 
-				/* message type */
-				switch (buffer[0]) {
-				case MSG_OPEN:
-					get_target_address(address, &buffer[1]);
-					handle_msg_open(server, pool, client, netconf_sessions_list, address);
+				switch (operation) {
+				case MSG_CONNECT:
+					ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect");
+
+					host = json_object_get_string(json_object_object_get(request, "host"));
+					port = json_object_get_string(json_object_object_get(request, "port"));
+					user = json_object_get_string(json_object_object_get(request, "user"));
+					pass = json_object_get_string(json_object_object_get(request, "pass"));
+					ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user);
+					session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass);
+					ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key);
+
+					reply =  json_object_new_object();
+					if (session_key == NULL) {
+						/* negative reply */
+						json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
+						json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
+					} else {
+						/* positive reply */
+						json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
+						json_object_object_add(reply, "session", json_object_new_string(session_key));
+					}
+
+					free(session_key);
 					break;
+				case MSG_GET:
+					session_key = (char*)json_object_get_string(json_object_object_get(request, "session"));
+					ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
+					/* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
+
+					filter = json_object_get_string(json_object_object_get(request, "filter"));
+
+					reply =  json_object_new_object();
+
+					if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) {
+						json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
+						json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
+						break;
+					}
+
+					json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
+					json_object_object_add(reply, "data", json_object_new_string(data));
+					break;
+				case MSG_GETCONFIG:
+					session_key = (char*)json_object_get_string(json_object_object_get(request, "session"));
+					ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key);
+					/* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
+
+					source = json_object_get_string(json_object_object_get(request, "source"));
+					filter = json_object_get_string(json_object_object_get(request, "filter"));
+
+					reply =  json_object_new_object();
+
+					if (strcmp(source, "running") == 0) {
+						ds_type1 = NC_DATASTORE_RUNNING;
+					} else if (strcmp(source, "startup") == 0) {
+						ds_type1 = NC_DATASTORE_STARTUP;
+					} else if (strcmp(source, "candidate") == 0) {
+						ds_type1 = NC_DATASTORE_CANDIDATE;
+					} else {
+						json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
+						json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested."));
+						break;
+					}
+
+					if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type1, filter)) == NULL) {
+						json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
+						json_object_object_add(reply, "error-message", json_object_new_string("get-config failed."));
+						break;
+					}
+
+					json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA));
+					json_object_object_add(reply, "data", json_object_new_string(data));
+					break;
+				case MSG_DISCONNECT:
+					session_key = (char*)json_object_get_string(json_object_object_get(request, "session"));
+					ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key);
+					/* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */
+
+					reply =  json_object_new_object();
+					if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) {
+						json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
+						json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier."));
+					} else {
+						json_object_object_add(reply, "type", json_object_new_int(REPLY_OK));
+					}
+					break;
+				default:
+					ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation);
+					reply =  json_object_new_object();
+					json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
+					json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported."));
+					break;
+				}
+				json_object_put(request);
+
+				/* send reply to caller */
+				if (reply != NULL) {
+					msgtext = json_object_to_json_string(reply);
+					send(client, msgtext, strlen(msgtext) + 1, 0);
+					json_object_put(reply);
+				} else {
+					break;
+				}
+
+#if 0
 				case MSG_DATA:
 					/* netconf data */
 					handle_netconf_request(server, client, netconf_sessions_list, &buffer[1]);
 					break;
-				case MSG_CLOSE:
-					handle_msg_close(server, client, netconf_sessions_list, &buffer[1]);
-					break;
-				default:
-					ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf message type");
-					break;
-				}
+#endif
 			}
 		}
 	}
@@ -524,6 +635,7 @@
 	mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg));
 	apr_pool_create(&config->pool, pool);
 	config->forkproc = NULL;
+	config->sockname = SOCKET_FILENAME;
 
 	return (void *)config;
 }
@@ -586,152 +698,25 @@
 	return OK;
 }
 
-static int mod_netconf_fixups(request_rec *r)
-{
-	apr_pool_t *temp_pool = NULL;
-	char* operation;
-	char *data;
-	int len;
-	int sock = -1;
-	char buffer[BUFFER_SIZE];
-	struct sockaddr_un addr;
-	int retval = OK;
-
-	/* process only requests for the mod_netconf handler */
-	if (!r->handler || (strcmp(r->handler, "netconf") != 0)) {
-		return DECLINED;
-	}
-
-	/* allow running module only as subrequest */
-	if (r->main == NULL) {
-		return DECLINED;
-	}
-
-	/* create temporary pool */
-	apr_pool_create(&temp_pool, NULL);
-
-	operation = apr_pstrdup(temp_pool, apr_table_get(r->subprocess_env, "NETCONF_OP"));
-	if (operation == NULL) {
-		apr_pool_destroy(temp_pool);
-		return HTTP_INTERNAL_SERVER_ERROR;
-	}
-
-	/* connect to the daemon */
-	sock = socket(PF_UNIX, SOCK_STREAM, 0);
-	if (sock == -1) {
-		ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot create client's mod_netconf socket");
-		apr_pool_destroy(temp_pool);
-		return HTTP_INTERNAL_SERVER_ERROR;
-	}
-	addr.sun_family = AF_UNIX;
-	strncpy(addr.sun_path, SOCKET_FILENAME, sizeof(addr.sun_path));
-	len = strlen(addr.sun_path) + sizeof(addr.sun_family);
-	if (connect(sock, (struct sockaddr *) &addr, len) == -1) {
-		ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot connect to the mod_netconf daemon");
-		apr_pool_destroy(temp_pool);
-		return HTTP_INTERNAL_SERVER_ERROR;
-	}
-
-	/* process the request */
-	if (strcmp(operation, "connect") == 0) {
-		char *host = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_HOST"));
-		char *port = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_PORT"));
-		char *user = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_USER"));
-		char *pass = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_PASS"));
-
-		/* <MSG_TYPE><host>\0<port>\0<user>\0<pass>\0 */
-		data = apr_psprintf(temp_pool, " %s %s %s %s", host, port, user, pass);
-		data[len = 0] = MSG_OPEN;
-		data[len += strlen(host) + 1] = 0; /* <host>\0 */
-		data[len += strlen(port) + 1] = 0; /* <port>\0 */
-		data[len += strlen(user) + 1] = 0; /* <user>\0 */
-		/* <pass>\0 is already done from printf, but we need to count index for send */
-		data[len += strlen(pass) + 1] = 0;
-
-		send(sock, data, len, 0);
-		len = recv(sock, buffer, BUFFER_SIZE, 0);
-
-		if (len < 2) {
-			ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Invalid reply from mod_netconf daemon");
-			apr_pool_destroy(temp_pool);
-			return HTTP_INTERNAL_SERVER_ERROR;
-		} else if (buffer[0] != MSG_OK) {
-			ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "received from daemon: %x%s", buffer[0], &(buffer[1]));
-			ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot connect with NETCONF server");
-			apr_pool_destroy(temp_pool);
-			return HTTP_INTERNAL_SERVER_ERROR;
-		} else {
-			/* ok, received OK message */
-			ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "received from daemon: %x%s", buffer[0], &(buffer[1]));
-			apr_table_set(r->main->subprocess_env, "NETCONF_NSID", &(buffer[1]));
-		}
-	} else if (strcmp(operation, "disconnect") == 0) {
-		char* nsid = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_NSID"));
-		data = apr_psprintf(temp_pool, " %s", nsid);
-		data[0] = MSG_CLOSE;
-		len = strlen(nsid) + 2;
-		send(sock, data, len, 0);
-		len = recv(sock, buffer, BUFFER_SIZE, 0);
-
-		if (len < 2) {
-			ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Invalid reply from mod_netconf daemon");
-			retval = HTTP_INTERNAL_SERVER_ERROR;
-			goto cleanup;
-		} else if (buffer[0] != MSG_OK) {
-			ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "received from daemon: %x%s", buffer[0], &(buffer[1]));
-			ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot connect with NETCONF server");
-			retval = HTTP_INTERNAL_SERVER_ERROR;
-			goto cleanup;
-		} else {
-			/* ok, received OK message */
-			ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "received from daemon: %x%s", buffer[0], &(buffer[1]));
-		}
-
-	} else if (strcmp(operation, "get-config") == 0) {
-
-	} else {
-		retval = HTTP_NOT_IMPLEMENTED;
-		goto cleanup;
-	}
-
-cleanup:
-	if (sock != -1) {
-		ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "client closes socket");
-		close (sock);
-	}
-	if (temp_pool != NULL) {
-		apr_pool_destroy(temp_pool);
-	}
-
-	return retval;
-}
-
-/**
- * Testing content handler
- * This function should handle NETCONF and return UI
- */
-static int mod_netconf_handler(request_rec * r)
-{
-	/* pseudo code of args to apr_proc_create() */
-	if (!r->handler || (strcmp(r->handler, "netconf") != 0)) {
-		return DECLINED;
-	}
-
-	/* everything was done in fixups */
-	return OK;
-}
-
 /**
  * Register module hooks
  */
 static void mod_netconf_register_hooks(apr_pool_t * p)
 {
 	ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST);
-	ap_hook_fixups(mod_netconf_fixups, NULL, NULL, APR_HOOK_FIRST);
-	ap_hook_handler(mod_netconf_handler, NULL, NULL, APR_HOOK_MIDDLE);
-
 }
 
+static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg)
+{
+	((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg);
+	return NULL;
+}
+
+static const command_rec netconf_cmds[] = {
+		AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."),
+		{NULL}
+};
+
 /* Dispatch list for API hooks */
 module AP_MODULE_DECLARE_DATA netconf_module = {
 	STANDARD20_MODULE_STUFF,
@@ -739,6 +724,6 @@
 	NULL,			/* merge  per-dir    config structures */
 	mod_netconf_create_conf,	/* create per-server config structures */
 	NULL,			/* merge  per-server config structures */
-	NULL,			/* table of config file commands       */
+	netconf_cmds,			/* table of config file commands       */
 	mod_netconf_register_hooks	/* register hooks                      */
 };
diff --git a/src/test-client.c b/src/test-client.c
new file mode 100644
index 0000000..f9e099e
--- /dev/null
+++ b/src/test-client.c
@@ -0,0 +1,227 @@
+/*!
+ * \file test-client.c
+ * \brief Testing client sending JSON requsts to the mod_netconf socket
+ * \author Radek Krejci <rkrejci@cesnet.cz>
+ * \date 2012
+ */
+/*
+ * Copyright (C) 2012 CESNET
+ *
+ * LICENSE TERMS
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. 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.
+ * 3. Neither the name of the Company nor the names of its contributors
+ *    may be used to endorse or promote products derived from this
+ *    software without specific prior written permission.
+ *
+ * ALTERNATIVELY, provided that this notice is retained in full, this
+ * product may be distributed under the terms of the GNU General Public
+ * License (GPL) version 2 or later, in which case the provisions
+ * of the GPL apply INSTEAD OF those given above.
+ *
+ * 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 the company or contributors 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 <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <json/json.h>
+
+#define SOCKET_FILENAME "/tmp/pcon.sock"
+#define BUFFER_SIZE 4096
+
+typedef enum MSG_TYPE {
+	REPLY_OK,
+	REPLY_DATA,
+	REPLY_ERROR,
+	MSG_CONNECT,
+	MSG_DISCONNECT,
+	MSG_GET,
+	MSG_GETCONFIG,
+	MSG_EDITCONFIG,
+	MSG_COPYCONFIG,
+	MSG_DELETECONFIG,
+	MSG_LOCK,
+	MSG_UNLOCK,
+	MSG_KILL
+} MSG_TYPE;
+
+void print_help(char* progname)
+{
+	printf("Usage: %s <command>\n", progname);
+	printf("Available commands:\n");
+	printf("\tconnect\n");
+	printf("\tdisconnect\n");
+	printf("\tcopy-config\n");
+	printf("\tdelete-config\n");
+	printf("\tedit-config\n");
+	printf("\tget\n");
+	printf("\tget-config\n");
+	printf("\tkill-session\n");
+	printf("\tlock\n");
+	printf("\tunlock\n");
+}
+
+int main (int argc, char* argv[])
+{
+	json_object* msg = NULL, *reply;
+	const char* msg_text;
+	int sock;
+	struct sockaddr_un addr;
+	size_t len;
+	char buffer[BUFFER_SIZE];
+	char* line = NULL;
+
+	if (argc != 2) {
+		print_help(argv[0]);
+		return (2);
+	}
+
+	/* connect to the daemon */
+	sock = socket(PF_UNIX, SOCK_STREAM, 0);
+	if (sock == -1) {
+		fprintf(stderr, "Creating socket failed (%s)", strerror(errno));
+		return (EXIT_FAILURE);
+	}
+	addr.sun_family = AF_UNIX;
+	strncpy(addr.sun_path, SOCKET_FILENAME, sizeof(addr.sun_path));
+	len = strlen(addr.sun_path) + sizeof(addr.sun_family);
+	if (connect(sock, (struct sockaddr *) &addr, len) == -1) {
+		fprintf(stderr, "Connecting to mod_netconf (%s) failed (%s)", SOCKET_FILENAME, strerror(errno));
+		close(sock);
+		return (EXIT_FAILURE);
+	}
+
+	line = malloc(sizeof(char) * BUFFER_SIZE);
+
+	if (strcmp(argv[1], "connect") == 0) {
+		msg = json_object_new_object();
+		json_object_object_add(msg, "type", json_object_new_int(MSG_CONNECT));
+		printf("Hostname: ");
+		getline (&line, &len, stdin);
+		line[(strlen(line)-1)] = 0;
+		json_object_object_add(msg, "host", json_object_new_string(line));
+		printf("Port: ");
+		getline (&line, &len, stdin);
+		line[(strlen(line)-1)] = 0;
+		json_object_object_add(msg, "port", json_object_new_string(line));
+		printf("Username: ");
+		getline (&line, &len, stdin);
+		line[(strlen(line)-1)] = 0;
+		json_object_object_add(msg, "user", json_object_new_string(line));
+		printf("Password: ");
+		system("stty -echo");
+		getline (&line, &len, stdin);
+		system("stty echo");
+		printf("\n");
+		line[(strlen(line)-1)] = 0;
+		json_object_object_add(msg, "pass", json_object_new_string(line));
+	} else if (strcmp(argv[1], "disconnect") == 0) {
+		msg = json_object_new_object();
+		json_object_object_add(msg, "type", json_object_new_int(MSG_DISCONNECT));
+		printf("Session: ");
+		getline (&line, &len, stdin);
+		line[(strlen(line)-1)] = 0;
+		json_object_object_add(msg, "session", json_object_new_string(line));
+	} else if (strcmp(argv[1], "copy-config") == 0) {
+	} else if (strcmp(argv[1], "delete-config") == 0) {
+	} else if (strcmp(argv[1], "edit-config") == 0) {
+	} else if (strcmp(argv[1], "get") == 0) {
+		msg = json_object_new_object();
+		json_object_object_add(msg, "type", json_object_new_int(MSG_GET));
+		printf("Session: ");
+		getline (&line, &len, stdin);
+		line[(strlen(line)-1)] = 0;
+		json_object_object_add(msg, "session", json_object_new_string(line));
+		printf("Filter: ");
+		getline(&line, &len, stdin);
+		line[(strlen(line)-1)] = 0;
+		if (strlen(line) > 0) {
+			json_object_object_add(msg, "filter", json_object_new_string(line));
+		}
+	} else if (strcmp(argv[1], "get-config") == 0) {
+		msg = json_object_new_object();
+		json_object_object_add(msg, "type", json_object_new_int(MSG_GETCONFIG));
+		printf("Session: ");
+		getline (&line, &len, stdin);
+		line[(strlen(line)-1)] = 0;
+		json_object_object_add(msg, "session", json_object_new_string(line));
+		printf("Source (running|startup|candidate): ");
+		getline (&line, &len, stdin);
+		line[(strlen(line)-1)] = 0;
+		json_object_object_add(msg, "source", json_object_new_string(line));
+		printf("Filter: ");
+		getline(&line, &len, stdin);
+		line[(strlen(line)-1)] = 0;
+		if (strlen(line) > 0) {
+			json_object_object_add(msg, "filter", json_object_new_string(line));
+		}
+	} else if (strcmp(argv[1], "kill-session") == 0) {
+	} else if (strcmp(argv[1], "lock") == 0) {
+	} else if (strcmp(argv[1], "unlock") == 0) {
+	} else {
+		fprintf(stderr, "Unknown command %s\n", argv[1]);
+		close(sock);
+		return (EXIT_FAILURE);
+	}
+
+	/* send the message */
+	if (msg != NULL) {
+		msg_text = json_object_to_json_string(msg);
+
+		printf("Sending: %s\n", msg_text);
+		send(sock, msg_text, strlen(msg_text) + 1, 0);
+
+		json_object_put(msg);
+	} else {
+		close(sock);
+		return (EXIT_FAILURE);
+	}
+
+	len = recv(sock, buffer, BUFFER_SIZE, 0);
+	reply = json_tokener_parse(buffer);
+	printf("Received:\n");
+	json_object_object_foreach(reply, key, value) {
+		printf("Key: %s, Value: ", key);
+		switch(json_object_get_type(value)) {
+		case json_type_string:
+			printf("%s\n", json_object_get_string(value));
+			break;
+		case json_type_int:
+			printf("%d\n", json_object_get_int(value));
+			break;
+		default:
+			printf("\n");
+			break;
+		}
+	}
+	json_object_put(reply);
+	close(sock);
+	free(line);
+
+	return (EXIT_SUCCESS);
+}