url_说明关系

​​​​​​

 
就像internet上面的邮政编号一样,在世界范围唯一标识并定位信息, URI包含两个子类,分别是URL和URN。 
URI - Uniform Resource Identifier  统一资源标识符             大类 
URL - Uniform Resource Location 统一资源定位符            小类  
URN - Uniform Resource Name 统一资源名称                   小类(现在还没有普及)
 
======================================================================
 
【出现前的情况:】
 
1.URL 出现前的黑暗岁月。
在web与url出现前,人们要靠杂乱的“应用程序”来访问分布在网络中的数据。大多数人不能够理解。也没有足够的耐心使用哪些应用程序例如:你要共享一个xls文件,传递到ftp等,( 时代在进步现代在的浏览器,都将上面的功能捆绑在一起了,提供的一个便捷包。通过url,就可以访问资源。使用者可能都不知道使用的是什么协议,方法等完全透明)
 
2.URL:统一资源定位符 (Uniform Resoure Locator:统一资源定位器)—不限于http协议
指定了一台特定服务器上面的某特定资源,例如 http://www.baidu.com/1.jsp,明确如何从一个精确固定的位置获取资源,url:并不是只对http协议。他是规范互联网资源的一种规范。
 
3.要想通过URL访问所有的资源必须的解决两个问题
1.信息的完整性
2.安全传输。
 
在url没出现之前。已经有很多协议,它们编码信息都是采用ascii信息。例如(simple mail transfer protocol smith) 使用7位二进制编码进行数据传输。
网上的资源还有新的要求,传递一些非西欧字符。这时候必须的将这些字符进行编码转换,转换的进行转义post 与get 方法都会对请求中的参数进行编码。原来理解只会对get后面的请求参数进行urlencode
 
======url中的特殊符号
访问一个网站的时候 www.baidu.com?123123123 ===========域名后面直接?----直接转换成为 www.baidu.com/?123123123( 百度测试)
#号, 连接页面的锚点
 
