Skip to content

Commit

Permalink
epcap_return: return on failure
Browse files Browse the repository at this point in the history
If opening the pcap descriptor fails, return and allow the main flow
control to exit.

Save the pcap error message to the epcap state. Currently the error
message is discarded. The plan is to send it to the port process.
  • Loading branch information
msantos committed Nov 20, 2017
1 parent 9566f04 commit 9e81915
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
46 changes: 33 additions & 13 deletions c_src/epcap.c
Expand Up @@ -170,12 +170,17 @@ main(int argc, char *argv[])
VERBOSE(2, "env:%s\n", *environ);
}

IS_LTZERO(fd = open("/dev/null", O_RDWR));
fd = open("/dev/null", O_RDWR);

if (fd < 0)
exit(errno);

if (epcap_priv_runasuser(ep) < 0)
err(EXIT_FAILURE, "epcap_priv_runasuser");

IS_LTZERO(epcap_open(ep));
if (epcap_open(ep) < 0)
exit(errno);

if (epcap_priv_drop(ep) < 0)
exit (1);

Expand Down Expand Up @@ -270,21 +275,32 @@ epcap_send(EPCAP_STATE *ep)
static int
epcap_open(EPCAP_STATE *ep)
{
char errbuf[PCAP_ERRBUF_SIZE];

if (ep->file) {
PCAP_ERRBUF(ep->p = pcap_open_offline(ep->file, errbuf));
ep->p = pcap_open_offline(ep->file, ep->errbuf);

if (ep->p == NULL)
return -1;
} else {
if (ep->dev == NULL)
PCAP_ERRBUF(ep->dev = pcap_lookupdev(errbuf));
if (ep->dev == NULL) {
ep->dev = pcap_lookupdev(ep->errbuf);

if (ep->dev == NULL)
return -1;
}

#ifdef HAVE_PCAP_CREATE
PCAP_ERRBUF(ep->p = pcap_create(ep->dev, errbuf));
ep->p = pcap_create(ep->dev, ep->errbuf);

if (ep->p == NULL)
return -1;

(void)pcap_set_snaplen(ep->p, ep->snaplen);
(void)pcap_set_promisc(ep->p, ep->opt & EPCAP_OPT_PROMISC);
(void)pcap_set_timeout(ep->p, ep->timeout);

if (ep->bufsz > 0)
(void)pcap_set_buffer_size(ep->p, ep->bufsz);

switch (pcap_activate(ep->p)) {
case 0:
break;
Expand All @@ -293,14 +309,18 @@ epcap_open(EPCAP_STATE *ep)
case PCAP_WARNING_PROMISC_NOTSUP:
case PCAP_ERROR_NO_SUCH_DEVICE:
case PCAP_ERROR_PERM_DENIED:
pcap_perror(ep->p, "pcap_activate: ");
exit(EXIT_FAILURE);
(void)strncpy(ep->errbuf, pcap_geterr(ep->p),
sizeof(ep->errbuf)-1);
return -1;
default:
exit(EXIT_FAILURE);
return -1;
}
#else
PCAP_ERRBUF(ep->p = pcap_open_live(ep->dev, ep->snaplen,
ep->opt & EPCAP_OPT_PROMISC, ep->timeout, errbuf));
ep->p = pcap_open_live(ep->dev, ep->snaplen,
ep->opt & EPCAP_OPT_PROMISC, ep->timeout, ep->errbuf);

if (ep->p == NULL)
return -1;
#endif

/* monitor mode */
Expand Down
6 changes: 1 addition & 5 deletions c_src/epcap.h
Expand Up @@ -66,11 +66,6 @@
#define EPCAP_CHROOT "/var/empty"
#define EPCAP_FILTER "" /* match any packet */

#define PCAP_ERRBUF(x) do { \
if ((x) == NULL) \
errx(EXIT_FAILURE, "%s: %s", #x, errbuf); \
} while (0);

#define IS_FALSE(x) do { \
if ((x) != 0) \
errx(EXIT_FAILURE, "%s", #x); \
Expand Down Expand Up @@ -110,6 +105,7 @@ typedef struct {
char *group; /* run as unprivilted group */
char *chroot; /* chroot directory */
char *file; /* filename in case we read from pcap file */
char errbuf[PCAP_ERRBUF_SIZE]; /* pcap error */
} EPCAP_STATE;


Expand Down

0 comments on commit 9e81915

Please sign in to comment.