System() function is hang in linux - linux

Situation: In my project, I want modify OVS source code to perform some my functions. I want when OVS receive a specific packet, it will add a flow to userspace don't need controller, to do that i used system() function in a source c file to execute the following command:
ovs-ofctl add-flow s1 priority=5,tcp,in_port="s1-eth1",eth_src=32:3b:8c:9d:13:5f,eth_dst=d2:5f:67:a6:80:81,ipv4_src=10.0.0.1,ipv4_dst=10.0.0.2,tcp_src=25000,tcp_dst=59174,action=output:"s1-eth2"
Problem: After remake source code, whenever OVS run to my funtions which I added, system() funtion will hang like in image. Even if I stop OVS, process system() have been running .
image
Someone can help me?
I add my functions to connmgr_send_async_msg() in ovs/ofproto/connmgr.c . This function will recieve packet coming to OVS.
It looks like :
void connmgr_send_async_msg(struct connmgr *mgr, const struct ofproto_async_msg *am)
{
struct dp_packet packet_in; // this is packet comes in OVS
// then i get interface name, MAC, IPv4, TCP port of packet_in into
//in, out, sMAC, dMAC, sIP, dIP, sPort, dPort
char cmd[1000]; // command i want pass to system()
snprintf(bar1, sizeof(bar1), "ovs-ofctl add-flow s1 priority=5,tcp,in_port=\"%s\",eth_src=%s,eth_dst=%s, ipv4_src=%s,ipv4_dst=%s,tcp_src=%u,tcp_dst=%u,action=output:\"%s\""
,in ,sMAC ,dMAC, sIP, dIP, sPort, dPort, out); // pass agr to cmd
// call system() funtion with cmd
int systemRet1 = system(cmd);
// logging to my log file
log = fopen("/home/log_file.txt", "a");
fprintf(log, "Status when add flow %d \n", systemRet1);
fclose(log);
.................................// normal OVS source code
}

Related

Do I ever need to re-create listening socket?

