Skip to content

Instantly share code, notes, and snippets.

@ghostmansd
Created July 11, 2014 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghostmansd/5cb5b6f41fe72e554696 to your computer and use it in GitHub Desktop.
Save ghostmansd/5cb5b6f41fe72e554696 to your computer and use it in GitHub Desktop.
libntoh never can find a stream
/*- clang -std=c89 bug.c -o bug -lntoh */
#ifdef __STRICT_ANSI__
#undef __STRICT_ANSI__
#endif
#ifdef _ISOC99_SOURCE
#undef _ISOC99_SOURCE
#endif
#ifdef _POSIX_SOURCE
#undef _POSIX_SOURCE
#endif
#ifdef _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
#endif
#ifdef _XOPEN_SOURCE
#undef _XOPEN_SOURCE
#endif
#ifdef _SVID_SOURCE
#undef _SVID_SOURCE
#endif
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif
#ifndef __FAVOR_BSD
#define __FAVOR_BSD
#endif
#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pcap.h>
#undef _BSD_SOURCE
#undef __FAVOR_BSD
#include <libntoh.h>
void callback
(
pntoh_tcp_stream_t stream,
pntoh_tcp_peer_t orig,
pntoh_tcp_peer_t dest,
pntoh_tcp_segment_t seg,
int reason,
int extra
)
{ /* pass */ }
static void info(char const *function, ntoh_tcp_tuple5_t *tuple)
{
char srcstr[40] = {0};
char deststr[40] = {0};
char const *address = NULL;
address = inet_ntoa(*(struct in_addr*)&(tuple->source));
memcpy(srcstr, address, strlen(address));
address = inet_ntoa(*(struct in_addr*)&(tuple->destination));
memcpy(deststr, address, strlen(address));
fprintf(stderr, "%s:%d -> %s:%d",
srcstr, ntohs(tuple->sport), deststr, ntohs(tuple->dport));
}
int main(int argc, char const **argv)
{
unsigned int status = 0;
ntoh_tcp_tuple5_t tuple;
ntoh_tcp_session_t *tcp = NULL;
ntoh_ipv4_session_t *ipv4 = NULL;
ntoh_tcp_stream_t *stream = NULL;
(void) argc;
(void) argv;
ntoh_init();
tuple.source = 3909091338;
tuple.destination = 1581761197;
tuple.sport = htons(48515);
tuple.dport = htons(80);
if ((tcp = ntoh_tcp_new_session(0, 0, &status)) == NULL)
{
fprintf(stderr, "IPv4: %s", ntoh_get_errdesc(status));
return EXIT_FAILURE;
}
for (int i = 0; i < 10; ++i)
{
stream = ntoh_tcp_find_stream(tcp, &tuple);
info("ntoh_tcp_find_stream", &tuple);
fprintf(stderr, " %p\n", (void*) stream);
if (stream == NULL)
{
stream = ntoh_tcp_new_stream(tcp, &tuple, callback,
NULL, &status, 0, 0);
info("ntoh_tcp_new_stream", &tuple);
fprintf(stderr, " %p\n", (void*) stream);
}
}
ntoh_exit();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment