aced8f9dc85589e5a75fd4e205530ecc58be7855
[platform/core/connectivity/stc-manager.git] / plugin / pcap / include / stc-plugin-pcap.h
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __STC_PLUGIN_PCAP_H__
18 #define __STC_PLUGIN_PCAP_H__
19
20 #include <glib.h>
21 #include <stdbool.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <pcap.h>
29 #include <pcap/nflog.h>
30 #include <arpa/inet.h>
31 #include <net/ethernet.h>
32 #include <net/if_arp.h>
33 #include <netinet/ether.h>
34 #include <netinet/ip.h>
35 #include <netinet/ip6.h>
36 #include <netinet/ip_icmp.h>
37 #include <netinet/tcp.h>
38 #include <netinet/udp.h>
39
40 #include "stc-error.h"
41 #include "stc-manager.h"
42
43 #define BUFF_SIZE_IP        16
44 #define BUFF_SIZE_IP6       46
45 #define BUFF_SIZE_HOST      32
46 #define BUFF_SIZE_ID        64
47 #define BUFF_SIZE_TIME      64
48 #define BUFF_SIZE_NAME      256
49
50 #define PORT_FTP_DATA       20
51 #define PORT_FTP_CTL        21
52 #define PORT_SMTP           25
53 #define PORT_DNS            53
54 #define PORT_BOOTP_S        67
55 #define PORT_BOOTP_C        68
56 #define PORT_HTTP           80
57 #define PORT_NTP            123
58 #define PORT_HTTPS          443
59 #define PORT_SMTP_MSG       587
60
61 #define ARP_REQUEST         1
62 #define ARP_REPLY           2
63 #define ARP_RREQUEST        3
64 #define ARP_RREPLY          4
65
66 #define BOOTP_REQUEST       1
67 #define BOOTP_REPLY         2
68
69 #define BOOTP_CHADDR_LEN    16
70 #define BOOTP_SNAME_LEN     64
71 #define BOOTP_FILE_LEN      128
72 #define BOOTP_MOPTION_LEN   60
73
74 #define BOOTP_MAGIC_DHCP    0x63825363
75
76 #define DHCP_TAG_SUBNET_MASK         1
77 #define DHCP_TAG_ROUTER              3
78 #define DHCP_TAG_DNS                 6
79 #define DHCP_TAG_HOST_NAME           12
80 #define DHCP_TAG_REQUESTED_IP        50
81 #define DHCP_TAG_IP_LEASE_TIME       51
82 #define DHCP_TAG_MSG_TYPE            53
83 #define DHCP_TAG_SERVER_ID           54
84 #define DHCP_TAG_MSG_SIZE            57
85 #define DHCP_TAG_CLIENT_ID           61
86 #define DHCP_TAG_END                 255
87
88 #define DHCP_MSG_TYPE_DISCOVER       1
89 #define DHCP_MSG_TYPE_OFFER          2
90 #define DHCP_MSG_TYPE_REQUEST        3
91 #define DHCP_MSG_TYPE_DECLINE        4
92 #define DHCP_MSG_TYPE_ACK            5
93 #define DHCP_MSG_TYPE_NAK            6
94 #define DHCP_MSG_TYPE_RELEASE        7
95 #define DHCP_MSG_TYPE_INFORM         8
96
97 #define DHCP_CLIENT_ID_ETHERNET      1
98 #define DHCP_CLIENT_ID_IEEE802       2
99 #define DHCP_CLIENT_ID_ARCNET        7
100 #define DHCP_CLIENT_ID_LOCALTALK     11
101 #define DHCP_CLIENT_ID_LOCALNET      12
102 #define DHCP_CLIENT_ID_SMDS          14
103 #define DHCP_CLIENT_ID_FRAMERELAY    15
104 #define DHCP_CLIENT_ID_ATM1          16
105 #define DHCP_CLIENT_ID_HDLC          17
106 #define DHCP_CLIENT_ID_FIBRECHANNEL  18
107 #define DHCP_CLIENT_ID_ATM2          19
108 #define DHCP_CLIENT_ID_SERIALLINE    20
109
110 #define NTP_STRATUM_UNSPECIFIED      0
111 #define NTP_STRATUM_PRIM_REF         1
112 #define NTP_STRATUM_INFO_QUERY       62
113 #define NTP_STRATUM_INFO_REPLY       63
114
115 #define NTP_JAN_1970                 2208988800U /* 1970 - 1900 in seconds */
116
117 #define DNS_QTYPE_A                  1
118 #define DNS_QTYPE_CNAME              5
119 #define DNS_QTYPE_AAAA               28
120
121 #define DNS_QCLASS_IN                1
122 #define DNS_QCLASS_CHAOS             3
123 #define DNS_QCLASS_HS                4
124 #define DNS_QCLASS_ANY               255
125
126 typedef struct {
127         char *ifname;
128         char *nfname;
129         int nflog_group;
130         GThread *thread;
131         pcap_t *handle;
132         int encap_type;
133 } stc_pcap_data_s;
134
135 typedef struct {
136         uint16_t id;
137         uint16_t flags;
138         uint16_t questions;
139         uint16_t answerRR;
140         uint16_t authorityRR;
141         uint16_t additionalRR;
142 } dns_t;
143
144 typedef struct {
145         uint16_t type;
146         uint16_t class;
147 } dns_query_t;
148
149 typedef struct {
150         uint16_t type;
151         uint16_t class;
152         uint16_t ttl;
153         uint16_t rdlen;
154         char rdata[];
155 } dns_recode_t;
156
157 typedef struct {
158         uint8_t op;                             /* packet opcode type */
159         uint8_t htype;                          /* hardware addr type */
160         uint8_t hlen;                           /* hardware addr length */
161         uint8_t hops;                           /* gateway hops */
162         uint32_t xid;                           /* transaction ID */
163         uint16_t secs;                          /* seconds since boot began */
164         uint16_t flags;                         /* flags */
165         struct in_addr ciaddr;                  /* client IP address */
166         struct in_addr yiaddr;                  /* 'your' IP address */
167         struct in_addr siaddr;                  /* server IP address */
168         struct in_addr giaddr;                  /* gateway(relay agent) IP address */
169         uint8_t chaddr[BOOTP_CHADDR_LEN];       /* client hardware address */
170         uint8_t sname[BOOTP_SNAME_LEN];         /* server host name */
171         uint8_t file[BOOTP_FILE_LEN];           /* boot file name */
172         uint32_t magic;                         /* must be 0x63825363 (network order) */
173         uint8_t moption[BOOTP_MOPTION_LEN];
174 } bootp_t;
175
176 typedef struct {
177         uint16_t second;
178         uint16_t fraction;
179 } ntp_short_t;
180
181 typedef struct {
182         uint32_t second;
183         uint32_t fraction;
184 } ntp_long_t;
185
186 typedef struct {
187         uint8_t flags;                          /* Mode, version and leap indicator */
188         uint8_t stratum;                        /* Stratum details */
189         uint8_t poll;                           /* Maximum interval in log2 seconds */
190         uint8_t precision;                      /* Clock precision in log2 seconds */
191         ntp_short_t rootdelay;                  /* Root delay */
192         ntp_short_t rootdisp;                   /* Root dispersion */
193         uint32_t refid;                         /* Reference ID */
194         ntp_long_t reftime;                     /* Reference timestamp */
195         ntp_long_t orgtime;                     /* Origin timestamp */
196         ntp_long_t rectime;                     /* Receive timestamp */
197         ntp_long_t xmttime;                     /* Transmit timestamp */
198 } ntp_t;
199
200 typedef struct ether_header    eth_t;
201 typedef struct ip              ip_t;
202 typedef struct ip6_hdr         ip6_t;
203 typedef struct arphdr          arp_t;
204 typedef struct icmphdr         icmp_t;
205 typedef struct tcphdr          tcp_t;
206 typedef struct udphdr          udp_t;
207
208 #define SIZE_ETHER_HEADER      sizeof(eth_t)
209 #define SIZE_IP_HEADER         sizeof(ip_t)
210 #define SIZE_ARP_HEADER        sizeof(arp_t)
211 #define SIZE_ICMP_HEADER       sizeof(icmp_t)
212 #define SIZE_TCP_HEADER        sizeof(tcp_t)
213 #define SIZE_UDP_HEADER        sizeof(udp_t)
214 #define SIZE_DNS_HEADER        sizeof(dns_t)
215
216 #define SIZE_NFLOG_HDR         sizeof(nflog_hdr_t)
217 #define SIZE_NFLOG_TLV         sizeof(nflog_tlv_t)
218
219 #define IS_SRC_OR_DST_PORT(p) (source == (p) || dest == (p))
220
221 typedef struct {
222         int (*initialize_plugin) (void);
223         int (*deinitialize_plugin) (void);
224         int (*lookup_dev) (void);
225         int (*lookup_net) (void);
226         int (*find_all_devs) (GVariantBuilder *builder);
227         int (*register_loop) (const char *ifname, int group);
228         int (*unregister_loop) (const char *ifname, int group);
229         int (*get_all_loop) (GVariantBuilder *builder);
230 } stc_plugin_pcap_s;
231
232 #endif /* __STC_PLUGIN_PCAP_H__ */