Add http infomation for pcap and checking length of packet
[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 #define PORT_HTTP_ALT       8080
61
62 #define ARP_REQUEST         1
63 #define ARP_REPLY           2
64 #define ARP_RREQUEST        3
65 #define ARP_RREPLY          4
66
67 #define BOOTP_REQUEST       1
68 #define BOOTP_REPLY         2
69
70 #define BOOTP_CHADDR_LEN    16
71 #define BOOTP_SNAME_LEN     64
72 #define BOOTP_FILE_LEN      128
73 #define BOOTP_MOPTION_LEN   60
74
75 #define BOOTP_MAGIC_DHCP    0x63825363
76
77 #define DHCP_TAG_SUBNET_MASK         1
78 #define DHCP_TAG_ROUTER              3
79 #define DHCP_TAG_DNS                 6
80 #define DHCP_TAG_HOST_NAME           12
81 #define DHCP_TAG_REQUESTED_IP        50
82 #define DHCP_TAG_IP_LEASE_TIME       51
83 #define DHCP_TAG_MSG_TYPE            53
84 #define DHCP_TAG_SERVER_ID           54
85 #define DHCP_TAG_MSG_SIZE            57
86 #define DHCP_TAG_CLIENT_ID           61
87 #define DHCP_TAG_END                 255
88
89 #define DHCP_MSG_TYPE_DISCOVER       1
90 #define DHCP_MSG_TYPE_OFFER          2
91 #define DHCP_MSG_TYPE_REQUEST        3
92 #define DHCP_MSG_TYPE_DECLINE        4
93 #define DHCP_MSG_TYPE_ACK            5
94 #define DHCP_MSG_TYPE_NAK            6
95 #define DHCP_MSG_TYPE_RELEASE        7
96 #define DHCP_MSG_TYPE_INFORM         8
97
98 #define DHCP_CLIENT_ID_ETHERNET      1
99 #define DHCP_CLIENT_ID_IEEE802       2
100 #define DHCP_CLIENT_ID_ARCNET        7
101 #define DHCP_CLIENT_ID_LOCALTALK     11
102 #define DHCP_CLIENT_ID_LOCALNET      12
103 #define DHCP_CLIENT_ID_SMDS          14
104 #define DHCP_CLIENT_ID_FRAMERELAY    15
105 #define DHCP_CLIENT_ID_ATM1          16
106 #define DHCP_CLIENT_ID_HDLC          17
107 #define DHCP_CLIENT_ID_FIBRECHANNEL  18
108 #define DHCP_CLIENT_ID_ATM2          19
109 #define DHCP_CLIENT_ID_SERIALLINE    20
110
111 #define NTP_STRATUM_UNSPECIFIED      0
112 #define NTP_STRATUM_PRIM_REF         1
113 #define NTP_STRATUM_INFO_QUERY       62
114 #define NTP_STRATUM_INFO_REPLY       63
115
116 #define NTP_JAN_1970                 2208988800U /* 1970 - 1900 in seconds */
117
118 #define DNS_QTYPE_A                  1
119 #define DNS_QTYPE_CNAME              5
120 #define DNS_QTYPE_AAAA               28
121
122 #define DNS_QCLASS_IN                1
123 #define DNS_QCLASS_CHAOS             3
124 #define DNS_QCLASS_HS                4
125 #define DNS_QCLASS_ANY               255
126
127 #define ETHERTYPE_EAPOL              0x888e
128
129 #define HR_SINGLE       "----------------------------------------" \
130                                                                                 "-------------------------"
131 #define HR_DOUBLE       "========================================" \
132                                                                                 "========================="
133
134 typedef struct {
135         char *ifname;
136         char *nfname;
137         int nflog_group;
138         GThread *thread;
139         pcap_t *handle;
140         int encap_type;
141 } stc_pcap_data_s;
142
143 typedef struct {
144         uint16_t id;
145         uint16_t flags;
146         uint16_t questions;
147         uint16_t answerRR;
148         uint16_t authorityRR;
149         uint16_t additionalRR;
150 } dns_t;
151
152 typedef struct {
153         uint16_t type;
154         uint16_t class;
155 } dns_query_t;
156
157 typedef struct {
158         uint16_t type;
159         uint16_t class;
160         uint16_t ttl;
161         uint16_t rdlen;
162         char rdata[];
163 } dns_recode_t;
164
165 typedef struct {
166         uint8_t op;                             /* packet opcode type */
167         uint8_t htype;                          /* hardware addr type */
168         uint8_t hlen;                           /* hardware addr length */
169         uint8_t hops;                           /* gateway hops */
170         uint32_t xid;                           /* transaction ID */
171         uint16_t secs;                          /* seconds since boot began */
172         uint16_t flags;                         /* flags */
173         struct in_addr ciaddr;                  /* client IP address */
174         struct in_addr yiaddr;                  /* 'your' IP address */
175         struct in_addr siaddr;                  /* server IP address */
176         struct in_addr giaddr;                  /* gateway(relay agent) IP address */
177         uint8_t chaddr[BOOTP_CHADDR_LEN];       /* client hardware address */
178         uint8_t sname[BOOTP_SNAME_LEN];         /* server host name */
179         uint8_t file[BOOTP_FILE_LEN];           /* boot file name */
180         uint32_t magic;                         /* must be 0x63825363 (network order) */
181         uint8_t moption[BOOTP_MOPTION_LEN];
182 } bootp_t;
183
184 typedef struct {
185         uint16_t second;
186         uint16_t fraction;
187 } ntp_short_t;
188
189 typedef struct {
190         uint32_t second;
191         uint32_t fraction;
192 } ntp_long_t;
193
194 typedef struct {
195         uint8_t flags;                          /* Mode, version and leap indicator */
196         uint8_t stratum;                        /* Stratum details */
197         uint8_t poll;                           /* Maximum interval in log2 seconds */
198         uint8_t precision;                      /* Clock precision in log2 seconds */
199         ntp_short_t rootdelay;                  /* Root delay */
200         ntp_short_t rootdisp;                   /* Root dispersion */
201         uint32_t refid;                         /* Reference ID */
202         ntp_long_t reftime;                     /* Reference timestamp */
203         ntp_long_t orgtime;                     /* Origin timestamp */
204         ntp_long_t rectime;                     /* Receive timestamp */
205         ntp_long_t xmttime;                     /* Transmit timestamp */
206 } ntp_t;
207
208 typedef struct {
209         uint32_t ts[4];
210 } nflog_timestamp_s;
211
212 typedef struct ether_header    eth_t;
213 typedef struct ip              ip_t;
214 typedef struct ip6_hdr         ip6_t;
215 typedef struct arphdr          arp_t;
216 typedef struct icmphdr         icmp_t;
217 typedef struct tcphdr          tcp_t;
218 typedef struct udphdr          udp_t;
219
220 #define SIZE_ETHER_HEADER      sizeof(eth_t)
221 #define SIZE_IP_HEADER         sizeof(ip_t)
222 #define SIZE_IP6_HEADER        sizeof(ip6_t)
223 #define SIZE_ARP_HEADER        sizeof(arp_t)
224 #define SIZE_ICMP_HEADER       sizeof(icmp_t)
225 #define SIZE_TCP_HEADER        sizeof(tcp_t)
226 #define SIZE_UDP_HEADER        sizeof(udp_t)
227 #define SIZE_DNS_HEADER        sizeof(dns_t)
228
229 #define SIZE_NFLOG_HDR         sizeof(nflog_hdr_t)
230 #define SIZE_NFLOG_TLV         sizeof(nflog_tlv_t)
231
232 #define IS_SRC_OR_DST_PORT(p) (source == (p) || dest == (p))
233
234 typedef struct {
235         int (*initialize_plugin) (void);
236         int (*deinitialize_plugin) (void);
237         int (*lookup_dev) (void);
238         int (*lookup_net) (void);
239         int (*find_all_devs) (GVariantBuilder *builder);
240         int (*register_loop) (const char *ifname, int group);
241         int (*unregister_loop) (const char *ifname, int group);
242         int (*get_all_loop) (GVariantBuilder *builder);
243 } stc_plugin_pcap_s;
244
245 #endif /* __STC_PLUGIN_PCAP_H__ */