summaryrefslogtreecommitdiffstats
path: root/_conntrack/utils/ctnl_test.c
blob: 5124d22bbe9fa3151d50f176566155151616db94 (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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>

       #include <sys/socket.h>
       #include <netinet/in.h>
       #include <arpa/inet.h>


#include <linux/types.h>
#include <linux/netlink.h>
#include <linux/netfilter/nfnetlink.h>

#include <libnfnetlink_conntrack/libnfnetlink_conntrack.h>

#if 0
static struct ctnl_handle *cth;

#if 0
char *display_tuple_flat(struct ip_conntrack_tuple *tuple)
{
        static char buff[250];
        char psb[20];
        int len = 0;

        memset(buff, '\0', sizeof(buff));
	len += sprintf(buff + len, "%s:", inet_ntoa((struct in_addr){tuple->src.ip}));
        switch(tuple->dst.protonum) {
                case (IPPROTO_ICMP):
                        len += sprintf(buff + len, "Icmp (id %d)",
                                ntohs(tuple->src.u.icmp.id));
                        break;
                case (IPPROTO_TCP):
                        sprintf(psb, "%d", ntohs(tuple->src.u.tcp.port));
                        len += sprintf(buff + len, "%s", psb);
                        break;
                case (IPPROTO_UDP):
                        sprintf(psb, "%d", ntohs(tuple->src.u.udp.port));
                        len += sprintf(buff + len, "%s", psb);
                        break;
                default:
                        len += sprintf(buff + len, "Unknown");
                        break;
        }

	len += sprintf(buff + len, "->");
        len += sprintf(buff + len, "%s:", inet_ntoa((struct in_addr){tuple->dst.ip}));
        switch(tuple->dst.protonum) {
                case (IPPROTO_ICMP):
                        len += sprintf(buff + len, "Icmp (%d, code %d)",
                                tuple->dst.u.icmp.type,
                                tuple->dst.u.icmp.code);
                        break;
                case (IPPROTO_TCP):
                        sprintf(psb, "%d", ntohs(tuple->dst.u.tcp.port));
                        len += sprintf(buff + len, "%s", psb);
                        break;
                case (IPPROTO_UDP):
                        sprintf(psb, "%d", ntohs(tuple->dst.u.udp.port));
                        len += sprintf(buff + len, "%s", psb);
                        break;
                default:
                        len += sprintf(buff + len, "Unknown");
                        break;
        }

        return (buff);
}
#else
char *display_tuple_flat(void *foo)
{
	return "not implemented";
}
#endif

int ctnl_parse_attr(struct nfattr *tb[], int max, struct nfattr *cta, int len)
{
        while(NFA_OK(cta, len)) {
                if(cta->nfa_type <= max)
                        tb[cta->nfa_type] = cta;
                cta = NFA_NEXT(cta,len);
        }
        if (len)
		printf("ctnl_parse_attr: deficit (%d) len (%d).\n",
			len, cta->nfa_len);
        return 0;
}

#if 0
int dump()
{
	struct {
		struct nlmsghdr nlh;
		struct nfgenmsg nfmsg;
	} req;
	struct sockaddr_nl nladdr;

	memset(&nladdr, 0, sizeof(nladdr));
	nladdr.nl_family = AF_NETLINK;

	req.nlh.nlmsg_len = sizeof(req);
	req.nlh.nlmsg_type = (NFNL_SUBSYS_CTNETLINK << 8)|CTNL_MSG_CT_GET;
	req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_DUMP|NLM_F_REQUEST;
	req.nlh.nlmsg_pid = 0;
	req.nlh.nlmsg_seq = 1;
	req.nfmsg.nfgen_family = AF_INET;

	return (sendto(ctnlfd, &req, sizeof(req), 0,
			(struct sockaddr *) &nladdr, sizeof(nladdr)));

}
#endif

int print_msg(struct nfgenmsg *cm, size_t len)
{
	struct nfattr *cb[CTA_MAX + 1];

	printf("ctm_family=0x%x\n", cm->nfgen_family);

	ctnl_parse_attr(cb, CTA_MAX, NFM_NFA(cm), len);

	if (cb[CTA_TUPLE_ORIG]) {
		printf("orig: %s\n", 
				display_tuple_flat(NFA_DATA(cb[CTA_TUPLE_ORIG])));
		ctnl_del_conntrack(cth, NFA_DATA(cb[CTA_TUPLE_ORIG]), CTA_TUPLE_ORIG);
	}
	if (cb[CTA_TUPLE_REPLY])
		printf("rply: %s\n", 
				display_tuple_flat(NFA_DATA(cb[CTA_TUPLE_REPLY])));


	return 0;
}

struct nlmsghdr *ctnl_get_packet(struct nlmsghdr **last_nlhdr, 
			      char *buf, size_t len)
{
	struct nlmsghdr *nlh;
	size_t remain_len;

	if ((char *)(*last_nlhdr) > (buf + len) ||
	    (char *)(*last_nlhdr) < buf)
		*last_nlhdr = NULL;

	if (!*last_nlhdr) {
		nlh = (struct nlmsghdr *) buf;
		if (!NLMSG_OK(nlh, len)) {
			printf("error parsing nlmsg\n");
			return NULL;
		}
	} else {
		/* we are n-th part of multipart mesasge */
		if ((*last_nlhdr)->nlmsg_type == NLMSG_DONE ||
		    !((*last_nlhdr)->nlmsg_flags & NLM_F_MULTI)) {
			*last_nlhdr = NULL;
			return NULL;
		}

		remain_len = (len - ((char *)(*last_nlhdr) - buf));
		nlh = NLMSG_NEXT(*last_nlhdr, remain_len);
	}

	*last_nlhdr = nlh;
	return nlh;
}

int main(int argc, char **argv)
{
	char buf[20480];
	struct nfgenmsg *last_cm = NULL, *cm;
	struct nlmsghdr *nlh;
	int len;

	cth = malloc(sizeof(*cth));
	if (ctnl_open(cth, NFNL_SUBSYS_CTNETLINK, 0) < 0) {
		exit(2);
	}

	ctnl_wilddump_request(cth, AF_INET, IPCTNL_MSG_CT_GET);

	while (len = recv(cth->nfnlh.fd, &buf, sizeof(buf), 0)) {
		printf("pkt received\n");
		while (nlh = ctnl_get_packet(&last_cm, (char *)&buf, len)) {
			printf("  decoding msg type 0x%04x\n", nlh->nlmsg_type);
			if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == 
					NFNL_SUBSYS_CTNETLINK) {
				cm = NLMSG_DATA(nlh);
				print_msg(cm, nlh->nlmsg_len);
			}
		}
	}

	return 0;
}

#endif