[PATCH 0/2] iptraf-ng: use pkt_ip_protocol()
by Vitezslav Samel
Vitezslav Samel (2):
use pkt_ip_protocol() in place of ip6_hdr->ip6_nxt
use pkt_ip_protocol() in place of iphdr->protocol
src/itrafmon.c | 12 ++++++------
src/packet.c | 2 +-
2 files changed, 7 insertions(+), 7 deletions(-)
--
1.7.8.4
10 years, 10 months
[PATCH] packet.h: pkt_iph_len(): make it function rather than macro
by Vitezslav Samel
( ... I don't like complex macros for simple things)
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/packet.h | 24 +++++++++++-------------
1 files changed, 11 insertions(+), 13 deletions(-)
diff --git a/src/packet.h b/src/packet.h
index 21d4c37..4979a5d 100644
--- a/src/packet.h
+++ b/src/packet.h
@@ -55,19 +55,17 @@ static inline void PACKET_INIT_STRUCT(struct pkt_hdr *p)
struct pkt_hdr packet; \
PACKET_INIT_STRUCT(&packet)
-#define pkt_iph_len(pkt) __extension__ ({ \
- __u8 len__ = 0; \
- switch ((pkt)->pkt_protocol) { \
- case ETH_P_IP: \
- len__ = (pkt)->iphdr->ihl * 4; \
- break; \
- case ETH_P_IPV6: \
- len__ = 40; \
- break; \
- }; \
- len__; \
- })
-
+static inline __u8 pkt_iph_len(const struct pkt_hdr *pkt)
+{
+ switch (pkt->pkt_protocol) {
+ case ETH_P_IP:
+ return pkt->iphdr->ihl * 4;
+ case ETH_P_IPV6:
+ return 40;
+ default:
+ return 0;
+ }
+}
static inline __u8 pkt_ip_protocol(const struct pkt_hdr *p)
{
--
1.7.8.4
10 years, 10 months
[PATCH] packet: introduce pkt_iph_len()
by Nikola Pajkovsky
func returns internet header length from pkt_hdr dependently ETH_P_IP or ETH_P_IPV6
--
I do love macros ;)
Signed-off-by: Nikola Pajkovsky <npajkovs(a)redhat.com>
---
src/itrafmon.c | 7 ++-----
src/packet.c | 10 +++++-----
src/packet.h | 15 +++++++++++++--
3 files changed, 20 insertions(+), 12 deletions(-)
diff --git a/src/itrafmon.c b/src/itrafmon.c
index 1142fdb..c0c8373 100644
--- a/src/itrafmon.c
+++ b/src/itrafmon.c
@@ -585,9 +585,6 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
unsigned int br; /* bytes read. Differs from readlen */
- /* only when packets fragmented */
- unsigned int iphlen;
-
struct tcptable table;
struct tcptableent *tcpentry;
struct tcptableent *tmptcp;
@@ -1016,11 +1013,9 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
switch(pkt.pkt_protocol) {
case ETH_P_IP:
- iphlen = pkt_ipv4_len(&pkt);
frag_off = pkt.iphdr->frag_off;
break;
case ETH_P_IPV6:
- iphlen = 40;
frag_off = 0;
break;
default:
@@ -1033,6 +1028,8 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
continue;
}
+ /* only when packets fragmented */
+ __u8 iphlen = pkt_iph_len(&pkt);
transpacket = (struct tcphdr *) (pkt.pkt_payload + iphlen);
__u8 ip_protocol = pkt_ip_protocol(&pkt);
diff --git a/src/packet.c b/src/packet.c
index 25d11ff..0c262be 100644
--- a/src/packet.c
+++ b/src/packet.c
@@ -206,7 +206,7 @@ again:
ip_checksum = ip->check;
ip->check = 0;
- hdr_check = in_cksum((u_short *) pkt->iphdr, pkt_ipv4_len(pkt));
+ hdr_check = in_cksum((u_short *) pkt->iphdr, pkt_iph_len(pkt));
if ((hdr_check != ip_checksum))
return CHECKSUM_ERROR;
@@ -240,7 +240,7 @@ again:
} else {
struct tcphdr *tcp;
struct udphdr *udp;
- char *ip_payload = (char *) ip + pkt_ipv4_len(pkt);
+ char *ip_payload = (char *) ip + pkt_iph_len(pkt);
switch (ip->protocol) {
case IPPROTO_TCP:
@@ -283,8 +283,8 @@ again:
return PACKET_FILTERED;
if (v6inv4asv6 && (ip->protocol == IPPROTO_IPV6)) {
pkt->pkt_protocol = ETH_P_IPV6;
- pkt->pkt_payload += pkt_ipv4_len(pkt);
- pkt->pkt_len -= pkt_ipv4_len(pkt);
+ pkt->pkt_payload += pkt_iph_len(pkt);
+ pkt->pkt_len -= pkt_iph_len(pkt);
goto again;
}
return PACKET_OK;
@@ -292,7 +292,7 @@ again:
struct tcphdr *tcp;
struct udphdr *udp;
struct ip6_hdr *ip6 = pkt->ip6_hdr;
- char *ip_payload = (char *) ip6 + 40;
+ char *ip_payload = (char *) ip6 + pkt_iph_len(pkt);
//TODO: Filter packets
switch (ip6->ip6_nxt) { /* FIXME: extension headers ??? */
diff --git a/src/packet.h b/src/packet.h
index 2b4f802..21d4c37 100644
--- a/src/packet.h
+++ b/src/packet.h
@@ -55,8 +55,19 @@ static inline void PACKET_INIT_STRUCT(struct pkt_hdr *p)
struct pkt_hdr packet; \
PACKET_INIT_STRUCT(&packet)
-#define pkt_ipv4_len(pkt) \
- (pkt)->iphdr->ihl * 4
+#define pkt_iph_len(pkt) __extension__ ({ \
+ __u8 len__ = 0; \
+ switch ((pkt)->pkt_protocol) { \
+ case ETH_P_IP: \
+ len__ = (pkt)->iphdr->ihl * 4; \
+ break; \
+ case ETH_P_IPV6: \
+ len__ = 40; \
+ break; \
+ }; \
+ len__; \
+ })
+
static inline __u8 pkt_ip_protocol(const struct pkt_hdr *p)
{
--
1.7.10.2
10 years, 10 months
[PATCH 4/4 v2] iphdr, ip6_hdr: add new members into pkt_hdr struct
by Nikola Pajkovsky
Signed-off-by: Nikola Pajkovsky <npajkovs(a)redhat.com>
---
src/detstats.c | 6 ++---
src/itrafmon.c | 70 +++++++++++++++++++++++++-------------------------------
src/packet.c | 52 +++++++++++++++++++++++++++++++++++------
src/packet.h | 25 +++++++++++---------
src/serv.c | 13 +++++------
5 files changed, 98 insertions(+), 68 deletions(-)
diff --git a/src/detstats.c b/src/detstats.c
index 2364662..4eebdaf 100644
--- a/src/detstats.c
+++ b/src/detstats.c
@@ -259,7 +259,6 @@ void detstats(char *iface, const struct OPTIONS *options, time_t facilitytime,
WINDOW *statwin;
PANEL *statpanel;
- struct iphdr *ipacket = NULL;
struct ip6_hdr *ip6packet = NULL;
int framelen = 0;
@@ -562,9 +561,8 @@ void detstats(char *iface, const struct OPTIONS *options, time_t facilitytime,
continue;
}
- ipacket = (struct iphdr *) pkt.pkt_payload;
- iplen = ntohs(ipacket->tot_len);
- ipproto = ipacket->protocol;
+ iplen = ntohs(pkt.iphdr->tot_len);
+ ipproto = pkt.iphdr->protocol;
update_proto_counter(&ifcounts.ipv4, outgoing, iplen);
break;
diff --git a/src/itrafmon.c b/src/itrafmon.c
index ce68688..c2baaf7 100644
--- a/src/itrafmon.c
+++ b/src/itrafmon.c
@@ -552,8 +552,6 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
{
int logging = options->logging;
- struct iphdr *ippacket;
- struct ip6_hdr *ip6packet;
unsigned int protocol;
unsigned int frag_off;
struct tcphdr *transpacket; /* IP-encapsulated packet */
@@ -1019,17 +1017,13 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
switch(pkt.pkt_protocol) {
case ETH_P_IP:
- ippacket = (struct iphdr *) pkt.pkt_payload;
- iphlen = ippacket->ihl * 4;
- ip6packet = NULL;
- protocol = ippacket->protocol;
- frag_off = ippacket->frag_off;
+ iphlen = pkt.iphdr->ihl * 4;
+ protocol = pkt.iphdr->protocol;
+ frag_off = pkt.iphdr->frag_off;
break;
case ETH_P_IPV6:
- ip6packet = (struct ip6_hdr *) pkt.pkt_payload;
iphlen = 40;
- ippacket = NULL;
- protocol = ip6packet->ip6_nxt;
+ protocol = pkt.ip6_hdr->ip6_nxt;
frag_off = 0;
break;
default:
@@ -1045,11 +1039,11 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
transpacket = (struct tcphdr *) (pkt.pkt_payload + iphlen);
if (protocol == IPPROTO_TCP) {
- if (ippacket != NULL) {
+ if (pkt.iphdr) {
tcpentry =
in_table(&table,
- ippacket->saddr,
- ippacket->daddr,
+ pkt.iphdr->saddr,
+ pkt.iphdr->daddr,
NULL, NULL,
ntohs(sport),
ntohs(dport),
@@ -1058,14 +1052,12 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
} else {
tcpentry =
in_table(&table, 0, 0,
- (uint8_t *) (&ip6packet->
- ip6_src.
- s6_addr),
- (uint8_t *) (&ip6packet->
- ip6_dst.
- s6_addr),
- ntohs(sport), ntohs(dport),
- ifname, logging, logfile, options);
+ (uint8_t *) &pkt.ip6_hdr->
+ ip6_src.s6_addr,
+ (uint8_t *) &pkt.ip6_hdr->
+ ip6_dst.s6_addr,
+ ntohs(sport), ntohs(dport),
+ ifname, logging, logfile, options);
}
/*
@@ -1082,21 +1074,21 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
* is not yet closed, or if it is a SYN packet.
*/
wasempty = (table.head == NULL);
- if (ippacket != NULL)
+ if (pkt.iphdr)
tcpentry =
addentry(&table,
- (unsigned long) ippacket->saddr,
- (unsigned long) ippacket->daddr,
+ (unsigned long) pkt.iphdr->saddr,
+ (unsigned long) pkt.iphdr->daddr,
NULL, NULL, sport, dport,
- ippacket->protocol,
+ pkt.iphdr->protocol,
ifname, &revlook, rvnfd,
options->servnames);
else
tcpentry =
addentry(&table, 0, 0,
- (uint8_t *) (&ip6packet->ip6_src.s6_addr),
- (uint8_t *) (&ip6packet->ip6_dst.s6_addr),
- sport, dport, ip6packet->ip6_nxt,
+ (uint8_t *) &pkt.ip6_hdr->ip6_src.s6_addr,
+ (uint8_t *) &pkt.ip6_hdr->ip6_dst.s6_addr,
+ sport, dport, pkt.ip6_hdr->ip6_nxt,
ifname, &revlook, rvnfd,
options->servnames);
if (tcpentry != NULL) {
@@ -1128,10 +1120,10 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
p_dstat = tcpentry->d_fstat;
}
- if (ippacket != NULL)
+ if (pkt.iphdr)
updateentry(&table, tcpentry, transpacket,
pkt.pkt_buf, pkt.pkt_hatype,
- pkt.pkt_len, br, ippacket->frag_off,
+ pkt.pkt_len, br, pkt.iphdr->frag_off,
logging, &revlook, rvnfd, options,
logfile);
else
@@ -1190,10 +1182,10 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
printentry(&table, tcpentry->oth_connection,
screen_idx, mode);
}
- } else if (ippacket != NULL) {
- fragment = ((ntohs(ippacket->frag_off) & 0x1fff) != 0);
+ } else if (pkt.iphdr) {
+ fragment = ((ntohs(pkt.iphdr->frag_off) & 0x1fff) != 0);
- if (ippacket->protocol == IPPROTO_ICMP) {
+ if (pkt.iphdr->protocol == IPPROTO_ICMP) {
/*
* Cancel the corresponding TCP entry if an ICMP
@@ -1205,23 +1197,23 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
process_dest_unreach(&table, (char *) transpacket,
ifname);
}
- add_othp_entry(&othptbl, &pkt, ippacket->saddr,
- ippacket->daddr, NULL, NULL, IS_IP,
- ippacket->protocol,
+ add_othp_entry(&othptbl, &pkt, pkt.iphdr->saddr,
+ pkt.iphdr->daddr, NULL, NULL, IS_IP,
+ pkt.iphdr->protocol,
(char *) transpacket,
ifname, &revlook, rvnfd,
logging, logfile,
options->servnames, fragment);
} else {
- if (ip6packet->ip6_nxt == IPPROTO_ICMPV6
+ if (pkt.ip6_hdr->ip6_nxt == IPPROTO_ICMPV6
&& (((struct icmp6_hdr *) transpacket)->icmp6_type == ICMP6_DST_UNREACH))
process_dest_unreach(&table, (char *) transpacket,
ifname);
add_othp_entry(&othptbl, &pkt, 0, 0,
- &ip6packet->ip6_src, &ip6packet->ip6_dst,
- IS_IP, ip6packet->ip6_nxt,
+ &pkt.ip6_hdr->ip6_src, &pkt.ip6_hdr->ip6_dst,
+ IS_IP, pkt.ip6_hdr->ip6_nxt,
(char *) transpacket, ifname,
&revlook, rvnfd,
logging, logfile, options->servnames,
diff --git a/src/packet.c b/src/packet.c
index fbd94f1..2da6686 100644
--- a/src/packet.c
+++ b/src/packet.c
@@ -20,6 +20,23 @@ packet.c - routines to open the raw socket, read socket data and
#include "packet.h"
#include "ipfrag.h"
+#define pkt_cast_hdrp_l2off_t(hdr, pkt, off) \
+ do { \
+ pkt->hdr = (struct hdr *) pkt->pkt_buf + off; \
+ } while (0)
+
+#define pkt_cast_hdrp_l2(hdr, pkt) \
+ pkt_cast_hdrp_l2off_t(hdr, pkt, 0)
+
+
+#define pkt_cast_hdrp_l3off_t(hdr, pkt, off) \
+ do { \
+ pkt->hdr = (struct hdr *) pkt->pkt_payload + off; \
+ } while (0)
+
+#define pkt_cast_hdrp_l3(hdr, pkt) \
+ pkt_cast_hdrp_l3off_t(hdr, pkt, 0)
+
/* code taken from http://www.faqs.org/rfcs/rfc1071.html. See section 4.1 "C" */
static int in_cksum(u_short * addr, int len)
{
@@ -46,7 +63,7 @@ static int packet_adjust(struct pkt_hdr *pkt)
switch (pkt->pkt_hatype) {
case ARPHRD_ETHER:
case ARPHRD_LOOPBACK:
- pkt_cast_hdrp(ethhdr, pkt, 0);
+ pkt_cast_hdrp_l2(ethhdr, pkt);
pkt->pkt_payload = pkt->pkt_buf;
pkt->pkt_payload += ETH_HLEN;
pkt->pkt_len -= ETH_HLEN;
@@ -76,7 +93,7 @@ static int packet_adjust(struct pkt_hdr *pkt)
pkt->pkt_len -= 4;
break;
case ARPHRD_FDDI:
- pkt_cast_hdrp(fddihdr, pkt, 0);
+ pkt_cast_hdrp_l2(fddihdr, pkt);
pkt->pkt_payload = pkt->pkt_buf;
pkt->pkt_payload += sizeof(struct fddihdr);
pkt->pkt_len -= sizeof(struct fddihdr);
@@ -92,6 +109,26 @@ static int packet_adjust(struct pkt_hdr *pkt)
return retval;
}
+/* initialize all layer3 protocol pointers (we need to initialize all
+ * of them, because of case we change pkt->pkt_protocol) */
+static void packet_set_l3_hdrp(struct pkt_hdr *pkt)
+{
+ switch (pkt->pkt_protocol) {
+ case ETH_P_IP:
+ pkt_cast_hdrp_l3(iphdr, pkt);
+ pkt->ip6_hdr = NULL;
+ break;
+ case ETH_P_IPV6:
+ pkt->iphdr = NULL;
+ pkt_cast_hdrp_l3(ip6_hdr, pkt);
+ break;
+ default:
+ pkt->iphdr = NULL;
+ pkt->ip6_hdr = NULL;
+ break;
+ }
+}
+
/* IPTraf input function; reads both keystrokes and network packets. */
int packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win)
{
@@ -114,7 +151,7 @@ int packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win)
ss = poll(pfds, nfds, DEFAULT_UPDATE_DELAY / 1000);
} while ((ss == -1) && (errno == EINTR));
- pkt->pkt_len = 0; /* signalize we have no packet prepared */
+ PACKET_INIT_STRUCT(pkt);
if ((ss > 0) && (pfds[0].revents & POLLIN) != 0) {
struct sockaddr_ll from;
socklen_t fromlen = sizeof(struct sockaddr_ll);
@@ -155,8 +192,10 @@ int packet_process(struct pkt_hdr *pkt, unsigned int *total_br,
if (packet_adjust(pkt) != 0)
return INVALID_PACKET;
-again: if (pkt->pkt_protocol == ETH_P_IP) {
- struct iphdr *ip;
+again:
+ packet_set_l3_hdrp(pkt);
+ if (pkt->pkt_protocol == ETH_P_IP) {
+ struct iphdr *ip = pkt->iphdr;
int hdr_check;
register int ip_checksum;
register int iphlen;
@@ -166,7 +205,6 @@ again: if (pkt->pkt_protocol == ETH_P_IP) {
* At this point, we're now processing IP packets. Start by getting
* IP header and length.
*/
- ip = (struct iphdr *) (pkt->pkt_payload);
iphlen = ip->ihl * 4;
/*
@@ -260,7 +298,7 @@ again: if (pkt->pkt_protocol == ETH_P_IP) {
} else if (pkt->pkt_protocol == ETH_P_IPV6) {
struct tcphdr *tcp;
struct udphdr *udp;
- struct ip6_hdr *ip6 = (struct ip6_hdr *) pkt->pkt_payload;
+ struct ip6_hdr *ip6 = pkt->ip6_hdr;
char *ip_payload = (char *) ip6 + 40;
//TODO: Filter packets
diff --git a/src/packet.h b/src/packet.h
index 72a7062..f032a39 100644
--- a/src/packet.h
+++ b/src/packet.h
@@ -35,22 +35,25 @@ struct pkt_hdr {
unsigned char pkt_addr[8]; /* Physical layer address */
struct ethhdr *ethhdr;
struct fddihdr *fddihdr;
+ struct iphdr *iphdr;
+ struct ip6_hdr *ip6_hdr;
char pkt_buf[MAX_PACKET_SIZE];
};
-#define pkt_cast_hdrp(hdr, pkt, off) \
- do { \
- pkt->hdr = (struct hdr *) pkt->pkt_buf + off; \
- } while (0)
-
+static inline void PACKET_INIT_STRUCT(struct pkt_hdr *p)
+{
+ p->pkt_bufsize = MAX_PACKET_SIZE;
+ p->pkt_payload = NULL;
+ p->ethhdr = NULL;
+ p->fddihdr = NULL;
+ p->iphdr = NULL;
+ p->ip6_hdr = NULL;
+ p->pkt_len = 0; /* signalize we have no packet prepared */
+}
#define PACKET_INIT(packet) \
- struct pkt_hdr packet = { \
- .pkt_bufsize = MAX_PACKET_SIZE, \
- .pkt_payload = NULL, \
- .ethhdr = NULL, \
- .fddihdr = NULL, \
- };
+ struct pkt_hdr packet; \
+ PACKET_INIT_STRUCT(&packet)
void open_socket(int *fd);
int packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win);
diff --git a/src/serv.c b/src/serv.c
index aeea6b2..caa8a6c 100644
--- a/src/serv.c
+++ b/src/serv.c
@@ -1048,9 +1048,6 @@ void servmon(char *ifname, const struct OPTIONS *options,
if (pkt.pkt_len <= 0)
continue;
- unsigned short ipproto;
- unsigned short iplen;
-
pkt_result =
packet_process(&pkt, &tot_br, &sport, &dport,
ofilter,
@@ -1060,14 +1057,16 @@ void servmon(char *ifname, const struct OPTIONS *options,
if (pkt_result != PACKET_OK)
continue;
+ unsigned short ipproto;
+ unsigned short iplen;
switch (pkt.pkt_protocol) {
case ETH_P_IP:
- ipproto = ((struct iphdr *) pkt.pkt_payload)->protocol;
- iplen = ntohs(((struct iphdr *) pkt.pkt_payload)->tot_len);
+ ipproto = pkt.iphdr->protocol;
+ iplen = ntohs(pkt.iphdr->tot_len);
break;
case ETH_P_IPV6:
- ipproto = ((struct ip6_hdr *) pkt.pkt_payload)->ip6_nxt; /* FIXME: extension headers ??? */
- iplen = ntohs(((struct ip6_hdr *) pkt.pkt_payload)->ip6_plen) + 40;
+ ipproto = pkt.ip6_hdr->ip6_nxt; /* FIXME: extension headers ??? */
+ iplen = ntohs(pkt.ip6_hdr->ip6_plen) + 40;
break;
default:
/* unknown link protocol */
--
1.7.10.2
10 years, 10 months
[PATCH 1/4] ethhdr: add new member into pkt_hdr struct
by Nikola Pajkovsky
before patch, when you want to access struct ethhdr you have to define
a new struct ethhdr and cast pkt->pkt_buf to struct ethhdr, which is
pretty annoying. Now ethhdr is in pkt_hdr struct and it's casted right
away ARPHDR_ETHER/ARPHDR_LOOP packet arrives.
Signed-off-by: Nikola Pajkovsky <npajkovs(a)redhat.com>
---
src/hostmon.c | 8 ++------
src/othptab.c | 6 ++----
src/packet.c | 1 +
src/packet.h | 16 ++++++++++++----
4 files changed, 17 insertions(+), 14 deletions(-)
diff --git a/src/hostmon.c b/src/hostmon.c
index feeb629..c4c8fb1 100644
--- a/src/hostmon.c
+++ b/src/hostmon.c
@@ -980,12 +980,8 @@ void hostmon(const struct OPTIONS *options, time_t facilitytime, char *ifptr,
/* get HW addresses */
switch (pkt.pkt_hatype) {
case ARPHRD_ETHER: {
- struct ethhdr *hdr_eth =
- (struct ethhdr *)pkt.pkt_buf;
- memcpy(scratch_saddr, hdr_eth->h_source,
- ETH_ALEN);
- memcpy(scratch_daddr, hdr_eth->h_dest,
- ETH_ALEN);
+ memcpy(scratch_saddr, pkt.ethhdr->h_source, ETH_ALEN);
+ memcpy(scratch_daddr, pkt.ethhdr->h_dest, ETH_ALEN);
list = elist;
break; }
case ARPHRD_FDDI: {
diff --git a/src/othptab.c b/src/othptab.c
index 62ce73f..831a907 100644
--- a/src/othptab.c
+++ b/src/othptab.c
@@ -183,10 +183,8 @@ struct othptabent *add_othp_entry(struct othptable *table, struct pkt_hdr *pkt,
if ((table->mac) || (!is_ip)) {
if (pkt->pkt_hatype == ARPHRD_ETHER) {
- convmacaddr((char *) (((struct ethhdr *) packet)->
- h_source), new_entry->smacaddr);
- convmacaddr((char *) (((struct ethhdr *) packet)->
- h_dest), new_entry->dmacaddr);
+ convmacaddr((char *) pkt->ethhdr->h_source, new_entry->smacaddr);
+ convmacaddr((char *) pkt->ethhdr->h_dest, new_entry->dmacaddr);
} else if (pkt->pkt_hatype == ARPHRD_FDDI) {
convmacaddr((char *) (((struct fddihdr *) packet)->
saddr), new_entry->smacaddr);
diff --git a/src/packet.c b/src/packet.c
index 20f20bf..1be23ec 100644
--- a/src/packet.c
+++ b/src/packet.c
@@ -46,6 +46,7 @@ static int packet_adjust(struct pkt_hdr *pkt)
switch (pkt->pkt_hatype) {
case ARPHRD_ETHER:
case ARPHRD_LOOPBACK:
+ pkt_cast_hdrp(ethhdr, pkt);
pkt->pkt_payload = pkt->pkt_buf;
pkt->pkt_payload += ETH_HLEN;
pkt->pkt_len -= ETH_HLEN;
diff --git a/src/packet.h b/src/packet.h
index 0bed4a0..6c6e1be 100644
--- a/src/packet.h
+++ b/src/packet.h
@@ -33,13 +33,21 @@ struct pkt_hdr {
unsigned char pkt_pkttype; /* Packet type: PACKET_OUTGOING, PACKET_BROADCAST, ... */
unsigned char pkt_halen; /* Length of address */
unsigned char pkt_addr[8]; /* Physical layer address */
+ struct ethhdr *ethhdr;
char pkt_buf[MAX_PACKET_SIZE];
};
-#define PACKET_INIT(packet) \
- struct pkt_hdr packet = { \
- .pkt_bufsize = MAX_PACKET_SIZE, \
- .pkt_payload = NULL \
+#define pkt_cast_hdrp(hdr, pkt) \
+ do { \
+ pkt->hdr = (struct hdr *) pkt->pkt_buf; \
+ } while (0)
+
+
+#define PACKET_INIT(packet) \
+ struct pkt_hdr packet = { \
+ .pkt_bufsize = MAX_PACKET_SIZE, \
+ .pkt_payload = NULL, \
+ .ethhdr = NULL, \
};
void open_socket(int *fd);
--
1.7.10.2
10 years, 10 months
[PATCH] capture-pkt.c: fflush() stdout only
by Vitezslav Samel
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/capture-pkt.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/src/capture-pkt.c b/src/capture-pkt.c
index 3cb403d..6df4611 100644
--- a/src/capture-pkt.c
+++ b/src/capture-pkt.c
@@ -57,7 +57,7 @@ int cmd_capture(int argc, char **argv)
continue;
printf(".");
- fflush(NULL);
+ fflush(stdout);
if (fp)
fwrite(&p, sizeof(p), 1, fp);
--
1.7.8.4
10 years, 10 months
[PATCH] ethhdr: add new member into pkt_hdr struct
by Nikola Pajkovsky
before patch, when you want to access struct ethhdr you have to define
a new struct ethhdr and cast pkt->pkt_buf to struct ethhdr, which is
pretty annoying. Now ethhdr is in pkt_hdr struct and it's casted right
away ARPHDR_ETHER/ARPHDR_LOOP packet arrives.
Signed-off-by: Nikola Pajkovsky <npajkovs(a)redhat.com>
---
src/hostmon.c | 8 ++------
src/othptab.c | 6 ++----
src/packet.c | 1 +
src/packet.h | 16 ++++++++++++----
4 files changed, 17 insertions(+), 14 deletions(-)
diff --git a/src/hostmon.c b/src/hostmon.c
index feeb629..c4c8fb1 100644
--- a/src/hostmon.c
+++ b/src/hostmon.c
@@ -980,12 +980,8 @@ void hostmon(const struct OPTIONS *options, time_t facilitytime, char *ifptr,
/* get HW addresses */
switch (pkt.pkt_hatype) {
case ARPHRD_ETHER: {
- struct ethhdr *hdr_eth =
- (struct ethhdr *)pkt.pkt_buf;
- memcpy(scratch_saddr, hdr_eth->h_source,
- ETH_ALEN);
- memcpy(scratch_daddr, hdr_eth->h_dest,
- ETH_ALEN);
+ memcpy(scratch_saddr, pkt.ethhdr->h_source, ETH_ALEN);
+ memcpy(scratch_daddr, pkt.ethhdr->h_dest, ETH_ALEN);
list = elist;
break; }
case ARPHRD_FDDI: {
diff --git a/src/othptab.c b/src/othptab.c
index 62ce73f..831a907 100644
--- a/src/othptab.c
+++ b/src/othptab.c
@@ -183,10 +183,8 @@ struct othptabent *add_othp_entry(struct othptable *table, struct pkt_hdr *pkt,
if ((table->mac) || (!is_ip)) {
if (pkt->pkt_hatype == ARPHRD_ETHER) {
- convmacaddr((char *) (((struct ethhdr *) packet)->
- h_source), new_entry->smacaddr);
- convmacaddr((char *) (((struct ethhdr *) packet)->
- h_dest), new_entry->dmacaddr);
+ convmacaddr((char *) pkt->ethhdr->h_source, new_entry->smacaddr);
+ convmacaddr((char *) pkt->ethhdr->h_dest, new_entry->dmacaddr);
} else if (pkt->pkt_hatype == ARPHRD_FDDI) {
convmacaddr((char *) (((struct fddihdr *) packet)->
saddr), new_entry->smacaddr);
diff --git a/src/packet.c b/src/packet.c
index 20f20bf..b01c0ac 100644
--- a/src/packet.c
+++ b/src/packet.c
@@ -46,6 +46,7 @@ static int packet_adjust(struct pkt_hdr *pkt)
switch (pkt->pkt_hatype) {
case ARPHRD_ETHER:
case ARPHRD_LOOPBACK:
+ cast_hdr(ethhdr, pkt);
pkt->pkt_payload = pkt->pkt_buf;
pkt->pkt_payload += ETH_HLEN;
pkt->pkt_len -= ETH_HLEN;
diff --git a/src/packet.h b/src/packet.h
index 0bed4a0..561f74e 100644
--- a/src/packet.h
+++ b/src/packet.h
@@ -33,13 +33,21 @@ struct pkt_hdr {
unsigned char pkt_pkttype; /* Packet type: PACKET_OUTGOING, PACKET_BROADCAST, ... */
unsigned char pkt_halen; /* Length of address */
unsigned char pkt_addr[8]; /* Physical layer address */
+ struct ethhdr *ethhdr;
char pkt_buf[MAX_PACKET_SIZE];
};
-#define PACKET_INIT(packet) \
- struct pkt_hdr packet = { \
- .pkt_bufsize = MAX_PACKET_SIZE, \
- .pkt_payload = NULL \
+#define pkt_cast_hdr(hdr, pkt) \
+ do { \
+ pkt.hdr = (struct hdr *) pkt.pkt_buf; \
+ } while (0)
+
+
+#define PACKET_INIT(packet) \
+ struct pkt_hdr packet = { \
+ .pkt_bufsize = MAX_PACKET_SIZE, \
+ .pkt_payload = NULL, \
+ .ethhdr = NULL, \
};
void open_socket(int *fd);
--
1.7.10.2
10 years, 10 months
[PATCH 0/3] iptraf-ng: cleanups
by Vitezslav Samel
Vitezslav Samel (3):
packet.c: do not poll() for key press when not needed
usage.c: fix spelling (*_buildin() --> *_builtin())
define __attribute__((format(printf, x, y))) as __printf(x,y)
src/error.h | 2 +-
src/iptraf-ng-compat.h | 12 ++++++------
src/packet.c | 2 +-
src/tui/msgboxes.h | 2 +-
src/usage.c | 10 +++++-----
5 files changed, 14 insertions(+), 14 deletions(-)
--
1.7.8.4
10 years, 10 months