Suppose I've created a socket, started listen()ing on it and run accept() in a loop to process incoming connections. I.e. smth like this:
s = socket();
bind(s, ...);
listen(s, ...);
loop {
new_s = accept(s, ...);
... // do smth with new_s
}
For various reasons accept() can return an error and most of these errors say this particular connection attempt failed, please carry on. Is there any scenario when you have to close the socket and start from scratch (i.e. make new socket + bind + listen) in order to be (eventually) reachable by clients? What error (returned from accept()) tell me that? I.e. should I ever structure my logic like this:
loop {
loop {
s = socket();
bind(s, ...);
listen(s, ...);
if !error { break; }
sleep(1second); // avoid busy loop
}
loop {
new_s = accept(s, ...);
if error {
if error == ??? break; <--- which error code(s)?
continue;
}
... // do smth with new_s
}
}
Notes:
Specifically I am looking at ENETDOWN (Linux) and WSAENETDOWN (Winsock2) -- looks like these happen when someone restarts the network (interface). Will my previously created socket continue accepting connections once network is up? I doubt it, but even if it is the case -- how to properly avoid busy accept loop?
Other platforms may have other error codes -- how to write a code that will work on all of them?
You don't need to recreate the listening socket if accept() fails on that listener (at least on Windows).
If one called bind on 0.0.0.0:(some port) - then you almost never need to worry about recreating the listening socket.
If one called bind on a specific IP address, and that IP address goes away, then you definitely need to recreate the listening socket (you aren't listening to anything anymore).

nodejs serialport Get the number of bytes in the input buffer

I listen to a port in real time, I use the nodejs serialport module, but I use the same port to send other information. I use this following code which prevents me from using the port:
```port.on('readable', function () {
if(port.read() === "h"){
//
}
})```
I was thinking of something like checking the number of bytes in the buffer like in python serialport and arduino, the objective is to avoid blocking the port as the previous code does.
I was thinking of using port.read() to evaluate the buffer bits but it always returns null .
```
if (port.read() > 0){
//read data
}
```

Bro IDS signature_match trigger

I am new to BRO and just started to test signature on BRO. I have one script, main.bro, and a signature file, protosigs.sig. The idea is to compare the signature and do something within the rewritten event function - signature_match. I tried to use the following measure to test a pcap file but the test didn't generate a notice.log. It seemed the function - signature_match wasn't get called. Can anyone let me know what's going on here? Many thanks!
How I test the script and signature:
bro -r ./bittorrent.Transfer.pcap ./main.bro -s ./protosigs.sig
My signature:
signature torrent-request-tcp {
ip-proto == tcp
payload /^\x13/
tcp-state originator
event "torrent-request-tcp"
}
My script - main.bro:
#load base/frameworks/notice
#load base/frameworks/signatures/main
#load base/utils/addrs
#load base/utils/directions-and-hosts
##load-sigs ./protosigs.sig
module bittorrent;
export {
redef enum Notice::Type += {
Torrent,
};
}
event signature_match(state: signature_state, msg: string, data: string)
&priority=-5
{print "Triggerd!"; //at least this one should be triggered, but..
if ( /torrent/ in state$sig_id )
{
print "TTTTTTTTTTTTTTTTTTT";
NOTICE([$note=bittorrent::Torrent,
$msg="Torrent whatsoever",
$sub=data,
$conn=state$conn,
$identifier=fmt("%s%s", state$conn$id$orig_h, state$conn$id$resp_h)]);
}
}
Xifeng your setup seems to work fine here (putting aside the "//" comment delimiter, where you'll want "#" instead). The signature should be fine too, since the BitTorrent protocol indeed starts with 0x13 by the originator. The pcap I used is this one:
https://pcapr.net/view/gerald/2009/0/3/10/Comcast_Bittorrent_no_RST.pcap.html
Are you sure your pcap is okay? Make sure it contains the TCP handshake so Bro can properly bootstrap the connection state.

Develop simple net_device in the linux kernel

I am trying to implement a netdevice (net_device) in linux kernel. This is simple net_device which pass the command/data from user space to kernel space and vice versa this is the goal of this simple net_device. I am using socket for passing command/data from user space to kernel space . After googling i successed in registering net_device and able to see my device in /sys/class/net/abc0 (device name)
when coming to file operation there is no clear idea of the flow
struct net_device_ops
{
.ndo_open =open,
.ndo_close = close,
.ndo_start_xmit = start_xmit
}
if i issue write in socket will it call start_xmit in data link layer.
If i want to call open method, how to call it using socket
How to call start_xmit using socket
How will i find , there is data packet in the receive buffer and pass it to user space.
There is no clear flow/information about simple net_device (except ethernet) can any suggest a link/pdf.
I tried writing simple socket program to test open,close,start_xmit. where socket read/write is not calling open,close,star_xmit .
Is there any way to test the developed net_device ?
Thank you
I found how to test the open,close function .
type : ifconfig abc0(Device name) up will call open method
type : ifconfig abc0(Device name) down will call close method
Can some one help me how to test these methods with sockets.
SIOCSIFFLAGS, -> IFF_UP you can actually set or unset it while doing an ioctl to the netdevice abc0.
first off you have to create a dgram socket,
then use ifreq structure defined in net/if.h
and fill interface and iff_flags
iff_flags can be set with IFF_UP or can be negated with the same IFF_UP to make interface down
and then close the socket.
#include <net/if.h>
....
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
goto fail;
}
struct ifreq ifreq;
strcpy(ifreq.ifr_name, "abcd0");
ifreq.iff_flags |= IFF_UP;
if (ioctl(sock, &ifreq, sizeof(ifreq) < 0) {
perror("ioctl setting interface up");
}
ifreq.iff_flags ~= IFF_UP;
if (ioctl(sock, &ifreq, sizeof(ifreq) < 0) {
perror("ioctl setting interface down");
}
close(sock);
offtopic:
will you please share your code? we can understand too about your network device :)

Getting Arduino LilyPad to switch BlueSmirf v2.11 to/from command mode

A battery powered (2 x AA) Arduino LilyPad should switch a BlueSmirf v2.11 Bluetooth modem to/from command mode (see source code below). The BlueSmirf has been set to 9600 baud.
If the PC connects via Bluetooth (see source code below), the Arduino program runs fine at the beginning (sending multiple "ping\n"). After some time it (LilyPad/BlueSmirf) starts to also send "$$$" and "---\n" over the Bluetooth connection instead of switching to/from command mode.
Any ideas?
Regards,
tamberg
// Arduino source code:
void setup () {
Serial.begin(9600);
}
void loop () {
Serial.print("$$$");
delay(2000); // TODO: Inquiry, etc.
Serial.print("---\n");
delay(100);
Serial.print("ping\n");
delay(2000);
}
// C# source code (runs on PC)
using System;
using System.IO.Ports;
class Program {
static void Main () {
SerialPort p = new SerialPort(
"COM20", 9600, Parity.None, 8, StopBits.One);
using (p) {
p.Open();
while (p.IsOpen) {
Console.Write((char) p.ReadChar());
}
}
}
}
From the datasheet, page 6:
NOTE1 : You can enter command mode
locally over the serial port at any
time when not connected. Once a
connection is made, you can only enter
command mode if the config timer has
not expired. To enable continuous
configuration, set the config timer to
255. Also, if the device is in Auto Master mode 3, you will NOT be able to
enter command mode when connected over
Bluetooth.
My guess would be that the config timer is expiring.

Resources