diff options
Diffstat (limited to 'pcap-sita.c')
-rw-r--r-- | pcap-sita.c | 134 |
1 files changed, 101 insertions, 33 deletions
diff --git a/pcap-sita.c b/pcap-sita.c index d507e1e..b9dda9c 100644 --- a/pcap-sita.c +++ b/pcap-sita.c @@ -25,7 +25,7 @@ */ #ifdef HAVE_CONFIG_H -#include "config.h" +#include <config.h> #endif #include <stdio.h> @@ -183,7 +183,7 @@ static int read_client_nbytes(int fd, int count, unsigned char *buf) { find_unit_by_fd(fd, &chassis, &geoslot, &u); while (count) { if ((len = recv(fd, buf, count, 0)) <= 0) return -1; /* read in whatever data was sent to us */ - count -= len; + count -= len; buf += len; } /* till we have everything we are looking for */ return 0; @@ -208,10 +208,14 @@ static void empty_unit(int chassis, int geoslot) { empty_unit_iface(u); if (u->imsg) { /* then if an inbound message buffer exists */ - u->imsg = (char *)realloc(u->imsg, 1); /* and re-allocate the old large buffer into a new small one */ - if (u->imsg == NULL) { /* oops, realloc call failed */ + void *bigger_buffer; + + bigger_buffer = (char *)realloc(u->imsg, 1); /* and re-allocate the old large buffer into a new small one */ + if (bigger_buffer == NULL) { /* oops, realloc call failed */ fprintf(stderr, "Warning...call to realloc() failed, value of errno is %d\n", errno); - + return; + } + u->imsg = bigger_buffer; } } @@ -262,7 +266,7 @@ int acn_parse_hosts_file(char *errbuf) { /* returns: -1 = error, 0 = OK */ empty_unit_table(); if ((fp = fopen("/etc/hosts", "r")) == NULL) { /* try to open the hosts file and if it fails */ - snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot open '/etc/hosts' for reading."); /* return the nohostsfile error response */ + pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Cannot open '/etc/hosts' for reading."); /* return the nohostsfile error response */ return -1; } while (fgets(buf, MAX_LINE_SIZE-1, fp)) { /* while looping over the file */ @@ -285,11 +289,12 @@ int acn_parse_hosts_file(char *errbuf) { /* returns: -1 = error, 0 = OK */ geoslot = *(ptr2 + 5) - '0'; /* and geo-slot number */ if (chassis < 1 || chassis > MAX_CHASSIS || geoslot < 1 || geoslot > MAX_GEOSLOT) { /* if the chassis and/or slot numbers appear to be bad... */ - snprintf(errbuf, PCAP_ERRBUF_SIZE, "Invalid ACN name in '/etc/hosts'."); /* warn the user */ + pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Invalid ACN name in '/etc/hosts'."); /* warn the user */ continue; /* and ignore the entry */ } if ((ptr2 = (char *)malloc(strlen(ptr) + 1)) == NULL) { - snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); continue; } strcpy(ptr2, ptr); /* copy the IP address into our malloc'ed memory */ @@ -402,14 +407,14 @@ static void acn_freealldevs(void) { static void nonUnified_IOP_port_name(char *buf, size_t bufsize, const char *proto, unit_t *u) { - snprintf(buf, bufsize, "%s_%d_%d", proto, u->chassis, u->geoslot); + pcap_snprintf(buf, bufsize, "%s_%d_%d", proto, u->chassis, u->geoslot); } static void unified_IOP_port_name(char *buf, size_t bufsize, const char *proto, unit_t *u, int IOPportnum) { int portnum; portnum = ((u->chassis - 1) * 64) + ((u->geoslot - 1) * 8) + IOPportnum + 1; - snprintf(buf, bufsize, "%s_%d", proto, portnum); + pcap_snprintf(buf, bufsize, "%s_%d", proto, portnum); } static char *translate_IOP_to_pcap_name(unit_t *u, char *IOPname, bpf_u_int32 iftype) { @@ -553,10 +558,10 @@ static void sort_if_table(void) { } if (has_swapped == 0) return; - } + } return; } - + static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 = OK */ int chassis, geoslot; unit_t *u; @@ -568,6 +573,7 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 char *newname; bpf_u_int32 interfaceType; unsigned char flags; + void *bigger_buffer; prev_iff = 0; for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) { @@ -577,7 +583,8 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 ptr = u->imsg; /* point to the start of the msg for this IOP */ while (ptr < (u->imsg + u->len)) { if ((iff = malloc(sizeof(pcap_if_t))) == NULL) { - snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, "malloc"); return -1; } memset((char *)iff, 0, sizeof(pcap_if_t)); /* bzero() is deprecated, replaced with memset() */ @@ -586,7 +593,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 if (*ptr) { /* if there is a count for the name */ if ((iff->name = malloc(*ptr + 1)) == NULL) { /* get that amount of space */ - snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc"); return -1; } memcpy(iff->name, (ptr + 1), *ptr); /* copy the name into the malloc'ed space */ @@ -597,7 +606,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 if (*ptr) { /* if there is a count for the description */ if ((iff->description = malloc(*ptr + 1)) == NULL) { /* get that amount of space */ - snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc"); return -1; } memcpy(iff->description, (ptr + 1), *ptr); /* copy the name into the malloc'ed space */ @@ -617,15 +628,19 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 prev_addr = 0; while (address_count--) { if ((addr = malloc(sizeof(pcap_addr_t))) == NULL) { - snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc"); return -1; } -+ memset((char *)addr, 0, sizeof(pcap_addr_t)); /* bzero() is deprecated, replaced with memset() */ + memset((char *)addr, 0, sizeof(pcap_addr_t)); /* bzero() is deprecated, replaced with memset() */ if (iff->addresses == 0) iff->addresses = addr; if (prev_addr) prev_addr->next = addr; /* insert a forward link */ if (*ptr) { /* if there is a count for the address */ if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { /* get that amount of space */ - snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, + errno, "malloc"); return -1; } memset((char *)s, 0, sizeof(struct sockaddr_in)); /* bzero() is deprecated, replaced with memset() */ @@ -637,7 +652,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 ptr++; /* then forwards one more for the 'length of the address' field */ if (*ptr) { /* process any netmask */ if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { - snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, + errno, "malloc"); return -1; } /* bzero() is deprecated, replaced with memset() */ @@ -651,7 +668,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 ptr++; if (*ptr) { /* process any broadcast address */ if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { - snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, + errno, "malloc"); return -1; } /* bzero() is deprecated, replaced with memset() */ @@ -665,7 +684,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 ptr++; if (*ptr) { /* process any destination address */ if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { - snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, + errno, "malloc"); return -1; } /* bzero() is deprecated, replaced with memset() */ @@ -682,10 +703,13 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 prev_iff = iff; newname = translate_IOP_to_pcap_name(u, iff->name, interfaceType); /* add a translation entry and get a point to the mangled name */ - if ((iff->name = realloc(iff->name, strlen(newname) + 1)) == NULL) { /* we now re-write the name stored in the interface list */ - snprintf(errbuf, PCAP_ERRBUF_SIZE, "realloc: %s", pcap_strerror(errno)); + bigger_buffer = realloc(iff->name, strlen(newname) + 1)); + if (bigger_buffer == NULL) { /* we now re-write the name stored in the interface list */ + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, "realloc"); return -1; } + iff->name = bigger_buffer; strcpy(iff->name, newname); /* to this new name */ } } @@ -814,9 +838,9 @@ static int acn_open_live(const char *name, char *errbuf, int *linktype) { /* re int chassis, geoslot; unit_t *u; iface_t *p; - pcap_if_t *alldevsp; + pcap_if_list_t devlist; - pcap_findalldevs_interfaces(&alldevsp, errbuf); + pcap_platform_finddevs(&devlist, errbuf); for (chassis = 0; chassis <= MAX_CHASSIS; chassis++) { /* scan the table... */ for (geoslot = 0; geoslot <= MAX_GEOSLOT; geoslot++) { u = &units[chassis][geoslot]; @@ -860,7 +884,7 @@ static void acn_start_monitor(int fd, int snaplen, int timeout, int promiscuous, } static int pcap_inject_acn(pcap_t *p, const void *buf _U_, size_t size _U_) { - strlcpy(p->errbuf, "Sending packets isn't supported on ACN adapters", + pcap_strlcpy(p->errbuf, "Sending packets isn't supported on ACN adapters", PCAP_ERRBUF_SIZE); return (-1); } @@ -892,7 +916,7 @@ static int pcap_setfilter_acn(pcap_t *handle, struct bpf_program *bpf) { } static int pcap_setdirection_acn(pcap_t *handle, pcap_direction_t d) { - snprintf(handle->errbuf, sizeof(handle->errbuf), + pcap_snprintf(handle->errbuf, sizeof(handle->errbuf), "Setting direction is not supported on ACN adapters"); return -1; } @@ -951,7 +975,7 @@ static int pcap_read_acn(pcap_t *handle, int max_packets, pcap_handler callback, pcap_header.caplen = ntohl(*(uint32_t *)&packet_header[8]); /* caplen */ pcap_header.len = ntohl(*(uint32_t *)&packet_header[12]); /* len */ - handle->bp = handle->buffer + handle->offset; /* start off the receive pointer at the right spot */ + handle->bp = (u_char *)handle->buffer + handle->offset; /* start off the receive pointer at the right spot */ if (acn_read_n_bytes_with_timeout(handle, pcap_header.caplen) == -1) return 0; /* then try to read in the rest of the data */ callback(user, &pcap_header, handle->bp); /* call the user supplied callback function */ @@ -981,10 +1005,22 @@ static int pcap_activate_sita(pcap_t *handle) { handle->read_op = pcap_read_acn; handle->stats_op = pcap_stats_acn; - fd = acn_open_live(handle->opt.source, handle->errbuf, + fd = acn_open_live(handle->opt.device, handle->errbuf, &handle->linktype); if (fd == -1) return PCAP_ERROR; + + /* + * Turn a negative snapshot value (invalid), a snapshot value of + * 0 (unspecified), or a value bigger than the normal maximum + * value, into the maximum allowed value. + * + * If some application really *needs* a bigger snapshot + * length, we should just increase MAXIMUM_SNAPLEN. + */ + if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN) + handle->snapshot = MAXIMUM_SNAPLEN; + handle->fd = fd; handle->bufsize = handle->snapshot; @@ -992,8 +1028,8 @@ static int pcap_activate_sita(pcap_t *handle) { handle->buffer = malloc(handle->bufsize + handle->offset); if (!handle->buffer) { - snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); pcap_cleanup_acn(handle); return PCAP_ERROR; } @@ -1007,13 +1043,45 @@ static int pcap_activate_sita(pcap_t *handle) { return 0; } -pcap_t *pcap_create_interface(const char *device, char *ebuf) { +pcap_t *pcap_create_interface(const char *device _U_, char *ebuf) { pcap_t *p; - p = pcap_create_common(device, ebuf, 0); + p = pcap_create_common(ebuf, 0); if (p == NULL) return (NULL); p->activate_op = pcap_activate_sita; return (p); } + +int pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { + + //printf("pcap_findalldevs()\n"); // fulko + + *alldevsp = 0; /* initialize the returned variables before we do anything */ + strcpy(errbuf, ""); + if (acn_parse_hosts_file(errbuf)) /* scan the hosts file for potential IOPs */ + { + //printf("pcap_findalldevs() returning BAD after parsehosts\n"); // fulko + return -1; + } + //printf("pcap_findalldevs() got hostlist now finding devs\n"); // fulko + if (acn_findalldevs(errbuf)) /* then ask the IOPs for their monitorable devices */ + { + //printf("pcap_findalldevs() returning BAD after findalldevs\n"); // fulko + return -1; + } + devlistp->beginning = acn_if_list; + acn_if_list = 0; /* then forget our list head, because someone will call pcap_freealldevs() to empty the malloc'ed stuff */ + //printf("pcap_findalldevs() returning ZERO OK\n"); // fulko + return 0; +} + +/* + * Libpcap version string. + */ +const char * +pcap_lib_version(void) +{ + return PCAP_VERSION_STRING " (SITA-only)"; +} |