Skip to content

Commit

Permalink
redid sending TCP data
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdavidgraham committed Nov 10, 2023
1 parent 19cc149 commit ab952a7
Show file tree
Hide file tree
Showing 12 changed files with 368 additions and 293 deletions.
22 changes: 22 additions & 0 deletions src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ vLOGip(int level, ipaddress ip, unsigned port, const char *fmt, va_list marker)
}
}

static void
vLOGnet(unsigned port_me, ipaddress ip_them, const char *fmt, va_list marker)
{
char sz_ip[64];
ipaddress_formatted_t fmt1 = ipaddress_fmt(ip_them);

sprintf_s(sz_ip, sizeof(sz_ip), "%s", fmt1.string);
fprintf(stderr, "%u:%s: ", port_me, sz_ip);
vfprintf(stderr, fmt, marker);
fflush(stderr);
}


/***************************************************************************
***************************************************************************/
Expand All @@ -73,3 +85,13 @@ LOGip(int level, ipaddress ip, unsigned port, const char *fmt, ...)
va_end(marker);
}

void
LOGnet(unsigned port_me, ipaddress ip_them, const char *fmt, ...)
{
va_list marker;

va_start(marker, fmt);
vLOGnet(port_me, ip_them, fmt, marker);
va_end(marker);
}

2 changes: 2 additions & 0 deletions src/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

void LOG(int level, const char *fmt, ...);
void LOGip(int level, ipaddress ip, unsigned port, const char *fmt, ...);
void LOGnet(unsigned port_me, ipaddress ip_them, const char *fmt, ...);


void LOG_add_level(int level);

Expand Down
3 changes: 0 additions & 3 deletions src/proto-banner1.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,6 @@ struct StreamState {
} sub;
};

enum {
CTRL_SMALL_WINDOW = 1,
};

/**
* A registration structure for various TCP stream protocols
Expand Down
25 changes: 3 additions & 22 deletions src/proto-interactive.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,20 @@
#include "util-malloc.h"
#include <stdlib.h>

/*
* TODO: we need to track the memory used for this better than with malloc(), such
* as using a preallocated array of packet buffers. But for now, I'm just using
* malloc() 'cause I'm a lazy programmer.
*/
unsigned char *
tcp_transmit_alloc(struct InteractiveData *more, size_t length)
{
/* Note using this parameter yet, but in the future, we are going to have
* memory pools instead of heap malloc(), which will use this parameter */
UNUSEDPARM(more);

return MALLOC(length);
}

void
tcp_close(struct InteractiveData *more)
{
if (more == NULL)
return;
more->is_closing = 1;
more->send(more->tcpcon, more->tcb, "", 0, TCP__static, true, more->secs, more->usecs);
}

/*
* This doesn't actually transmit right now. Instead, marks the payload as ready
* to transmit, which will be transmitted later
*/
void
tcp_transmit(struct InteractiveData *more, const void *payload, size_t length, unsigned flags)
tcp_transmit(struct InteractiveData *more, const void *payload, size_t length, enum TCP__flags flags)
{
more->m_payload = payload;
more->m_length = (unsigned)length;

if (flags & TCPTRAN_DYNAMIC)
more->is_payload_dynamic = 1;
more->send(more->tcpcon, more->tcb, payload, length, flags, more->is_closing, more->secs, more->usecs);
}
25 changes: 13 additions & 12 deletions src/proto-interactive.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
#ifndef PROTO_INTERACTIVE_H
#define PROTO_INTERACTIVE_H
#include <stdio.h>
#include "util-bool.h" /* <stdbool.h> */

enum TCP__flags {
TCP__static,/* it's static data, so the send function can point to it */
TCP__copy, /* the send function must copy the data */
TCP__adopt /* the buffer was just allocated, so the send function can adopt the pointer */
};

struct InteractiveData {
const void *m_payload;
unsigned m_length;
unsigned is_payload_dynamic:1;
void *tcpcon;
void *tcb;
void (*send)(void *tcpcon, void *tcb, const void *buf, size_t length, enum TCP__flags flags, bool is_fin, unsigned secs, unsigned usecs);
unsigned secs;
unsigned usecs;
unsigned is_closing:1;
};
enum {
TCPTRAN_DYNAMIC = 0x0001,
};

/**
* Called to 'transmit' TCP packet payload.
*/
void
tcp_transmit(struct InteractiveData *more, const void *data, size_t length, unsigned flags);
tcp_transmit(struct InteractiveData *more, const void *data, size_t length, enum TCP__flags flags);

/**
* Called to close the connection
*/
void
tcp_close(struct InteractiveData *more);

/**
* Called to allocate a TCP buffer.
*/
unsigned char *
tcp_transmit_alloc(struct InteractiveData *more, size_t length);

#endif
4 changes: 2 additions & 2 deletions src/proto-smb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1456,14 +1456,14 @@ smb_parse_smb(struct SMBSTUFF *smb, const unsigned char *px, size_t max, struct
if (smb->parms.negotiate.SessionKey) {
unsigned char *buf;

buf = tcp_transmit_alloc(more, sizeof(smb1_null_session_setup));
buf = malloc(sizeof(smb1_null_session_setup));

memcpy(buf, smb1_null_session_setup, sizeof(smb1_null_session_setup));
buf[0x2f] = (unsigned char)(smb->parms.negotiate.SessionKey>> 0) & 0xFF;
buf[0x30] = (unsigned char)(smb->parms.negotiate.SessionKey>> 8) & 0xFF;
buf[0x31] = (unsigned char)(smb->parms.negotiate.SessionKey>>16) & 0xFF;
buf[0x32] = (unsigned char)(smb->parms.negotiate.SessionKey>>24) & 0xFF;
tcp_transmit(more, buf, sizeof(smb1_null_session_setup), TCPTRAN_DYNAMIC);
tcp_transmit(more, buf, sizeof(smb1_null_session_setup), TCP__adopt);

/* NOTE: the following line is here to silence LLVM warnings about a potential
* memory leak. The 'tcp_transmit' function 'adopts' the pointer and will be
Expand Down
2 changes: 1 addition & 1 deletion src/proto-tcp-telnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ telnet_parse( const struct Banner1 *banner1,
if (r_offset) {
unsigned char *outbuf = MALLOC(r_offset);
memcpy(outbuf, reply, r_offset);
tcp_transmit(more, outbuf, r_offset, 1);
tcp_transmit(more, outbuf, r_offset, TCP__copy);
}
}
pstate->state = state;
Expand Down
Loading

0 comments on commit ab952a7

Please sign in to comment.