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