I am trying to pass some extra arguments so they can be used at the SIGINT call on my program, but I am getting a compile error, is this actually achievable or am I misinformed?
If it is achievable, could someone please explain my mistake here;
void signal_handler(bool &txMode, char* &txData, int &sendresult, int &sockFD,_
char* &txBuffer, struct sockaddr_ll &socket_address);
int main(int argc, char *argv[]) {
unsigned char sourceMAC[6];
unsigned char destMAC[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
bool txMode = true;
char* txBuffer = (char*)operator new(ETH_FRAME_LEN);
unsigned char* txEtherhead = (unsigned char*)txBuffer;
struct ethhdr *eh = (struct ethhdr *)txEtherhead;
int sockFD;
char* txData = txBuffer + headersLength;
struct sockaddr_ll socket_address;
int sendresult = 0;
signal (SIGINT,signal_handler(txMode, txData, sendresult, sockFD, txBuffer,_
socket_address));
return 0;
}
void signal_handler(bool &txMode, char* &txData, int &sendresult, int &sockFD,_
char* &txBuffer, struct sockaddr_ll &socket_address) {
if(txMode==true) {
std::string param;
std::stringstream ss;
ss << "etheratedeath";
param = ss.str();
strncpy(txData,param.c_str(), param.length());
sendresult = sendto(sockFD, txBuffer, ETH_FRAME_LEN, 0,
(struct sockaddr*)&socket_address, sizeof(socket_address));
}
cout << "You killed me!" << endl;
}
I am getting the following error at compile time;
user@machine:~/c/ethernet1$ gcc -o ethernet1 ethernet1.cpp -lrt
ethernet1.cpp: In function ‘int main(int, char**)’:
ethernet1.cpp:358: error: invalid use of void expression
Which refers to this line in main();
signal (SIGINT,signal_handler(txMode, txData, sendresult, sockFD, txBuffer, socket_address));
For this to work you need a construct called a closure, Neither C nor C++ have closures built in. However you can implement closure behaviour using the ffcall library. The idea is to build an in-situ function from a generic handler and arguments to be passed to that function. The arguments, of course must remain valid even if the scope is left, which means, either have them be global variables, or allocate them on the heap.
Now please think about what you did write there and how the compiler does interpret this. Function parameters are passed by value. So the expression signal_handler(…) is evaluated and the result passed to signal. This is not what you want, you want signal_handler being called with the arguments you passed when SIGINT arrives. But this is not, what this line expresses. What it expresses is: Call signal_handler with the parameters now, and pass the result of this as handler parameter to signal for later calling when SIGINT arrives.