Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
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?

//CODE BLOCK
#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

// *** Prototypes
void translate_ip(DWORD _ip, char *_cip);
void decode_tcp(char *_packet);
void decode_icmp(char *_packet);
void get_this_machine_ip(char *_retIP);

// *** Defines and Typedefs

#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;        // Version (4 bits) and Internet Header Length (4 bits)
   BYTE  type;           // Type of Service (8 bits)
   WORD  length;         // Total size of packet (header + data)(16 bits)
   WORD  packet_id;      // (16 bits)
   WORD  flags_foff;     // Flags (3 bits) and Fragment Offset (13 bits)
   BYTE  time_to_live;   // (8 bits)
   BYTE  protocol;       // (8 bits)
   WORD  hdr_chksum;     // Header check sum (16 bits)
   DWORD source_ip;      // Source Address (32 bits)
   DWORD destination_ip; // Destination Address (32 bits)
} IPHEADER;

typedef struct _TCP_HEADER_
{
   WORD  source_port;       // (16 bits)
   WORD  destination_port;  // (16 bits)
   DWORD seq_number;        // Sequence Number (32 bits)
   DWORD ack_number;        // Acknowledgment Number (32 bits)
   WORD  info_ctrl;         // Data Offset (4 bits), Reserved (6 bits), Control bits (6 bits)
   WORD  window;            // (16 bits)
   WORD  checksum;          // (16 bits)
   WORD  urgent_pointer;    // (16 bits)
} TCPHEADER;

typedef struct _ICMP_HEADER_
{
   BYTE type;               // (8 bits)  
   BYTE code;               // (8 bits)  
   WORD checksum;           // (16 bits)  
} ICMPHEADER;

// *********************************************************************
//                               MAIN
// *********************************************************************
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;
 
   // Check arguments
   if ( _argc > 1 )
   {
      if ( !_stricmp(_argv[1], "icmp") )
         bShowTCP = FALSE;
      else if ( !_stricmp(_argv[1], "tcp") )
         bShowICMP = FALSE;
      else
      {
         exit(0);
      }
   }

   // Init Windows sockets version 2.2   
   ver = MAKEWORD(2,2);
   WSAStartup(ver, &sa_data);

   // Get a socket in RAW mode
	sniff_socket = socket( AF_INET, SOCK_RAW, IPPROTO_IP );
	if ( sniff_socket == SOCKET_ERROR )
	{
      exit(-1);
	}

   // Bind it
   memset( thisIP, 0x00, sizeof(thisIP) );
   get_this_machine_ip(thisIP);

	sock_sniff.sin_family = AF_INET;
	sock_sniff.sin_port = htons(0);
   // If your machine has more than one IP you might put another one instead thisIP value
	sock_sniff.sin_addr.s_addr = inet_addr(thisIP);
	
	if ( bind( sniff_socket, (struct sockaddr *)&sock_sniff, sizeof(sock_sniff) ) == SOCKET_ERROR )
	{
      exit(-2);
	}

   // Set socket to promiscuous mode
   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;

      // I only want IPv4 not IPv6
      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); // size in 32 bits words

      // Checks the protocol IP is encapsulating
      memset( ipSrc, 0x00, sizeof(ipSrc) );
      memset( ipDest, 0x00, sizeof(ipDest) );
      translate_ip(ip_header->source_ip, ipSrc);
      translate_ip(ip_header->destination_ip, ipDest);

      // Read http://www.ietf.org/rfc/rfc1700.txt?number=1700
      switch( ip_header->protocol )
      {
         case 1: // ICMP
            if ( bShowICMP )
            {
               char szIP[256];
				sprintf("\n   Source      IP: %s", ipSrc);
				MessageBox(NULL, szIP, "", MB_OK);
            }
			 break;

         case 6: // TCP
            if ( bShowTCP )
            {
               char szIP[256];
				sprintf("\n   Source      IP: %s", ipSrc);
				MessageBox(NULL, szIP, "", MB_OK);
            }
            break;

         case 17: // UPD
			 
            break;

         default:
            break;
      }
   
   } // end-while

   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;
   }

 }
