C++

57K

Solutions

24K

Contributors

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

Share tech news, updates, or what's on your mind.

Sign up to Post

CppComet
CppComet is open source comet server. In this article i tell about CppComet features and will show an example of a chat on PHP and CppComet.
0
Free Tool: Site Down Detector
LVL 9
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Hello ,

I want to appy a system command with arguments from variables but i could not find an example any body has experience about this ?


This is what i want to do
system("/bin/bash  /etc/a.sh   %d %d",val1,val2);
0
Hello ,


Any body knows a sample for re-calculate checksums and drop packets on netfilter module ?

Thank you
0
Hello ,

We want to create fork subroutines on runtime and let it die after 180 seconds. But ;
We do not want to let the mirror all global variables that should cause high memory usage on the system , we just want to pass the variables to the fork and it will do a few small jobs then will end it self after 180 seconds is it possible to isolate from the other app ?
As far as i know when a fork start it completely clone the existing maps / arrays etc. and work in another road.
0
Hello ,

For a project we are using ip.h library of the linux.
As far as i check the library , http://www.cse.scu.edu/~dclark/am_256_graph_theory/linux_2_6_stack/linux_2ip_8h-source.html  , i could not find a way to check Window size , is there any way to read complete packet hex with this library and get the window size of packet hex like given below :

			long dec_Window_size = std::stol (packet_hex.substr(96,4),nullptr,16);

Open in new window

0
Hello ,

We are building an IDS/IPS/Waf system that reads the packets from PF_Ring and apply rules to IPTables / Netfilter / Ntuple .
That software needs to access to userspace for iptables and kernelspace for netfilter. So we already pipe the both side.
But our main concern is about the performace issue. Time is our biggest enemy and need to solve the problem in mikro / mili seconds.
So where is the best place working area for our  main core of software for performance ? kernel area or user area ? and why .

Thanks for information
0
How should i solve this :)


	std::string RANGE ;
			std::stringstream RANGEstream;
			for (std::pair<long int, int> element : TTL) {
				RANGEstream <<  element.first;
				RANGE = RANGE + RANGEstream;
			}

Open in new window



dinleyici.c:352:19: error: no match for ‘operator+’ (operand types are ‘std::string {aka std::basic_string<char>}’ and ‘std::stringstream {aka std::basic_stringstream<char>}’)
     RANGE = RANGE + RANGEstream;
                   ^

Open in new window

0
Hello ,


Is there any body know a way to send live message to kernel module like the given below. I want to send a message to it and let it write to logs the message
My message will be sended from the user space and the kernel module is working on kernel space




#include <linux/module.h>	/* Needed by all modules */
#include <linux/kernel.h>	/* Needed for KERN_INFO */

int init_module(void)
{
	printk(KERN_INFO "Hello world 1.\n");
	return 0;
}

void cleanup_module(void)
{
	printk(KERN_INFO "Goodbye world 1.\n");
}

Open in new window

0
Hello ,

I want to know if there is a possible way to apply BPF filter to drop packets on netfilter  


Thank you



#define __KERNEL__
#define MODULE
#include <linux/ip.h>             
#include <linux/kernel.h> 
#include <linux/module.h> 
#include <linux/netdevice.h>      
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h> 
#include <linux/skbuff.h>         
#include <linux/udp.h>      

