1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
/*
* COPYRIGHT AND DISCLAIMER
*
* Copyright (C) 1996-1997 by the Regents of the University of California.
*
* IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
* EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS
* PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO
* OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
* MODIFICATIONS.
*
* For inquiries email Steve Gribble <gribble@cs.berkeley.edu>.
*/
/*
* Author: Steve Gribble
* Date: Nov. 23rd, 1996
* File: logparse.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <errno.h>
#include "logparse.h"
#include "utils.h"
// extern int errno; // -- this should come from errno.h
/**
** lf_get_next_entry will suck the next record out of the logfile, and
** return a lf_entry record witih the information stuffed into it. Note
** that memory WILL be allocated for the url field; the caller is
** responsible for freeing the memory when done. The logfile should have
** everything stored in network order, if all is well.
**
** This function returns 0 on success, 1 for EOF, and something else
** otherwise. On failure,
** no memory will have been allocated.
**/
int lf_get_next_entry(int logfile_fd, lf_entry *nextentry, int vers)
{
unsigned char blockbuf[60], *tmp;
int uln, ret;
if ((ret = correct_read(logfile_fd, (char *)blockbuf, (size_t) 60)) != 60) {
if (ret == 0)
return 1;
/* fprintf(stderr, "read 60 failed...%d\n", ret); */
return 2;
}
/* We got one! */
nextentry->version = vers;
memcpy( &(nextentry->crs), blockbuf+0, 4);
memcpy( &(nextentry->cru), blockbuf+4, 4);
memcpy( &(nextentry->srs), blockbuf+8, 4);
memcpy( &(nextentry->sru), blockbuf+12, 4);
memcpy( &(nextentry->sls), blockbuf+16, 4);
memcpy( &(nextentry->slu), blockbuf+20, 4);
memcpy( &(nextentry->cip), blockbuf+24, 4);
memcpy( &(nextentry->cpt), blockbuf+28, 2);
memcpy( &(nextentry->sip), blockbuf+30, 4);
memcpy( &(nextentry->spt), blockbuf+34, 2);
memcpy( &(nextentry->cprg), blockbuf+36, 1);
memcpy( &(nextentry->sprg), blockbuf+37, 1);
memcpy( &(nextentry->cims), blockbuf+38, 4);
memcpy( &(nextentry->sexp), blockbuf+42, 4);
memcpy( &(nextentry->slmd), blockbuf+46, 4);
memcpy( &(nextentry->rhl), blockbuf+50, 4);
memcpy( &(nextentry->rdl), blockbuf+54, 4);
memcpy( &(nextentry->urllen), blockbuf+58, 2);
/* Now let's read in that url */
uln = ntohs(nextentry->urllen);
nextentry->url = (unsigned char *) malloc(sizeof(char) *
(int) (uln + 1));
if (nextentry->url == NULL) {
fprintf(stderr, "out of memory in lf_get_next_netry!\n");
exit(1);
}
if ((ret = correct_read(logfile_fd, (char *) (nextentry->url), (size_t) uln))
!= uln ) {
if (ret == 0) {
free(nextentry->url);
return 1;
}
fprintf(stderr, "read of %d failed %d\n", uln, ret);
perror("aargh.");
free(nextentry->url);
return 2;
}
tmp = nextentry->url;
*(tmp + uln) = '\0';
return 0;
}
/**
** This function will convert all of the entries in the record into/from
** host order. This function is its own inverse. This function cannot
** fail.
**/
void lf_convert_order(lf_entry *convertme)
{
convertme->crs = ntohl(convertme->crs);
convertme->cru = ntohl(convertme->cru);
convertme->srs = ntohl(convertme->srs);
convertme->sru = ntohl(convertme->sru);
convertme->sls = ntohl(convertme->sls);
convertme->slu = ntohl(convertme->slu);
convertme->cip = ntohl(convertme->cip);
convertme->cpt = ntohs(convertme->cpt);
convertme->sip = ntohl(convertme->sip);
convertme->spt = ntohs(convertme->spt);
convertme->cims = ntohl(convertme->cims);
convertme->sexp = ntohl(convertme->sexp);
convertme->slmd = ntohl(convertme->slmd);
convertme->rhl = ntohl(convertme->rhl);
convertme->rdl = ntohl(convertme->rdl);
convertme->urllen = ntohs(convertme->urllen);
}
/**
** This function will write the entry pointed to by writeme back
** out to the file outf, in the canonical logfile binary format.
** It returns 0 on success, something else on failure.
**/
int lf_write(FILE *outf, lf_entry *writeme)
{
unsigned char blockbuf[60];
int uln, ret;
memcpy( blockbuf+0, &(writeme->crs), 4);
memcpy( blockbuf+4, &(writeme->cru), 4);
memcpy( blockbuf+8, &(writeme->srs), 4);
memcpy( blockbuf+12, &(writeme->sru), 4);
memcpy( blockbuf+16, &(writeme->sls), 4);
memcpy( blockbuf+20, &(writeme->slu), 4);
memcpy( blockbuf+24, &(writeme->cip), 4);
memcpy( blockbuf+28, &(writeme->cpt), 2);
memcpy( blockbuf+30, &(writeme->sip), 4);
memcpy( blockbuf+34, &(writeme->spt), 2);
memcpy( blockbuf+36, &(writeme->cprg), 1);
memcpy( blockbuf+37, &(writeme->sprg), 1);
memcpy( blockbuf+38, &(writeme->cims), 4);
memcpy( blockbuf+42, &(writeme->sexp), 4);
memcpy( blockbuf+46, &(writeme->slmd), 4);
memcpy( blockbuf+50, &(writeme->rhl), 4);
memcpy( blockbuf+54, &(writeme->rdl), 4);
memcpy( blockbuf+58, &(writeme->urllen), 2);
ret = fwrite(&(blockbuf[0]), 60, 1, outf);
if (ret != 1) {
fprintf(stderr, "write 60 failed...%d\n", ret);
perror("arrgh1");
return 1;
}
/* Now let's write out that url */
uln = ntohs(writeme->urllen);
ret = fwrite(writeme->url, (size_t) uln, 1, outf);
if (ret != 1) {
fprintf(stderr, "write of %d failed %d\n", uln, ret);
perror("aargh.");
return 2;
}
return 0;
}
/**
** This function will dump a human-readable output version of the record
** to the passed-in file pointer. Assume that the record is in NETWORK
** order. Nothing can possibly go wrong. :)
**/
void lf_dump(FILE *dumpf, lf_entry *dumpme)
{
char addr1buf[128], addr2buf[128];
lf_convert_order(dumpme);
lf_ntoa(dumpme->cip, addr1buf);
lf_ntoa(dumpme->sip, addr2buf);
fprintf(dumpf, "%lu:%lu %lu:%lu %lu:%lu %s:%hu %s:%hu %u %u ",
dumpme->crs, dumpme->cru, dumpme->srs, dumpme->sru,
dumpme->sls, dumpme->slu, addr1buf, dumpme->cpt,
addr2buf, dumpme->spt, dumpme->cprg, dumpme->sprg);
fprintf(dumpf, "%lu %lu %lu %lu %lu %u %s\n",
dumpme->cims, dumpme->sexp, dumpme->slmd, dumpme->rhl,
dumpme->rdl, dumpme->urllen, dumpme->url);
lf_convert_order(dumpme);
}
/**
** lf_ntoa takes a network IP address (in host order) and converts it
** to an ascii representation. addrbuf had better be 16 bytes or
** more...
**/
void lf_ntoa(unsigned long addr, char *addrbuf)
{
sprintf(addrbuf, "%lu.%lu.%lu.%lu",
(addr >> 24),
(addr >> 16) % 256,
(addr >> 8) % 256,
(addr) % 256);
}
|