Posted
Comments
Tarakeshwar Reddy 26-Apr-11 15:09pm    
SA was just ranting about dos box. I guess he is going to add a comment here ;)

It seems you are creating a console mode application. If you do not need the console, then you need to create a windows mode application. I reckon the best way to do this for a beginner is to create a new VC++ project and use the Win32 project wizard (and not the Win32 console application wizard). Instead of main, you'll see a WinMain there. And you don't have an UI and you don't need a message loop. You code is old style top-down code. So this will work for you.

Alternatively, you can modify your existing compiler settings to use /SUBSYSTEM:WINDOWS, manually #include <windows.h> and then change main to WinMain. That will work too, but since you are new to C++, this may be a little harder for you to do, specially the first time.

And btw, most people just say console or command shell/prompt these days. Saying Dos Box is not very accurate.
 
Share this answer
 
v3
Comments
Member 7766180 26-Apr-11 15:06pm    
Thank you. I will give it a try. Command prompt it is! I appreciate that. Having the correct words always help!
DS
Nish Nishant 26-Apr-11 15:24pm    
You're welcome :-)
Hans Dietrich 26-Apr-11 15:18pm    
MUST.NOT.SAY.DOS.BOX!
Member 7766180 26-Apr-11 15:21pm    
I will never say/use that term again :)
Espen Harlinn 26-Apr-11 16:01pm    
Nice reply, 5ed!
You could try this as a shortcut to turning your Dos Box console app into a Windows app: QuickWin - Turn a console application into a Windows program[^]
 
Share this answer
 
Comments
Member 7766180 26-Apr-11 15:25pm    
Thank You. Before I tread deeper...after this is convertedwill I be able to run my code with only a messagebox popping up during the test phase and then nothing showing when I'm running the program for real? The point of this is to get the IP of the current packet and check that IP against an online database.
Hans Dietrich 26-Apr-11 15:31pm    
I suggest you give QuickWin a try first - it's very simple to use. After that, you could go with a "real" Windows app if you needed to. Post a new thread if you want to do that.
Member 7766180 26-Apr-11 17:12pm    
When I create a Windows App, now I get a form popping open. I need not to have any forms, just want to get a varaible that I can store, no forms or consoles.
DS
Hans Dietrich 26-Apr-11 17:17pm    
If you have created a Windows dialog-based app, just use ::MessageBox() to display the info in InitInstance() and then return - don't display the dialog.
Sergey Alexandrovich Kryukov 26-Apr-11 17:24pm    
Good point, especially scratched out "Dos Box", my 5.
(I added my rant about it, please see :-)
--SA
There is no such thing as "DOS box" anymore!

There is just an application using console or combining console with Windows UI. For example, "CMD.EXE" is just one of such applications, normal Win32 or 64-bit one, depending on Windows version.

—SA
 
Share this answer
 
Comments
Hans Dietrich 26-Apr-11 17:35pm    
Yes, exactly.
Sergey Alexandrovich Kryukov 26-Apr-11 19:50pm    
Thank you, Hans.

I cannot understand why it sparks so much of argument. See my other recent answer and the discussion:
http://www.codeproject.com/Answers/187213/Save-to-a-Variable-instead-of-print-to-screen.aspx

--SA
Another simple method (works on MS compilers and related C runtime libraries (C++ as well links to them) is
- Configure the linker to have windows as a subsystem and ...
- Set the linker configuration to have MainCRTStartup as an entry point.

This will produce a windows-mode app which boots from the C++ runtime initialization routine, that -after initializing and constructing all global objects- will call the good old int main(int, char**);
 
Share this answer
 
Comments
Member 7766180 28-Apr-11 8:59am    
Tried this and it didn't work.
Emilio Garavaglia 28-Apr-11 11:02am    
By me, it worked every time I used it!
Member 7766180 28-Apr-11 17:57pm    
It's probably me!! Any suggestions?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900