From 382d8f78352424b6767fd7ff43adaa0929266c7f Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 8 Jan 2019 17:34:58 +0900 Subject: [PATCH 01/16] Add http infomation for pcap and checking length of packet Change-Id: I84486ec739c1ed0baf3feacebd22c475b7468971 Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- plugin/appstatus/stc-plugin-appstatus.c | 10 +- plugin/pcap/include/stc-plugin-pcap.h | 13 +++ plugin/pcap/stc-plugin-pcap.c | 192 +++++++++++++++++++++----------- 4 files changed, 146 insertions(+), 71 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 4f177eb..e34256e 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.86 +Version: 0.0.87 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/appstatus/stc-plugin-appstatus.c b/plugin/appstatus/stc-plugin-appstatus.c index 5d5b653..108e0ab 100755 --- a/plugin/appstatus/stc-plugin-appstatus.c +++ b/plugin/appstatus/stc-plugin-appstatus.c @@ -127,10 +127,13 @@ signal_map_s signal_map[] = { static stc_error_e __ground_status_monitor_init(stc_s *stc) { guint i = 0; + guint size = 0; ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); - for (i = 0; signal_map[i].member != NULL; i++) { + size = sizeof(signal_map) / sizeof(signal_map[0]); + + for (i = 0; signal_map[i].member != NULL && i < size; i++) { signal_map[i].sub_id = g_dbus_connection_signal_subscribe(stc->connection, NULL, @@ -152,10 +155,13 @@ static stc_error_e __ground_status_monitor_init(stc_s *stc) static stc_error_e __ground_status_monitor_deinit(stc_s *stc) { guint i = 0; + guint size = 0; ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data"); - for (i = 0; signal_map[i].member != NULL; i++) { + size = sizeof(signal_map) / sizeof(signal_map[0]); + + for (i = 0; signal_map[i].member != NULL && i < size; i++) { g_dbus_connection_signal_unsubscribe(stc->connection, signal_map[i].sub_id); signal_map[i].sub_id = 0; diff --git a/plugin/pcap/include/stc-plugin-pcap.h b/plugin/pcap/include/stc-plugin-pcap.h index aced8f9..ccd97d8 100755 --- a/plugin/pcap/include/stc-plugin-pcap.h +++ b/plugin/pcap/include/stc-plugin-pcap.h @@ -57,6 +57,7 @@ #define PORT_NTP 123 #define PORT_HTTPS 443 #define PORT_SMTP_MSG 587 +#define PORT_HTTP_ALT 8080 #define ARP_REQUEST 1 #define ARP_REPLY 2 @@ -123,6 +124,13 @@ #define DNS_QCLASS_HS 4 #define DNS_QCLASS_ANY 255 +#define ETHERTYPE_EAPOL 0x888e + +#define HR_SINGLE "----------------------------------------" \ + "-------------------------" +#define HR_DOUBLE "========================================" \ + "=========================" + typedef struct { char *ifname; char *nfname; @@ -197,6 +205,10 @@ typedef struct { ntp_long_t xmttime; /* Transmit timestamp */ } ntp_t; +typedef struct { + uint32_t ts[4]; +} nflog_timestamp_s; + typedef struct ether_header eth_t; typedef struct ip ip_t; typedef struct ip6_hdr ip6_t; @@ -207,6 +219,7 @@ typedef struct udphdr udp_t; #define SIZE_ETHER_HEADER sizeof(eth_t) #define SIZE_IP_HEADER sizeof(ip_t) +#define SIZE_IP6_HEADER sizeof(ip6_t) #define SIZE_ARP_HEADER sizeof(arp_t) #define SIZE_ICMP_HEADER sizeof(icmp_t) #define SIZE_TCP_HEADER sizeof(tcp_t) diff --git a/plugin/pcap/stc-plugin-pcap.c b/plugin/pcap/stc-plugin-pcap.c index 7921a1e..6c53ef3 100755 --- a/plugin/pcap/stc-plugin-pcap.c +++ b/plugin/pcap/stc-plugin-pcap.c @@ -28,10 +28,6 @@ #define NFLOG_IFNAME "nflog" -typedef struct { - u_int32_t ts[4]; -} nflog_timestamp_s; - //LCOV_EXCL_START static GHashTable *g_pcap_tables = NULL; static bool g_pcap_start_fm = false; @@ -118,7 +114,7 @@ static uint16_t __pcap_ntp_fraction_info(uint16_t f) return fraction; } -static void __pcap_ntp_info(const u_char **packet) +static void __pcap_ntp_info(const u_char **packet, uint32_t *pktlen) { ntp_t *ntp_h = (ntp_t *)*packet; char refid[BUFF_SIZE_ID]; @@ -133,7 +129,8 @@ static void __pcap_ntp_info(const u_char **packet) __pcap_ntp_time_info(ntp_h->rectime.second, rectime); __pcap_ntp_time_info(ntp_h->xmttime.second, xmttime); - if (STC_PCAP_LOG) + if (STC_PCAP_LOG) { + STC_LOGD(HR_SINGLE); STC_LOGD("Flags[0x%02x] Stratum[%u] Poll[%u:%us] Precision[%u] " "Root delay[%u.%06us] Root dispersion[%u.%06us] Ref ID[%s]", ntp_h->flags, ntp_h->stratum, ntp_h->poll, @@ -143,10 +140,10 @@ static void __pcap_ntp_info(const u_char **packet) ntohs(ntp_h->rootdisp.second), __pcap_ntp_fraction_info(ntp_h->rootdisp.fraction), refid); - - if (STC_PCAP_LOG) STC_LOGD("Reference[%s] Origin[%s] Receive[%s] Transmit[%s]", reftime, orgtime, rectime, xmttime); + STC_LOGD(HR_SINGLE); + } } static const char *__pcap_dhcp_client_id_info(uint8_t data) @@ -245,7 +242,7 @@ static void __pcap_bootp_magic_info(uint32_t magic, if (STC_PCAP_LOG) STC_LOGD("Magic cookie[DHCP]"); - while(len > 0) { + while (len > 0) { uint8_t tag = opt[0]; uint8_t length = opt[1]; uint8_t *data = &opt[2]; @@ -369,19 +366,22 @@ static void __pcap_bootp_info(const u_char **packet, u_int16_t len) ether_ntoa((const struct ether_addr *)bootp_h->chaddr), sizeof(chaddr)); - if (STC_PCAP_LOG) + if (STC_PCAP_LOG) { + STC_LOGD(HR_SINGLE); STC_LOGD("Message type[%u:%s] HW type[0x%02x] HW len[%u] Hops[%u] " "Transaction ID[0x%08x] Seconds elapsed[%u] Flags[0x%04x]", bootp_h->op, __pcap_bootp_op_info(bootp_h->op), bootp_h->htype, bootp_h->hlen, bootp_h->hops, ntohl(bootp_h->xid), ntohs(bootp_h->secs), ntohs(bootp_h->flags)); - - if (STC_PCAP_LOG) STC_LOGD("Client[%s] Your(client)[%s] Next server[%s] " "Relay agent[%s] Client MAC[%s]", ciaddr, yiaddr, siaddr, giaddr, chaddr); + } __pcap_bootp_magic_info(bootp_h->magic, bootp_h->moption, len); + + if (STC_PCAP_LOG) + STC_LOGD(HR_SINGLE); } static char *__pcap_dns_type_info(uint16_t type) @@ -451,7 +451,7 @@ static uint8_t * __pcap_dns_name_info(uint8_t *dns_h, break; } - if (dname - name + *sec + 1 > BUFF_SIZE_NAME - 1) { + if (dname + *sec >= name + BUFF_SIZE_NAME) { *name = '\0'; return NULL; } @@ -472,7 +472,7 @@ static uint8_t * __pcap_dns_name_info(uint8_t *dns_h, return (uint8_t *)nxt; } -static void __pcap_dns_data_info(const u_char **packet, dns_t *dns_h) +static void __pcap_dns_data_info(const u_char **packet, dns_t *dns_h, uint32_t *pktlen) { uint8_t *data = (uint8_t *)*packet; uint16_t qdcount = ntohs(dns_h->questions); @@ -567,20 +567,26 @@ static void __pcap_dns_data_info(const u_char **packet, dns_t *dns_h) } } -static void __pcap_dns_info(const u_char **packet) +static void __pcap_dns_info(const u_char **packet, uint32_t *pktlen) { dns_t *dns_h = (dns_t *)*packet; - if (STC_PCAP_LOG) + if (STC_PCAP_LOG) { + STC_LOGD(HR_SINGLE); STC_LOGD("Transaction ID[0x%x] Flags[0x%x] Questions[%u] " "Answer RRs[%u] Authority RRs[%u] Additional RRs[%u]", ntohs(dns_h->id), ntohs(dns_h->flags), ntohs(dns_h->questions), ntohs(dns_h->answerRR), ntohs(dns_h->authorityRR), ntohs(dns_h->additionalRR)); + } *packet += SIZE_DNS_HEADER; + *pktlen -= SIZE_DNS_HEADER; + + __pcap_dns_data_info(packet, dns_h, pktlen); - __pcap_dns_data_info(packet, dns_h); + if (STC_PCAP_LOG) + STC_LOGD(HR_SINGLE); } static const char *__pcap_icmp_code_info(u_int8_t type, u_int8_t code) @@ -728,7 +734,7 @@ static const char *__pcap_icmp_type_info(u_int8_t type) return info; } -static void __pcap_icmp_info(const u_char **packet) +static void __pcap_icmp_info(const u_char **packet, uint32_t *pktlen) { icmp_t *icmp_h = (icmp_t *)*packet; @@ -737,9 +743,36 @@ static void __pcap_icmp_info(const u_char **packet) icmp_h->type, __pcap_icmp_type_info(icmp_h->type), icmp_h->code, __pcap_icmp_code_info(icmp_h->type, icmp_h->code), ntohs(icmp_h->checksum)); + + *packet += SIZE_ICMP_HEADER; + *pktlen -= SIZE_ICMP_HEADER; } -static void __pcap_tcp_info(const u_char **packet) +static void __pcap_http_info(const u_char **packet, uint32_t *pktlen) +{ + GString *value; + gsize value_len; + u_char *value_pos = NULL; + gchar *value_str = NULL; + + value_len = *pktlen; + value = g_string_sized_new(value_len); + if (value) { + value_pos = (u_char *)*packet; + while (value_len--) + g_string_append_printf(value, "%c", *(value_pos++)); + value_str = g_string_free(value, FALSE); + } + + if (STC_PCAP_LOG) { + STC_LOGD(HR_SINGLE "\n%s", value_str); + STC_LOGD(HR_SINGLE); + } + + FREE(value_str); +} + +static void __pcap_tcp_info(const u_char **packet, uint32_t *pktlen) { tcp_t *tcp_h = (tcp_t *)*packet; u_int16_t source = ntohs(tcp_h->source); @@ -759,13 +792,19 @@ static void __pcap_tcp_info(const u_char **packet) tcp_h->urg, tcp_h->ack, tcp_h->psh, tcp_h->rst, tcp_h->syn, tcp_h->fin); - *packet += SIZE_TCP_HEADER; + *packet += (tcp_h->th_off * 4); + *pktlen -= (tcp_h->th_off * 4); - if (IS_SRC_OR_DST_PORT(PORT_DNS)) - __pcap_dns_info(packet); + if (*pktlen > 0) { + if (IS_SRC_OR_DST_PORT(PORT_DNS)) + __pcap_dns_info(packet, pktlen); + else if (IS_SRC_OR_DST_PORT(PORT_HTTP) || + IS_SRC_OR_DST_PORT(PORT_HTTP_ALT)) + __pcap_http_info(packet, pktlen); + } } -static void __pcap_udp_info(const u_char **packet) +static void __pcap_udp_info(const u_char **packet, uint32_t *pktlen) { udp_t *udp_h = (udp_t *)*packet; u_int16_t source = ntohs(udp_h->source); @@ -777,14 +816,17 @@ static void __pcap_udp_info(const u_char **packet) source, dest, len, ntohs(udp_h->check)); *packet += SIZE_UDP_HEADER; - - if (IS_SRC_OR_DST_PORT(PORT_DNS)) - __pcap_dns_info(packet); - else if (IS_SRC_OR_DST_PORT(PORT_BOOTP_C) || - IS_SRC_OR_DST_PORT(PORT_BOOTP_S)) - __pcap_bootp_info(packet, len); - else if (IS_SRC_OR_DST_PORT(PORT_NTP)) - __pcap_ntp_info(packet); + *pktlen -= SIZE_UDP_HEADER; + + if (*pktlen > 0) { + if (IS_SRC_OR_DST_PORT(PORT_DNS)) + __pcap_dns_info(packet, pktlen); + else if (IS_SRC_OR_DST_PORT(PORT_BOOTP_C) || + IS_SRC_OR_DST_PORT(PORT_BOOTP_S)) + __pcap_bootp_info(packet, len); + else if (IS_SRC_OR_DST_PORT(PORT_NTP)) + __pcap_ntp_info(packet, pktlen); + } } static const char *__pcap_eth_type_info(u_int16_t type) @@ -815,6 +857,10 @@ static const char *__pcap_eth_type_info(u_int16_t type) return info; } +static void __pcap_eapol_info(const u_char **packet, uint32_t *pktlen) +{ +} + static const char *__pcap_arp_opcode_info(u_int16_t opcode) { char *info = NULL; @@ -840,7 +886,7 @@ static const char *__pcap_arp_opcode_info(u_int16_t opcode) return info; } -static void __pcap_arp_info(const u_char **packet) +static void __pcap_arp_info(const u_char **packet, uint32_t *pktlen) { arp_t *arp_h = (arp_t *)*packet; u_int8_t *sha = (u_int8_t *)(*packet + SIZE_ARP_HEADER); @@ -878,6 +924,9 @@ static void __pcap_arp_info(const u_char **packet) STC_LOGD("Sender MAC[%s] Sender IP[%s] " "Target MAC[%s] Target IP[%s]", sma, sia, tma, tia); + + *packet += SIZE_ARP_HEADER; + *pktlen -= SIZE_ARP_HEADER; } static const char *__pcap_ip_protocol_info(u_int8_t p) @@ -914,7 +963,7 @@ static const char *__pcap_ip_protocol_info(u_int8_t p) return info; } -static void __pcap_ipv6_info(const u_char **packet) +static void __pcap_ipv6_info(const u_char **packet, uint32_t *pktlen) { ip6_t *ip6_h = (ip6_t *)*packet; char ip6_src[BUFF_SIZE_IP6]; @@ -930,27 +979,25 @@ static void __pcap_ipv6_info(const u_char **packet) ip6_h->ip6_nxt, __pcap_ip_protocol_info(ip6_h->ip6_nxt), ip6_h->ip6_hlim, ip6_src, ip6_dst); + *packet += SIZE_IP6_HEADER; + *pktlen -= SIZE_IP6_HEADER; + switch (ip6_h->ip6_nxt) { - case IPPROTO_IP: - break; - case IPPROTO_ICMP: - break; - case IPPROTO_IPIP: - break; case IPPROTO_TCP: + __pcap_tcp_info(packet, pktlen); break; case IPPROTO_UDP: - break; - case IPPROTO_IPV6: + __pcap_udp_info(packet, pktlen); break; case IPPROTO_ICMPV6: + __pcap_icmp_info(packet, pktlen); break; default: break; } } -static void __pcap_ip_info(const u_char **packet) +static void __pcap_ip_info(const u_char **packet, uint32_t *pktlen) { ip_t *ip_h = (ip_t *)*packet; char ip_src[BUFF_SIZE_IP]; @@ -971,23 +1018,24 @@ static void __pcap_ip_info(const u_char **packet) ntohs(ip_h->ip_sum), ip_src, ip_dst); *packet += SIZE_IP_HEADER; + *pktlen -= SIZE_IP_HEADER; switch (ip_h->ip_p) { case IPPROTO_ICMP: - __pcap_icmp_info(packet); + __pcap_icmp_info(packet, pktlen); break; case IPPROTO_TCP: - __pcap_tcp_info(packet); + __pcap_tcp_info(packet, pktlen); break; case IPPROTO_UDP: - __pcap_udp_info(packet); + __pcap_udp_info(packet, pktlen); break; default: break; } } -static void __pcap_eth_info(const u_char **packet) +static void __pcap_eth_info(const u_char **packet, uint32_t *pktlen) { eth_t *eth_h = (eth_t *)*packet; u_int8_t *eth_shost = eth_h->ether_shost; @@ -1011,6 +1059,7 @@ static void __pcap_eth_info(const u_char **packet) shost, dhost, ether_type, __pcap_eth_type_info(ether_type)); *packet += SIZE_ETHER_HEADER; + *pktlen -= SIZE_ETHER_HEADER; } static const char *__pcap_family_info(u_int8_t family) @@ -1097,7 +1146,7 @@ static const char *__pcap_tlv_type_info(u_int16_t type) } static void __pcap_nflog_tlv_info(const u_char **packet, - u_int32_t length, u_int32_t caplen) + u_int32_t *pktlen) { nflog_tlv_t *tlv_h; u_int16_t tlv_length; @@ -1107,11 +1156,10 @@ static void __pcap_nflog_tlv_info(const u_char **packet, u_char *value_pos = NULL; gchar *value_str = NULL; - length -= SIZE_NFLOG_HDR; - caplen -= SIZE_NFLOG_HDR; + *pktlen -= SIZE_NFLOG_HDR; - while (length > 0) { - if (caplen < SIZE_NFLOG_TLV || length < SIZE_NFLOG_TLV) + while (*pktlen > 0) { + if (*pktlen < SIZE_NFLOG_TLV) break; tlv_h = (nflog_tlv_t *)*packet; @@ -1123,7 +1171,7 @@ static void __pcap_nflog_tlv_info(const u_char **packet, if (tlv_length < SIZE_NFLOG_TLV) break; - if (caplen < tlv_length || length < tlv_length) + if (*pktlen < tlv_length) break; if (STC_PCAP_LOG) { @@ -1198,16 +1246,16 @@ static void __pcap_nflog_tlv_info(const u_char **packet, if (tlv_type == NFULA_PAYLOAD) { *packet += SIZE_NFLOG_TLV; + *pktlen -= SIZE_NFLOG_TLV; break; } *packet += tlv_length; - length -= tlv_length; - caplen -= tlv_length; + *pktlen -= tlv_length; } } -static void __pcap_nflog_hdr_info(const u_char **packet) +static void __pcap_nflog_hdr_info(const u_char **packet, uint32_t *pktlen) { nflog_hdr_t *hdr_h = (nflog_hdr_t *)*packet; u_int8_t family = hdr_h->nflog_family; @@ -1219,6 +1267,7 @@ static void __pcap_nflog_hdr_info(const u_char **packet) __pcap_family_info(family), family, version, resource_id); *packet += SIZE_NFLOG_HDR; + *pktlen -= SIZE_NFLOG_HDR; } static void __pcap_fm_info(const struct pcap_pkthdr *pkthdr) @@ -1249,12 +1298,14 @@ static void __pcap_fm_info(const struct pcap_pkthdr *pkthdr) static void __pcap_handler(u_char *param, const struct pcap_pkthdr *pkthdr, const u_char *packet) { - /* int len = pkthdr->len; */ + uint32_t pktlen = pkthdr->len; stc_pcap_data_s *pcap_data = (stc_pcap_data_s *)param; eth_t *eth_h; - u_int16_t eth_type; + uint16_t eth_type; nflog_hdr_t *nflog_h; - u_int8_t nflog_family; + uint8_t nflog_family; + + STC_LOGD(HR_DOUBLE); __pcap_fm_info(pkthdr); @@ -1263,22 +1314,25 @@ static void __pcap_handler(u_char *param, eth_h = (eth_t *)packet; eth_type = ntohs(eth_h->ether_type); - __pcap_eth_info(&packet); + __pcap_eth_info(&packet, &pktlen); switch (eth_type) { case ETHERTYPE_IP: - __pcap_ip_info(&packet); - /* __pcap_data_info(pcaket, len); */ + __pcap_ip_info(&packet, &pktlen); + /* __pcap_data_info(pcaket, pktlen); */ break; case ETHERTYPE_IPV6: - __pcap_ipv6_info(&packet); + __pcap_ipv6_info(&packet, &pktlen); break; case ETHERTYPE_ARP: case ETHERTYPE_REVARP: - __pcap_arp_info(&packet); + __pcap_arp_info(&packet, &pktlen); break; case ETHERTYPE_LOOPBACK: break; + case ETHERTYPE_EAPOL: + __pcap_eapol_info(&packet, &pktlen); + break; default: break; } @@ -1287,15 +1341,15 @@ static void __pcap_handler(u_char *param, nflog_h = (nflog_hdr_t *)packet; nflog_family = nflog_h->nflog_family; - __pcap_nflog_hdr_info(&packet); - __pcap_nflog_tlv_info(&packet, pkthdr->len, pkthdr->caplen); + __pcap_nflog_hdr_info(&packet, &pktlen); + __pcap_nflog_tlv_info(&packet, &pktlen); switch (nflog_family) { case AF_INET: - __pcap_ip_info(&packet); + __pcap_ip_info(&packet, &pktlen); break; case AF_INET6: - __pcap_ipv6_info(&packet); + __pcap_ipv6_info(&packet, &pktlen); break; default: break; @@ -1304,6 +1358,8 @@ static void __pcap_handler(u_char *param, default: break; } + + STC_LOGD(HR_DOUBLE); } static gboolean __pcap_thread_source_func(gpointer data) @@ -1362,7 +1418,7 @@ static gpointer __pcap_thread_func(gpointer data) pcap_data->encap_type = ENCAPTYPE_ETHERNET; } break; - } + } } pcap_freealldevs(alldevs); -- 2.7.4 From 5ac5a543cf2c66f3a9264136d1fe118a15ec8ae4 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 16 Jan 2019 15:54:49 +0900 Subject: [PATCH 02/16] Fix a svace issue for negative code error Change-Id: I4ed552ae7de2d691a26239c3390fde62f2a0d40f Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- plugin/procfs/stc-plugin-procfs.c | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index e34256e..f83c3f0 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.87 +Version: 0.0.88 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/procfs/stc-plugin-procfs.c b/plugin/procfs/stc-plugin-procfs.c index 668f15c..9538a61 100755 --- a/plugin/procfs/stc-plugin-procfs.c +++ b/plugin/procfs/stc-plugin-procfs.c @@ -447,7 +447,7 @@ static int __subscribe_proc_events(void) if (sock == -1) { __STC_LOG_FUNC_EXIT__; - return -1; + return STC_ERROR_INVALID_PARAMETER; } memset(&msg, 0, sizeof(nl_connector_msg_s)); @@ -466,11 +466,11 @@ static int __subscribe_proc_events(void) if (ret == -1) { STC_LOGE("Error sending netlink connector message"); __STC_LOG_FUNC_EXIT__; - return -1; + return STC_ERROR_FAIL; } __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_NONE; } static int __unsubscribe_proc_events(void) @@ -482,7 +482,7 @@ static int __unsubscribe_proc_events(void) if (sock == -1) { __STC_LOG_FUNC_EXIT__; - return -1; + return STC_ERROR_INVALID_PARAMETER; } memset(&msg, 0, sizeof(nl_connector_msg_s)); @@ -501,11 +501,11 @@ static int __unsubscribe_proc_events(void) if (ret == -1) { STC_LOGE("Error sending netlink connector message"); __STC_LOG_FUNC_EXIT__; - return -1; + return STC_ERROR_FAIL; } __STC_LOG_FUNC_EXIT__; - return 0; + return STC_ERROR_NONE; } static bool __process_pid_cb(pid_t pid, void *user_data) @@ -542,6 +542,7 @@ static bool __process_pid_cb(pid_t pid, void *user_data) int stc_plugin_procfs_initialize(void) { __STC_LOG_FUNC_ENTER__; + int ret = STC_ERROR_NONE; proc_tree = g_tree_new_full(__proc_tree_key_compare, NULL, __proc_tree_key_free, @@ -550,28 +551,30 @@ int stc_plugin_procfs_initialize(void) /* TODO: Fill proc tree with current procfs state */ __open_nl_connector_sock(); - __subscribe_proc_events(); + ret = __subscribe_proc_events(); + __STC_LOG_FUNC_EXIT__; - return STC_ERROR_NONE; + return ret; } int stc_plugin_procfs_deinitialize(void) { __STC_LOG_FUNC_ENTER__; + int ret = STC_ERROR_NONE; if (nl_connector_sock == -1) { STC_LOGD("socket already closed"); return STC_ERROR_NONE; } - __unsubscribe_proc_events(); + ret = __unsubscribe_proc_events(); __close_nl_connector_sock(); g_tree_destroy(proc_tree); proc_tree = NULL; __STC_LOG_FUNC_EXIT__; - return STC_ERROR_NONE; + return ret; } stc_error_e stc_plugin_procfs_load(void) -- 2.7.4 From 310dd4f2b2c1be903aad780c092f1ed11cd5e549 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 17 Jan 2019 13:15:48 +0900 Subject: [PATCH 03/16] Fix some svace issues for unchecking return value Change-Id: I87e056e9f8538f199a27f2bba7a204c5db21e7a5 Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- src/database/tables/table-counters.c | 28 ++++++++++++---- src/database/tables/table-firewall.c | 44 ++++++++++++++++++------- src/database/tables/table-restrictions.c | 56 +++++++++++++++++++++++++------- src/database/tables/table-statistics.c | 22 ++++++++++--- 5 files changed, 116 insertions(+), 36 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index f83c3f0..6c7bbcf 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.88 +Version: 0.0.89 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/database/tables/table-counters.c b/src/database/tables/table-counters.c index 78b7723..fa22cc5 100755 --- a/src/database/tables/table-counters.c +++ b/src/database/tables/table-counters.c @@ -246,7 +246,6 @@ static bool __table_counters_is_entry_present(long long int restriction_id) if (sqlite3_bind_int(stmt, 1, restriction_id) != SQLITE_OK) { ret = FALSE; - __STC_LOG_FUNC_EXIT__; goto handle_error; } @@ -267,7 +266,10 @@ static bool __table_counters_is_entry_present(long long int restriction_id) } handle_error: - sqlite3_reset(stmt); + rc = sqlite3_reset(stmt); + if (rc != SQLITE_OK) + ret = FALSE; + return ret; } @@ -317,7 +319,10 @@ API stc_error_e table_counters_get(long long int restriction_id, } while (rc == SQLITE_ROW); handle_error: - sqlite3_reset(stmt); + rc = sqlite3_reset(stmt); + if (rc != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } @@ -354,7 +359,9 @@ API stc_error_e table_counters_update_counters(const table_counters_info *info) info->restriction_id); handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } //LCOV_EXCL_STOP @@ -406,7 +413,10 @@ API stc_error_e table_counters_get_timestamps(long long int restriction_id, } while (rc == SQLITE_ROW); handle_error: - sqlite3_reset(stmt); + rc = sqlite3_reset(stmt); + if (rc != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } @@ -437,7 +447,9 @@ API stc_error_e table_counters_update_timestamps(const table_counters_info *info info->restriction_id); handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } //LCOV_EXCL_STOP @@ -460,7 +472,9 @@ API stc_error_e table_counters_delete(long long int restriction_id) STC_LOGD("Counter deleted for restriction_id [%llu]", restriction_id); handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } diff --git a/src/database/tables/table-firewall.c b/src/database/tables/table-firewall.c index c80161c..5e179e3 100755 --- a/src/database/tables/table-firewall.c +++ b/src/database/tables/table-firewall.c @@ -324,7 +324,9 @@ API stc_error_e table_firewall_insert_lock(char *name, int state) STC_LOGD("Firewall lock state inserted [%d]", state); handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } @@ -348,7 +350,9 @@ API stc_error_e table_firewall_update_lock(char *name, int state) STC_LOGD("Firewall lock state updated [%d]", state); handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } @@ -386,7 +390,9 @@ API stc_error_e table_firewall_get_lock(char *name, int *state) STC_LOGD("Firewall lock state [%d]", *state); handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } @@ -415,7 +421,9 @@ API stc_error_e table_firewall_insert_chain(firewall_chain_s *info) STC_LOGD("Firewall chain inserted [%s]", info->chain); handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } @@ -442,8 +450,9 @@ API stc_error_e table_firewall_delete_chain(firewall_chain_s *info) STC_LOGD("Firewall chain deleted [%s]", info->chain); handle_error: + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; - sqlite3_reset(stmt); return error_code; } @@ -470,8 +479,9 @@ API stc_error_e table_firewall_flush_chain(firewall_chain_s *info) STC_LOGD("Firewall chain flushed [%s]", info->chain); handle_error: + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; - sqlite3_reset(stmt); return error_code; } @@ -500,7 +510,9 @@ API stc_error_e table_firewall_update_chain(firewall_chain_s *info) STC_LOGD("Firewall chain updated [%s]", info->chain); handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } @@ -537,7 +549,9 @@ API stc_error_e table_firewall_foreach_chain(firewall_chain_cb info_cb, } } while (rc == SQLITE_ROW); - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } @@ -636,7 +650,9 @@ API stc_error_e table_firewall_insert_rule(firewall_rule_s *info) STC_LOGD("Firewall rule inserted [%s]", info->chain); handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } @@ -662,8 +678,9 @@ API stc_error_e table_firewall_delete_rule(firewall_rule_s *info) STC_LOGD("Firewall rule deleted [%s]", info->chain); handle_error: + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; - sqlite3_reset(stmt); return error_code; } @@ -763,8 +780,9 @@ API stc_error_e table_firewall_update_rule(firewall_rule_s *info, guint key) STC_LOGD("Firewall rule updated [%s]", info->chain); handle_error: + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; - sqlite3_reset(stmt); return error_code; } @@ -843,7 +861,9 @@ API stc_error_e table_firewall_foreach_rule(firewall_rule_cb info_cb, } } while (rc == SQLITE_ROW); - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } diff --git a/src/database/tables/table-restrictions.c b/src/database/tables/table-restrictions.c index debb04d..71167ee 100755 --- a/src/database/tables/table-restrictions.c +++ b/src/database/tables/table-restrictions.c @@ -315,7 +315,10 @@ stc_error_e table_restrictions_per_app(const gchar* app_id, } while (rc == SQLITE_ROW); handle_error: - sqlite3_reset(stmt); + rc = sqlite3_reset(stmt); + if (rc != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; return error_code; } @@ -366,7 +369,10 @@ API stc_error_e table_restrictions_foreach(const table_restrictions_info_cb rest } } while (rc == SQLITE_ROW); - sqlite3_reset(stmt); + rc = sqlite3_reset(stmt); + if (rc != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; return error_code; } @@ -388,8 +394,17 @@ stc_error_e table_restrictions_get_restriction_type_subscriber_id(const char *ap } *type = STC_RSTN_TYPE_UNKNOWN; - sqlite3_reset(select_restriction_type_subscriber_id); - sqlite3_reset(select_restriction_type); + ret = sqlite3_reset(select_restriction_type_subscriber_id); + if (ret != SQLITE_OK) { + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE + } + + ret = sqlite3_reset(select_restriction_type); + if (ret != SQLITE_OK) { + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE + } if (subscriber_id == NULL) { state_subscriber_id = 0; @@ -428,8 +443,14 @@ stc_error_e table_restrictions_get_restriction_type_subscriber_id(const char *ap } handle_error: - sqlite3_reset(select_restriction_type); - sqlite3_reset(select_restriction_type_subscriber_id); + ret = sqlite3_reset(select_restriction_type); + if (ret != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + + ret = sqlite3_reset(select_restriction_type_subscriber_id); + if (ret != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } @@ -475,7 +496,9 @@ stc_error_e table_restrictions_delete(const char *app_id, handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } @@ -508,10 +531,14 @@ stc_error_e __get_restriction_id(table_restrictions_info *info) default: STC_LOGE("Failed to get restriction id: %s\n", //LCOV_EXCL_LINE sqlite3_errmsg(stc_db_get_database())); + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } handle_error: - sqlite3_reset(stmt); + rc = sqlite3_reset(stmt); + if (rc != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; return error_code; } @@ -526,7 +553,11 @@ stc_error_e table_restrictions_update(table_restrictions_info *info) goto handle_error; //LCOV_EXCL_LINE } - __get_restriction_id(info); + if (__get_restriction_id(info) != STC_ERROR_NONE) { + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE + goto handle_error; //LCOV_EXCL_LINE + } + if (info->restriction_id) stmt = update_net_restrictions; @@ -561,11 +592,14 @@ stc_error_e table_restrictions_update(table_restrictions_info *info) STC_LOGD("Restriction updated app_id [%s]", info->app_id); } else { STC_LOGD("Restriction inserted app_id [%s]", info->app_id); - __get_restriction_id(info); + if (__get_restriction_id(info) != STC_ERROR_NONE) + error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE } handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index 7670e28..719b9a2 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -375,7 +375,9 @@ stc_error_e table_statistics_reset_first_n_entries(int num) __STC_LOG_FUNC_EXIT__; } handle_error: - sqlite3_reset(delete_query[4]); + if (sqlite3_reset(delete_query[4]) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } //LCOV_EXCL_STOP @@ -419,7 +421,9 @@ stc_error_e table_statistics_reset(const table_statistics_reset_rule *rule) STC_LOGD("Entry deleted successfully."); handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } @@ -495,7 +499,10 @@ stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rul } while (rc == SQLITE_ROW); handle_error: - sqlite3_reset(stmt); + rc = sqlite3_reset(stmt); + if (rc != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; return error_code; } @@ -573,7 +580,10 @@ API stc_error_e table_statistics_per_app(const char *app_id, } while (rc == SQLITE_ROW); handle_error: - sqlite3_reset(stmt); + rc = sqlite3_reset(stmt); + if (rc != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + __STC_LOG_FUNC_EXIT__; return error_code; } @@ -626,7 +636,9 @@ API stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key, "rcv[%lld] snd[%lld]", stat->app_id, rcv, snd); handle_error: - sqlite3_reset(stmt); + if (sqlite3_reset(stmt) != SQLITE_OK) + error_code = STC_ERROR_DB_FAILED; + return error_code; } -- 2.7.4 From 4941ccbc7b096e8898226d715a698023202b0a09 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Fri, 1 Feb 2019 10:34:11 +0900 Subject: [PATCH 04/16] Fix some coverity issues 1020742 : Unchecked return value 1037124 : Logically dead code Change-Id: I60af7ca69b055884bd41047435b2bfff8213f3a7 Signed-off-by: hyunuktak --- plugin/pcap/stc-plugin-pcap.c | 2 -- src/helper/helper-nfacct-rule.c | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/plugin/pcap/stc-plugin-pcap.c b/plugin/pcap/stc-plugin-pcap.c index 6c53ef3..17fe73e 100755 --- a/plugin/pcap/stc-plugin-pcap.c +++ b/plugin/pcap/stc-plugin-pcap.c @@ -541,8 +541,6 @@ static void __pcap_dns_data_info(const u_char **packet, dns_t *dns_h, uint32_t * break; case DNS_QTYPE_CNAME: __pcap_dns_name_info((uint8_t *)dns_h, &data[10], cname); - if (data == NULL) - return; if (STC_PCAP_LOG) STC_LOGD("Name[%s] Type[%u:%s] Class[0x%04x:%s] " "TTL[%u] Data length[%u] CName[%s]", diff --git a/src/helper/helper-nfacct-rule.c b/src/helper/helper-nfacct-rule.c index 24376f3..070052b 100755 --- a/src/helper/helper-nfacct-rule.c +++ b/src/helper/helper-nfacct-rule.c @@ -511,14 +511,33 @@ static stc_error_e exec_iptables_cmd(nfacct_rule_s *rule) iptables_rule.d_iprange_type = rule->dst_iprange_type; /* specify source and destination ip address if any */ - if (rule->src_ip1) - inet_aton(rule->src_ip1, &iptables_rule.s_ip1); - if (rule->src_ip2) - inet_aton(rule->src_ip2, &iptables_rule.s_ip2); - if (rule->dst_ip1) - inet_aton(rule->dst_ip1, &iptables_rule.d_ip1); - if (rule->dst_ip2) - inet_aton(rule->dst_ip2, &iptables_rule.d_ip2); + if (rule->src_ip1) { + if (!inet_aton(rule->src_ip1, &iptables_rule.s_ip1)) { + ret = STC_ERROR_INVALID_PARAMETER; + goto free; + } + } + + if (rule->src_ip2) { + if (!inet_aton(rule->src_ip2, &iptables_rule.s_ip2)) { + ret = STC_ERROR_INVALID_PARAMETER; + goto free; + } + } + + if (rule->dst_ip1) { + if (!inet_aton(rule->dst_ip1, &iptables_rule.d_ip1)) { + ret = STC_ERROR_INVALID_PARAMETER; + goto free; + } + } + + if (rule->dst_ip2) { + if (!inet_aton(rule->dst_ip2, &iptables_rule.d_ip2)) { + ret = STC_ERROR_INVALID_PARAMETER; + goto free; + } + } if (rule->action == NFACCT_ACTION_DELETE) { /* delete interface rule */ @@ -528,6 +547,7 @@ static stc_error_e exec_iptables_cmd(nfacct_rule_s *rule) ret = iptables_add(&iptables_rule, iptype); } +free: g_free(iptables_rule.nfacct_name); g_free(iptables_rule.ifname); g_free(iptables_rule.target); -- 2.7.4 From 72abd9f62109bdd946278752b3e65a5ce6673f11 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Fri, 1 Feb 2019 14:42:37 +0900 Subject: [PATCH 05/16] First check the index size of signal map Change-Id: I592a919ef58281be6038157ba1e2f9acd059f233 Signed-off-by: hyunuktak --- plugin/appstatus/stc-plugin-appstatus.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/appstatus/stc-plugin-appstatus.c b/plugin/appstatus/stc-plugin-appstatus.c index 108e0ab..ced6be6 100755 --- a/plugin/appstatus/stc-plugin-appstatus.c +++ b/plugin/appstatus/stc-plugin-appstatus.c @@ -133,7 +133,7 @@ static stc_error_e __ground_status_monitor_init(stc_s *stc) size = sizeof(signal_map) / sizeof(signal_map[0]); - for (i = 0; signal_map[i].member != NULL && i < size; i++) { + for (i = 0; i < size && signal_map[i].member != NULL; i++) { signal_map[i].sub_id = g_dbus_connection_signal_subscribe(stc->connection, NULL, @@ -161,7 +161,7 @@ static stc_error_e __ground_status_monitor_deinit(stc_s *stc) size = sizeof(signal_map) / sizeof(signal_map[0]); - for (i = 0; signal_map[i].member != NULL && i < size; i++) { + for (i = 0; i < size && signal_map[i].member != NULL; i++) { g_dbus_connection_signal_unsubscribe(stc->connection, signal_map[i].sub_id); signal_map[i].sub_id = 0; -- 2.7.4 From 3442dca7a4676306c8f61fc13d08051fad20141b Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Tue, 12 Feb 2019 20:41:44 +0530 Subject: [PATCH 06/16] Fixed build fail issue when building only plugin-firewall Description: This patch fixes a build fail issue when we try to build only plugin-firewall. Below is the error which is generated when build fails:- ======================================================== [ 11s] In file included from /home/abuild/rpmbuild/BUILD/stc-manager-0.0.90/include/stc-manager.h:21:0, [ 11s] from /home/abuild/rpmbuild/BUILD/stc-manager-0.0.90/plugin/firewall/include/stc-plugin-firewall.h:30, [ 11s] from /home/abuild/rpmbuild/BUILD/stc-manager-0.0.90/plugin/firewall/stc-plugin-firewall.c:17: [ 11s] /home/abuild/rpmbuild/BUILD/stc-manager-0.0.90/include/stc-manager-util.h:21:18: fatal error: glib.h: No such file or directory [ 11s] #include [ 11s] ^ [ 11s] compilation terminated. ======================================================= Change-Id: Id8a01d4ab761c39f9c2bfd1cf3bbfd664e1322b0 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- plugin/firewall/CMakeLists.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 6c7bbcf..a7acd9a 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.89 +Version: 0.0.90 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/firewall/CMakeLists.txt b/plugin/firewall/CMakeLists.txt index 1b8404b..05c4d90 100644 --- a/plugin/firewall/CMakeLists.txt +++ b/plugin/firewall/CMakeLists.txt @@ -10,7 +10,7 @@ PKG_CHECK_MODULES(firewall_plugin REQUIRED glib-2.0 ) -FOREACH(flag ${pcap_plugin_CFLAGS}) +FOREACH(flag ${firewall_plugin_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") ENDFOREACH(flag) -- 2.7.4 From 8ab755ee84d54aabefe0a1e0988544283286e4d5 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Tue, 12 Feb 2019 20:05:53 +0530 Subject: [PATCH 07/16] Fixed Build fail issue when plugin-pcap build is diabled Description: This patch resolves the error which comes when plugin-pcap build is disabled. Below error comes without this patch when plugin-pcap is diabled:- ================================================================= [ 20s] In file included from /home/abuild/rpmbuild/BUILD/stc-manager-0.0.89/include/stc-manager-plugin-pcap.h:22:0, [ 20s] from /home/abuild/rpmbuild/BUILD/stc-manager-0.0.89/src/stc-manager-plugin-pcap.c:20: [ 20s] /home/abuild/rpmbuild/BUILD/stc-manager-0.0.89/plugin/pcap/include/stc-plugin-pcap.h:28:18: fatal error: pcap.h: No such file or directory [ 20s] #include [ 20s] ^ [ 20s] compilation terminated. [ 20s] src/CMakeFiles/stc-manager.dir/build.make:209: recipe for target 'src/CMakeFiles/stc-manager.dir/stc-manager-plugin-pcap.c.o' failed [ 20s] make[2]: *** [src/CMakeFiles/stc-manager.dir/stc-manager-plugin-pcap.c.o] Error 1 [ 20s] make[2]: *** Waiting for unfinished jobs.... [ 20s] In file included from /home/abuild/rpmbuild/BUILD/stc-manager-0.0.89/include/stc-manager-plugin-pcap.h:22:0, [ 20s] from /home/abuild/rpmbuild/BUILD/stc-manager-0.0.89/src/stc-pcap.c:19: [ 20s] /home/abuild/rpmbuild/BUILD/stc-manager-0.0.89/plugin/pcap/include/stc-plugin-pcap.h:28:18: fatal error: pcap.h: No such file or directory [ 20s] #include [ 20s] ^ [ 20s] compilation terminated. [ 20s] src/CMakeFiles/stc-manager.dir/build.make:329: recipe for target 'src/CMakeFiles/stc-manager.dir/stc-pcap.c.o' failed [ 20s] make[2]: *** [src/CMakeFiles/stc-manager.dir/stc-pcap.c.o] Error 1 [ 20s] In file included from /home/abuild/rpmbuild/BUILD/stc-manager-0.0.89/include/stc-manager-plugin-pcap.h:22:0, [ 20s] from /home/abuild/rpmbuild/BUILD/stc-manager-0.0.89/src/stc-manager.c:34: [ 20s] /home/abuild/rpmbuild/BUILD/stc-manager-0.0.89/plugin/pcap/include/stc-plugin-pcap.h:28:18: fatal error: pcap.h: No such file or directory [ 20s] #include [ 20s] ^ [ 20s] compilation terminated. ================================================================= Change-Id: I03c007d0adf19361304cc24acc85e4c5cd9d329d Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- plugin/pcap/include/stc-plugin-pcap-internal.h | 234 +++++++++++++++++++++++++ plugin/pcap/include/stc-plugin-pcap.h | 212 ---------------------- plugin/pcap/stc-plugin-pcap.c | 1 + 4 files changed, 236 insertions(+), 213 deletions(-) create mode 100755 plugin/pcap/include/stc-plugin-pcap-internal.h diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index a7acd9a..11549fe 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.90 +Version: 0.0.91 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/pcap/include/stc-plugin-pcap-internal.h b/plugin/pcap/include/stc-plugin-pcap-internal.h new file mode 100755 index 0000000..2817613 --- /dev/null +++ b/plugin/pcap/include/stc-plugin-pcap-internal.h @@ -0,0 +1,234 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STC_PLUGIN_PCAP_INTERNAL_H__ +#define __STC_PLUGIN_PCAP_INTERNAL_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "stc-error.h" +#include "stc-manager.h" + +#define BUFF_SIZE_IP 16 +#define BUFF_SIZE_IP6 46 +#define BUFF_SIZE_HOST 32 +#define BUFF_SIZE_ID 64 +#define BUFF_SIZE_TIME 64 +#define BUFF_SIZE_NAME 256 + +#define PORT_FTP_DATA 20 +#define PORT_FTP_CTL 21 +#define PORT_SMTP 25 +#define PORT_DNS 53 +#define PORT_BOOTP_S 67 +#define PORT_BOOTP_C 68 +#define PORT_HTTP 80 +#define PORT_NTP 123 +#define PORT_HTTPS 443 +#define PORT_SMTP_MSG 587 +#define PORT_HTTP_ALT 8080 + +#define ARP_REQUEST 1 +#define ARP_REPLY 2 +#define ARP_RREQUEST 3 +#define ARP_RREPLY 4 + +#define BOOTP_REQUEST 1 +#define BOOTP_REPLY 2 + +#define BOOTP_CHADDR_LEN 16 +#define BOOTP_SNAME_LEN 64 +#define BOOTP_FILE_LEN 128 +#define BOOTP_MOPTION_LEN 60 + +#define BOOTP_MAGIC_DHCP 0x63825363 + +#define DHCP_TAG_SUBNET_MASK 1 +#define DHCP_TAG_ROUTER 3 +#define DHCP_TAG_DNS 6 +#define DHCP_TAG_HOST_NAME 12 +#define DHCP_TAG_REQUESTED_IP 50 +#define DHCP_TAG_IP_LEASE_TIME 51 +#define DHCP_TAG_MSG_TYPE 53 +#define DHCP_TAG_SERVER_ID 54 +#define DHCP_TAG_MSG_SIZE 57 +#define DHCP_TAG_CLIENT_ID 61 +#define DHCP_TAG_END 255 + +#define DHCP_MSG_TYPE_DISCOVER 1 +#define DHCP_MSG_TYPE_OFFER 2 +#define DHCP_MSG_TYPE_REQUEST 3 +#define DHCP_MSG_TYPE_DECLINE 4 +#define DHCP_MSG_TYPE_ACK 5 +#define DHCP_MSG_TYPE_NAK 6 +#define DHCP_MSG_TYPE_RELEASE 7 +#define DHCP_MSG_TYPE_INFORM 8 + +#define DHCP_CLIENT_ID_ETHERNET 1 +#define DHCP_CLIENT_ID_IEEE802 2 +#define DHCP_CLIENT_ID_ARCNET 7 +#define DHCP_CLIENT_ID_LOCALTALK 11 +#define DHCP_CLIENT_ID_LOCALNET 12 +#define DHCP_CLIENT_ID_SMDS 14 +#define DHCP_CLIENT_ID_FRAMERELAY 15 +#define DHCP_CLIENT_ID_ATM1 16 +#define DHCP_CLIENT_ID_HDLC 17 +#define DHCP_CLIENT_ID_FIBRECHANNEL 18 +#define DHCP_CLIENT_ID_ATM2 19 +#define DHCP_CLIENT_ID_SERIALLINE 20 + +#define NTP_STRATUM_UNSPECIFIED 0 +#define NTP_STRATUM_PRIM_REF 1 +#define NTP_STRATUM_INFO_QUERY 62 +#define NTP_STRATUM_INFO_REPLY 63 + +#define NTP_JAN_1970 2208988800U /* 1970 - 1900 in seconds */ + +#define DNS_QTYPE_A 1 +#define DNS_QTYPE_CNAME 5 +#define DNS_QTYPE_AAAA 28 + +#define DNS_QCLASS_IN 1 +#define DNS_QCLASS_CHAOS 3 +#define DNS_QCLASS_HS 4 +#define DNS_QCLASS_ANY 255 + +#define ETHERTYPE_EAPOL 0x888e + +#define HR_SINGLE "----------------------------------------" \ + "-------------------------" +#define HR_DOUBLE "========================================" \ + "=========================" + +typedef struct { + char *ifname; + char *nfname; + int nflog_group; + GThread *thread; + pcap_t *handle; + int encap_type; +} stc_pcap_data_s; + +typedef struct { + uint16_t id; + uint16_t flags; + uint16_t questions; + uint16_t answerRR; + uint16_t authorityRR; + uint16_t additionalRR; +} dns_t; + +typedef struct { + uint16_t type; + uint16_t class; +} dns_query_t; + +typedef struct { + uint16_t type; + uint16_t class; + uint16_t ttl; + uint16_t rdlen; + char rdata[]; +} dns_recode_t; + +typedef struct { + uint8_t op; /* packet opcode type */ + uint8_t htype; /* hardware addr type */ + uint8_t hlen; /* hardware addr length */ + uint8_t hops; /* gateway hops */ + uint32_t xid; /* transaction ID */ + uint16_t secs; /* seconds since boot began */ + uint16_t flags; /* flags */ + struct in_addr ciaddr; /* client IP address */ + struct in_addr yiaddr; /* 'your' IP address */ + struct in_addr siaddr; /* server IP address */ + struct in_addr giaddr; /* gateway(relay agent) IP address */ + uint8_t chaddr[BOOTP_CHADDR_LEN]; /* client hardware address */ + uint8_t sname[BOOTP_SNAME_LEN]; /* server host name */ + uint8_t file[BOOTP_FILE_LEN]; /* boot file name */ + uint32_t magic; /* must be 0x63825363 (network order) */ + uint8_t moption[BOOTP_MOPTION_LEN]; +} bootp_t; + +typedef struct { + uint16_t second; + uint16_t fraction; +} ntp_short_t; + +typedef struct { + uint32_t second; + uint32_t fraction; +} ntp_long_t; + +typedef struct { + uint8_t flags; /* Mode, version and leap indicator */ + uint8_t stratum; /* Stratum details */ + uint8_t poll; /* Maximum interval in log2 seconds */ + uint8_t precision; /* Clock precision in log2 seconds */ + ntp_short_t rootdelay; /* Root delay */ + ntp_short_t rootdisp; /* Root dispersion */ + uint32_t refid; /* Reference ID */ + ntp_long_t reftime; /* Reference timestamp */ + ntp_long_t orgtime; /* Origin timestamp */ + ntp_long_t rectime; /* Receive timestamp */ + ntp_long_t xmttime; /* Transmit timestamp */ +} ntp_t; + +typedef struct { + uint32_t ts[4]; +} nflog_timestamp_s; + +typedef struct ether_header eth_t; +typedef struct ip ip_t; +typedef struct ip6_hdr ip6_t; +typedef struct arphdr arp_t; +typedef struct icmphdr icmp_t; +typedef struct tcphdr tcp_t; +typedef struct udphdr udp_t; + +#define SIZE_ETHER_HEADER sizeof(eth_t) +#define SIZE_IP_HEADER sizeof(ip_t) +#define SIZE_IP6_HEADER sizeof(ip6_t) +#define SIZE_ARP_HEADER sizeof(arp_t) +#define SIZE_ICMP_HEADER sizeof(icmp_t) +#define SIZE_TCP_HEADER sizeof(tcp_t) +#define SIZE_UDP_HEADER sizeof(udp_t) +#define SIZE_DNS_HEADER sizeof(dns_t) + +#define SIZE_NFLOG_HDR sizeof(nflog_hdr_t) +#define SIZE_NFLOG_TLV sizeof(nflog_tlv_t) + +#define IS_SRC_OR_DST_PORT(p) (source == (p) || dest == (p)) + +#endif /* __STC_PLUGIN_PCAP_INTERNAL_H__ */ diff --git a/plugin/pcap/include/stc-plugin-pcap.h b/plugin/pcap/include/stc-plugin-pcap.h index ccd97d8..521f27a 100755 --- a/plugin/pcap/include/stc-plugin-pcap.h +++ b/plugin/pcap/include/stc-plugin-pcap.h @@ -18,218 +18,6 @@ #define __STC_PLUGIN_PCAP_H__ #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "stc-error.h" -#include "stc-manager.h" - -#define BUFF_SIZE_IP 16 -#define BUFF_SIZE_IP6 46 -#define BUFF_SIZE_HOST 32 -#define BUFF_SIZE_ID 64 -#define BUFF_SIZE_TIME 64 -#define BUFF_SIZE_NAME 256 - -#define PORT_FTP_DATA 20 -#define PORT_FTP_CTL 21 -#define PORT_SMTP 25 -#define PORT_DNS 53 -#define PORT_BOOTP_S 67 -#define PORT_BOOTP_C 68 -#define PORT_HTTP 80 -#define PORT_NTP 123 -#define PORT_HTTPS 443 -#define PORT_SMTP_MSG 587 -#define PORT_HTTP_ALT 8080 - -#define ARP_REQUEST 1 -#define ARP_REPLY 2 -#define ARP_RREQUEST 3 -#define ARP_RREPLY 4 - -#define BOOTP_REQUEST 1 -#define BOOTP_REPLY 2 - -#define BOOTP_CHADDR_LEN 16 -#define BOOTP_SNAME_LEN 64 -#define BOOTP_FILE_LEN 128 -#define BOOTP_MOPTION_LEN 60 - -#define BOOTP_MAGIC_DHCP 0x63825363 - -#define DHCP_TAG_SUBNET_MASK 1 -#define DHCP_TAG_ROUTER 3 -#define DHCP_TAG_DNS 6 -#define DHCP_TAG_HOST_NAME 12 -#define DHCP_TAG_REQUESTED_IP 50 -#define DHCP_TAG_IP_LEASE_TIME 51 -#define DHCP_TAG_MSG_TYPE 53 -#define DHCP_TAG_SERVER_ID 54 -#define DHCP_TAG_MSG_SIZE 57 -#define DHCP_TAG_CLIENT_ID 61 -#define DHCP_TAG_END 255 - -#define DHCP_MSG_TYPE_DISCOVER 1 -#define DHCP_MSG_TYPE_OFFER 2 -#define DHCP_MSG_TYPE_REQUEST 3 -#define DHCP_MSG_TYPE_DECLINE 4 -#define DHCP_MSG_TYPE_ACK 5 -#define DHCP_MSG_TYPE_NAK 6 -#define DHCP_MSG_TYPE_RELEASE 7 -#define DHCP_MSG_TYPE_INFORM 8 - -#define DHCP_CLIENT_ID_ETHERNET 1 -#define DHCP_CLIENT_ID_IEEE802 2 -#define DHCP_CLIENT_ID_ARCNET 7 -#define DHCP_CLIENT_ID_LOCALTALK 11 -#define DHCP_CLIENT_ID_LOCALNET 12 -#define DHCP_CLIENT_ID_SMDS 14 -#define DHCP_CLIENT_ID_FRAMERELAY 15 -#define DHCP_CLIENT_ID_ATM1 16 -#define DHCP_CLIENT_ID_HDLC 17 -#define DHCP_CLIENT_ID_FIBRECHANNEL 18 -#define DHCP_CLIENT_ID_ATM2 19 -#define DHCP_CLIENT_ID_SERIALLINE 20 - -#define NTP_STRATUM_UNSPECIFIED 0 -#define NTP_STRATUM_PRIM_REF 1 -#define NTP_STRATUM_INFO_QUERY 62 -#define NTP_STRATUM_INFO_REPLY 63 - -#define NTP_JAN_1970 2208988800U /* 1970 - 1900 in seconds */ - -#define DNS_QTYPE_A 1 -#define DNS_QTYPE_CNAME 5 -#define DNS_QTYPE_AAAA 28 - -#define DNS_QCLASS_IN 1 -#define DNS_QCLASS_CHAOS 3 -#define DNS_QCLASS_HS 4 -#define DNS_QCLASS_ANY 255 - -#define ETHERTYPE_EAPOL 0x888e - -#define HR_SINGLE "----------------------------------------" \ - "-------------------------" -#define HR_DOUBLE "========================================" \ - "=========================" - -typedef struct { - char *ifname; - char *nfname; - int nflog_group; - GThread *thread; - pcap_t *handle; - int encap_type; -} stc_pcap_data_s; - -typedef struct { - uint16_t id; - uint16_t flags; - uint16_t questions; - uint16_t answerRR; - uint16_t authorityRR; - uint16_t additionalRR; -} dns_t; - -typedef struct { - uint16_t type; - uint16_t class; -} dns_query_t; - -typedef struct { - uint16_t type; - uint16_t class; - uint16_t ttl; - uint16_t rdlen; - char rdata[]; -} dns_recode_t; - -typedef struct { - uint8_t op; /* packet opcode type */ - uint8_t htype; /* hardware addr type */ - uint8_t hlen; /* hardware addr length */ - uint8_t hops; /* gateway hops */ - uint32_t xid; /* transaction ID */ - uint16_t secs; /* seconds since boot began */ - uint16_t flags; /* flags */ - struct in_addr ciaddr; /* client IP address */ - struct in_addr yiaddr; /* 'your' IP address */ - struct in_addr siaddr; /* server IP address */ - struct in_addr giaddr; /* gateway(relay agent) IP address */ - uint8_t chaddr[BOOTP_CHADDR_LEN]; /* client hardware address */ - uint8_t sname[BOOTP_SNAME_LEN]; /* server host name */ - uint8_t file[BOOTP_FILE_LEN]; /* boot file name */ - uint32_t magic; /* must be 0x63825363 (network order) */ - uint8_t moption[BOOTP_MOPTION_LEN]; -} bootp_t; - -typedef struct { - uint16_t second; - uint16_t fraction; -} ntp_short_t; - -typedef struct { - uint32_t second; - uint32_t fraction; -} ntp_long_t; - -typedef struct { - uint8_t flags; /* Mode, version and leap indicator */ - uint8_t stratum; /* Stratum details */ - uint8_t poll; /* Maximum interval in log2 seconds */ - uint8_t precision; /* Clock precision in log2 seconds */ - ntp_short_t rootdelay; /* Root delay */ - ntp_short_t rootdisp; /* Root dispersion */ - uint32_t refid; /* Reference ID */ - ntp_long_t reftime; /* Reference timestamp */ - ntp_long_t orgtime; /* Origin timestamp */ - ntp_long_t rectime; /* Receive timestamp */ - ntp_long_t xmttime; /* Transmit timestamp */ -} ntp_t; - -typedef struct { - uint32_t ts[4]; -} nflog_timestamp_s; - -typedef struct ether_header eth_t; -typedef struct ip ip_t; -typedef struct ip6_hdr ip6_t; -typedef struct arphdr arp_t; -typedef struct icmphdr icmp_t; -typedef struct tcphdr tcp_t; -typedef struct udphdr udp_t; - -#define SIZE_ETHER_HEADER sizeof(eth_t) -#define SIZE_IP_HEADER sizeof(ip_t) -#define SIZE_IP6_HEADER sizeof(ip6_t) -#define SIZE_ARP_HEADER sizeof(arp_t) -#define SIZE_ICMP_HEADER sizeof(icmp_t) -#define SIZE_TCP_HEADER sizeof(tcp_t) -#define SIZE_UDP_HEADER sizeof(udp_t) -#define SIZE_DNS_HEADER sizeof(dns_t) - -#define SIZE_NFLOG_HDR sizeof(nflog_hdr_t) -#define SIZE_NFLOG_TLV sizeof(nflog_tlv_t) - -#define IS_SRC_OR_DST_PORT(p) (source == (p) || dest == (p)) typedef struct { int (*initialize_plugin) (void); diff --git a/plugin/pcap/stc-plugin-pcap.c b/plugin/pcap/stc-plugin-pcap.c index 17fe73e..0859700 100755 --- a/plugin/pcap/stc-plugin-pcap.c +++ b/plugin/pcap/stc-plugin-pcap.c @@ -15,6 +15,7 @@ */ #include "stc-plugin-pcap.h" +#include "stc-plugin-pcap-internal.h" #define ENCAPTYPE_ETHERNET 1 #define ENCAPTYPE_NFLOG 141 -- 2.7.4 From 1e76489ca3c3a418d17ccd0412c31e728f5d3bb7 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 13 Feb 2019 11:18:01 +0900 Subject: [PATCH 08/16] Fix a bug by wrong declaration Change-Id: I36a3cef3e79267dbb7fcb8068c30f9add0e6c4fb Signed-off-by: hyunuktak --- plugin/firewall/stc-plugin-firewall.c | 2 +- src/stc-manager-plugin-firewall.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/firewall/stc-plugin-firewall.c b/plugin/firewall/stc-plugin-firewall.c index 2b13d62..6a497cc 100755 --- a/plugin/firewall/stc-plugin-firewall.c +++ b/plugin/firewall/stc-plugin-firewall.c @@ -1271,7 +1271,7 @@ int stc_plugin_firewall_get_lock(int *state) return STC_ERROR_NONE; } -API int stc_plugin_firewall_update(void) +int stc_plugin_firewall_update(void) { __fw_chain_foreach(__fw_foreach_to_add_chain, NULL); __fw_chain_foreach(__fw_foreach_to_set_rule_to_chain, NULL); diff --git a/src/stc-manager-plugin-firewall.c b/src/stc-manager-plugin-firewall.c index cc423f5..adb87fb 100755 --- a/src/stc-manager-plugin-firewall.c +++ b/src/stc-manager-plugin-firewall.c @@ -117,7 +117,7 @@ int stc_plugin_firewall_get_lock(int *state) return stc_plugin->get_lock_firewall(state); } -int stc_plugin_firewall_update(void) +API int stc_plugin_firewall_update(void) { if (!stc_plugin_enabled) { if (STC_DEBUG_LOG) -- 2.7.4 From 2fee9eb1239fb74c1fa15182391abc6328b56bb3 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 19 Feb 2019 15:22:47 +0900 Subject: [PATCH 09/16] Add to set/unset restriction list Change-Id: I87e0278ccfac1aa10ae112494f66e75e2ef07a41 Signed-off-by: hyunuktak --- include/stc-restriction.h | 10 +++ interfaces/stcmanager-iface-restriction.xml | 10 +++ src/stc-manager-gdbus.c | 6 ++ src/stc-restriction.c | 99 +++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+) diff --git a/include/stc-restriction.h b/include/stc-restriction.h index fe25ea2..1ee0255 100755 --- a/include/stc-restriction.h +++ b/include/stc-restriction.h @@ -52,6 +52,16 @@ gboolean handle_restriction_unset(StcRestriction *object, GVariant *parameters, void *user_data); +gboolean handle_restriction_set_list(StcRestriction *object, + GDBusMethodInvocation *invocation, + GVariant *parameters, + void *user_data); + +gboolean handle_restriction_unset_list(StcRestriction *object, + GDBusMethodInvocation *invocation, + GVariant *parameters, + void *user_data); + gboolean handle_restriction_get(StcRestriction *object, GDBusMethodInvocation *invocation, const gchar *app_id, diff --git a/interfaces/stcmanager-iface-restriction.xml b/interfaces/stcmanager-iface-restriction.xml index 1fbfc3f..b8c89e0 100644 --- a/interfaces/stcmanager-iface-restriction.xml +++ b/interfaces/stcmanager-iface-restriction.xml @@ -28,6 +28,16 @@ + + + + + + + + + + diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index 61a81b4..7990287 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -140,6 +140,12 @@ static gboolean __stc_manager_gdbus_restriction_init(stc_s *stc) g_signal_connect(restriction, "handle-unset", G_CALLBACK(handle_restriction_unset), stc); + g_signal_connect(restriction, "handle-set-list", + G_CALLBACK(handle_restriction_set_list), stc); + + g_signal_connect(restriction, "handle-unset-list", + G_CALLBACK(handle_restriction_unset_list), stc); + /* Export the object (@manager takes its own reference to @object) */ g_dbus_object_manager_server_export(stc->obj_mgr, G_DBUS_OBJECT_SKELETON(object)); diff --git a/src/stc-restriction.c b/src/stc-restriction.c index 42bf9bc..d49eb42 100755 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -344,6 +344,105 @@ gboolean handle_restriction_unset(StcRestriction *object, return TRUE; } +gboolean handle_restriction_set_list(StcRestriction *object, + GDBusMethodInvocation *invocation, + GVariant *parameters, + void *user_data) +{ + __STC_LOG_FUNC_ENTER__; + GVariantIter *iter = NULL; + GVariantIter *iter_row = NULL; + stc_error_e ret = STC_ERROR_NONE; + + g_variant_get(parameters, "aa{sv}", &iter); + while (g_variant_iter_next(iter, "a{sv}", &iter_row)) { + table_restrictions_info rule; + + memset(&rule, 0, sizeof(table_restrictions_info)); + __initialize_rstn_rule(&rule); + + stc_manager_gdbus_dict_foreach(iter_row, + __stc_extract_restriction_rule, &rule); + + if (__validate_rstn_rule(&rule)) { + ret = table_restrictions_update(&rule); + if (ret != STC_ERROR_NONE) { + STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + g_variant_iter_free(iter_row); + g_variant_iter_free(iter); + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return TRUE; + } + + ret = stc_plugin_monitor_add_rstn(&rule); + if (ret != STC_ERROR_NONE) { + STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + g_variant_iter_free(iter_row); + g_variant_iter_free(iter); + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return TRUE; + } + } + + g_variant_iter_free(iter_row); + } + g_variant_iter_free(iter); + + STC_DBUS_REPLY_ERROR_NONE(invocation); + __STC_LOG_FUNC_EXIT__; + return TRUE; +} + +gboolean handle_restriction_unset_list(StcRestriction *object, + GDBusMethodInvocation *invocation, + GVariant *parameters, + void *user_data) +{ + __STC_LOG_FUNC_ENTER__; + GVariantIter *iter = NULL; + GVariantIter *iter_row = NULL; + stc_error_e ret = STC_ERROR_NONE; + + g_variant_get(parameters, "aa{sv}", &iter); + while (g_variant_iter_next(iter, "a{sv}", &iter_row)) { + table_restrictions_info rule; + + memset(&rule, 0, sizeof(table_restrictions_info)); + __initialize_rstn_rule(&rule); + + stc_manager_gdbus_dict_foreach(iter_row, + __stc_extract_restriction_rule, &rule); + + if (__validate_rstn_rule(&rule)) { + ret = table_restrictions_delete(rule.app_id, rule.iftype, + rule.ifname, rule.subscriber_id, rule.roaming); + if (ret != STC_ERROR_NONE) { + STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + g_variant_iter_free(iter_row); + g_variant_iter_free(iter); + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return TRUE; + } + + ret = stc_plugin_monitor_remove_rstn(&rule); + if (ret != STC_ERROR_NONE) { + STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + g_variant_iter_free(iter_row); + g_variant_iter_free(iter); + __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE + return TRUE; + } + } + + g_variant_iter_free(iter_row); + } + g_variant_iter_free(iter); + + STC_DBUS_REPLY_ERROR_NONE(invocation); + __STC_LOG_FUNC_EXIT__; + return TRUE; +} + gboolean handle_restriction_get(StcRestriction *object, GDBusMethodInvocation *invocation, const gchar *app_id, -- 2.7.4 From 4ae33b34d91dca201989ee7e6f72a64d93d0af0a Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 12 Mar 2019 10:22:49 +0900 Subject: [PATCH 10/16] Add and remove restriction rule appropriately Change-Id: Icb7d614eb5b4197b05df880e91d59fe21b77b6c5 Signed-off-by: hyunuktak --- plugin/monitor/stc-plugin-monitor-rstn.c | 112 ++++++++++++++++++------------- 1 file changed, 67 insertions(+), 45 deletions(-) diff --git a/plugin/monitor/stc-plugin-monitor-rstn.c b/plugin/monitor/stc-plugin-monitor-rstn.c index 99951a2..365207c 100755 --- a/plugin/monitor/stc-plugin-monitor-rstn.c +++ b/plugin/monitor/stc-plugin-monitor-rstn.c @@ -31,31 +31,33 @@ static void __print_rstn(stc_rstn_data_s *rstn_data) { - STC_LOGI("RstnID[%llu] AppID[%s] classid[%u] " - "iftype[%d] ifname[%s] rstn_state[%d] " - "rstn_type[%d] roaming[%d] subscriber_id[%s]", - rstn_data->restriction_id, - rstn_data->app_id, rstn_data->classid, - rstn_data->iftype, rstn_data->ifname, - rstn_data->rstn_state, rstn_data->rstn_type, - rstn_data->roaming, rstn_data->subscriber_id); - STC_LOGI("month_start_date[%d] limit[%lld] " - "warn_limit[%lld] monthly_limit[%lld] " - "weekly_limit[%lld] daily_limit[%lld] ", - rstn_data->month_start_date, - rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA], - rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN], - rstn_data->limit[STC_RSTN_LIMIT_TYPE_MONTHLY], - rstn_data->limit[STC_RSTN_LIMIT_TYPE_WEEKLY], - rstn_data->limit[STC_RSTN_LIMIT_TYPE_DAILY]); - STC_LOGI("data_counter[%lld] warn_counter[%lld] " - "monthly_counter[%lld] weekly_counter[%lld] " - "daily_counter[%lld]", - rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA], - rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN], - rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY], - rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY], - rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY]); + if (STC_DEBUG_LOG && STC_RSTN_LOG) { + STC_LOGI("RstnID[%llu] AppID[%s] classid[%u] " + "iftype[%d] ifname[%s] rstn_state[%d] " + "rstn_type[%d] roaming[%d] subscriber_id[%s]", + rstn_data->restriction_id, + rstn_data->app_id, rstn_data->classid, + rstn_data->iftype, rstn_data->ifname, + rstn_data->rstn_state, rstn_data->rstn_type, + rstn_data->roaming, rstn_data->subscriber_id); + STC_LOGI("month_start_date[%d] limit[%lld] " + "warn_limit[%lld] monthly_limit[%lld] " + "weekly_limit[%lld] daily_limit[%lld] ", + rstn_data->month_start_date, + rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA], + rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN], + rstn_data->limit[STC_RSTN_LIMIT_TYPE_MONTHLY], + rstn_data->limit[STC_RSTN_LIMIT_TYPE_WEEKLY], + rstn_data->limit[STC_RSTN_LIMIT_TYPE_DAILY]); + STC_LOGI("data_counter[%lld] warn_counter[%lld] " + "monthly_counter[%lld] weekly_counter[%lld] " + "daily_counter[%lld]", + rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA], + rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN], + rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY], + rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY], + rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY]); + } } static int __vconf_get_int(const char *key, int *value) @@ -487,11 +489,12 @@ static void __rstn_process(enum traffic_restriction_type rstn_type, if (conn == NULL || conn->ifname == NULL) continue; + connection = conn; + if (rstn_data->ifname != NULL && g_strcmp0(rstn_data->ifname, "") != 0 && - g_strcmp0(conn->ifname, rstn_data->ifname) == 0 && - g_strcmp0(conn->tether_iface.ifname, rstn_data->ifname) == 0) - connection = conn; + g_strcmp0(conn->ifname, rstn_data->ifname) != 0) + connection = NULL; } if (connection == NULL) @@ -671,12 +674,12 @@ static void __rstn_add(gpointer data, gpointer user_data) rstn_data->app_id, rstn_data, user_data); } - if (STC_DEBUG_LOG && STC_RSTN_LOG) { - __print_rstn(rstn_data); + __print_rstn(rstn_data); + + if (STC_DEBUG_LOG && STC_RSTN_LOG) STC_LOGD("\033[1;32mRestriction added\033[0;m " "[\033[1;36m%d\033[0;m:\033[1;35m%lld\033[0;m]", rstn_data->classid, rstn_data->restriction_id); - } } static void __rstn_add_by_connection(gpointer key, @@ -703,12 +706,12 @@ static void __rstn_remove(gpointer data, gpointer user_data) __rstn_tethering_process(RST_UNSET, rstn_data->app_id, rstn_data, user_data); - if (STC_DEBUG_LOG && STC_RSTN_LOG) { - __print_rstn(rstn_data); + __print_rstn(rstn_data); + + if (STC_DEBUG_LOG && STC_RSTN_LOG) STC_LOGD("\033[1;31mRestriction removed\033[0;m " "[\033[1;36m%d\033[0;m:\033[1;35m%lld\033[0;m]", rstn_data->classid, rstn_data->restriction_id); - } } static void __rstn_remove_by_connection(gpointer key, @@ -770,6 +773,12 @@ static gint __rstn_data_comp(gconstpointer a, gconstpointer b) return -1; } +static void __rstn_foreach_print(gpointer data, gpointer user_data) +{ + stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data; + __print_rstn(rstn_data); +} + static stc_error_e __rstn_data_remove(stc_rstn_data_s *data) { stc_rstn_value_s *lookup_value; @@ -808,6 +817,8 @@ static stc_error_e __rstn_data_remove(stc_rstn_data_s *data) lookup_data); __rstn_data_destroy(lookup_data); + g_slist_foreach(lookup_value->rules, __rstn_foreach_print, NULL); + if (!lookup_value->rules) g_hash_table_remove(rstns, GUINT_TO_POINTER(data->classid)); @@ -820,25 +831,18 @@ static stc_error_e __rstn_data_add(stc_rstn_data_s *data) stc_rstn_value_s *lookup_value; stc_rstn_value_s *rstn_value; stc_rstn_data_s *rstn_data; + GSList *lookup_list; GHashTable *rstns = stc_monitor_get_system_rstns(); if (!rstns) return STC_ERROR_NO_DATA; - rstn_data = MALLOC0(stc_rstn_data_s, 1); - if (!rstn_data) { - if (STC_DEBUG_LOG && STC_RSTN_LOG) - STC_LOGE("Rstn_data allocation failed"); - return STC_ERROR_OUT_OF_MEMORY; - } - lookup_value = g_hash_table_lookup(rstns, GUINT_TO_POINTER(data->classid)); if (!lookup_value) { rstn_value = MALLOC0(stc_rstn_value_s, 1); if (!rstn_value) { if (STC_DEBUG_LOG && STC_RSTN_LOG) STC_LOGE("Rstn_value allocation failed"); - FREE(rstn_data); return STC_ERROR_OUT_OF_MEMORY; } @@ -848,12 +852,31 @@ static stc_error_e __rstn_data_add(stc_rstn_data_s *data) rstn_value = lookup_value; } + lookup_list = g_slist_find_custom(rstn_value->rules, + data, __rstn_data_comp); + if (!lookup_list) { + rstn_data = MALLOC0(stc_rstn_data_s, 1); + if (!rstn_data) { + if (STC_DEBUG_LOG && STC_RSTN_LOG) + STC_LOGE("Rstn_data allocation failed"); + return STC_ERROR_OUT_OF_MEMORY; + } + + rstn_value->rules = g_slist_append(rstn_value->rules, rstn_data); + } else { + rstn_data = lookup_list->data; + } + rstn_data->classid = data->classid; + FREE(rstn_data->app_id); rstn_data->app_id = g_strdup(data->app_id); rstn_data->iftype = data->iftype; + FREE(rstn_data->ifname); rstn_data->ifname = g_strdup(data->ifname); + FREE(rstn_data->subscriber_id); rstn_data->subscriber_id = g_strdup(data->subscriber_id); rstn_data->roaming = data->roaming; + FREE(rstn_data->mac); rstn_data->mac = g_strdup(data->mac); rstn_data->restriction_id = data->restriction_id; @@ -870,11 +893,10 @@ static stc_error_e __rstn_data_add(stc_rstn_data_s *data) rstn_data->month_start_date = data->month_start_date; rstn_data->month_start_ts = data->month_start_ts; - __rstn_data_remove(rstn_data); - rstn_value->rules = g_slist_append(rstn_value->rules, rstn_data); - __rstn_add(rstn_data, NULL); + g_slist_foreach(rstn_value->rules, __rstn_foreach_print, NULL); + return STC_ERROR_NONE; } -- 2.7.4 From f96096d10de30f2feae573ba758d8a8277a2f607 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 19 Mar 2019 16:21:14 +0900 Subject: [PATCH 11/16] Change to operate in on-demand mode Change-Id: Iea3e5df460e906e6790d66f65e9db338667a1c13 Signed-off-by: hyunuktak --- data/exceptions | 1 + include/stc-manager.h | 4 ++++ packaging/stc-manager.spec | 4 ++++ resources/dbus/net.stc-manager.service | 5 ++++ resources/systemd/stc-manager.service | 4 ++-- src/stc-firewall.c | 26 +++++++++++++++++++++ src/stc-manager-gdbus.c | 6 +++++ src/stc-manager.c | 42 ++++++++++++++++++++++++++-------- src/stc-pcap.c | 8 +++++++ src/stc-restriction.c | 14 ++++++++++++ src/stc-statistics.c | 8 +++++++ 11 files changed, 111 insertions(+), 11 deletions(-) create mode 100755 resources/dbus/net.stc-manager.service diff --git a/data/exceptions b/data/exceptions index ef89dd3..c3d4cbe 100644 --- a/data/exceptions +++ b/data/exceptions @@ -28,6 +28,7 @@ ps:inst wlan.sh:script wpa_supp.sh:script stc-manager:sys +stc-iptables:sys dlogutil:sys wifi-loader:sys wrt-loader:sys diff --git a/include/stc-manager.h b/include/stc-manager.h index 82d8747..bc2047f 100755 --- a/include/stc-manager.h +++ b/include/stc-manager.h @@ -236,6 +236,9 @@ typedef struct { typedef struct { GMainLoop *main_loop; + guint timer; + gboolean keep_alive; + gboolean ondemand_mode; gpointer statistics_obj; gpointer restriction_obj; @@ -254,5 +257,6 @@ typedef struct { stc_s *stc_get_manager(void); void stc_stop_manager(void); int stc_commit_iptables(char *cmd, int *err_num, char **err_str); +void stc_set_keep_alive(gboolean keep_alive); #endif /* __STC_MANAGER__ */ diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 11549fe..b86122b 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -136,6 +136,9 @@ ln -s ../stc-manager.service %{buildroot}%{_libdir}/systemd/system/multi-user.ta mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/system.d cp resources/dbus/stc-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/stc-manager.conf +mkdir -p %{buildroot}%{_datadir}/dbus-1/system-services/ +cp resources/dbus/net.stc-manager.service %{buildroot}%{_datadir}/dbus-1/system-services/net.stc-manager.service + %files %manifest %{name}.manifest %license LICENSE @@ -150,6 +153,7 @@ cp resources/dbus/stc-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/st #DBus DAC %attr(644,root,root) %{_sysconfdir}/dbus-1/system.d/* +%attr(644,root,root) %{_datadir}/dbus-1/system-services/* %if %{?enable_database} == YES %config(noreplace) %attr(660, root, root) %{database_full_path} diff --git a/resources/dbus/net.stc-manager.service b/resources/dbus/net.stc-manager.service new file mode 100755 index 0000000..13f4097 --- /dev/null +++ b/resources/dbus/net.stc-manager.service @@ -0,0 +1,5 @@ +[D-BUS Service] +Name=net.stc +Exec=/bin/false +User=root +SystemdService=stc-manager.service diff --git a/resources/systemd/stc-manager.service b/resources/systemd/stc-manager.service index 70a9eef..e3bce9f 100644 --- a/resources/systemd/stc-manager.service +++ b/resources/systemd/stc-manager.service @@ -4,10 +4,10 @@ Requires=dbus.socket After=dbus.socket [Service] -Type=forking +Type=dbus +BusName=net.stc SmackProcessLabel=System ExecStart=/usr/bin/stc-manager -Restart=always [Install] WantedBy=multi-user.target diff --git a/src/stc-firewall.c b/src/stc-firewall.c index bc71cc9..c13d406 100755 --- a/src/stc-firewall.c +++ b/src/stc-firewall.c @@ -47,6 +47,8 @@ gboolean handle_firewall_lock(StcFirewall *object, { __STC_LOG_FUNC_ENTER__; + stc_set_keep_alive(TRUE); + stc_plugin_firewall_lock(); STC_DBUS_REPLY_ERROR_NONE(invocation); @@ -60,6 +62,8 @@ gboolean handle_firewall_unlock(StcFirewall *object, { __STC_LOG_FUNC_ENTER__; + stc_set_keep_alive(TRUE); + stc_plugin_firewall_unlock(); STC_DBUS_REPLY_ERROR_NONE(invocation); @@ -75,6 +79,8 @@ gboolean handle_firewall_get_lock(StcFirewall *object, GVariant *return_parameters = NULL; int state = 0; + stc_set_keep_alive(TRUE); + stc_plugin_firewall_get_lock(&state); return_parameters = g_variant_new("(i)", state); @@ -91,6 +97,8 @@ gboolean handle_firewall_add_chain(StcFirewall *object, __STC_LOG_FUNC_ENTER__; int ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + if (chain == NULL) { STC_FIREWALL_DBUS_REPLY_ERROR(invocation, STC_ERROR_INVALID_PARAMETER); @@ -118,6 +126,8 @@ gboolean handle_firewall_remove_chain(StcFirewall *object, __STC_LOG_FUNC_ENTER__; int ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + if (chain == NULL) { STC_FIREWALL_DBUS_REPLY_ERROR(invocation, STC_ERROR_INVALID_PARAMETER); @@ -145,6 +155,8 @@ gboolean handle_firewall_flush_chain(StcFirewall *object, __STC_LOG_FUNC_ENTER__; int ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + if (chain == NULL) { STC_FIREWALL_DBUS_REPLY_ERROR(invocation, STC_ERROR_INVALID_PARAMETER); @@ -172,6 +184,8 @@ gboolean handle_firewall_get_all_chain(StcFirewall *object, GVariantBuilder *builder = NULL; GVariant *return_parameters = NULL; + stc_set_keep_alive(TRUE); + builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}")); stc_plugin_firewall_get_all_chain(builder); @@ -194,6 +208,8 @@ gboolean handle_firewall_set_chain(StcFirewall *object, __STC_LOG_FUNC_ENTER__; int ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + if (chain == NULL || target >= STC_FW_CHAIN_TARGET_MAX) { STC_FIREWALL_DBUS_REPLY_ERROR(invocation, @@ -222,6 +238,8 @@ gboolean handle_firewall_unset_chain(StcFirewall *object, __STC_LOG_FUNC_ENTER__; int ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + if (chain == NULL) { STC_FIREWALL_DBUS_REPLY_ERROR(invocation, STC_ERROR_INVALID_PARAMETER); @@ -249,6 +267,8 @@ gboolean handle_firewall_add_rule(StcFirewall *object, __STC_LOG_FUNC_ENTER__; int ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + ret = stc_plugin_firewall_add_rule(parameters); if (ret != STC_ERROR_NONE) { STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret); @@ -269,6 +289,8 @@ gboolean handle_firewall_remove_rule(StcFirewall *object, __STC_LOG_FUNC_ENTER__; int ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + ret = stc_plugin_firewall_remove_rule(parameters); if (ret != STC_ERROR_NONE) { STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret); @@ -289,6 +311,8 @@ gboolean handle_firewall_update_rule(StcFirewall *object, __STC_LOG_FUNC_ENTER__; int ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + ret = stc_plugin_firewall_update_rule(parameters); if (ret != STC_ERROR_NONE) { STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret); @@ -309,6 +333,8 @@ gboolean handle_firewall_get_all_rule(StcFirewall *object, GVariantBuilder *builder = NULL; GVariant *return_parameters = NULL; + stc_set_keep_alive(TRUE); + builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}")); stc_plugin_firewall_get_all_rule(builder); diff --git a/src/stc-manager-gdbus.c b/src/stc-manager-gdbus.c index 7990287..943f466 100755 --- a/src/stc-manager-gdbus.c +++ b/src/stc-manager-gdbus.c @@ -568,6 +568,8 @@ gboolean handle_manager_stop(StcManager *object, __STC_LOG_FUNC_ENTER__; GVariant *return_parameters = NULL; + stc_set_keep_alive(TRUE); + STC_LOGI("stc manager stop"); return_parameters = g_variant_new("(i)", STC_ERROR_NONE); @@ -593,6 +595,8 @@ gboolean handle_manager_commit_iptables(StcManager *object, char *err_str = NULL; char cmd[STC_CMD_SIZE] = { 0, }; + stc_set_keep_alive(TRUE); + if (option == NULL) { STC_MANAGER_DBUS_REPLY_ERROR(invocation, STC_ERROR_INVALID_PARAMETER); @@ -626,6 +630,8 @@ gboolean handle_manager_commit_ip6tables(StcManager *object, char *err_str = NULL; char cmd[STC_CMD_SIZE] = { 0, }; + stc_set_keep_alive(TRUE); + if (option == NULL) { STC_MANAGER_DBUS_REPLY_ERROR(invocation, STC_ERROR_INVALID_PARAMETER); diff --git a/src/stc-manager.c b/src/stc-manager.c index 5e5bcef..a376e7f 100755 --- a/src/stc-manager.c +++ b/src/stc-manager.c @@ -126,12 +126,16 @@ static stc_s *__stc_manager_init(void) return NULL; //LCOV_EXCL_LINE } + g_stc->ondemand_mode = TRUE; + stc_plugin_appstatus_init(); stc_plugin_exception_init(); stc_plugin_procfs_init(); stc_plugin_tether_init(); - stc_plugin_pcap_init(); - stc_plugin_monitor_init(); + if (stc_plugin_pcap_init() == STC_ERROR_NONE) + g_stc->ondemand_mode = FALSE; + if (stc_plugin_monitor_init() == STC_ERROR_NONE) + g_stc->ondemand_mode = FALSE; stc_plugin_firewall_init(); stc_plugin_procfs_load_pid(); @@ -143,6 +147,18 @@ static stc_s *__stc_manager_init(void) return stc; } +static gboolean __stc_timer_expired(gpointer data) +{ + if (g_stc->keep_alive) { + g_stc->keep_alive = FALSE; + return TRUE; + } + + g_main_loop_quit(g_stc->main_loop); + + return FALSE; +} + API stc_s *stc_get_manager(void) { return g_stc; @@ -220,10 +236,14 @@ int stc_commit_iptables(char *cmd, int *err_num, char **err_str) return STC_ERROR_FAIL; } +void stc_set_keep_alive(gboolean keep_alive) +{ + g_stc->keep_alive = keep_alive; +} + gint32 main(gint32 argc, gchar *argv[]) { GMainLoop *main_loop = NULL; - gint32 ret = -1; STC_LOGI("Smart Traffic Control Manager"); @@ -231,33 +251,37 @@ gint32 main(gint32 argc, gchar *argv[]) setenv("GCOV_PREFIX", "/tmp/daemon", 1); #endif +/* if (daemon(0, 0) != 0) STC_LOGE("Can't start daemon"); //LCOV_EXCL_LINE +*/ /* Initialize required subsystems */ #if !GLIB_CHECK_VERSION(2, 35, 0) g_type_init(); #endif - /* Crate the GLIB main loop */ - main_loop = g_main_loop_new(NULL, FALSE); - g_stc = __stc_manager_init(); if (!g_stc) goto fail; + /* Crate the GLIB main loop */ + main_loop = g_main_loop_new(NULL, FALSE); g_stc->main_loop = main_loop; + if (g_stc->ondemand_mode) { + g_stc->timer = g_timeout_add_seconds(10, __stc_timer_expired, NULL); + g_stc->keep_alive = FALSE; + } + /* Run the main loop */ g_main_loop_run(main_loop); - ret = 0; - fail: __stc_manager_deinit(); if (main_loop) g_main_loop_unref(main_loop); - return ret; + return 0; } diff --git a/src/stc-pcap.c b/src/stc-pcap.c index 91dd1e2..3faa7ec 100755 --- a/src/stc-pcap.c +++ b/src/stc-pcap.c @@ -91,6 +91,8 @@ gboolean handle_pcap_start(StcPcap *object, stc_pcap_s pcap; int ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + memset(&pcap, 0, sizeof(stc_pcap_s)); g_variant_get(parameters, "a{sv}", &iter); @@ -130,6 +132,8 @@ gboolean handle_pcap_stop(StcPcap *object, stc_pcap_s pcap; int ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + memset(&pcap, 0, sizeof(stc_pcap_s)); g_variant_get(parameters, "a{sv}", &iter); @@ -167,6 +171,8 @@ gboolean handle_pcap_get_all(StcPcap *object, GVariantBuilder *builder = NULL; GVariant *return_parameters = NULL; + stc_set_keep_alive(TRUE); + builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}")); stc_plugin_pcap_get_all_loop(builder); @@ -188,6 +194,8 @@ gboolean handle_pcap_find_all_devs(StcPcap *object, GVariantBuilder *builder = NULL; GVariant *return_parameters = NULL; + stc_set_keep_alive(TRUE); + builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}")); stc_plugin_pcap_find_all_devs(builder); diff --git a/src/stc-restriction.c b/src/stc-restriction.c index d49eb42..d6bae55 100755 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -269,6 +269,8 @@ gboolean handle_restriction_set(StcRestriction *object, table_restrictions_info rule; stc_error_e ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + memset(&rule, 0, sizeof(table_restrictions_info)); __initialize_rstn_rule(&rule); @@ -311,6 +313,8 @@ gboolean handle_restriction_unset(StcRestriction *object, table_restrictions_info rule; stc_error_e ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + memset(&rule, 0, sizeof(table_restrictions_info)); __initialize_rstn_rule(&rule); @@ -354,6 +358,8 @@ gboolean handle_restriction_set_list(StcRestriction *object, GVariantIter *iter_row = NULL; stc_error_e ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + g_variant_get(parameters, "aa{sv}", &iter); while (g_variant_iter_next(iter, "a{sv}", &iter_row)) { table_restrictions_info rule; @@ -403,6 +409,8 @@ gboolean handle_restriction_unset_list(StcRestriction *object, GVariantIter *iter_row = NULL; stc_error_e ret = STC_ERROR_NONE; + stc_set_keep_alive(TRUE); + g_variant_get(parameters, "aa{sv}", &iter); while (g_variant_iter_next(iter, "a{sv}", &iter_row)) { table_restrictions_info rule; @@ -453,6 +461,8 @@ gboolean handle_restriction_get(StcRestriction *object, GVariant *return_parameters = NULL; stc_error_e ret; + stc_set_keep_alive(TRUE); + builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); ret = table_restrictions_per_app(app_id, @@ -483,6 +493,8 @@ gboolean handle_restriction_get_all(StcRestriction *object, GVariant *return_parameters = NULL; stc_error_e ret; + stc_set_keep_alive(TRUE); + builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}")); ret = table_restrictions_foreach(__table_restrictions_foreach_app_cb, @@ -514,6 +526,8 @@ gboolean handle_restriction_get_type(StcRestriction *object, stc_rstn_type_e type = STC_RSTN_TYPE_UNKNOWN; stc_error_e ret; + stc_set_keep_alive(TRUE); + ret = table_restrictions_get_restriction_type(app_id, iftype, &type); if (ret < STC_ERROR_NONE) { STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE diff --git a/src/stc-statistics.c b/src/stc-statistics.c index f2ff8e9..0d5a761 100755 --- a/src/stc-statistics.c +++ b/src/stc-statistics.c @@ -247,6 +247,8 @@ gboolean handle_statistics_get_all(StcStatistics *object, GVariant *return_parameters = NULL; stc_error_e ret; + stc_set_keep_alive(TRUE); + /* Total statistics since epoch */ rule.from = epoch; rule.to = cur_time; @@ -294,6 +296,8 @@ gboolean handle_statistics_init(StcStatistics *object, { __STC_LOG_FUNC_ENTER__; + stc_set_keep_alive(TRUE); + STC_LOGI("stc statistics initialized"); stc_statistics_complete_init(object, invocation); @@ -315,6 +319,8 @@ gboolean handle_statistics_get(StcStatistics *object, GVariant *return_parameters = NULL; stc_error_e ret; + stc_set_keep_alive(TRUE); + /* Total statistics since epoch */ rule.from = epoch; rule.to = cur_time; @@ -367,6 +373,8 @@ gboolean handle_statistics_reset(StcStatistics *object, GVariant *return_parameters = NULL; stc_error_e ret; + stc_set_keep_alive(TRUE); + if (reset_rule != NULL) { DEBUG_GDBUS_VARIANT("Selection rule: ", reset_rule); GVariantIter *iter = NULL; -- 2.7.4 From 3f428d201241eed5bddb4d7ad28d8d554daaec0b Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Mon, 13 May 2019 11:35:48 +0900 Subject: [PATCH 12/16] Add monitor plugin requires for install dependency Change-Id: I91be1576eeaf46259c953b7fbc1430c90c4f03ee Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index b86122b..fbcc37b 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.91 +Version: 0.0.92 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 @@ -40,6 +40,7 @@ A smart traffic control manager to manage traffic counting and bandwidth limitat Summary: STC manager application status plugin BuildRequires: pkgconfig(bundle) BuildRequires: pkgconfig(syspopup-caller) +Requires: stc-manager-plugin-monitor %description plugin-appstatus A smart traffic control manager extension for application status plugin @@ -47,12 +48,14 @@ A smart traffic control manager extension for application status plugin %package plugin-exception Summary: STC manager exception list plugin BuildRequires: pkgconfig(pkgmgr-info) +Requires: stc-manager-plugin-monitor %description plugin-exception A smart traffic control manager extension for exception list plugin %package plugin-procfs Summary: STC manager exception proc file system plugin +Requires: stc-manager-plugin-monitor %description plugin-procfs A smart traffic control manager extension for proc file system plugin @@ -66,6 +69,7 @@ A smart traffic control manager extension for packet capture plugin %package plugin-tether Summary: Tethering plugin for data usage of tethering clients +Requires: stc-manager-plugin-monitor %description plugin-tether A smart traffic control manager extension for tethering client data usage plugin -- 2.7.4 From d86257fc63b36492d2675bd6063935bf2badca0b Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 21 May 2019 11:25:31 +0900 Subject: [PATCH 13/16] Remove realpath check to create file Change-Id: I9b9c80aaad07f403c8712aa1ce2b4770450f6aba Signed-off-by: hyunuktak --- src/helper/helper-file.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/helper/helper-file.c b/src/helper/helper-file.c index 53dcad0..e2f284f 100755 --- a/src/helper/helper-file.c +++ b/src/helper/helper-file.c @@ -23,15 +23,10 @@ int fwrite_str(const char *path, const char *str) { _cleanup_fclose_ FILE *f = NULL; int ret; - char * t; assert(path); assert(str); - t = realpath(path, NULL); - ret_value_if(!t, -errno); - free(t); - f = fopen(path, "w"); ret_value_if(!f, -errno); -- 2.7.4 From 0ce9aca0bd562d6baa82e9a936d461b6238851f8 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Fri, 24 May 2019 17:14:09 +0900 Subject: [PATCH 14/16] Remove gvariant free for dauble free Change-Id: Ia3ed9d4ae536019ec735aaa02643a1258d2cb826 Signed-off-by: hyunuktak --- packaging/stc-manager.spec | 2 +- plugin/monitor/stc-plugin-monitor-connection.c | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index fbcc37b..55396af 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.92 +Version: 0.0.93 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/plugin/monitor/stc-plugin-monitor-connection.c b/plugin/monitor/stc-plugin-monitor-connection.c index 010a6f2..1988a43 100755 --- a/plugin/monitor/stc-plugin-monitor-connection.c +++ b/plugin/monitor/stc-plugin-monitor-connection.c @@ -445,10 +445,6 @@ static stc_error_e __get_connected_profiles(GDBusConnection *connection) } g_connection_list = g_slist_append(g_connection_list, conn); - - g_free(object_path); - g_variant_iter_free(next); - continue; } } -- 2.7.4 From 6e941f446a33913f02b02ffa42836dc4e48936ce Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Tue, 9 Jul 2019 14:53:05 +0530 Subject: [PATCH 15/16] Add support of app_id in GetAll method call on statistics interface Change-Id: I8afdfedd60ee2ce1b6708eec4034119dd405f85a Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/database/include/table-statistics.h | 3 ++- src/database/tables/table-statistics.c | 12 +++++++++--- src/stc-statistics.c | 9 ++++++++- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 55396af..4228bd8 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.93 +Version: 0.0.94 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/database/include/table-statistics.h b/src/database/include/table-statistics.h index 284fb48..a510f32 100755 --- a/src/database/include/table-statistics.h +++ b/src/database/include/table-statistics.h @@ -37,6 +37,7 @@ typedef struct { } table_statistics_reset_rule; typedef struct { + char *app_id; time_t from; time_t to; stc_iface_type_e iftype; @@ -55,7 +56,7 @@ stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rul void *user_data); stc_error_e table_statistics_per_app(const char *app_id, - table_statistics_select_rule *rule, + const table_statistics_select_rule *rule, table_statistics_info_cb info_cb, void *user_data); diff --git a/src/database/tables/table-statistics.c b/src/database/tables/table-statistics.c index 719b9a2..7d13a3f 100755 --- a/src/database/tables/table-statistics.c +++ b/src/database/tables/table-statistics.c @@ -440,13 +440,19 @@ stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rul bind parameters in the query */ stc_db_tm_interval_s interval; - memset(&data, 0, sizeof(data)); - if (!rule || !info_cb) { __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } + memset(&data, 0, sizeof(data)); + + if (rule->app_id) { + int ret = table_statistics_per_app(rule->app_id, rule, info_cb, user_data); + __STC_LOG_FUNC_EXIT__; + return ret; + } + /* pick a statement depending on parameters */ if (rule->granularity) { stmt = __is_iftype_defined(rule->iftype) ? @@ -508,7 +514,7 @@ handle_error: } API stc_error_e table_statistics_per_app(const char *app_id, - table_statistics_select_rule *rule, + const table_statistics_select_rule *rule, table_statistics_info_cb info_cb, void *user_data) { diff --git a/src/stc-statistics.c b/src/stc-statistics.c index 0d5a761..9c0559b 100755 --- a/src/stc-statistics.c +++ b/src/stc-statistics.c @@ -76,6 +76,11 @@ void __stc_extract_select_rule(const char *key, GVariant *value, rule->iftype = g_variant_get_uint16(value); STC_LOGD("iftype: [%u]", (unsigned int) rule->iftype); + } else if (!g_strcmp0(key, "app_id")) { + gsize length = 0; + rule->app_id = g_variant_dup_string(value, &length); + STC_LOGD("app_id: [%s]", rule->app_id); + } else { STC_LOGD("Unknown select rule"); //LCOV_EXCL_LINE } @@ -277,6 +282,7 @@ gboolean handle_statistics_get_all(StcStatistics *object, if (ret < STC_ERROR_NONE) { g_variant_builder_unref(builder); //LCOV_EXCL_LINE STC_STATISTICS_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE + g_free(rule.app_id); __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE return TRUE; //LCOV_EXCL_LINE } @@ -287,12 +293,13 @@ gboolean handle_statistics_get_all(StcStatistics *object, DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters); STC_DBUS_REPLY(invocation, return_parameters); + g_free(rule.app_id); __STC_LOG_FUNC_EXIT__; return TRUE; } gboolean handle_statistics_init(StcStatistics *object, - GDBusMethodInvocation *invocation) + GDBusMethodInvocation *invocation) { __STC_LOG_FUNC_ENTER__; -- 2.7.4 From 3a79f8e428cc94e355de1248f37c021c6e9c45f8 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Tue, 9 Jul 2019 17:19:15 +0530 Subject: [PATCH 16/16] Add granularity support in GetAll method call on statistics interface Change-Id: Ic70dfb6aa08365175180d8d9f61092022b6641a8 Signed-off-by: Nishant Chaprana --- packaging/stc-manager.spec | 2 +- src/stc-statistics.c | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 4228bd8..f516dff 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.94 +Version: 0.0.95 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/stc-statistics.c b/src/stc-statistics.c index 9c0559b..a0b5a07 100755 --- a/src/stc-statistics.c +++ b/src/stc-statistics.c @@ -81,6 +81,10 @@ void __stc_extract_select_rule(const char *key, GVariant *value, rule->app_id = g_variant_dup_string(value, &length); STC_LOGD("app_id: [%s]", rule->app_id); + } else if (!g_strcmp0(key, "granularity")) { + rule->granularity = g_variant_get_int32(value); + STC_LOGD("granularity: [%d]", rule->granularity); + } else { STC_LOGD("Unknown select rule"); //LCOV_EXCL_LINE } -- 2.7.4