OpenCALEA
Date Prev | Date Next |
Date Index |
Thread Index |
Author Index |
Historical
[OpenCALEA] misc patch
- From: Jesse Norell
- Date: Mon Mar 12 13:53:16 2007
Hello,
Ok, here's a patch that does the following:
- minor include reordering to build under freebsd
- fixes usage messages to reflect actual required/supported options
- checks return values on some system calls
- uses connected udp sockets
On that last bit, using connected sockets allows you to catch errors
like "Connection refused" if the collector isn't listening, etc. There
are some implications that go along with this, eg. if a remote collector
is temporarily down and a router sends a host unreachable back, do you
want tap to exit or keep trying, knowing that the collector should come
back up eventually? I made it exit on most errors, but only print an
error for the cases I believe are temporary (see error codes from "man 2
send); this may be desirable or not, depending on whether the controller
can catch tap failures and restart intercepts.
This is against 0.40 ... I'll use svn from now on, but had this
already (mostly) done.
Jesse
--
Jesse Norell - jesse@kci.net
Kentec Communications, Inc.
diff -ruN open_calea-0.4/src/calea.c open_calea-0.4-freebsd/src/calea.c
--- open_calea-0.4/src/calea.c Sun Feb 25 18:53:13 2007
+++ open_calea-0.4-freebsd/src/calea.c Mon Mar 12 11:08:35 2007
@@ -2,8 +2,11 @@
#include <stdio.h>
#include <stdlib.h>
+#include <errno.h>
#include <pcap.h>
#include <net/ethernet.h>
+#include <netinet/in.h>
+#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
@@ -11,7 +14,6 @@
#include <search.h>
#include <sys/types.h>
#include <sys/socket.h>
-#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include "calea.h"
@@ -40,8 +42,10 @@
int bytes_sent;
- if ( (bytes_sent = sendto ( *send_socket, packet, length, 0,
- (struct sockaddr *) send_addr, sizeof(struct sockaddr))) == -1 ) {
+ if ( (bytes_sent = send ( *send_socket, packet, length, 0)) == -1 ) {
+ perror("send");
+ if (! (errno == EHOSTUNREACH || errno == EHOSTDOWN || errno == ENETDOWN))
+ exit ( -1 );
return -1;
}
return bytes_sent;
@@ -55,8 +59,10 @@
int bytes_sent;
- if ( (bytes_sent = sendto ( *cmii_send_socket, packet, length, 0,
- (struct sockaddr *) cmii_send_addr, sizeof(struct sockaddr))) == -1 ) {
+ if ( (bytes_sent = send ( *cmii_send_socket, packet, length, 0)) == -1 ) {
+ perror("send");
+ if (! (errno == EHOSTUNREACH || errno == EHOSTDOWN || errno == ENETDOWN))
+ exit ( -1 );
return -1;
}
return bytes_sent;
diff -ruN open_calea-0.4/src/lea_collection.c open_calea-0.4-freebsd/src/lea_collection.c
--- open_calea-0.4/src/lea_collection.c Sun Feb 25 18:53:07 2007
+++ open_calea-0.4-freebsd/src/lea_collection.c Mon Mar 12 11:15:23 2007
@@ -5,6 +5,8 @@
#include <pcap.h>
#include <net/ethernet.h>
+#include <netinet/in.h>
+#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
@@ -12,11 +14,10 @@
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/socket.h>
-#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
-
#include <time.h>
+
#include "lea_collection.h"
#include "calea.h"
@@ -40,8 +41,8 @@
void usage ( void ) {
- printf ( "Usage: lea_collector -t <cmii-capture-file> " );
- printf ( "[-f <capture-file>] " );
+ printf ( "Usage: lea_collector -t cmii-capture-file " );
+ printf ( "-f capture-file " );
printf ( " [-m cmc-port] [-n cmii-port] [-x cooked-format]\n" );
}
@@ -109,6 +110,8 @@
exit ( 1 );
}
+ memset ( (char *)&cmc_receiver_addr, '\0', sizeof(cmc_receiver_addr) );
+
cmc_receiver_addr.sin_family = AF_INET;
cmc_receiver_addr.sin_port = htons ( cmc_port );
cmc_receiver_addr.sin_addr.s_addr = INADDR_ANY;
@@ -127,6 +130,8 @@
exit ( 1 );
}
+ memset ( (char *)&cmii_receiver_addr, '\0', sizeof(cmii_receiver_addr) );
+
cmii_receiver_addr.sin_family = AF_INET;
cmii_receiver_addr.sin_port = htons ( cmii_port );
cmii_receiver_addr.sin_addr.s_addr = INADDR_ANY;
@@ -147,6 +152,11 @@
FD_SET( cmii_receiver_socket, &sock_fds );
cmii_fp = fopen ( cmii_capture_file , "w" );
+ if (cmii_fp == NULL) {
+ perror ( "fopen" );
+ exit ( 1 );
+ }
+
if ( capture_file != NULL ) {
FD_SET( cmc_receiver_socket, &sock_fds );
if ( cooked_format == 1 ) {
@@ -157,6 +167,10 @@
}
pd = pcap_dump_open( pt, capture_file );
+ if (pd == NULL) {
+ perror ( "pcap_dump_open" );
+ exit ( 1 );
+ }
}
len = sizeof ( struct sockaddr );
@@ -186,14 +200,18 @@
myaddr2.s_addr = ntohl(cmiipkt->pkt_header.dstIP);
snprintf ( ts, 24, "%s", cmiipkt->cmiih.ts);
- fprintf ( cmii_fp, "%s, ", cmiipkt->cmiih.contentID);
- fprintf ( cmii_fp, "%s, ", cmiipkt->cmiih.caseID);
- fprintf ( cmii_fp, "%s, ", cmiipkt->cmiih.IAPSystemID);
- fprintf ( cmii_fp, "%s, ", ts);
- fprintf ( cmii_fp, "%s, ", inet_ntoa(myaddr));
- fprintf ( cmii_fp, "%s, ", inet_ntoa(myaddr2));
- fprintf ( cmii_fp, "%d, ", ntohs(cmiipkt->pkt_header.srcPort));
- fprintf ( cmii_fp, "%d\n", ntohs(cmiipkt->pkt_header.dstPort));
+ if ( (fprintf ( cmii_fp, "%s, ", cmiipkt->cmiih.contentID) < 0)
+ || (fprintf ( cmii_fp, "%s, ", cmiipkt->cmiih.caseID) < 0)
+ || (fprintf ( cmii_fp, "%s, ", cmiipkt->cmiih.IAPSystemID) < 0)
+ || (fprintf ( cmii_fp, "%s, ", ts) < 0)
+ || (fprintf ( cmii_fp, "%s, ", inet_ntoa(myaddr)) < 0)
+ || (fprintf ( cmii_fp, "%s, ", inet_ntoa(myaddr2)) < 0)
+ || (fprintf ( cmii_fp, "%d, ", ntohs(cmiipkt->pkt_header.srcPort)) < 0)
+ || (fprintf ( cmii_fp, "%d\n", ntohs(cmiipkt->pkt_header.dstPort)) < 0)
+ ) {
+ perror("fprintf");
+ exit( -1 );
+ }
}
if ( capture_file != NULL ) {
diff -ruN open_calea-0.4/src/tap.c open_calea-0.4-freebsd/src/tap.c
--- open_calea-0.4/src/tap.c Sun Feb 25 18:58:05 2007
+++ open_calea-0.4-freebsd/src/tap.c Fri Mar 9 17:02:39 2007
@@ -7,6 +7,8 @@
#define __FAVOR_BSD
#include <unistd.h>
#include <net/ethernet.h>
+#include <netinet/in.h>
+#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
@@ -14,7 +16,6 @@
#include <search.h>
#include <sys/types.h>
#include <sys/socket.h>
-#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
@@ -96,9 +97,10 @@
void usage ( void ) {
- printf ( "Usage: main -d <dest-ip> [-i interface] [-c]" );
- printf ( " [-m cmc-port] [-n cmii-port] [-x content-id]" );
- printf ( " [-y case-id] [-z iap-system-id]\n" );
+ printf ( "Usage: tap -i interface -x content-id -y case-id" );
+ printf ( " -z iap-system-id [-d dest-ip] [-c]" );
+ printf ( " [-m cmc-port] [-n cmii-port]" );
+ printf ( " [-f capture-filter]\n" );
}
@@ -158,6 +160,7 @@
}
if ( interface == NULL ) {
+ printf ( "interface must be specified...\n");
usage ();
exit ( -1 );
}
@@ -197,6 +200,7 @@
filter, pcap_geterr(handle) );
return( 2 );
}
+
if ( pcap_setfilter( handle, &fp ) == -1 ) {
fprintf( stderr, "Couldn't install filter %s: %s\n",
filter, pcap_geterr(handle) );
@@ -208,18 +212,34 @@
exit ( 1 );
}
+ memset ( (char *)&send_cmc_addr, '\0', sizeof(send_cmc_addr) );
+
send_cmc_addr.sin_family = AF_INET;
send_cmc_addr.sin_port = htons( cmc_port );
- send_cmc_addr.sin_addr.s_addr = inet_addr(dest_ip);;
+ send_cmc_addr.sin_addr.s_addr = inet_addr(dest_ip);
+
+ if (( connect(send_cmc_socket, (struct sockaddr *)&send_cmc_addr,
+ sizeof(send_cmc_addr)) ) == -1 ) {
+ perror( "connect" );
+ exit ( 1 );
+ }
if (( send_cmii_socket = socket ( PF_INET, SOCK_DGRAM, 0 )) == -1 ) {
perror( "socket" );
exit ( 1 );
}
+ memset ( (char *)&send_cmii_addr, '\0', sizeof(send_cmii_addr) );
+
send_cmii_addr.sin_family = AF_INET;
send_cmii_addr.sin_port = htons( cmii_port );
- send_cmii_addr.sin_addr.s_addr = inet_addr(dest_ip);;
+ send_cmii_addr.sin_addr.s_addr = inet_addr(dest_ip);
+
+ if (( connect(send_cmii_socket, (struct sockaddr *)&send_cmii_addr,
+ sizeof(send_cmii_addr)) ) == -1 ) {
+ perror( "connect" );
+ exit ( 1 );
+ }
printf ( "cmc send socket: %d\n", send_cmc_socket);
printf ( "cmii send socket: %d\n", send_cmii_socket);
diff -ruN open_calea-0.4/src/tap.h open_calea-0.4-freebsd/src/tap.h
--- open_calea-0.4/src/tap.h Sun Feb 25 18:53:45 2007
+++ open_calea-0.4-freebsd/src/tap.h Fri Mar 9 13:24:47 2007
@@ -3,8 +3,6 @@
#ifndef _TAP_H
#define _TAP_H
-#include <time.h>
-
int send_cmc_socket;
struct sockaddr_in send_cmc_addr;
|