buf_t buf_tok(buf_t buf, void *sep, int len) { static struct buf *savebuf, tokbuf; int off; if (buf != NULL) savebuf = buf; if (sep == NULL && buf_len(savebuf) >= len) { tokbuf.base = buf_ptr(savebuf); tokbuf.offset = 0; tokbuf.size = tokbuf.end = len; buf_skip(savebuf, len); } else if ((off = buf_index(savebuf, sep, len)) != -1) { tokbuf.base = buf_ptr(savebuf); tokbuf.offset = 0; tokbuf.size = tokbuf.end = off; buf_skip(savebuf, off + len); } else if (buf_len(savebuf) > 0) { tokbuf.base = buf_ptr(savebuf); tokbuf.offset = 0; tokbuf.size = tokbuf.end = buf_len(savebuf); savebuf->offset = savebuf->end; } else return (NULL); return (&tokbuf); }
int decode_imap(u_char *buf, int len, u_char *obuf, int olen) { struct buf *line, inbuf, outbuf; int i; buf_init(&inbuf, buf, len); buf_init(&outbuf, obuf, olen); while ((i = buf_index(&inbuf, "\r\n", 2)) != -1) { line = buf_tok(&inbuf, NULL, i); buf_skip(&inbuf, 2); if ((i = buf_index(line, " ", 1)) != -1) { buf_skip(line, i + 1); if (buf_cmp(line, "LOGIN ", 6) == 0) { buf_putf(&outbuf, "%.*s\n", buf_len(line), buf_ptr(line)); } } } buf_end(&outbuf); return (buf_len(&outbuf)); }
/* non-blocking, returns socket descriptor or -1 if none available * NOTE: This function assumes that the cursor of all buf's is zero, * and it can call buf_reset(source, tag). */ int MPINU_rank_of_msg_avail_in_cache(int source, int tag) { if (source != MPI_ANY_SOURCE) { if (tag == MPI_ANY_TAG) { if (buf_avail(source, sizeof(struct msg_hdr))) return source; } else while (buf_avail(source, sizeof(struct msg_hdr))) { struct msg_hdr *hdr = buf_peek(source, sizeof(struct msg_hdr)); if ( ntohl( hdr->tag ) == tag ) { buf_reset(source, tag); /* clean up for next caller */ return source; } else { /* Else skip header and body */ buf_skip( source, sizeof(struct msg_hdr) + ntohl(hdr->size) ); } } buf_reset(source, tag); /* clean up for next caller */ } else /* else source == MPI_ANY_SOURCE */ for (source = 0; source < MPINU_num_slaves; source++) { int rank = MPINU_rank_of_msg_avail_in_cache(source, tag); if (rank != -1) return rank; } /* if no source had buf_avail, then no msg avail */ return -1; }
int decode_vrrp(u_char *buf, int len, u_char *obuf, int olen) { struct buf *b, inbuf, outbuf; struct vrrp_header *vrrp; buf_init(&inbuf, buf, len); buf_init(&outbuf, obuf, olen); vrrp = (struct vrrp_header *)buf_ptr(&inbuf); if (buf_len(&inbuf) < sizeof(*vrrp)) return (0); /* We only care about VRRP_AUTH_SIMPLE */ if (ntohs(vrrp->vr_auth) != VRRP_AUTH_SIMPLE) return (0); /* XXX - probably want to verify checksum */ /* Forward to Authentication Data */ buf_skip(&inbuf, sizeof(*vrrp) + 8 + (vrrp->vr_naddr * 4)); if ((b = buf_tok(&inbuf, NULL, VRRP_AUTH_DATA_LEN)) == NULL) return (0); buf_put(&outbuf, buf_ptr(b), buf_len(b)); buf_put(&outbuf, "\n", 1); buf_end(&outbuf); return (buf_len(&outbuf)); }
/* On receiving a msg_hdr, we first do buf_reset(source, tag), * but _not_ on receiving a msg_body */ ssize_t MPINU_recv_msg_hdr_with_cache(int s, int tag, void *buf, size_t len, int flags) { int msg_found = 0; int source = MPINU_rank_from_socket(s); buf_reset(source, tag); while ( !msg_found && buf_avail(source, len) ) { if ( ntohl( ((struct msg_hdr *)buf_peek(source, len))->tag ) == tag || tag == MPI_ANY_TAG ) { buf_dequeue(source, buf, len, flags & MSG_PEEK); msg_found = 1; } else { int body_len; buf_skip(source, len); /* Skip header */ body_len = ntohl( ((struct msg_hdr *)buf)->size ); buf_skip(source, body_len); /* Skip body */ } } if ( !msg_found ) assert( !buf_avail(source, len) ); while ( !msg_found ) { len = MPINU_recvall(MPINU_pg_array[source].sd, buf, len, flags); assert( len == sizeof(struct msg_hdr) ); if ( ntohl( ((struct msg_hdr *)buf)->tag ) == tag || tag == MPI_ANY_TAG ) msg_found = 1; else if ( ! (flags & MSG_PEEK) ) { # define STATIC_SIZE 1000 static char body_buf_array[STATIC_SIZE]; void *body_buf; int body_len; buf_enqueue(source, buf, len); /* Store header */ /* After storing the header, we have to store the corresponding body */ body_len = ntohl( ((struct msg_hdr *)buf)->size ); if (body_len > STATIC_SIZE) body_buf = malloc(body_len); else body_buf = body_buf_array; body_len = MPINU_recvall(MPINU_pg_array[source].sd, body_buf, body_len, flags); buf_enqueue(source, body_buf, body_len); /* Store body */ if (body_len > STATIC_SIZE) free(body_buf); } } return len; }
static int process_smtp_client(struct smtp_info *smtp, char *data, int len) { struct buf *line, *body, buf; char *p; int i; buf_init(&buf, data, len); if (smtp->state != SMTP_DATA) { while ((i = buf_index(&buf, "\r\n", 2)) >= 0) { line = buf_tok(&buf, NULL, i + 2); line->base[line->end-1] = '\0'; p = buf_ptr(line); if (strncasecmp(p, "RSET", 4) == 0) { smtp->state = SMTP_HELO; } else if (smtp->state == SMTP_NONE && (strncasecmp(p, "HELO", 4) == 0 || strncasecmp(p, "EHLO", 4) == 0)) { smtp->state = SMTP_HELO; } else if (smtp->state == SMTP_HELO && (strncasecmp(p, "MAIL ", 5) == 0 || strncasecmp(p, "SEND ", 5) == 0 || strncasecmp(p, "SAML ", 5) == 0)) { smtp->from = grep_mail_address(p); smtp->state = SMTP_MAIL; } else if (smtp->state == SMTP_MAIL && strncasecmp(p, "RCPT ", 5) == 0) { smtp->state = SMTP_RCPT; } else if (smtp->state == SMTP_RCPT && strncasecmp(p, "DATA", 4) == 0) { smtp->state = SMTP_DATA; break; } } } if (smtp->state == SMTP_DATA) { if ((i = buf_index(&buf, "\r\n.\r\n", 5)) >= 0) { body = buf_tok(&buf, NULL, i); buf_skip(&buf, 5); body->base[body->end] = '\0'; if (regex_match(buf_ptr(body))) print_mbox_msg(smtp->from, buf_ptr(body)); if (smtp->from) { free(smtp->from); smtp->from = NULL; } smtp->state = SMTP_HELO; } } return (len - buf_len(&buf)); }
int decode_citrix(u_char *buf, int len, u_char *obuf, int olen) { struct buf inbuf, outbuf; u_char key, c, t[2]; int i; buf_init(&inbuf, buf, len); buf_init(&outbuf, obuf, olen); while ((i = buf_index(&inbuf, ica_magic, sizeof(ica_magic))) >= 0) { buf_skip(&inbuf, i); if (buf_len(&inbuf) < 60) break; buf_skip(&inbuf, 17); if (buf_get(&inbuf, &key, 1) != 1) break; buf_skip(&inbuf, 42); if (buf_get(&inbuf, &c, 1) != 1) break; c ^= ('C' | key); buf_put(&outbuf, &c, 1); i = 0; while (buf_get(&inbuf, t, 2) == 2) { c = t[0] ^ t[1] ^ key; if (c == '\0') { buf_put(&outbuf, "\n", 1); if (++i > 2) break; } buf_put(&outbuf, &c, 1); } } buf_end(&outbuf); return (buf_len(&outbuf)); }
static bool buf_skip_item(packet_state_t *state) { jshort item = buf_get_jshort(state); if (item == -1) return true; if (!buf_skip(state, 3)) return false; jshort len; switch (item) { #include "enchantable.c" len = buf_get_jshort(state); if (len == -1) break; if (!buf_skip(state, len)) return false; break; } return true; }
buf_t buf_getbuf(buf_t buf, int offset, int len) { buf_t b; if (buf->offset + offset + len > buf->end) return (NULL); buf_skip(buf, offset); if ((b = buf_new(len)) != NULL) { buf_put(b, buf_ptr(buf), len); buf_end(b); } buf_skip(buf, len); return (b); }
/*-------------------------------------------------------------------------*\ * Reads a line terminated by a CR LF pair or just by a LF. The CR and LF * are not returned by the function and are discarded from the buffer \*-------------------------------------------------------------------------*/ static int recvline(p_buf buf, luaL_Buffer *b) { int err = IO_DONE; while (err == IO_DONE) { size_t count, pos; const char *data; err = buf_get(buf, &data, &count); pos = 0; while (pos < count && data[pos] != '\n') { /* we ignore all \r's */ if (data[pos] != '\r') luaL_putchar(b, data[pos]); pos++; } if (pos < count) { /* found '\n' */ buf_skip(buf, pos+1); /* skip '\n' too */ break; /* we are done */ } else /* reached the end of the buffer */ buf_skip(buf, pos); } return err; }
/*-------------------------------------------------------------------------*\ * Reads everything until the connection is closed (buffered) \*-------------------------------------------------------------------------*/ static int recvall(p_buf buf, luaL_Buffer *b) { int err = IO_DONE; while (err == IO_DONE) { const char *data; size_t count; err = buf_get(buf, &data, &count); luaL_addlstring(b, data, count); buf_skip(buf, count); } if (err == IO_CLOSED) return IO_DONE; else return err; }
/*-------------------------------------------------------------------------*\ * Reads a fixed number of bytes (buffered) \*-------------------------------------------------------------------------*/ static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b) { int err = IO_DONE; size_t total = 0; while (total < wanted && err == IO_DONE) { size_t count; const char *data; err = buf_get(buf, &data, &count); count = MIN(count, wanted - total); luaL_addlstring(b, data, count); buf_skip(buf, count); total += count; } return err; }
int buf_putf(buf_t buf, const char *fmt, ...) { va_list ap; int i; va_start(ap, fmt); i = vsnprintf(buf_ptr(buf), buf_len(buf), fmt, ap); va_end(ap); buf_skip(buf, i); return (i); }
int main() { char buf[100]; buf_init(); buf_enqueue( 1, "abcde", 5 ); buf_enqueue( 1, "fghij", 5 ); buf_enqueue( 1, "klmno", 5 ); buf_skip( 1, 5 ); buf_dequeue( 1, buf, 5, DONT_JUST_PEEK ); buf[5] = '\0'; printf(buf); buf_dequeue( 1, buf, 5, DONT_JUST_PEEK ); buf_reset( 1 ); buf_dequeue( 1, buf, 5, DONT_JUST_PEEK ); return 0; }
buf_t buf_getword(buf_t buf, void *sep, int len) { buf_t b; int off; if ((off = buf_index(buf, sep, len)) < 0) return (NULL); if ((b = buf_new(off)) != NULL) { buf_put(b, buf_ptr(buf), off); buf_end(b); buf_skip(buf, off + len); } return (b); }
CTEST2(buf_test, mix) { uint64_t v1 = 123456789101112, v2; buf_putnstr(data->b, "nessdata", 8); buf_putuint32(data->b, 0); buf_putuint32(data->b, 1); buf_putuint64(data->b, v1); buf_skip(data->b, 8); uint32_t v; buf_getuint32(data->b, &v); ASSERT_EQUAL(0, v); buf_getuint32(data->b, &v); ASSERT_EQUAL(1, v); buf_getuint64(data->b, &v2); ASSERT_EQUAL(v1, v2); }
CTEST2(buf_test, checksum) { buf_clear(data->b); uint32_t cs1, cs2, cs3; uint64_t v1 = 123456789101112; buf_putnstr(data->b, "nessdata", 8); buf_putuint32(data->b, 0); buf_putuint32(data->b, 1); buf_putuint64(data->b, v1); buf_xsum(data->b->buf, data->b->NUL, &cs1); buf_putuint32(data->b, cs1); buf_skip(data->b, 8 + 4 + 4 + 8); buf_getuint32(data->b, &cs2); buf_xsum(data->b->buf, 8 + 4 + 4 + 8, &cs3); ASSERT_EQUAL(cs2, cs3); }
int decode_ftp(u_char *buf, int len, u_char *obuf, int olen) { struct buf *line, inbuf, outbuf; int i, n; if ((len = strip_telopts(buf, len)) == 0) return (0); buf_init(&inbuf, buf, len); buf_init(&outbuf, obuf, olen); if (!buf_isascii(&inbuf)) return (0); n = 0; while ((i = buf_index(&inbuf, "\n", 1)) != -1) { line = buf_tok(&inbuf, NULL, i); buf_skip(&inbuf, 1); if (i > 0 && line->base[i - 1] == '\r') line->end--; line->base[line->end] = '\0'; if (strncasecmp(buf_ptr(line), "USER ", 5) == 0 || strncasecmp(buf_ptr(line), "ACCT ", 5) == 0 || strncasecmp(buf_ptr(line), "PASS ", 5) == 0) { buf_putf(&outbuf, "%s\n", buf_ptr(line)); n++; } } if (n < 2) return (0); buf_end(&outbuf); return (buf_len(&outbuf)); }
static int process_pop_server(struct pop_info *pop, char *data, int len) { struct buf *line, *body, buf; int i; buf_init(&buf, data, len); if (pop->state == POP_NONE) return (len); if (pop->state == POP_RETR) { if ((i = buf_index(&buf, "\r\n", 2)) < 0) return (0); line = buf_tok(&buf, NULL, i + 2); if (buf_cmp(line, "+OK", 3) == 0) { pop->state = POP_DATA; } else pop->state = POP_NONE; } if (pop->state == POP_DATA) { if ((i = buf_index(&buf, "\r\n.\r\n", 5)) >= 0) { body = buf_tok(&buf, NULL, i); buf_skip(&buf, 5); body->base[body->end] = '\0'; if (regex_match(buf_ptr(body))) print_mbox_msg(NULL, buf_ptr(body)); pop->state = POP_NONE; } } return (len - buf_len(&buf)); }
int _deserialize_blockpairs_from_disk(int fd, struct block *b, struct hdr *hdr) { int r = NESS_ERR; uint32_t read_size; uint32_t align_size; struct buffer *rbuf; struct block_pair *pairs; read_size = hdr->blocksize; align_size = ALIGN(read_size); rbuf = buf_new(align_size); if (ness_os_pread(fd, rbuf->buf, align_size, hdr->blockoff) != (ssize_t)align_size) { r = NESS_READ_ERR; goto ERR; } if (!buf_seek(rbuf, read_size - CRC_SIZE)) goto ERR; uint32_t exp_xsum, act_xsum; if (!buf_getuint32(rbuf, &exp_xsum)) goto ERR; if (!buf_xsum(rbuf->buf, hdr->blocksize - CRC_SIZE, &act_xsum)) goto ERR; if (exp_xsum != act_xsum) { __ERROR("blockpairs xsum check error," "exp_xsum: [%" PRIu32 "]," "act_xsum: [%" PRIu32 "]", exp_xsum, act_xsum); goto ERR; } buf_seekfirst(rbuf); /* * skip magic with 8bytes */ if (!buf_skip(rbuf, 8)) goto ERR; uint32_t i; uint32_t block_count = 0U; if (!buf_getuint32(rbuf, &block_count)) goto ERR; pairs = xcalloc(block_count, sizeof(*pairs)); for (i = 0; i < block_count; i++) { if (!buf_getuint64(rbuf, &pairs[i].nid)) goto ERR1; if (!buf_getuint64(rbuf, &pairs[i].offset)) goto ERR1; if (!buf_getuint32(rbuf, &pairs[i].real_size)) goto ERR1; if (!buf_getuint32(rbuf, &pairs[i].skeleton_size)) goto ERR1; if (!buf_getuint32(rbuf, &pairs[i].height)) goto ERR1; pairs[i].used = 1; } if (block_count > 0) block_init(b, pairs, block_count); xfree(pairs); buf_free(rbuf); return NESS_OK; ERR: buf_free(rbuf); return r; ERR1: buf_free(rbuf); xfree(pairs); return r; }
int read_archive() #endif { Stat sb; char name[PATH_MAX + 1]; int match; int pad; name_gather(); /* get names from command line */ name[0] = '\0'; while (get_header(name, &sb) == 0) { match = name_match(name) ^ f_reverse_match; if (f_list) { /* only wanted a table of contents */ if (match) { print_entry(name, &sb); } if (((ar_format == TAR) ? buf_skip(ROUNDUP((OFFSET) sb.sb_size, BLOCKSIZE)) : buf_skip((OFFSET) sb.sb_size)) < 0) { warn(name, "File data is corrupt"); } } else if (match) { if (rplhead != (Replstr *)NULL) { rpl_name(name); if (strlen(name) == 0) { continue; } } if (get_disposition("extract", name) || get_newname(name, sizeof(name))) { /* skip file... */ if (((ar_format == TAR) ? buf_skip(ROUNDUP((OFFSET) sb.sb_size, BLOCKSIZE)) : buf_skip((OFFSET) sb.sb_size)) < 0) { warn(name, "File data is corrupt"); } continue; } if (inentry(name, &sb) < 0) { warn(name, "File data is corrupt"); } if (f_verbose) { print_entry(name, &sb); } if (ar_format == TAR && sb.sb_nlink > 1) { /* * This kludge makes sure that the link table is cleared * before attempting to process any other links. */ if (sb.sb_nlink > 1) { linkfrom(name, &sb); } } if (ar_format == TAR && (pad = sb.sb_size % BLOCKSIZE) != 0) { pad = BLOCKSIZE - pad; buf_skip((OFFSET) pad); } } else { if (((ar_format == TAR) ? buf_skip(ROUNDUP((OFFSET) sb.sb_size, BLOCKSIZE)) : buf_skip((OFFSET) sb.sb_size)) < 0) { warn(name, "File data is corrupt"); } } } close_archive(); }
static jshort buf_get_jshort(packet_state_t *state) { if (!buf_skip(state, 2)) die("out of data while getting jshort"); return jshort_read(&state->buf[state->buf_pos-2]); }
static jint buf_get_jint(packet_state_t *state) { if (!buf_skip(state, 4)) die("out of data while getting jint"); return jint_read(&state->buf[state->buf_pos-4]); }
packet_t *packet_read(packet_state_t *state) { jint t = buf_getc(state); if (t < 0) return 0; unsigned type = t; state->p.type = type; struct packet_format_desc *fmt; if (type >= MAX_PACKET_FORMAT || !(fmt = &packet_format[t])->known) { #if DEBUG_PROTOCOL >= 1 log_print("IMMINENT CRASH, reading tail for log"); unsigned char buf[256]; for (int i = 0; i < sizeof buf; i++) buf[i] = buf_getc(state); for (int i = 0; i < sizeof buf; i+=16) log_print("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", buf[i+0],buf[i+1],buf[i+2],buf[i+3],buf[i+4],buf[i+5],buf[i+6],buf[i+7], buf[i+8],buf[i+9],buf[i+10],buf[i+11],buf[i+12],buf[i+13],buf[i+14],buf[i+15]); #endif dief("Unknown packet id: 0x%02x", type); } state->p.field_offset = state->offset; for (unsigned f = 0; f < fmt->nfields; f++) { state->p.field_offset[f] = state->buf_pos - state->buf_start; switch (fmt->ftype[f]) { case FIELD_BYTE: case FIELD_UBYTE: if (!buf_skip(state, 1)) return 0; break; case FIELD_SHORT: if (!buf_skip(state, 2)) return 0; break; case FIELD_INT: case FIELD_FLOAT: if (!buf_skip(state, 4)) return 0; break; case FIELD_LONG: case FIELD_DOUBLE: if (!buf_skip(state, 8)) return 0; break; case FIELD_STRING: t = buf_get_jshort(state); if (!buf_skip(state, t*2)) return 0; break; case FIELD_ITEM: if (!buf_skip_item(state)) return 0; break; case FIELD_BYTE_ARRAY: t = buf_get_jint(state); if (!buf_skip(state, t)) return 0; break; case FIELD_BLOCK_ARRAY: t = buf_get_jshort(state); if (!buf_skip(state, 4*t)) return 0; break; case FIELD_ITEM_ARRAY: t = buf_get_jshort(state); for (int i = 0; i < t; i++) if (!buf_skip_item(state)) return 0; break; case FIELD_EXPLOSION_ARRAY: t = buf_get_jint(state); // FIXME: Possible over/underflow? if (!buf_skip(state, 3*t)) return 0; break; case FIELD_MAP_ARRAY: t = buf_getc(state); // Note: Unsigned if (!buf_skip(state, t)) return 0; break; case FIELD_ENTITY_DATA: while (1) { t = buf_getc(state); if (t == 127) break; switch (t >> 5) { case 0: if (!buf_skip(state, 1)) return 0; break; case 1: if (!buf_skip(state, 2)) return 0; break; case 2: case 3: if (!buf_skip(state, 4)) return 0; break; case 4: t = buf_get_jshort(state); if (!buf_skip(state, t)) return 0; break; case 5: if (!buf_skip(state, 5)) return 0; break; } } break; case FIELD_OBJECT_DATA: t = buf_get_jint(state); if (t > 0) if (!buf_skip(state, 6)) return 0; // Skip 3 short break; } } state->p.field_offset[fmt->nfields] = state->buf_pos - state->buf_start; state->p.size = state->buf_pos - state->buf_start; state->p.bytes = &state->buf[state->buf_start]; state->buf_start = state->buf_pos; return &state->p; }
int decode_aim(u_char *buf, int len, u_char *obuf, int olen) { struct buf *msg, inbuf, outbuf; struct flap *flap; u_char c, *p; int i, j; buf_init(&inbuf, buf, len); buf_init(&outbuf, obuf, olen); if (buf_cmp(&inbuf, "FLAPON\r\n\r\n", 10) == 0) buf_skip(&inbuf, 10); while (buf_len(&inbuf) > sizeof(*flap)) { flap = (struct flap *)buf_ptr(&inbuf); flap->datalen = ntohs(flap->datalen); i = sizeof(*flap) + flap->datalen; if ((msg = buf_tok(&inbuf, NULL, i)) == NULL) break; buf_skip(msg, sizeof(*flap)); if (buf_cmp(msg, "toc_signon ", 11) == 0) { msg->base[msg->end - 1] = '\0'; p = buf_ptr(msg); for (i = 0; i < 4; i++) { if ((j = strcspn(p, " ")) > 0) p += (j + 1); } if (strtok(p, " ") == NULL) continue; buf_putf(&outbuf, "%s ", buf_ptr(msg)); i = strlen(p); j = hex_decode(p, i, p, i); for (i = 0; i < j; i++) p[i] = p[i] ^ aim_xor1[i % 7]; p[i] = '\0'; buf_putf(&outbuf, "[%s]\n", p); } else if (flap->start == 0x2a && flap->channel == 0x01 && buf_cmp(msg, "\x00\x00\x00\x01", 4) == 0) { buf_skip(msg, 7); buf_get(msg, &c, 1); p = buf_ptr(msg); if (c == 0 || buf_skip(msg, c + 3) < 0) continue; p[c] = '\0'; buf_get(msg, &c, 1); if (buf_len(msg) < c + 1) continue; buf_putf(&outbuf, "%s\n", p); p = buf_ptr(msg); for (i = 0; i < c; i++) { p[i] = p[i] ^ aim_xor2[i % sizeof(aim_xor2)]; } p[i] = '\0'; buf_putf(&outbuf, "%s\n", p); break; } } buf_end(&outbuf); return (buf_len(&outbuf)); }
int decode_mmxp(u_char *buf, int len, u_char *obuf, int olen) { struct buf inbuf, outbuf; u_char *p, c; u_int32_t i; int encrypt; buf_init(&inbuf, buf, len); buf_init(&outbuf, obuf, len); while ((i = buf_index(&inbuf, "\x00\x00\x24\x55", 4)) != -1) { buf_skip(&inbuf, i + 4); if (buf_cmp(&inbuf, "\x7f\xff", 2) == 0) encrypt = 1; else if (buf_cmp(&inbuf, "\xff\xff", 2) == 0) encrypt = 0; else continue; buf_skip(&inbuf, 4); /* LPPPg? */ if (buf_get(&inbuf, &i, sizeof(i)) < 0) break; i = ntohl(i); if (buf_skip(&inbuf, i + 4 + 4) < 0) continue; /* Server. */ if (buf_get(&inbuf, &c, 1) != 1) break; if (buf_len(&inbuf) < c) break; buf_put(&outbuf, buf_ptr(&inbuf), c); buf_put(&outbuf, "\n", 1); buf_skip(&inbuf, c + 4); /* Username. */ if (buf_get(&inbuf, &c, 1) != 1) break; if (buf_len(&inbuf) < c) break; buf_put(&outbuf, buf_ptr(&inbuf), c); buf_put(&outbuf, "\n", 1); buf_skip(&inbuf, c + 4); /* Password. */ if (buf_get(&inbuf, &c, 1) != 1) break; if (buf_len(&inbuf) < c) break; p = buf_ptr(&inbuf); if (encrypt) { for (i = 0; i < c; i++) p[i] ^= mm_xor[i % (sizeof(MM_SECRET) - 1)]; } buf_put(&outbuf, p, c); buf_put(&outbuf, "\n", 1); } buf_end(&outbuf); return (buf_len(&outbuf)); }
int process_http_request(struct tuple4 *addr, u_char *data, int len) { struct buf *msg, buf; char *p, *req, *uri, *user, *vhost, *referer, *agent; int i; buf_init(&buf, data, len); while ((i = buf_index(&buf, "\r\n\r\n", 4)) >= 0) { msg = buf_tok(&buf, NULL, i); msg->base[msg->end] = '\0'; buf_skip(&buf, 4); if (!regex_match(buf_ptr(msg))) continue; if ((req = strtok(buf_ptr(msg), "\r\n")) == NULL) continue; if (strncmp(req, "GET ", 4) != 0 && strncmp(req, "POST ", 5) != 0 && strncmp(req, "CONNECT ", 8) != 0) continue; if ((uri = strchr(req, ' ')) == NULL) continue; *uri++ = '\0'; user = vhost = referer = agent = NULL; while ((p = strtok(NULL, "\r\n")) != NULL) { if (strncasecmp(p, "Authorization: Basic ", 21) == 0) { p += 21; i = base64_pton(p, p, strlen(p)); p[i] = '\0'; user = p; if ((p = strchr(p, ':')) != NULL) *p = '\0'; } else if (strncasecmp(p, "Host: ", 6) == 0) { vhost = p + 6; } else if (strncasecmp(p, "Referer: ", 9) == 0) { referer = p + 9; } else if (strncasecmp(p, "User-Agent: ", 12) == 0) { agent = p + 12; } else if (strncasecmp(p, "Content-length: ", 16) == 0) { i = atoi(p + 16); buf_tok(NULL, NULL, i); } } if (user == NULL) user = "******"; if (vhost == NULL) vhost = "none";// libnet_host_lookup(addr->daddr, Opt_dns); if (referer == NULL) referer = "-"; if (agent == NULL) agent = "-"; printf("%s - %s [%s] \"%s http://%s%s\" - - \"%s\" \"%s\"\n", //"0.0.0.0", libnet_addr2name4(addr->saddr, Opt_dns), user, timestamp(), req, vhost, uri, referer, agent); } fflush(stdout); return (len - buf_len(&buf)); }
int read_hdr_from_disk(int fd, struct block *b, struct hdr **h, DISKOFF off) { int r = NESS_ERR; struct hdr *hdr = NULL; struct buffer *rbuf = NULL; uint32_t exp_xsum, act_xsum; uint32_t read_size, align_size; hdr = xcalloc(1, sizeof(*hdr)); read_size = ( + 8 /* magic */ + 8 /* last nid */ + 8 /* root nid */ + 4 /* version */ + 4 /* block size */ + 8 /* block offset */ + CRC_SIZE); /* checksum */ align_size = ALIGN(read_size); rbuf = buf_new(align_size); if (ness_os_pread(fd, rbuf->buf, align_size, off) != (ssize_t)align_size) { __ERROR("ness pread error, read size [%" PRIu32 "], " "offset [%" PRIu64 "]", align_size, 0UL); r = NESS_READ_ERR; goto ERR; } if (!buf_seek(rbuf, read_size - CRC_SIZE)) goto ERR; if (!buf_getuint32(rbuf, &exp_xsum)) goto ERR; if (!buf_xsum(rbuf->buf, read_size - CRC_SIZE, &act_xsum)) goto ERR; if (exp_xsum != act_xsum) { __ERROR("header xsum check error, " "exp_xsum: [%" PRIu32 "], " "act_xsum: [%" PRIu32 "], ", exp_xsum, act_xsum); r = NESS_HDR_XSUM_ERR; goto ERR; } buf_seekfirst(rbuf); if (!buf_skip(rbuf, 8)) goto ERR; if (!buf_getuint32(rbuf, &hdr->version)) goto ERR; if (!buf_getuint64(rbuf, &hdr->last_nid)) goto ERR; if (!buf_getuint64(rbuf, &hdr->root_nid)) goto ERR; if (!buf_getuint32(rbuf, &hdr->blocksize)) goto ERR; if (!buf_getuint64(rbuf, &hdr->blockoff)) goto ERR; nassert(hdr->root_nid >= NID_START); if (hdr->version < LAYOUT_MIN_SUPPORTED_VERSION) { r = NESS_LAYOUT_VERSION_OLDER_ERR; __ERROR("tree layout too older [%d], " "min_support_version [%d]", hdr->version, LAYOUT_MIN_SUPPORTED_VERSION); goto ERR; } /* block pairs */ r = _deserialize_blockpairs_from_disk(fd, b, hdr); if (r != NESS_OK) { r = NESS_DESERIAL_BLOCKPAIR_ERR; goto ERR; } *h = hdr; buf_free(rbuf); return NESS_OK; ERR: buf_free(rbuf); xfree(hdr); return r; }