Skip to content

Commit

Permalink
remove unrecoginized service
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengyuli committed May 5, 2015
1 parent 3e46b05 commit 096109f
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 185 deletions.
21 changes: 8 additions & 13 deletions src/app_service/app_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,16 @@ newAppService (char *ip, u_short port, char *proto) {
if (svc == NULL)
return NULL;

if (proto) {
analyzer = getProtoAnalyzer (proto);
if (analyzer == NULL) {
LOGE ("Unsupported appService proto type: %s.\n", proto);
free (svc);
return NULL;
}

svc->proto = analyzer->proto;
svc->analyzer = analyzer;
} else {
svc->proto = "UNKNOWN";
svc->analyzer = NULL;
analyzer = getProtoAnalyzer (proto);
if (analyzer == NULL) {
LOGE ("Unsupported appService proto type: %s.\n", proto);
free (svc);
return NULL;
}

svc->proto = analyzer->proto;
svc->analyzer = analyzer;

svc->ip = strdup (ip);
if (svc->ip == NULL) {
svc->analyzer = NULL;
Expand Down
118 changes: 1 addition & 117 deletions src/app_service/app_service_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ static pthread_rwlock_t appServiceDetectedHashTableRWLock;
/* AppService detected hash table */
static hashTablePtr appServiceDetectedHashTable = NULL;

/* AppService unrecognized hash table rwlock */
static pthread_rwlock_t appServiceUnrecognizedHashTableRWLock;
/* AppService unrecognized hash table */
static hashTablePtr appServiceUnrecognizedHashTable = NULL;

/**
* @brief Get appService proto analyzer.
* Get appService proto analyzer from appService map.
Expand Down Expand Up @@ -90,29 +85,6 @@ getAppServiceDetected (char *ip, u_short port) {
return svc;
}

/**
* @brief Get appService unrecognized.
* Get appService unrecognized from appService unrecognized map.
*
* @param ip service ip
* @param port service port
*
* @return appService unrecognized if success, else NULL
*/
appServicePtr
getAppServiceUnrecognized (char *ip, u_short port) {
char key [32];
appServicePtr svc;

snprintf (key, sizeof (key), "%s:%u", ip, port);
pthread_rwlock_rdlock (&appServiceUnrecognizedHashTableRWLock);
svc = (appServicePtr) hashLookup (appServiceUnrecognizedHashTable, key);
pthread_rwlock_unlock (&appServiceUnrecognizedHashTableRWLock);

return svc;
}


/**
* @brief Get appServices padding filter.
* Get appServices padding filter to pause packet sniff.
Expand Down Expand Up @@ -259,40 +231,6 @@ getJsonFromAppServicesDetected (void) {
return root;
}

/**
* @brief Get json from all appServices unrecognized.
* Get json from appService unrecognized map, it will
* loop all appServices unrecognized and get json from
* each.
*
* @return json object if success, else NULL
*/
json_t *
getJsonFromAppServicesUnrecognized (void) {
int ret;
json_t *root;

root = json_array ();
if (root == NULL) {
LOGE ("Create json array object error.\n");
return NULL;
}

pthread_rwlock_rdlock (&appServiceUnrecognizedHashTableRWLock);
ret = hashLoopDo (appServiceUnrecognizedHashTable,
getJsonForEachAppService,
root);
pthread_rwlock_unlock (&appServiceUnrecognizedHashTableRWLock);

if (ret < 0) {
LOGE ("Get appServices unrecognized json from each appService unrecognized error.\n");
json_object_clear (root);
return NULL;
}

return root;
}

/**
* @brief Get appServices from json.
* Get appServices from json array, it will parse appServices
Expand Down Expand Up @@ -657,38 +595,6 @@ addAppServiceDetected (char *ip, u_short port, char *proto) {
return 0;
}

/**
* @brief Add appService unrecognized to appService unrecognized map.
*
* @param ip appService unrecognized ip
* @param port appService unrecognized port
*
* @return 0 if success, else -1
*/
int
addAppServiceUnrecognized (char *ip, u_short port) {
int ret;
appServicePtr svc;
char key [32];

/* Add to appService detected map */
svc = newAppService (ip, port, NULL);
if (svc == NULL) {
LOGE ("Create unrecognized appService %s:%u error\n", ip, port);
return -1;
}
snprintf (key, sizeof (key), "%s:%u", ip, port);
pthread_rwlock_wrlock (&appServiceUnrecognizedHashTableRWLock);
ret = hashInsert (appServiceUnrecognizedHashTable, key, svc, freeAppServiceForHash);
pthread_rwlock_unlock (&appServiceUnrecognizedHashTableRWLock);
if (ret < 0) {
LOGE ("Insert unrecognized appService %s error\n", key);
return -1;
}

return 0;
}

/* Init appService manager */
int
initAppServiceManager (void) {
Expand Down Expand Up @@ -724,31 +630,14 @@ initAppServiceManager (void) {
goto destroyAppServiceDetectedHashTableRWLock;
}

ret = pthread_rwlock_init (&appServiceUnrecognizedHashTableRWLock, NULL);
if (ret) {
LOGE ("Init appServiceUnrecognizedHashTableRWLock error.\n");
goto destroyAppServiceDetectedHashTable;
}

appServiceUnrecognizedHashTable = hashNew (0);
if (appServiceUnrecognizedHashTable == NULL) {
LOGE ("Create appServiceUnrecognizedHashTable error.\n");
goto destroyAppServiceUnrecognizedHashTableRWLock;
}

ret = updateAppServicesFromCache ();
if (ret < 0) {
LOGE ("Update appServices from cache error.\n");
goto destroyAppServiceUnrecognizedHashTable;
goto destroyAppServiceDetectedHashTable;
}

return 0;

destroyAppServiceUnrecognizedHashTable:
hashDestroy (appServiceUnrecognizedHashTable);
appServiceUnrecognizedHashTable = NULL;
destroyAppServiceUnrecognizedHashTableRWLock:
pthread_rwlock_destroy (&appServiceUnrecognizedHashTableRWLock);
destroyAppServiceDetectedHashTable:
hashDestroy (appServiceDetectedHashTable);
appServiceDetectedHashTable = NULL;
Expand Down Expand Up @@ -780,11 +669,6 @@ destroyAppServiceManager (void) {
hashDestroy (appServiceDetectedHashTable);
appServiceDetectedHashTable = NULL;

/* Destroy appService unrecognized map */
pthread_rwlock_destroy (&appServiceUnrecognizedHashTableRWLock);
hashDestroy (appServiceUnrecognizedHashTable);
appServiceUnrecognizedHashTable = NULL;

/* Clean appServices cache */
remove (NTRACE_APP_SERVICES_CACHE);
}
6 changes: 0 additions & 6 deletions src/app_service/app_service_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ protoAnalyzerPtr
getAppServiceProtoAnalyzer (char *ip, u_short port);
appServicePtr
getAppServiceDetected (char *ip, u_short port);
appServicePtr
getAppServiceUnrecognized (char *ip, u_short port);
char *
getAppServicesPaddingFilter (void);
char *
Expand All @@ -20,15 +18,11 @@ json_t *
getJsonFromAppServices (void);
json_t *
getJsonFromAppServicesDetected (void);
json_t *
getJsonFromAppServicesUnrecognized (void);
int
updateAppServices (json_t * appServices);
int
addAppServiceDetected (char *ip, u_short port, char *proto);
int
addAppServiceUnrecognized (char *ip, u_short port);
int
initAppServiceManager (void);
void
destroyAppServiceManager (void);
Expand Down
24 changes: 0 additions & 24 deletions src/management/management_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,25 +187,6 @@ handleGetDetectedServicesInfoRequest (json_t *body) {
return 0;
}

/**
* @brief Get unrecognized services info request handler.
*
* @param body data to handle
*
* @return 0 if success else -1
*/
static int
handleGetUnrecognizedServicesInfoRequest (json_t *body) {
services = getJsonFromAppServicesUnrecognized ();
if (services == NULL) {
snprintf (errMsg, sizeof (errMsg), "Get unrecognized services info error.");
LOGE ("%s\n", errMsg);
return -1;
}

return 0;
}

/**
* @brief Get topology entries info request handler.
*
Expand Down Expand Up @@ -340,9 +321,6 @@ buildManagementResponse (char *cmd, int code) {
} else if (strEqual (cmd, MANAGEMENT_REQUEST_COMMAND_DETECTED_SERVICES_INFO)) {
json_object_set_new (body, MANAGEMENT_RESPONSE_BODY_DETECTED_SERVICES, services);
services = NULL;
} else if (strEqual (cmd, MANAGEMENT_REQUEST_COMMAND_UNRECOGNIZED_SERVICES_INFO)) {
json_object_set_new (body, MANAGEMENT_RESPONSE_BODY_UNRECOGNIZED_SERVICES, services);
services = NULL;
} else if (strEqual (cmd, MANAGEMENT_REQUEST_COMMAND_TOPOLOGY_ENTRIES_INFO)) {
json_object_set_new (body, MANAGEMENT_RESPONSE_BODY_TOPOLOGY_ENTRIES, topologyEntries);
topologyEntries = NULL;
Expand Down Expand Up @@ -429,8 +407,6 @@ managementService (void *args) {
ret = handleGetServicesInfoRequest (body);
else if (strEqual (MANAGEMENT_REQUEST_COMMAND_DETECTED_SERVICES_INFO, cmdStr))
ret = handleGetDetectedServicesInfoRequest (body);
else if (strEqual (MANAGEMENT_REQUEST_COMMAND_UNRECOGNIZED_SERVICES_INFO, cmdStr))
ret = handleGetUnrecognizedServicesInfoRequest (body);
else if (strEqual (MANAGEMENT_REQUEST_COMMAND_TOPOLOGY_ENTRIES_INFO, cmdStr))
ret = handleGetTopologyEntriesInfoRequest (body);
else if (strEqual (MANAGEMENT_REQUEST_COMMAND_UPDATE_SERVICES, cmdStr))
Expand Down
3 changes: 0 additions & 3 deletions src/management/management_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#define MANAGEMENT_REQUEST_COMMAND_PROTOS_INFO "protos_info"
#define MANAGEMENT_REQUEST_COMMAND_SERVICES_INFO "services_info"
#define MANAGEMENT_REQUEST_COMMAND_DETECTED_SERVICES_INFO "detected_services_info"
#define MANAGEMENT_REQUEST_COMMAND_UNRECOGNIZED_SERVICES_INFO "unrecognized_services_info"
#define MANAGEMENT_REQUEST_COMMAND_TOPOLOGY_ENTRIES_INFO "topology_entries_info"
#define MANAGEMENT_REQUEST_COMMAND_UPDATE_SERVICES "update_services"

Expand All @@ -40,8 +39,6 @@

#define MANAGEMENT_RESPONSE_BODY_DETECTED_SERVICES "detected_services"

#define MANAGEMENT_RESPONSE_BODY_UNRECOGNIZED_SERVICES "unrecognized_services"

#define MANAGEMENT_RESPONSE_BODY_TOPOLOGY_ENTRIES "topology_entries"

/* Default management error response */
Expand Down
14 changes: 1 addition & 13 deletions src/protocol/tcp_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,6 @@ delTcpStreamFromHash (tcpStreamPtr stream) {
(*tcpProcessCallback) (NULL);
}
}
} else {
/* Add appService unrecognized */
if (getAppServiceUnrecognized (ipDestStr, addr->dest) == NULL) {
ret = addAppServiceUnrecognized (ipDestStr, addr->dest);
if (ret < 0)
LOGE ("Add new unrecognized appService ip:%s port:%u error.\n",
ipDestStr, addr->dest);
else
LOGD ("Add new unrecognized appService ip:%s port:%u success.\n",
ipDestStr, addr->dest);
}
}
}

Expand Down Expand Up @@ -517,8 +506,7 @@ addNewTcpStream (tcphdrPtr tcph, iphdrPtr iph, timeValPtr tm) {
}

/* Skip service has been scanned */
if (getAppServiceDetected (ipDestStr, ntohs (tcph->dest)) ||
getAppServiceUnrecognized (ipDestStr, ntohs (tcph->dest)))
if (getAppServiceDetected (ipDestStr, ntohs (tcph->dest)))
return NULL;
} else {
analyzer = getAppServiceProtoAnalyzer (ipDestStr, ntohs (tcph->dest));
Expand Down
10 changes: 1 addition & 9 deletions tools/ntrace_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /usr/bin/env python
# Time-stamp: <2015-05-05 16:39:53 Tuesday by zhengyuli>
# Time-stamp: <2015-05-05 21:43:47 Tuesday by zhengyuli>
#
# Author: zhengyu li
# Created: 2015-05-02
Expand Down Expand Up @@ -51,11 +51,6 @@ def cmdDetectedServicesInfo(sock):
sock.send_json(req)
print sock.recv_string()

def cmdUnrecognizedServicesInfo(sock):
req = {'command':'unrecognized_services_info'}
sock.send_json(req)
print sock.recv_string()

def cmdTopologyEntriesInfo(sock):
req = {'command':'topology_entries_info'}
sock.send_json(req)
Expand Down Expand Up @@ -86,7 +81,6 @@ def cmdUpdateServices(sock):
"protosInfo",
"servicesInfo",
"detectedServicesInfo",
"unrecognizedServicesInfo",
"topologyEntriesInfo",
"updateServices"],
help="nTrace request command")
Expand Down Expand Up @@ -115,8 +109,6 @@ def cmdUpdateServices(sock):
cmdServicesInfo(zmqSock)
elif cmd == 'detectedServicesInfo':
cmdDetectedServicesInfo(zmqSock)
elif cmd == 'unrecognizedServicesInfo':
cmdUnrecognizedServicesInfo(zmqSock)
elif cmd == 'topologyEntriesInfo':
cmdTopologyEntriesInfo(zmqSock)
elif cmd == 'updateServices':
Expand Down

0 comments on commit 096109f

Please sign in to comment.