I'm running this code and I need to return the value as a variable only. Its a console app. I need to stop the "DOS BOX" from opening. How would I do this?
#pragma warning( disable: 4996 )
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma comment( lib, "ws2_32.lib" ) // linker must use this lib for sockets
void translate_ip(DWORD _ip, char *_cip);
void decode_tcp(char *_packet);
void decode_icmp(char *_packet);
void get_this_machine_ip(char *_retIP);
#define LS_HI_PART(x) ((x>>4) & 0x0F)
#define LS_LO_PART(x) ((x) & 0x0F)
#define LS_MAX_PACKET_SIZE 65535
#ifndef SIO_RCVALL
# define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#endif
typedef struct _IP_HEADER_
{
BYTE ver_ihl;
BYTE type;
WORD length;
WORD packet_id;
WORD flags_foff;
BYTE time_to_live;
BYTE protocol;
WORD hdr_chksum;
DWORD source_ip;
DWORD destination_ip;
} IPHEADER;
typedef struct _TCP_HEADER_
{
WORD source_port;
WORD destination_port;
DWORD seq_number;
DWORD ack_number;
WORD info_ctrl;
WORD window;
WORD checksum;
WORD urgent_pointer;
} TCPHEADER;
typedef struct _ICMP_HEADER_
{
BYTE type;
BYTE code;
WORD checksum;
} ICMPHEADER;
int main( int _argc, char *_argv[] )
{
struct sockaddr_in sock_sniff;
SOCKET sniff_socket = -1;
WSAData sa_data;
WORD ver;
IPHEADER *ip_header = NULL;
int optval = 1;
DWORD dwLen = 0;
char packet[LS_MAX_PACKET_SIZE];
int iRet = 0;
int ip_header_size = 0;
char ipSrc[20], ipDest[20], thisIP[20];
BOOL bShowTCP = TRUE, bShowICMP = TRUE;
if ( _argc > 1 )
{
if ( !_stricmp(_argv[1], "icmp") )
bShowTCP = FALSE;
else if ( !_stricmp(_argv[1], "tcp") )
bShowICMP = FALSE;
else
{
exit(0);
}
}
ver = MAKEWORD(2,2);
WSAStartup(ver, &sa_data);
sniff_socket = socket( AF_INET, SOCK_RAW, IPPROTO_IP );
if ( sniff_socket == SOCKET_ERROR )
{
exit(-1);
}
memset( thisIP, 0x00, sizeof(thisIP) );
get_this_machine_ip(thisIP);
sock_sniff.sin_family = AF_INET;
sock_sniff.sin_port = htons(0);
sock_sniff.sin_addr.s_addr = inet_addr(thisIP);
if ( bind( sniff_socket, (struct sockaddr *)&sock_sniff, sizeof(sock_sniff) ) == SOCKET_ERROR )
{
exit(-2);
}
if ( WSAIoctl( sniff_socket,
SIO_RCVALL,
&optval,
sizeof(optval),
NULL,
0,
&dwLen,
NULL,
NULL ) == SOCKET_ERROR )
{
exit(-3);
}
while ( TRUE )
{
(void) memset( packet, 0x00, sizeof(packet) );
iRet = recv( sniff_socket, packet, LS_MAX_PACKET_SIZE, 0 );
if ( iRet < sizeof(IPHEADER) )
continue;
ip_header = (IPHEADER *)packet;
if ( LS_HI_PART(ip_header->ver_ihl) != 4 )
continue;
ip_header_size = LS_LO_PART(ip_header->ver_ihl);
ip_header_size *= sizeof(DWORD);
memset( ipSrc, 0x00, sizeof(ipSrc) );
memset( ipDest, 0x00, sizeof(ipDest) );
translate_ip(ip_header->source_ip, ipSrc);
translate_ip(ip_header->destination_ip, ipDest);
switch( ip_header->protocol )
{
case 1:
if ( bShowICMP )
{
char szIP[256];
sprintf("\n Source IP: %s", ipSrc);
MessageBox(NULL, szIP, "", MB_OK);
}
break;
case 6:
if ( bShowTCP )
{
char szIP[256];
sprintf("\n Source IP: %s", ipSrc);
MessageBox(NULL, szIP, "", MB_OK);
}
break;
case 17:
break;
default:
break;
}
}
return 0;
}
void get_this_machine_ip(char *_retIP)
{
char host_name[128];
struct hostent *hs;
struct in_addr in;
memset( host_name, 0x00, sizeof(host_name) );
gethostname(host_name,128);
hs = gethostbyname(host_name);
memcpy( &in, hs->h_addr, hs->h_length );
strcpy( _retIP, inet_ntoa(in) );
}
void translate_ip(DWORD _ip, char *_cip)
{
struct in_addr in;
in.S_un.S_addr = _ip;
strcpy( _cip, inet_ntoa(in) );
}
void decode_tcp(char *_packet)
{
TCPHEADER *tcp_header = (TCPHEADER *)_packet;
BYTE flags = ( ntohs(tcp_header->info_ctrl) & 0x003F );
>destination_port));
}
void decode_icmp(char *_packet)
{
ICMPHEADER *icmp_header = (ICMPHEADER *)_packet;
switch ( icmp_header->type )
{
case 0:
break;
case 8:
break;
default:
break;
}
}