Skip to content

Commit ab8eceb

Browse files
committed
WIP: Parse & write mac addr; compiles!
1 parent a1f2945 commit ab8eceb

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/include/p2f.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ typedef struct tcp_retrans_ {
116116
uint16_t len;
117117
} tcp_retrans_t;
118118

119+
typedef struct mac_addr_ {
120+
unsigned char bytes[6];
121+
} mac_addr_t;
122+
119123
#include "procwatch.h"
120124
#include "config.h"
121125

@@ -141,6 +145,10 @@ typedef struct flow_record_ {
141145
uint8_t np; /*!< number of packets */
142146
uint8_t op; /*!< number of packets (w/nonzero data) */
143147
uint16_t ob; /*!< number of bytes of application data */
148+
149+
mac_addr_t src_mac; /*!< Source MAC address */
150+
mac_addr_t dst_mac; /*!< Destination MAC address */
151+
144152
struct timeval start; /*!< start time */
145153
struct timeval end; /*!< end time */
146154
uint16_t last_pkt_len; /*!< last observed appdata length */
@@ -153,7 +161,7 @@ typedef struct flow_record_ {
153161
double bd_mean;
154162
double bd_variance;
155163
header_description_t hd; /*!< header description (proto ident) */
156-
bool idp_packet; /*!< determines if packet is used for IDP */
164+
bool idp_packet; /*!< determines if packet is used for IDP */
157165
int32_t idp_seq_num; /*!< marks the SYN packet for IDP determination */
158166
void *idp;
159167
uint16_t idp_len;

src/p2f.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,12 @@ static void flow_record_print_json
13451345
*/
13461346
zprintf(ctx->output, "{");
13471347

1348+
/* New: Print the MAC addresses */
1349+
unsigned char *sm = (unsigned char *)&(rec->src_mac);
1350+
unsigned char *dm = (unsigned char *)&(rec->dst_mac);
1351+
zprintf(ctx->output, "\"sm\":\"%02x:%02x:%02x:%02x:%02x:%02x\",", sm[0], sm[1], sm[2], sm[3], sm[4], sm[5]);
1352+
zprintf(ctx->output, "\"dm\":\"%02x:%02x:%02x:%02x:%02x:%02x\",", dm[0], dm[1], dm[2], dm[3], dm[4], dm[5]);
1353+
13481354
if (rec->ip_type == ETH_TYPE_IPV6) {
13491355
inet_ntop(AF_INET6, &rec->key.sa.v6_sa, ipv6_addr, INET6_ADDRSTRLEN);
13501356
zprintf(ctx->output, "\"sa\":\"%s\",", ipv6_addr);
@@ -2134,4 +2140,3 @@ int upload_file (char *filename) {
21342140

21352141
return 0;
21362142
}
2137-

src/pkt_proc.c

+23
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,10 @@ void* process_packet (unsigned char *ctx_ptr,
971971
const struct pcap_pkthdr *header = pkt_header;
972972
struct pcap_pkthdr *dyn_header = NULL;
973973

974+
/* initialize MAC addresses */
975+
mac_addr_t src_mac = {0x00};
976+
mac_addr_t dst_mac = {0x00};
977+
974978
/* declare pointers to packet headers */
975979
ip_hdr_t *ip = NULL;
976980
ip_hdrv6_t *ipv6 = NULL;
@@ -995,7 +999,16 @@ void* process_packet (unsigned char *ctx_ptr,
995999
joy_log_info("++++++++++ Packet %lu ++++++++++", ctx->stats.num_packets);
9961000
// packet_count++;
9971001

1002+
// Ethernet Type II Header: [[Dest MAC][Source MAC][ETH_TYPE]]
1003+
// 6bytes 6bytes 2bytes
9981004
// ethernet = (struct ethernet_hdr*)(packet);
1005+
unsigned char *sm = (unsigned char *)&(src_mac);
1006+
unsigned char *dm = (unsigned char *)&(dst_mac);
1007+
for (int i = 0; i < 6; i++)
1008+
{
1009+
*(sm + i) = packet[i];
1010+
*(dm + i) = packet[i + 6];
1011+
}
9991012
ether_type = ntohs(*(const uint16_t *)(packet + 12));//Offset to get ETH_TYPE
10001013
ctx->curr_pkt_type = 0;
10011014
/* Support for both normal ethernet, 802.1q and 802.1ad. Distinguish between
@@ -1203,13 +1216,19 @@ void* process_packet (unsigned char *ctx_ptr,
12031216
joy_log_info("Source IPv6: %s", ipv6_addr);
12041217
inet_ntop(AF_INET6, &ipv6->ip_dst, ipv6_addr, INET6_ADDRSTRLEN);
12051218
joy_log_info("Dest IP: %s", ipv6_addr);
1219+
1220+
joy_log_info("Source MAC: %02x:%02x:%02x:%02x:%02x:%02x:", *sm, *(sm + 1), *(sm + 2), *(sm + 3), *(sm + 4), *(sm + 5));
1221+
joy_log_info("Dest MAC: %02x:%02x:%02x:%02x:%02x:%02x:", *dm, *(dm + 1), *(dm + 2), *(dm + 3), *(dm + 4), *(dm + 5));
12061222
joy_log_info("Len: %u", ip_len);
12071223
joy_log_debug("IPv6 header len: %u", (ip_hdr_len + (ipv6_ext_hdrs * 8)));
12081224
} else {
12091225
inet_ntop(AF_INET, &ip->ip_src, ipv4_addr, INET_ADDRSTRLEN);
12101226
joy_log_info("Source IP: %s", ipv4_addr);
12111227
inet_ntop(AF_INET, &ip->ip_dst, ipv4_addr, INET_ADDRSTRLEN);
12121228
joy_log_info("Dest IP: %s", ipv4_addr);
1229+
1230+
joy_log_info("Source MAC: %02x:%02x:%02x:%02x:%02x:%02x:", *sm, *(sm + 1), *(sm + 2), *(sm + 3), *(sm + 4), *(sm + 5));
1231+
joy_log_info("Dest MAC: %02x:%02x:%02x:%02x:%02x:%02x:", *dm, *(dm + 1), *(dm + 2), *(dm + 3), *(dm + 4), *(dm + 5));
12131232
joy_log_info("Len: %u", ip_len);
12141233
joy_log_debug("IP header len: %u", ip_hdr_len);
12151234
}
@@ -1305,6 +1324,10 @@ void* process_packet (unsigned char *ctx_ptr,
13051324
}
13061325
record->ip_type = ctx->curr_pkt_type;
13071326

1327+
/* Add the MAC addresses */
1328+
record->src_mac = src_mac;
1329+
record->dst_mac = dst_mac;
1330+
13081331
/*
13091332
* Get IP ID
13101333
*/

0 commit comments

Comments
 (0)