#include <linux/ip.h>


              
static struct nf_hook_ops netfilter_ops;                        
static unsigned char *ip_address = "\x5D\xBB\xCD\x73"; 
static char *interface = "p1p2";                          
unsigned char *port = "\x00\x17";                       
struct sk_buff *sock_buff;                              
struct udphdr *udp_header;                              
unsigned int main_hook(unsigned int hooknum,
                  struct sk_buff **skb,
                  const struct net_device *in,
                  const struct net_device *out,
                  int (*okfn)(struct sk_buff*))
{
  if(strcmp(in->name,interface) == 0){ return NF_DROP; }  
  struct iphdr* iph = ip_hdr(skb);
  if(iph->saddr == *(unsigned int*)ip_address){ return NF_DROP; }   
        
        
        
	/**sock_buff = *skb;
	if(!sock_buff){ return NF_ACCEPT; }                   
	if(!(iph)){ return NF_ACCEPT; }              
	if(iph->protocol != 17){ return NF_ACCEPT; }                 
	udp_header = (struct udphdr *)(sock_buff->data + (iph->ihl *4)); 
	

Open in new window

0
on the C++2011 compiler why that warnings happens for this code part and how should i do the legal app.
(*((unsigned int*)&ip4[0]))




dinleyici.c: In constructor ‘IP4::IP4(const char*)’:
dinleyici.c:36:28: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
   *((unsigned int *)&ip4[0]) = 0;
                            ^
dinleyici.c: In function ‘bool operator<(const IP4&, const IP4&)’:
dinleyici.c:44:96: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
  friend bool operator<(const IP4 & ip1, const IP4 & ip2) { return *((unsigned int *)&ip1.ip4[0]) < *((unsigned int *)&ip2.ip4[0]); }
                                                                                                ^
dinleyici.c:44:129: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
  friend bool operator<(const IP4 & ip1, const IP4 & ip2) { return *((unsigned int *)&ip1.ip4[0]) < *((unsigned int *)&ip2.ip4[0]); }
                                                                                                                                 ^
dinleyici.c: In member function ‘IP4::operator unsigned int()’:
dinleyici.c:51:67: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
         operator unsigned int() { return (*((unsigned int*)&ip4[0])); }  // cast the pointer to low byte to unsigned int  }
                    

Open in new window

0
Independent Software Vendors: We Want Your Opinion
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

The given code below returning the given fault. I think it is a memory issue because we are always dumping and cleaning the map. How can i over come this issue


std::map<int, int > Protocol;  //Global Structure

Protocol.clear(); // Before pushing items i am clearing old values
	
int dec_protocol = std::stol (hex.substr(46,2),nullptr,16);
Protocol[dec_protocol]++;

Open in new window




*** Error in `./dinleyici': double free or corruption (!prev): 0x0000000000d727f0 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7c503)[0x7f60126a8503]
./dinleyici[0x407de7]
./dinleyici[0x407d97]
./dinleyici[0x407d97]
./dinleyici[0x407d97]
./dinleyici[0x4072ca]
./dinleyici[0x4079e5]
./dinleyici[0x408fc4]
./dinleyici[0x404953]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f601264db35]
./dinleyici[0x404f74]
======= Memory map: ========
00400000-004a1000 r-xp 00000000 fd:02 67684348                           /home/c++/calismalar/Project_V1_09.08.2017/dinleyici
006a0000-006a2000 r--p 000a0000 fd:02 67684348                           /home/c++/calismalar/Project_V1_09.08.2017/dinleyici
006a2000-006a3000 rw-p 000a2000 fd:02 67684348                           /home/c++/calismalar/Project_V1_09.08.2017/dinleyici
006a3000-006a5000 rw-p 00000000 00:00 0 
00d52000-00d93000 rw-p 00000000 00:00 0                                  [heap]
7f600c000000-7f600c021000 rw-p 00000000 00:00 0 
7f600c021000-7f6010000000 ---p 00000000 00:00 0 
7f6011ca9000-7f601262c000 rw-s 00000000 00:09 584508            

Open in new window

0
Hello ,

Is there any possible way remove if the number of rows exceeds X the oldest (Total_Row - X ) count rows.
0
hello

static std::map<Log, int> Logmap_;
static std::map<Log, int> Logmap_new;


Is there any possible way to search in a hash table which has values like given below 16-24. char for 4F94CFAE  and put founded lines to Logmap_new from  Logmap_ with values



DC38E1AC9B89A0369F557CAA080045100104FC62400040069BD5257B628A4EAFCAF70016F273CA35B6FD50ED7F8D50180094CE830000BC2411DB2762C01F5AE797C64F94CFAE2833E406D8376DB4005717643E0DBA31A7D5BA073799AB93CEE4E22A357C
DC38E1AC9B89A0369F557CAA080045100338FC634000400699A0257B628A4EAFCAF70016F273CA35B7D950ED7F8D501800940CEE0000974CEDC48B7C50744D96523A13FBD77D4CCAE45BF24F9FB91E7B43600A7397E2DD9788F7E7837D69170BD5A94E83
A0369F557CAADC38E1AC9B8908004500004CCC3440003A06D2CB4EAFCAF7257B628AF273001650ED7F8DCA35B2B150182000D53900003068613A5E81D88EEB70EC9A0B45304A8266890A03CB36E112653052C767CCFEC9281625
DC38E1AC9B89A0369F557CAA08004510004CFC64400040069C8B257B628A4EAFCAF70016F273CA35BAE950ED7FB15018009413FA0000E1FC591EA86C2AEA5245848504E41776637B03D3261D943B020BA01464A715C32B4E4610
A0369F557CAADC38E1AC9B89080045000028014040003A069DE44EAFCAF7257B628AF273001650ED7FB1CA35B4CD50101FEFAC0D0000000000000000
A0369F557CAADC38E1AC9B89080045000028DDF640003A06C12D4EAFCAF7257B628AF273001650ED7FB1CA35B6FD50101FEEA9DE0000000000000000
A0369F557CAADC38E1AC9B8908004500002807BA40003A06976A4EAFCAF7257B628AF273001650ED7FB1CA35B7D950101FF9A8F70000000000000000

Open in new window

0
Hello

Is there any body knows ready to use sub routine when we send an array it will draw sth. like that :

value |-------------------------------------------------- count
    0 |                                                     32167
    1 |                                                      9999
    2 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  1723569
    4 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@                        973472
    8 |@@@                                                 111055
   16 |                                                     14777
   32 |                                                       376
   64 |                                                       585
  128 |                                                        19
  256 |                                                         0
  512 |                                                         0

Open in new window

0
Hello ,

The given code below returns an answer as given at the bottom . I need to put each line into an array .  how should i do it ? I mean i need to build line in for i loop and then need to add to array.

Thank you


	for (int i = 0; i < (bpf_u_int32) pkthdr->caplen; i++) {
		printf("%02X%s", (uint8_t)packet[i], (i + 1)%2 ? "" : "");
	}
	printf("\n");

Open in new window



DC 38E1 AC9B 89A0 369F 557C AA08 0045 C002 40B1 6900 0040 01ED A925 7B62 8A3B C115 2403 0337 A600 0000 0045 0004 1846 F000 00FD 1199 FA3B C115 2425 7B62 8AC1 3B00 1704 0400 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 00
A0 369F 557C AADC 38E1 AC9B 8908 0045 0004 1846 E900 00FD 11B0 6759 7DE1 0125 7B62 8A7D 5900 1704 0400 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 00
A0 369F 557C AADC 38E1 AC9B 8908 0045 0004 1847 7600 00FD 1188 6C7E 94E3 5825 7B62 8A94 7E00 1704 0400 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 00
A0 369F 557C AADC 38E1 AC9B 8908 0045 0004 1847 9100 00FD 1168 74B4 AECD 1B25 7B62 8AAE B400 1704 0400 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 00
A0 369F 557C AADC 38E1 AC9B 8908 0045 0004 1847 8C00 00FD 1147 4C4D D055 2725 7B62 8AD0 4D00 1704 0400 

Open in new window

0
Hello ,

I got a code like the given below , there is no begin and end values but it enters the if statement different parts but i could not understand why or how , should somebody tell me how this values changes on incrementIp function

	
struct SharedData
{
	bool done;
	pthread_mutex_t mutex;
	unsigned int ip_ring[100];
	int begin;
	int end;
	void shared_data() { memset(this, 0, sizeof(SharedData));}
	void incrementIp(unsigned int ip4){
		
		pthread_mutex_lock(&mutex);
		if ((end+1)%100 == begin)
		{			
			pthread_mutex_unlock(&mutex);
			incrementIp(ip4);
		}
		else
		{
			ip_ring[end] = ip4;
			end = (++end)%100;	
		
		}
		pthread_mutex_unlock(&mutex);

	}
	void synchronizeIps(std::map<IP4, int> & ip4map)
	
	{  
		pthread_mutex_lock(&mutex);
		for (int n = begin; n != (end+1)%100; n = (n+1)%100)
		{
			int ipaddress_decimal;
			memcpy(&ipaddress_decimal, &ip_ring[n], sizeof(unsigned int));
			struct in_addr dst_addr;
			dst_addr.s_addr = __bswap_32(ipaddress_decimal);
			char *dot_dst_ip = inet_ntoa(dst_addr);
			ip4map[dot_dst_ip]++;
			begin = n;
		}
		pthread_mutex_unlock(&mutex);
	}
};

Open in new window

0
when i add an ip to my hashtable from main process before start fork or in the forked process child it count independantly num_entries then the normal process in the program.

How should i solve to not behave them independently ?

	void child(){
		int j = 0;
		for (; j < 5000; ++j)
		{
		    int num_entries = ip4map.size();
		    printf("%d   \n",num_entries);
			usleep(500000);
	            
		}
	}

Open in new window


	struct IP4
	{
	    unsigned char ip4[4];
	    IP4(const char * szip) 
	    {
	        const char * psz = &szip[0]; int n = 4;
	        *((unsigned int *)&ip4[0]) = 0;
	        do 
	        {
	            ip4[--n] = (unsigned char)atoi(psz);
	            psz = (const char*)strchr(psz, '.');
	        }
	        while (n > 0 && NULL != psz++);
	    }   
	    friend bool operator<(const IP4 & ip1, const IP4 & ip2) { return *((unsigned int *)&ip1.ip4[0]) < *((unsigned int *)&ip2.ip4[0]); }
	    operator std::string() 
	    { 
	        std::ostringstream oss; 
	        oss << (int)ip4[3] << '.' << (int)ip4[2] << '.' << (int)ip4[1] << '.' << (int)ip4[0]; 
	        return oss.str(); 
	    }
	};
	std::map<IP4, int> ip4map;

Open in new window

0
Hello

we normally develop softwares on perl , we need a code structure like that for C++

	    $udp_dst_ip{$dst_ip}{src}++;
	    $udp_src_ip{$dst_ip}{$src_ip }{src}++;
	    $udp_src_port{$dst_ip}{$src_port }{src}++;
	    $udp_dst_port{$dst_ip}{$dest_port }{src}++;
	    $udp_tos{$dst_ip}{ $tos }{src}++;
	    $udp_ttl{$dst_ip}{ ($ttl) }{src}++;
	    $udp_packet_length{$dst_ip}{ $packet_length}{src}++;
	    $udp_data{$dst_ip}{$full_data }{src}++;

Open in new window


udp_dst_ip is an hash table when ever i add sth in it , it add one to souce count
for ex.

udp_dst_ip : 185.9.159.2
udp_dst_ip : 185.9.159.3
udp_dst_ip : 185.9.159.4
udp_dst_ip : 185.9.159.2


that returns -> $udp_dst_ip{185.9.159.2}{src} -> 2

and this are double layered hash tables :   $udp_src_ip{$dst_ip}{$ttl }{src}++;  

for ex.

udp_dst_ip : 185.9.159.2  & ttl : 115
udp_dst_ip : 185.9.159.2  & ttl : 116
udp_dst_ip : 185.9.159.2  & ttl : 117
udp_dst_ip : 185.9.159.2  & ttl : 115
udp_dst_ip : 185.9.159.2  & ttl : 115

and this time  $udp_src_ip{185.9.159.2 }{115}{src} returns 3


is there any mechanism on C++ like that ?
0
Hello


printf("0x%02X%02X", (uint8_t)packet[56], (uint8_t)packet[57]);

Open in new window


That returns me the hex of 1460 decimal value.   (0x05B4)  how should i get the value as integer 1460 ?

Thank you
0
New feature and membership benefit!
LVL 9
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Are there any instructors like
simon allardice
from lynda.com, plursight.com
who explain computer science concepts without using a scripting language.

Just verbally.

Please add (or subtract) zones
0
I'm programming Infineon TC29x Aurix Tricore Microcontroller based embedded system in 'C' language.
 
Consider stack starting at address 0x5000 0000.  The stack size is 0x1000.  

If I add 0x1000 to current stack pointer(Register A10) and store result in A10, then I should get underflow error.

What might be the code in assembly to change stack pointer?  something like following:

stw a10, 0x1000(a10)   # I'm not sure if stw is correct opcode ?

what are other options to create stack undeflow exception?
0
I'm attempting to integrate some functionality into a C-Language DLL that I use with my PowerBuilder application in order to manipulate Active Directory users without having to buy an expensive package or do the work manually.  I basically copied the following code (found on more than one web site) and the results of the compile return errors that both the "CreateDSObject" and "Release" functions are not members of the "IDirectoryObject" or "IDispatch" structures.  Here's the code I'm using:

        HRESULT                  hr;
      IDirectoryObject              *pDirObject;
      ADSVALUE                  sAMValue;
      ADSVALUE                  uPNValue;
      ADSVALUE                  classValue;
      LPDISPATCH                  pDisp;
      REFIID                        IID_IDirectoryObject;
      
      ADS_ATTR_INFO  attrInfo[] =
      {  
            { L"objectClass",            ADS_ATTR_UPDATE, ADSTYPE_CASE_IGNORE_STRING, &classValue,      1 },
            { L"sAMAccountName",      ADS_ATTR_UPDATE, ADSTYPE_CASE_IGNORE_STRING, &sAMValue,            1 },
            { L"userPrincipalName", ADS_ATTR_UPDATE, ADSTYPE_CASE_IGNORE_STRING, &uPNValue,            1 },
      };
      DWORD dwAttrs = sizeof(attrInfo)/sizeof(ADS_ATTR_INFO);
      
      classValue.dwType                        =      ADSTYPE_CASE_IGNORE_STRING;
      classValue.CaseIgnoreString            =      L"user";
      
      sAMValue.dwType                              =      ADSTYPE_CASE_IGNORE_STRING;
      sAMValue.CaseIgnoreString            =      L"browvick";
      
      uPNValue.dwType                              =      ADSTYPE_CASE_IGNORE_STRING;
      uPNValue.CaseIgnoreString            =      L"Vicky_Brown@gmail.com";
      
      hr = ADsGetObject(      L"LDAP://DC=domainname,DC=com",
                                    IID_IDirectoryObject,
                                    (void**) …
0
Hello Experts,

I have a C++ program which allocates named shared memory of a structure. I need to be able to read that same structure from a c# program. Here is my c# code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [StructLayout(LayoutKind.Sequential, Size = 218)]
        public struct TEST_STRUCT
        {
            public long tnum;                     //size = 8
            public long pass_count;               //size = 8
            public long fail_count;               //size = 8
            public long try_count;                //size = 8
            public double sum_result;               //size = 8
            public double sumsq_result;             //size = 8
            public double min;                      //size = 8
            public double max;                      //size = 8
            public long histogram;                //size = 8*40
            public bool tested;                   //size = 4
            public bool passed;                   //size = 4 
            public double last_result;              //size = 8
        };
        

Open in new window

0
Hi, dear experts:

I have a stupid question here, I used MFC for a long time, I still don't know why should I can the base class handler even when I provide the function to handle the message, here is an example:

class CMyDialog : CDialog
{....
}

void CMyDialog:OnLButtonUp(UINT nFlags, CPoint point)
{
    MyHandler();
    CDialogBox::OnLButtonUp(nFlags, point);
}

In fact when Class Wizard generate the message handler, it added base function call CDialogBox::OnLButtonUp(nFlags, point);

Here now here is my question, why do we need to call this function? what if I do not call this function?

Thanks for your help.
0
Hello,
my code works just fine and I got no error message.
But how can I check the the delete was done? Like a if - else routine

//%%%%%%%%%%%%%%%%%%%%%%%%%% Löschung %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	//SQL Server
	m_strConnection = _T("Driver={SQL Server}; Server=DSERVER; Database=OMSDATEN;Uid=;Pwd;");
		
	 //-----------------------------------------------------------------------------------------------------------
	//Initialize the Recordset and binding pointers
	m_ptrRs = NULL;   
       m_piAdoRecordBinding = NULL;   

	//Initialize the COM environment
	::CoInitialize(NULL);
	try
	{
		//Create the recordset object
		m_ptrRs.CreateInstance(__uuidof(Recordset));  

     
//======================================= Delete ==============================================================
   sql.Format("DELETE LogBuch FROM LogBuch WHERE Zeitstempel>='%s' AND Zeitstempel<='%s'",str_loeschen_von,str_loeschen_bis);
//============================================================================================================
		 
      _bstr_t bstrQuery(sql);
  
		//Open the recordset object Tabelle 
 m_ptrRs->Open(_variant_t(bstrQuery),(LPCTSTR)m_strConnection, adOpenDynamic, adLockOptimistic, adCmdUnknown);

		//Get a pointer to the record-binding interface Coils
		if(FAILED(m_ptrRs->QueryInterface(__uuidof(IADORecordBinding),(LPVOID *)&m_piAdoRecordBinding)))     _com_issue_error(E_NOINTERFACE);
  
		//Bind the record class to the recordset
	//	

Open in new window

0

C++

57K

Solutions

24K

Contributors

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.