Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
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 26-Apr-11 9:56am
Comments
Tarakeshwar Reddy at 26-Apr-11 15:09pm
   
SA was just ranting about dos box. I guess he is going to add a comment here ;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v3
Comments
Member 7766180 at 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
Nishant Sivakumar at 26-Apr-11 15:24pm
   
You're welcome :-)
Hans Dietrich at 26-Apr-11 15:18pm
   
MUST.NOT.SAY.DOS.BOX!
Member 7766180 at 26-Apr-11 15:21pm
   
I will never say/use that term again :)
Espen Harlinn at 26-Apr-11 16:01pm
   
Nice reply, 5ed!
SAKryukov at 26-Apr-11 17:23pm
   
Correct, my 5.
I added some rant on it, please see.
--SA
Member 7766180 at 26-Apr-11 18:50pm
   
Alternatively, you can modify your existing compiler settings to use /SUBSYSTEM:WINDOWS,
 
How or where is this done in Visual Studio 2010?
Thanks
DS
Member 7766180 at 26-Apr-11 19:00pm
   
Never Mind, Found it. I changed it, Also added the widowns.h and changed Main to WinMain. However; I'm receiving these errors on dbug.
 
Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup C:\WpdPack\lsniff\lsniff\LIBCMTD.lib(wincrt0.obj) lsniff
 
Error 2 error LNK1120: 1 unresolved externals C:\WpdPack\lsniff\lsniff\Debug\lsniff.exe lsniff
Member 7766180 at 26-Apr-11 19:03pm
   
Recompiled and now getting.....
 
Warning 1 warning C4007: 'WinMain' : must be '__stdcall' c:\wpdpack\lsniff\lsniff\lsniff_main.cpp 66 1 lsniff
Error 2 error C2731: 'WinMain' : function cannot be overloaded c:\wpdpack\lsniff\lsniff\lsniff_main.cpp 66 1 lsniff
Nishant Sivakumar at 26-Apr-11 19:06pm
   
Use this signature for WinMain:
http://msdn.microsoft.com/en-us/library/ms633559(v=vs.85).aspx
Nishant Sivakumar at 26-Apr-11 19:06pm
   
In your project Linker settings, select Advanced and set Entry Point to wWinMainCRTStartup.
Member 7766180 at 26-Apr-11 19:28pm
   
Added and I'm still receiving this on debug.
Warning 1 warning C4007: 'WinMain' : must be '__stdcall' c:\wpdpack\lsniff\lsniff\lsniff_main.cpp 66 1 lsniff
Error 2 error C2731: 'WinMain' : function cannot be overloaded c:\wpdpack\lsniff\lsniff\lsniff_main.cpp 66 1 lsniff
Nishant Sivakumar at 26-Apr-11 19:58pm
   
What is your current WinMain signature?
Member 7766180 at 26-Apr-11 21:05pm
   
int stdcall_WinMain()
Nishant Sivakumar at 27-Apr-11 8:56am
   
Well that's wrong. I linked above to the WinMain documentation which shows the correct signature.
Member 7766180 at 28-Apr-11 7:44am
   
OK it works now! Thank you!
DS
Nishant Sivakumar at 28-Apr-11 8:07am
   
Cool. Good to hear that!
Nishant Sivakumar at 28-Apr-11 8:09am
   
If you think your question has been answered, please accept one of the answers in this thread as the accepted answer.
Member 7766180 at 28-Apr-11 8:52am
   
Back to this. I have the signature added as you suggested. I did switch main to WinMain and in Linker/System I switched it to to windows(/Subsystem:Windows)
Member 7766180 at 28-Apr-11 8:53am
   
Sorry, didn't finish. Errors again.
Warning 1 warning C4007: 'WinMain' : must be '__stdcall' c:\isniffer\lsniff\lsniff\lsniff_main.cpp 74 1 lsniff
Error 2 error C2731: 'WinMain' : function cannot be overloaded c:\isniffer\lsniff\lsniff\lsniff_main.cpp 74 1 lsniff
Nishant Sivakumar at 28-Apr-11 9:02am
   
Post your code (just WinMain).
Member 7766180 at 28-Apr-11 9:25am
   
int WinMain( 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
{
printf( "\nUsage lsniff [ICMP|TCP]\n" );
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 )
{
printf( "Error: socket = %ld\n", WSAGetLastError() );
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 )
{
printf( "Error: bind = %ld\n", WSAGetLastError() );
exit(-2);
}
Nishant Sivakumar at 28-Apr-11 15:38pm
   
Ok, I've said this many times in this thread, but your WinMain sigtnature is wrong. Please see http://msdn.microsoft.com/en-us/library/ms633559(v=vs.85).aspx
Member 7766180 at 28-Apr-11 16:20pm
   
I guess its something that im not getting? Where and how does this signature fit in? Still lost, sorry!
Nishant Sivakumar at 28-Apr-11 16:24pm
   
Ok, I am really confused here. That page I linked to clearly shows the function signature as int CALLBACK WinMain( __in HINSTANCE hInstance, __in HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nCmdShow); but you are using a different signature. Surely you get what I mean here!
Member 7766180 at 28-Apr-11 17:56pm
   
Yes, and I have this in the begining of the code, near the #includes. Is this wrong. I'm sorry I'm confusing you, cause I'm really confused :)
DS
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
Comments
Member 7766180 at 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 at 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 at 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 at 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.
SAKryukov at 26-Apr-11 17:24pm
   
Good point, especially scratched out "Dos Box", my 5.
(I added my rant about it, please see :-)
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
Hans Dietrich at 26-Apr-11 17:35pm
   
Yes, exactly.
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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**);
  Permalink  
Comments
Member 7766180 at 28-Apr-11 8:59am
   
Tried this and it didn't work.
Emilio Garavaglia at 28-Apr-11 11:02am
   
By me, it worked every time I used it!
Member 7766180 at 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
0 Mathew Soji 330
1 BillWoodruff 260
2 Sergey Alexandrovich Kryukov 240
3 OriginalGriff 216
4 Afzaal Ahmad Zeeshan 208
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 27 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100