我有一个通过网络接收数据包的应用程序。我能够解释除有效载荷(数据)部分之外的所有字段值。
考虑数据包结构: src addr | src端口| dest addr | dest端口|数据|校验和
数据包本身存储为char *,我可以获得所有值src / des addr,port。但有效载荷部分让我困惑。我能够以十六进制格式打印它,但在char中打印它会给出非ascii值。当我将有效载荷部分复制到一个新的字符串并打印新的字符串时,我什么也得不到,即它打印任何东西,其长度为0.我没有得到任何错误或警告。有人可以告诉我哪里出错吗?我想我在这里遗漏了一些东西。
以下是代码部分:
typedef struct pcap_802_15_4_s{
guint16 fcf;
unsigned char seqno;
guint16 dpan;
guint16 daddr;
guint16 saddr;
char *payload_data;
} pcap_802_15_4_t;
char *packet = read_serial_packet(src, &len);
if (!packet)
exit(0);
else if (packet[0] != TOS_SERIAL_802_15_4_ID) {
printf("bad packet (serial type is %02x, not %02x)\n", packet[0], TOS_SERIAL_802_15_4_ID);
}
plen = packet[1];
printf("Received packet of length %i: \n", plen);
if (plen != len) {
printf("Packet format error: read packet length (%hhx) is different than expected from frame (%hhx).\n", plen, len);
}
struct timeval tv;
gettimeofday(&tv, NULL);
i = 2;
// Read in FCF and i+=2
fcf = packet[i+1] << 8 | packet[i];
packet_802_15_4.fcf = fcf;
i += 2;
{
if ((fcf & 0x7) == 0x01) {
printf(" Frame type: data\n");
}
else if ((fcf & 0x7) == 0x02) {
printf(" Frame type: acknowledgement\n");
}
else {
printf(" Frame type: other\n");
}
printf(" Security: %s\n", (fcf & (1 << 3)) ? "enabled":"disabled");
printf(" Frame pending: %s\n", (fcf & (1 << 4)) ? "yes":"no");
printf(" Ack request: %s\n", (fcf & (1 << 5)) ? "yes":"no");
printf(" Intra-PAN: %s\n", (fcf & (1 << 6)) ? "yes":"no");
intraPan = (fcf & (1 << 6));
}
{
unsigned char seqno = packet[i++];
packet_802_15_4.seqno = seqno;
printf(" Sequence number: 0x%hhx\n", seqno);
printf(" Sequence number(dump): 0x%hhx\n", packet_802_15_4.seqno);
}
{
char addrLen = (fcf >> 10) & 0x3;
short saddr = 0;
long long laddr = 0;
// 16- and 64-bit destinations have a PAN ID
if (addrLen == 2 || addrLen == 3) {
short destPan = packet[i++] << 8 | packet[i++];
packet_802_15_4.dpan = destPan;
printf(" Destination PAN: 0x%02hx\n", destPan);
printf(" Destination PAN (dump): 0x%02hx\n", packet_802_15_4.dpan);
}
switch (addrLen) {
case 0:
printf(" Destination address: none\n");
break;
case 1:
printf(" Destination address: invalid? (0x01)\n");
break;
case 2:
saddr = (packet[i] << 8 | packet[i+1]);
i += 2;
packet_802_15_4.daddr = saddr;
printf(" Destination address: 0x%04hx\n", saddr);
printf(" case 2: Destination address(dump): 0x%04hx\n", packet_802_15_4.daddr);
break;
case 3: {
int j;
for (j = 0; j < 8; j++) {
laddr = laddr << 8;
laddr |= packet[i++];
}
packet_802_15_4.daddr = saddr;
printf(" Destination address: 0x%016llx\n", laddr);
printf(" case 3: Destination address(dump): 0x%016llx\n", (long long unsigned int)packet_802_15_4.daddr);
break;
}
default:
printf(" Destination address: parse serror\n");
}
}
{
char addrLen = (fcf >> 14) & 0x3;
short saddr = 0;
long long laddr = 0;
if (!intraPan) { // Intra-PAN packet
short srcPan = packet[i] << 8 | packet[i+1];
//packet_802_15_4.span = srcPan;
i += 2;
printf(" Source PAN: 0x%02hx\n", srcPan);
}
switch (addrLen) {
case 0:
printf(" Source address: none\n");
break;
case 1:
printf(" Source address: invalid? (0x01)\n");
break;
case 2:
saddr = (packet[i] << 8 | packet[i + 1]);
packet_802_15_4.saddr = saddr;
i += 2;
printf(" case(2): Source address: 0x%04hx\n", saddr);
break;
case 3: {
int j;
for (j = 0; j < 8; j++) {
laddr = laddr << 8;
laddr |= packet[i++];
}
printf(" case(3): Source address: 0x%016llx\n", laddr);
packet_802_15_4.saddr = saddr;
break;
}
default:
printf(" Source address: parse serror\n");
}
}
if (iframes) {
printf(" I-Frame: %s\n", (packet[i++] == 0x3f)? "yes":"no");
}
printf(" AM type: 0x%02hhx\n", packet[i++]);
if (i >= plen) {
printf("Packet format error: read packet is shorter than expected.\n");
}
else {
int j=0;
packet_802_15_4.payload_data = (char *)malloc(sizeof(char) * (plen - i));
for (; i < plen; i++) {
printf("Payload byte %d: 0x%02hhx \n",i,packet[i]);
printf("Payload byte %d: %c \n",i,packet[i]);
packet_802_15_4.payload_data[j] = (char) packet[i];
j++;
}
有效载荷的十六进制输出: 为0x00 0×01 为0x10 0xdd
答案 0 :(得分:0)
1)如果您有Wireshark,请捕获几个数据包并检查那里的有效负载表示。如果Wireshark正确显示您的有效负载,那么接收器部分代码必须存在问题。
2)您确定没有任何IP标头或TCP标头吗?你在转移原始以太网吗?
3)你确定有效载荷只是字符吗?