href:   "http://"必须加否者跳不出去。如果写 www.baidu.com 相当于链接本站的一个资源
 
 
 
 
 
 
 
 
 
 
 
 
static void *aucQuery(void *unused) { int ret = 0; struct url_info url_info; bool is_mali = false; #if SUPPORT_CLOUD_UPDATE_AUC_INFO struct auc_info auc_info; #endif unsigned int cat_id = 0; int tmp_id = 0; int index = 0; int index_cat = 0; int index_subcat = 0; unsigned char matched = 0; struct url_entry *tmp_list = NULL; struct url_entry *clean = NULL; unsigned char tmp_url_len; char tmp_url[MAX_URL_LEN] = {0}; printWar("create AUC query thread\n"); while (1) { pthread_mutex_lock(&url_lock); tmp_list = url_list.next; pthread_mutex_unlock(&url_lock); while (tmp_list) { pthread_mutex_lock(&url_lock); tmp_list->process_flag = URL_PROCESSING; pthread_mutex_unlock(&url_lock); url_info.url = tmp_list->url; url_info.info_len = 0; ret = pthread_mutex_trylock(&mali_url_check_lock); if (!ret) { is_mali = web_protection? is_url_malicious(url_info.url) : false; if (is_mali) { cat_id = URL_CAT_SECURITY; URL_CLASS_LOG(LOG_LEVEL_DEBUG, "Malicious URL detected. URL: %s, Original Cat ID: %d, Mapped Cat ID: %d", url_info.url, cat_id, cat_id); #if DEBUG printWar("cat ret=%d url=%s id=%d info_id=%d\n", cat_id, url_info.url, tmp_list->id, tmp_list->info_id); #endif /* DEBUG */ pthread_mutex_lock(&url_lock); tmp_list->prev->next = tmp_list->next; if (tmp_list->next) { tmp_list->next->prev = tmp_list->prev; } tmp_list->query = 1; tmp_id = tmp_list->id; tmp_list->id = 0; tmp_list->info_id = 0; tmp_list->cat_map = 0; tmp_url_len = tmp_list->url_len; memset(tmp_url, 0, MAX_URL_LEN); strncpy_safe(tmp_url, tmp_list->url, tmp_list->url_len); clean = tmp_list; tmp_list = tmp_list->next; clean->next = NULL; clean->prev = NULL; pthread_mutex_unlock(&url_lock); pthread_mutex_unlock(&mali_url_check_lock); write_to_csv(url_info.url, cat_id, cat_id, "Security"); goto block_mali; } else { pthread_mutex_unlock(&mali_url_check_lock); } } if((!opts.use_tp_service && tmp_list->cat_map) || (opts.use_tp_service && (tmp_list->info_id != PCTL_WEB_URL_ID_ALL) && (tmp_list->cat_map & (~URL_CAT_SECURITY)))) { printWar("before send to auc\n"); #if SUPPORT_CLOUD_UPDATE_AUC_INFO update_libauc_url(&auc_info, opts.use_tp_service); ret = auc_query(&url_info, &auc_info, opts.use_tp_service); #else ret = auc_query(&url_info, opts.use_tp_service); #endif printWar("after send to auc\n"); for (index = 0; index < url_info.info_len; index++) { printWar("cat ret=%d url=%s id=%d name=%s\n", ret, url_info.url, url_info.info[index].id, url_info.info[index].name); } } //remove from url_list pthread_mutex_lock(&url_lock); tmp_list->prev->next = tmp_list->next; if (tmp_list->next) { tmp_list->next->prev = tmp_list->prev; } tmp_list->query = 1; tmp_id = tmp_list->id; tmp_list->id = 0; tmp_list->info_id = 0; tmp_list->cat_map = 0; tmp_url_len = tmp_list->url_len; memset(tmp_url, 0, MAX_URL_LEN); strncpy_safe(tmp_url, tmp_list->url, tmp_list->url_len); clean = tmp_list; tmp_list = tmp_list->next; clean->next = NULL; clean->prev = NULL; pthread_mutex_unlock(&url_lock); //send to kernel cat_id = 0; int original_cat_id = 0; const char *category_name = ""; for (index = 0; index < url_info.info_len; index++) { original_cat_id = url_info.info[index].id; category_name = url_info.info[index].name; int cat_id = 0; // 每次循环初始化 cat_id URL_CLASS_LOG(LOG_LEVEL_DEBUG, "Non-malicious URL. URL: %s, Original Cat ID: %d, Mapped Cat ID: %d", url_info.url, original_cat_id, cat_id); for (index_cat = 0; mapping_table[index_cat][0] > 0; index_cat++) { matched = 0; for (index_subcat = 1; mapping_table[index_cat][index_subcat] >= 0; index_subcat++) { if (url_info.info[index].id == mapping_table[index_cat][index_subcat] || mapping_table[index_cat][index_subcat] == MAX_URL_CAT_DEFAULT) { cat_id |= 0x1 << (mapping_table[index_cat][0] - 1); matched = 1; break; } } if (matched) { break; } } // 将结果写入CSV文件 write_to_csv(url_info.url, original_cat_id, cat_id, category_name); } block_mali: printWar("send to kernel, url=%s cat_id=%x\n", url_info.url, cat_id); url_send(tmp_id, cat_id, tmp_url, tmp_url_len, URL_REQ_CAT); printWar("after send to kernel\n"); } cond_timedwait(500);//500ms } return 0; } 这段代码实现了将分类结果存储到csv文件中的功能,下面第二个代码没有保存的功能,请你根据第一个代码,在不改变第二个代码原本的功能前提下,添加保存到csv文件的功能 static void *aucQuery(void *unused) { int ret = 0; struct url_info url_info; bool is_mali = false; #if SUPPORT_CLOUD_UPDATE_AUC_INFO struct auc_info auc_info; struct auc_info web_protection_info; #endif unsigned int cat_id = 0; int tmp_id = 0; int index = 0; int index_cat = 0; int index_subcat = 0; unsigned char matched = 0; struct url_entry *tmp_list = NULL; struct url_entry *clean = NULL; #ifdef SUPPORT_BLOCKING_WHITE_LIST bool in_whitelist = false; #endif unsigned char tmp_url_len; char tmp_url[MAX_URL_LEN] = {0}; printWar("create AUC query thread\n"); while (1) { pthread_mutex_lock(&url_lock); tmp_list = url_list.next; pthread_mutex_unlock(&url_lock); while (tmp_list) { pthread_mutex_lock(&url_lock); tmp_list->process_flag = URL_PROCESSING; pthread_mutex_unlock(&url_lock); url_info.url = tmp_list->url; url_info.info_len = 0; #ifdef SUPPORT_BLOCKING_WHITE_LIST if(tmp_list->in_whitelist) goto skip; #endif printWar("url_info: url=%s, info_id=0x%x, cat_map=0x%x\n", url_info.url, tmp_list->info_id, tmp_list->cat_map); ret = pthread_mutex_trylock(&mali_url_check_lock); if (!ret) { if(tmp_list->info_id == PCTL_WEB_URL_ID_ALL && web_protection) { #ifdef SUPPORT_WEB_PROTECTION_WHITELIST if(check_web_protection_whitelist(url_info.url)) { printWar("url=%s in web_protection whitelist, skip query!\n", url_info.url); is_mali = false; } else { #endif if(opts.no_avira_web_protection == USE_AVIRA_SPECIAL_WEB_PROTECION_SERVICE) { printWar("before send to avira web_protection\n"); #if SUPPORT_CLOUD_UPDATE_AUC_INFO update_avira_web_protection_url(&web_protection_info); ret = malicious_url_query(&url_info, &web_protection_info); #else ret = malicious_url_query(&url_info); #endif if(url_info.info_len >= 1) { is_mali = (url_info.info[0].id != 1); } else { is_mali = false; } printWar("after send to avira web_protection\n"); } #ifdef SUPPORT_TPLINK_WEB_PROTECTION else if(opts.no_avira_web_protection == USE_TPLINK_WEB_PROTECION_SERVICE) { printWar("before send to tplink web_protection\n"); #if SUPPORT_CLOUD_UPDATE_AUC_INFO update_libauc_url(&auc_info, opts.content_filter_status == USE_TPLINK_CONTENT_FILTER_SERVICE); auc_initialize(&auc_info, opts.content_filter_status == USE_TPLINK_CONTENT_FILTER_SERVICE); malicious_url_query_using_tplink(&url_info, &auc_info); #else auc_initialize(opts.content_filter_status == USE_TPLINK_CONTENT_FILTER_SERVICE); malicious_url_query_using_tplink(&url_info); #endif if(url_info.info_len >= 1) { is_mali = (url_info.info[0].id != 1); } else { is_mali = false; } printWar("after send to tplink web_protection\n"); } #endif else if(opts.no_avira_web_protection == USE_AUC_INSTEAD) { is_mali = false; //Force treat it as a original url, go to auc tmp_list->info_id = PCTL_WEB_URL_ID_ALL + 1; tmp_list->cat_map |= (0x1 << 11); //Fake it as blocking unknown, rather than pure security request(0x0030) printWar("Force run a AUC query\n"); } #ifdef SUPPORT_LOCAL_MALICIOUS_URL_PROTECTION else if(opts.no_avira_web_protection == USE_LOCAL_DATABASE) { printWar("before send to local web_protection datbase\n"); is_mali = web_protection ? is_url_malicious(url_info.url) : false; printWar("after send to avira web_protection datbase\n"); } #endif #ifdef SUPPORT_WEB_PROTECTION_WHITELIST } #endif } else { is_mali = false; } if (is_mali) { cat_id = URL_CAT_SECURITY; #if DEBUG printWar("cat ret=%d url=%s id=%d info_id=%d\n", cat_id, url_info.url, tmp_list->id, tmp_list->info_id); #endif /* DEBUG */ pthread_mutex_lock(&url_lock); tmp_list->prev->next = tmp_list->next; if (tmp_list->next) { tmp_list->next->prev = tmp_list->prev; } tmp_list->query = 1; tmp_id = tmp_list->id; tmp_list->id = 0; tmp_list->info_id = 0; tmp_list->cat_map = 0; #ifdef SUPPORT_BLOCKING_WHITE_LIST tmp_list->in_whitelist = false; #endif tmp_url_len = tmp_list->url_len; memset(tmp_url, 0, MAX_URL_LEN); strncpy_safe(tmp_url, tmp_list->url, tmp_list->url_len); clean = tmp_list; tmp_list = tmp_list->next; clean->next = NULL; clean->prev = NULL; pthread_mutex_unlock(&url_lock); pthread_mutex_unlock(&mali_url_check_lock); goto block_mali; } else { pthread_mutex_unlock(&mali_url_check_lock); } } #ifdef SUPPORT_BLOCKING_WHITE_LIST skip: #endif if(tmp_list->info_id != PCTL_WEB_URL_ID_ALL) { printWar("before send to auc\n"); #if SUPPORT_CLOUD_UPDATE_AUC_INFO update_libauc_url(&auc_info, opts.content_filter_status == USE_TPLINK_CONTENT_FILTER_SERVICE); ret = auc_query(&url_info, &auc_info, opts.content_filter_status == USE_TPLINK_CONTENT_FILTER_SERVICE); #else ret = auc_query(&url_info, opts.content_filter_status == USE_TPLINK_CONTENT_FILTER_SERVICE); #endif printWar("after send to auc\n"); for (index = 0; index < url_info.info_len; index++) { printWar("cat ret=%d url=%s id=%d name=%s\n", ret, url_info.url, url_info.info[index].id, url_info.info[index].name); } } //remove from url_list pthread_mutex_lock(&url_lock); tmp_list->prev->next = tmp_list->next; if (tmp_list->next) { tmp_list->next->prev = tmp_list->prev; } tmp_list->query = 1; tmp_id = tmp_list->id; tmp_list->id = 0; tmp_list->info_id = 0; tmp_list->cat_map = 0; #ifdef SUPPORT_BLOCKING_WHITE_LIST in_whitelist = tmp_list->in_whitelist; tmp_list->in_whitelist = false; #endif tmp_url_len = tmp_list->url_len; memset(tmp_url, 0, MAX_URL_LEN); strncpy_safe(tmp_url, tmp_list->url, tmp_list->url_len); clean = tmp_list; tmp_list = tmp_list->next; clean->next = NULL; clean->prev = NULL; pthread_mutex_unlock(&url_lock); //send to kernel cat_id = 0; for (index = 0; index < url_info.info_len; index++) { for (index_cat = 0; mapping_table[index_cat][0] > 0; index_cat++) { matched = 0; for (index_subcat = 1; mapping_table[index_cat][index_subcat] >= 0; index_subcat++) { if (url_info.info[index].id == mapping_table[index_cat][index_subcat] || mapping_table[index_cat][index_subcat] == MAX_URL_CAT_DEFAULT) { cat_id |= 0x1 << (mapping_table[index_cat][0] - 1); matched = 1; break; } } if (matched) { break; } } } #ifdef SUPPORT_BLOCKING_WHITE_LIST if (in_whitelist && (cat_id & URL_CAT_SECURITY)) { /* if device in whitelist, remove security property from result */ cat_id = cat_id & (~URL_CAT_SECURITY); printWar("url=%s is malicious(in auc), but allow it as its device(mac) is in device whitelist!\n", url_info.url); } #endif #ifdef SUPPORT_WEB_PROTECTION_WHITELIST if ((cat_id & URL_CAT_SECURITY) && check_web_protection_whitelist(url_info.url)) { /* if url is in web_protection whitelist, remove security property from result */ cat_id = cat_id & (~URL_CAT_SECURITY); printWar("url=%s is malicious(in auc), but allow it as it's in web_protection whitelist!\n", url_info.url); } #endif block_mali: printWar("send to kernel, url=%s cat_id=%x\n", url_info.url, cat_id); url_send(tmp_id, cat_id, tmp_url, tmp_url_len, URL_REQ_CAT); printWar("after send to kernel\n"); } cond_timedwait(500);//500ms } return 0; }
最新发布
09-26
<think> 我们有两个版本的aucQuery函数。第一个版本实现了将分类结果保存到CSV文件的功能,而第二个版本没有。 目标:在第二个版本的aucQuery函数中,添加保存到CSV文件的功能,同时不改变其原有逻辑。 观察第一个版本中的保存操作: 1. 当URL被判断为恶意时(is_mali=true),在block_mali标签之前,有调用: write_to_csv(url_info.url, cat_id, cat_id, "Security"); 注意:这里传入的原始分类ID和映射后的分类ID都是cat_id(即URL_CAT_SECURITY),类别名称为"Security"。 2. 对于非恶意URL,在从AUC查询后,对每个分类信息(url_info.info数组中的每个元素)进行映射,然后在循环中调用: write_to_csv(url_info.url, original_cat_id, cat_id, category_name); 注意:这里original_cat_id是原始分类ID,cat_id是映射后的分类ID,category_name是分类名称。 第二个版本中,我们同样需要在这两种情况下保存结果。 具体分析第二个版本: 1. 恶意URL的处理: 在判断为恶意URL后,设置cat_id=URL_CAT_SECURITY,然后跳转到block_mali标签。 在跳转之前,我们需要添加保存操作,类似于第一个版本。 2. 非恶意URL的处理: 在第二个版本中,非恶意URL会进行AUC查询(如果条件满足),然后通过映射表计算cat_id。 但是,第二个版本中并没有像第一个版本那样对每个分类信息进行循环,而是循环结束后得到一个总的cat_id(通过位或运算)。 然而,保存CSV文件的操作应该针对每个原始分类信息(即url_info.info数组中的每个元素)进行,因为每个元素代表一个分类结果。 第二个版本中,在AUC查询后,有一个循环遍历url_info.info数组,但该循环只用于计算cat_id(位或),没有保存CSV。 我们需要在这个循环中,对每个元素,记录原始分类ID、映射后的分类ID(注意:映射后的分类ID是每个元素映射后得到的,但注意第二个版本中映射后的cat_id是一个位图,而第一个版本中在循环内每次映射后得到的是一个位(然后进行位或),但保存时传入的是映射后的位(即0x1<<(category_index-1))?实际上,第一个版本在循环内每次调用write_to_csv时,传入的第三个参数cat_id是每次映射后得到的位(但注意在循环内cat_id是局部变量,每次循环重新赋值,然后位或到外部的cat_id?)实际上第一个版本中,在循环内部重新定义了一个局部变量cat_id(覆盖了外部的cat_id),然后进行映射,映射的结果是一个位(如0x0004),然后写入CSV。而外部的cat_id(全局循环外定义的)是通过位或每个映射结果得到的。 但是,第二个版本中,我们只需要模仿第一个版本的非恶意处理部分:在循环内部,对每个分类信息,进行映射(得到映射后的位,注意这里映射后的位是一个整数,代表一个分类位,比如0x0001,0x0002等),然后立即写入CSV(同时记录原始分类ID和映射后的分类位,以及分类名称)。同时,还要将这个映射后的位或到外部的cat_id(用于后续发送给内核)。 然而,第二个版本中,在非恶意URL处理的循环部分,目前只做了位或操作,没有保存CSV。我们需要在这个循环内加入保存操作。注意:第二个版本中,循环内部并没有计算每个原始分类ID映射后的单个分类位,而是直接位或到cat_id(一个累积的位图)。为了保存每个原始分类信息,我们需要在循环内部对每个元素单独处理:先计算出这个元素对应的映射位(注意不是累积的cat_id),然后写入CSV,再把这个映射位或到外部的cat_id上。 因此,我们需要修改第二个版本中非恶意URL的循环部分: 将原来的: for (index = 0; index < url_info.info_len; index++) { for (index_cat = 0; mapping_table[index_cat][0] > 0; index_cat++) { ... // 内部循环,匹配到就位或,然后break } } 改为: for (index = 0; index < url_info.info_len; index++) { int original_cat_id = url_info.info[index].id; const char *category_name = url_info.info[index].name; unsigned int mapped_cat_bit = 0; // 当前分类信息映射后的位 for (index_cat = 0; mapping_table[index_cat][0] > 0; index_cat++) { matched = 0; for (index_subcat = 1; mapping_table[index_cat][index_subcat] >= 0; index_subcat++) { if (original_cat_id == mapping_table[index_cat][index_subcat] || mapping_table[index_cat][index_subcat] == MAX_URL_CAT_DEFAULT) { // 计算映射后的位:0x1 << (mapping_table[index_cat][0]-1) mapped_cat_bit = (0x1 << (mapping_table[index_cat][0]-1)); matched = 1; break; } } if (matched) { break; } } // 如果没有匹配到,我们可以设置一个默认值?或者不设置?根据第一个版本,没有匹配则mapped_cat_bit为0? // 写入CSV:当前分类信息 write_to_csv(url_info.url, original_cat_id, mapped_cat_bit, category_name); // 将当前映射的位或到总的cat_id上 cat_id |= mapped_cat_bit; } 注意:这样修改后,我们就不再需要原来循环内的位或操作(因为现在每个分类信息处理时会计算一个位,然后或到cat_id上)。 3. 另外,在第二个版本中,恶意URL跳转到block_mali标签后,并没有保存操作,我们需要在跳转前(在释放锁之后,跳转之前)添加保存操作。 4. 注意:第二个版本中,在恶意URL处理部分,有一个条件判断:如果tmp_list->info_id == PCTL_WEB_URL_ID_ALL 并且 web_protection为真,才会进行恶意检查。如果检查为恶意,则设置cat_id=URL_CAT_SECURITY,然后跳转到block_mali。我们在跳转前(在释放锁之后,跳转之前)添加保存操作。 5. 对于恶意URL,我们保存时传入的原始分类ID和映射后的分类ID都是URL_CAT_SECURITY(即cat_id的值,此时cat_id就是URL_CAT_SECURITY),分类名称可以写为"Security"(字符串)。 6. 在非恶意URL处理部分,我们修改循环,对每个分类信息单独处理,并写入CSV。 7. 另外,第二个版本中,在非恶意URL的查询条件为:if(tmp_list->info_id != PCTL_WEB_URL_ID_ALL)。注意,在恶意URL处理中,如果使用了USE_AUC_INSTEAD选项,会强制设置tmp_list->info_id为PCTL_WEB_URL_ID_ALL+1,这样就会触发非恶意URL的AUC查询。 8. 注意:在第二个版本中,恶意URL处理部分,在跳转到block_mali之前,有一个解锁操作(pthread_mutex_unlock(&mali_url_check_lock);)和释放锁后跳转。我们在解锁后,跳转前添加write_to_csv。 9. 在非恶意URL处理部分,我们在循环中写入CSV,注意需要获取每个分类的名称(url_info.info[index].name)和原始ID。 修改步骤: 第一部分:恶意URL的保存 在恶意URL处理分支中(if (is_mali) 为真),在代码块的最后(在pthread_mutex_unlock(&mali_url_check_lock);之后,goto block_mali;之前)添加: write_to_csv(url_info.url, cat_id, cat_id, "Security"); 第二部分:非恶意URL的保存(在AUC查询后,对每个分类信息处理) 修改循环部分,将原来的循环(用于计算cat_id)改为逐个处理每个分类信息,计算映射位,写入CSV,并累积到cat_id。 注意:第二个版本中,在非恶意URL处理循环之后,还有对白名单的处理(SUPPORT_BLOCKING_WHITE_LIST和SUPPORT_WEB_PROTECTION_WHITELIST),这些处理会在cat_id的基础上修改(比如移除安全位)。但是,我们写入CSV的操作是在计算映射位之后立即进行的,所以写入的是映射后的位,而后续白名单调整的是发送给内核的cat_id,不影响CSV记录。这是合理的,因为CSV记录的是AUC返回的原始分类和映射关系,而白名单调整是后续策略。 代码修改: 由于第二个版本的代码较长,我们只修改关键部分。 恶意URL部分修改位置: 在第二个版本中,恶意URL处理分支内,在解锁mali_url_check_lock之后,跳转到block_mali之前,添加: write_to_csv(url_info.url, cat_id, cat_id, "Security"); 非恶意URL部分修改位置: 将原来的循环: cat_id = 0; for (index = 0; index < url_info.info_len; index++) { for (index_cat = 0; mapping_table[index_cat][0] > 0; index_cat++) { matched = 0; for (index_subcat = 1; mapping_table[index_cat][index_subcat] >= 0; index_subcat++) { if (url_info.info[index].id == mapping_table[index_cat][index_subcat] || mapping_table[index_cat][index_subcat] == MAX_URL_CAT_DEFAULT) { cat_id |= 0x1 << (mapping_table[index_cat][0] - 1); matched = 1; break; } } if (matched) { break; } } } 修改为: cat_id = 0; for (index = 0; index < url_info.info_len; index++) { int original_cat_id = url_info.info[index].id; const char *category_name = url_info.info[index].name; unsigned int mapped_cat_bit = 0; // 当前分类信息映射后的位 for (index_cat = 0; mapping_table[index_cat][0] > 0; index_cat++) { matched = 0; for (index_subcat = 1; mapping_table[index_cat][index_subcat] >= 0; index_subcat++) { if (original_cat_id == mapping_table[index_cat][index_subcat] || mapping_table[index_cat][index_subcat] == MAX_URL_CAT_DEFAULT) { // 计算映射后的位 mapped_cat_bit = (0x1 << (mapping_table[index_cat][0]-1)); matched = 1; break; } } if (matched) { break; } } // 写入CSV write_to_csv(url_info.url, original_cat_id, mapped_cat_bit, category_name); // 累积到总的cat_id cat_id |= mapped_cat_bit; } 注意:变量定义。第二个版本中,在循环之前已经定义了index, index_cat, index_subcat, matched。我们需要在循环内定义original_cat_id和category_name,注意original_cat_id是int,category_name是const char*。 另外,mapped_cat_bit是unsigned int,因为cat_id是unsigned int。 但是,第二个版本中,在非恶意URL处理部分,循环之前有定义: unsigned int cat_id = 0; // 注意:这个cat_id在循环外定义 ... for (index = 0; ...) { ... } 因此,我们按照上面的修改即可。 最后,注意在文件的某处必须定义了write_to_csv函数(第一个版本中使用了,所以第二个版本添加后应该可以正常工作,假设该函数已存在)。 修改后的第二个版本的aucQuery函数(仅修改两个位置): 1. 恶意URL处理分支内,解锁之后,跳转之前,添加一行写入CSV。 2. 非恶意URL处理部分,将原来的循环替换为新的循环(包含写入CSV和累积cat_id)。 注意:由于代码较长,我们只写出修改后的代码块,但为了清晰,我们将在完整的第二个版本代码中标注修改点。 由于问题要求不能改变原有功能,我们在非恶意URL的循环修改中,除了加入写CSV,其他逻辑不变(即cat_id的计算结果应该和原来一致)。 但是,注意原来的循环中,一旦匹配到一个映射规则(matched=1)就会break内层循环并跳出当前index_cat的循环(break到外层循环,然后继续下一个index)。而我们新的循环中,也是同样的逻辑,所以计算cat_id的位或结果应该和原来一致。 因此,我们按照上述修改即可。 由于代码太长,这里仅提供修改后的第二个版本的完整代码,但注意我们只修改了上述两个位置,其他保持不变。 但是,由于第二个版本中,在非恶意URL的循环之前,cat_id被重置为0,而我们的新循环中,每次循环将映射位或到cat_id上,所以最终cat_id就是所有映射位的或。这样计算得到的cat_id和原来一样。 修改后的第二个版本aucQuery函数(仅显示修改后的代码片段,不是完整函数): 恶意URL处理部分: ... pthread_mutex_unlock(&mali_url_check_lock); // 添加写入CSV write_to_csv(url_info.url, cat_id, cat_id, "Security"); // 注意:这里cat_id就是URL_CAT_SECURITY goto block_mali; ... 非恶意URL处理部分(循环部分): //send to kernel cat_id = 0; for (index = 0; index < url_info.info_len; index++) { int original_cat_id = url_info.info[index].id; const char *category_name = url_info.info[index].name; unsigned int mapped_cat_bit = 0; for (index_cat = 0; mapping_table[index_cat][0] > 0; index_cat++) { matched = 0; for (index_subcat = 1; mapping_table[index_cat][index_subcat] >= 0; index_subcat++) { if (original_cat_id == mapping_table[index_cat][index_subcat] || mapping_table[index_cat][index_subcat] == MAX_URL_CAT_DEFAULT) { mapped_cat_bit = (0x1 << (mapping_table[index_cat][0]-1)); matched = 1; break; } } if (matched) { break; } } write_to_csv(url_info.url, original_cat_id, mapped_cat_bit, category_name); cat_id |= mapped_cat_bit; } 注意:在非恶意URL循环中,我们使用了category_name,它是url_info.info[index].name,这个字段在第一个版本中也有使用。 但是,第二个版本中,在AUC查询后,有一个调试打印,打印了name字段,所以url_info.info[index].name是可用的。 最后,注意在第二个版本中,恶意URL处理部分,我们只写入了一次CSV(因为整个恶意URL只对应一个安全分类)。非恶意URL可能对应多个分类(info_len可能大于1),所以我们循环写入。 这样修改后,第二个版本就添加了保存到CSV的功能,同时保持原有逻辑不变。 由于第二个版本的代码较长,我们在此不重写整个函数,而是指出修改点。如果需要输出完整代码,请说明。 但是,问题要求根据第一个代码修改第二个代码,因此我们输出修改后的第二个版本的完整代码。 注意:由于代码很长,且大部分相同,我们只修改了两个地方,因此下面给出修改后的第二个版本完整代码,并在修改处添加注释。 </think> 根据第一个代码,在第二个代码中添加保存到CSV文件的功能,同时保持原有逻辑不变。主要修改点如下: 1. **恶意URL处理部分**(`is_mali`为真时): 在`pthread_mutex_unlock(&mali_url_check_lock)`后、`goto block_mali`前添加CSV写入操作: ```c write_to_csv(url_info.url, cat_id, cat_id, "Security"); ``` 2. **非恶意URL分类处理部分**: 重构原有分类映射循环,增加: - 获取原始分类ID和分类名称 - 计算映射后的分类位 - 写入CSV文件 - 累积分类位结果 修改后的完整代码: ```c static void *aucQuery(void *unused) { int ret = 0; struct url_info url_info; bool is_mali = false; #if SUPPORT_CLOUD_UPDATE_AUC_INFO struct auc_info auc_info; struct auc_info web_protection_info; #endif unsigned int cat_id = 0; int tmp_id = 0; int index = 0; int index_cat = 0; int index_subcat = 0; unsigned char matched = 0; struct url_entry *tmp_list = NULL; struct url_entry *clean = NULL; #ifdef SUPPORT_BLOCKING_WHITE_LIST bool in_whitelist = false; #endif unsigned char tmp_url_len; char tmp_url[MAX_URL_LEN] = {0}; printWar("create AUC query thread\n"); while (1) { pthread_mutex_lock(&url_lock); tmp_list = url_list.next; pthread_mutex_unlock(&url_lock); while (tmp_list) { pthread_mutex_lock(&url_lock); tmp_list->process_flag = URL_PROCESSING; pthread_mutex_unlock(&url_lock); url_info.url = tmp_list->url; url_info.info_len = 0; #ifdef SUPPORT_BLOCKING_WHITE_LIST if(tmp_list->in_whitelist) goto skip; #endif printWar("url_info: url=%s, info_id=0x%x, cat_map=0x%x\n", url_info.url, tmp_list->info_id, tmp_list->cat_map); ret = pthread_mutex_trylock(&mali_url_check_lock); if (!ret) { if(tmp_list->info_id == PCTL_WEB_URL_ID_ALL && web_protection) { #ifdef SUPPORT_WEB_PROTECTION_WHITELIST if(check_web_protection_whitelist(url_info.url))
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值