#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/types.h>
#include <errno.h>

#include "libnetlinku.h"
#include "policyconf.h"
#include "commuapinl.h"

struct upmnl_handle cfgnlh;

int set_user_policy()
{
	unsigned char debug[]={"17"};/*需要发送的数据*/
	int debug_len = strlen((const char *)debug);

	struct {
	  struct nlmsghdr	  n;
	  struct policyconfmsg   ncm;
	  char			  buf[1024];
	} req = {
	  .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct policyconfmsg)),
	  .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
	  .n.nlmsg_type = COMMNMSG_POLICYCONF,
	  .ncm.policy_id =17,
	  .n.nlmsg_pid = getpid(),
	};
	struct nlmsghdr **debugfs_nl;
	struct pdeliv_debugfs *debugfs_statis;
	struct nlmsghdr *recv_hdr;

	/*可选属性*/
	commnl_addattr_l(&req.n, sizeof(req), POLICYCONFA_SRCIP, debug, debug_len);
	
	/*发送组装好的netlink消息*/
	if (pdeliv_talk(0, &req.n, NULL) < 0)
		{
		return -2;
		}
/*

	if(recv_hdr->nlmsg_type == COMMNMSG_DEBUGFS)
	{
		debugfs_statis = NLMSG_DATA(recv_hdr);
		printf("******debugfs_statis->pde_fail = %d******\n",debugfs_statis->pde_fail);
		printf("******debugfs_statis->pde_succ = %d******\n",debugfs_statis->pde_succ);
		printf("******debugfs_statis->rev_fail = %d******\n",debugfs_statis->rev_fail);
		printf("******debugfs_statis->rev_succ = %d******\n",debugfs_statis->rev_succ);
	}

*/
	return 0;

}

int main( int argc, char **argv)
{	
	int ret = -1; 

	printf("cfgchannel main begin:\r\n");
	
	/*创建通道*/
	ret = pdelivnl_open(0);
	if(ret < 0)
	{
		printf("pdelivnl_open fail,exit.\r\n");
		return -1;
	}

	/*下发配置到内核态*/
	ret = set_user_policy();
	if(ret < 0)
	{
		printf("set_user_policy failed.\r\n");
		return -1;
	}

	/*关闭netlink通道*/
	pdelivnl_close(0);

	printf("cfgchannel main exit!\r\n");
	return 0;
}