在收发器模式下,我想丢弃所有数据包,除了那些来自一个特定MAC地址的数据包。我正确地应用了NWP过滤器程序,虽然我收到所有数据包。
这是代码。
1)创建过滤方法。
static int CreateFilters() {
long lRetVal;
SlrxFilterID_t FilterId = 0;
SlrxFilterRuleType_t RuleType;
SlrxFilterFlags_t FilterFlags;
SlrxFilterRule_t Rule;
SlrxFilterTrigger_t Trigger;
SlrxFilterAction_t Action;
unsigned char ucMacMask[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
//unsigned char ucIPMask[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
/*************************************************************************/
/* Build filter to drop incoming packets according to source MAC address */
/*************************************************************************/
//
//define filter as parent
//
Trigger.ParentFilterID = 0;
//
//no trigger to activate the filter.
//
Trigger.Trigger = NO_TRIGGER;
//
//connection state and role
//
Trigger.TriggerArgConnectionState.IntRepresentation = \
RX_FILTER_CONNECTION_STATE_STA_NOT_CONNECTED;
Trigger.TriggerArgRoleStatus.IntRepresentation = RX_FILTER_ROLE_PROMISCUOUS;
//
// header and Combination types are only supported. Combination for logical
// AND/OR between two filters
//
RuleType = HEADER;
Rule.HeaderType.RuleHeaderfield = MAC_DST_ADDRESS_FIELD;
memcpy(
Rule.HeaderType.RuleHeaderArgsAndMask.RuleHeaderArgs.RxFilterDB6BytesRuleArgs[0],
g_ucMacAddress, SL_MAC_ADDR_LEN);
memcpy(Rule.HeaderType.RuleHeaderArgsAndMask.RuleHeaderArgsMask, ucMacMask,
SL_MAC_ADDR_LEN);
Rule.HeaderType.RuleCompareFunc = COMPARE_FUNC_NOT_EQUAL_TO;
//
//Action
//
Action.ActionType.IntRepresentation = RX_FILTER_ACTION_DROP;
FilterFlags.IntRepresentation = RX_FILTER_BINARY;
lRetVal = sl_WlanRxFilterAdd(RuleType, FilterFlags, &Rule, &Trigger,
&Action, &FilterId);
ASSERT_ON_ERROR(lRetVal);
return 0;
}
2)启用所有过滤器。
signed long EnableAllFilters() {
signed long lRetVal = -1;
_WlanRxFilterOperationCommandBuff_t RxFilterIdMask;
//
//Enable all 64 filters (8*8) - bit=1 represents enabled filter
//
memset(RxFilterIdMask.FilterIdMask, 0xFF, 8);
lRetVal = sl_WlanRxFilterSet( SL_ENABLE_DISABLE_RX_FILTER,
(unsigned char *) &RxFilterIdMask,
sizeof(_WlanRxFilterOperationCommandBuff_t));
return lRetVal;
}
3)接收数据包的方法。
int iSoc;
char acBuffer[1500];
int iIndex, channelnumber;
long lRetVal = -1;
struct SlTimeval_t timeval;
TransceiverRxOverHead_t *frameRadioHeader = NULL;
timeval.tv_sec = 0; // Seconds
timeval.tv_usec = 20000; // Microseconds.
UART_PRINT(
"\n\rListerning to all channel to listen[1-13] for five entries:");
for (channelnumber = 8; channelnumber < 9; channelnumber++) {
lRetVal = CreateFilters();
if (lRetVal < 0) {
UART_PRINT("Filter creation failed\n\r");
LOOP_FOREVER()
;
}
//
// Activate pre-defined filters - The filters will be deleted upon reset
//
if (EnableAllFilters() < 0) {
UART_PRINT("Unable to enable filter \n\r");
LOOP_FOREVER()
;
}
UART_PRINT("\n\r");
UART_PRINT("\n\rListerning to channel number %d=:", channelnumber);
iSoc = sl_Socket(SL_AF_RF, SL_SOCK_RAW, channelnumber);
ASSERT_ON_ERROR(iSoc);
// Enable receive timeout
lRetVal = sl_SetSockOpt(iSoc, SL_SOL_SOCKET, SL_SO_RCVTIMEO, &timeval,
sizeof(timeval));
ASSERT_ON_ERROR(lRetVal);
int k;
for (k = 0; k < 50; k++) {
lRetVal = sl_Recv(iSoc, acBuffer, 1500, 0);
if (lRetVal < 0 && lRetVal != SL_EAGAIN) {
//error
ASSERT_ON_ERROR(sl_Close(iSoc));
ASSERT_ON_ERROR(lRetVal);
}
frameRadioHeader = (TransceiverRxOverHead_t *) acBuffer;
UART_PRINT(" \n\r===>>> Timestamp: %iuS, Signal Strength: %idB\n\r",
frameRadioHeader->timestamp, frameRadioHeader->rssi);
UART_PRINT(
" \n\r===>>> Destination MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",
acBuffer[12], acBuffer[13], acBuffer[14], acBuffer[15],
acBuffer[16], acBuffer[17]);
UART_PRINT(
" \n\r===>>> Source MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",
acBuffer[24], acBuffer[25], acBuffer[26], acBuffer[27],
acBuffer[28], acBuffer[29]);
UART_PRINT(" \n\r===>>> Source IP Address: %d.%d.%d.%d\n\r",
acBuffer[54], acBuffer[55], acBuffer[56], acBuffer[57]);
UART_PRINT(" \n\r===>>> Destination IP Address: %d.%d.%d.%d\n\r",
acBuffer[58], acBuffer[59], acBuffer[60], acBuffer[61]);
UART_PRINT("\n\r");
}
//
//Close the socket
//
lRetVal = sl_Close(iSoc);
}
ASSERT_ON_ERROR(lRetVal);
return SUCCESS;
}
任何帮助都将受到高度赞赏。
感谢。
答案 0 :(得分:0)
根据您的代码
unsigned char ucMacMask[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Rule.HeaderType.RuleHeaderfield = MAC_DST_ADDRESS_FIELD;
Rule.HeaderType.RuleCompareFunc = COMPARE_FUNC_NOT_EQUAL_TO;
结果是
的数据包DST_MAC_ADDR != ucMacMask
将收到。
因此,您获取所有数据包,因为它们的目标mac addr不等于{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
。
例如,你可以尝试这个
unsigned char ucMacMask[6] = { 0x04, 0x4B, 0xED, 0x3C, 0x89, 0x14 };
Rule.HeaderType.RuleHeaderfield = MAC_SRC_ADDRESS_FIELD;
Rule.HeaderType.RuleCompareFunc = COMPARE_FUNC_EQUAL;
所以,你只能接收数据包
那些来自一个特定的MAC地址
(这里是0x04,0x4B,0xED,0x3C,0x89,0x14,你可以自己设置)。
答案 1 :(得分:0)
@ Mitulkumar Patel (太久不能发表评论) 两行代码的说明(在Mitulkumar Patel的评论中回答问题):
&#34; memcpy
&#34;函数副本n
字节构成内存A到B.第一行是n=SL_MAC_ADDR_LEN
,A=g_ucMacAddress
,B=Rule.HeaderType.RuleHeaderArgsAndMask.RuleHeaderArgs.RxFilterDB6BytesRuleArgs[0]
。注意:您需要确认g_ucMacAddress
是否是内存地址(谁拥有该mac地址)?如果g_ucMacAddress
不是内存地址,则需要使用:&#34; &g_ucMacAddress
&#34;而不是&#34; g_ucMacAddress
&#34;在第一行。该行用于设置Rx过滤模块的mac地址。
第二行是设置mac地址掩码。这是在您传入数据包中的源MAC地址和g_ucMacAddress
相等之前,此操作src_mac_addr & ucMacMask
将首先完成。