how to print flags in TCP header of raw packets using libpcap - tcp

sniffex.c is a program that is based on libpcap , to sniff and display some packet information. How do i modify it so as to print the values of TCP flags - urg , ack , psh , rst , syn and fin ? please help..

If you check this code where sniff_tcp is used, make sure to print out
th_flags member of this structure which contians flags you need.
/* TCP header */
struct sniff_tcp {
u_short th_sport; /* source port */
u_short th_dport; /* destination port */
tcp_seq th_seq; /* sequence number */
tcp_seq th_ack; /* acknowledgement number */
u_char th_offx2; /* data offset, rsvd */
#define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)
u_char th_flags;
#define TH_FIN 0x01
#define TH_SYN 0x02
#define TH_RST 0x04
#define TH_PUSH 0x08
#define TH_ACK 0x10
#define TH_URG 0x20
#define TH_ECE 0x40
#define TH_CWR 0x80
#define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
u_short th_win; /* window */
u_short th_sum; /* checksum */
u_short th_urp; /* urgent pointer */
};
Generally you will have something like this (a bit ugly, you can do it better):
//this is already there in the code:
printf(" Src port: %d\n", ntohs(tcp->th_sport));
printf(" Dst port: %d\n", ntohs(tcp->th_dport))
//you add:
if (tcp->th_flags & TH_ECE){
printf(" Flag: TH_ECE");
}
if (tcp->th_flags & TH_RST){
printf(" Flag: TH_RST");
}

Related

Arduino Modbus RTU Response over serial?

I am developing a project where I have to read the holding registers data. I check everything using this http://www.freemodbus.com/ and it is working and get a proper response. While trying with the developed program I am not getting the proper response.
Software Response: 0x01 0x03 0x04 0x1a 0xa0 0x42 0x48 0xcd 0x9f
Arduino response: 0x01 0x04 0x83 0x43 0xff 0xff 0xff 0xff 0xff
note that in Arduino response there is no 0x03 after 0x01 I don't know why it is happening can anyone please help me with this.
please find attached Arduino code below.
static union
{
unsigned long a;
byte b[4];
float f;
}vr;
void readregister(unsigned int address)
{
byte rxbuf[]={0,0,0,0,0,0,0,0,0,0,0};
byte data[] = {0x01,0x03,0x00,0xab,0x00,0x02,0xb5,0xeb};
Serial3.flush();
for(int i=0;i<8;i++)
{
Serial3.write(data[i]);
}
delay(250);
while(Serial3.available()>0)
{
for(int v=0; v<=10;v++)
{
rxbuf[v]=Serial3.read();
Serial.println(rxbuf[v],HEX);
}
}
Serial3.flush();
vr.b[3]=rxbuf[3];
vr.b[2]=rxbuf[2];
vr.b[1]=rxbuf[5];
vr.b[0]=rxbuf[4];
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial3.begin(9600,SERIAL_8E1);
}
void loop() {
// put your main code here, to run repeatedly:
readregister(99);
Serial.print("\n");
delay(3000);
}
Yes, I think you're getting an Error code 4 back from the device. See http://www.simplymodbus.ca/exceptions.htm
On this line, where you prep your request:
byte data[] = {0x01,0x03,0x00,0xab,0x00,0x02,0xb5,0xeb};
You're saying, device 1, function 3, 0x00 is space, then register address 0xAB. Not sure about the extra zero then, then length 2, and then CRC. So I vote that either the CRC is wrong, or the extra zero is wrong.
Also, note that modbus fails on ocassion for no reason, even when you do everything right, so retry after a few seconds and see what happens

How can I change UART configurations(# of stop bits, parity bits etc.) on ENERGIA IDE?

I need to use UART of TI CC3220s launchpad with 2 stop bits.
how can I change the UART configurations on energia ide like number of stop bits?
config parameters do not work (https://energia.nu/reference/en/language/functions/communication/serial/begin/)
the following error occurs: 'SERIAL_5N1' was not declared in this scope
Any help will be appreciated.
Thanks in advance
edit
The link that you provided directly says:
Serial.begin(speed) Serial.begin(speed, config) Arduino Mega only
According to the code (I found), the Serial library of Energia-IDE is only a SoftwareSerial, that only provides the begin(speed) function (without a config).
Maybe you should have a look into that, so that you can adapt the function, for your own purpose.
/edit
In the Arduino-IDE, the config's are defined in HardwareSerial.h. If the Energia-IDE uses the same library, you might try to use the same values instead.
// Define config for Serial.begin(baud, config);
#define SERIAL_5N1 0x00
#define SERIAL_6N1 0x02
#define SERIAL_7N1 0x04
#define SERIAL_8N1 0x06
#define SERIAL_5N2 0x08
#define SERIAL_6N2 0x0A
#define SERIAL_7N2 0x0C
#define SERIAL_8N2 0x0E
#define SERIAL_5E1 0x20
#define SERIAL_6E1 0x22
#define SERIAL_7E1 0x24
#define SERIAL_8E1 0x26
#define SERIAL_5E2 0x28
#define SERIAL_6E2 0x2A
#define SERIAL_7E2 0x2C
#define SERIAL_8E2 0x2E
#define SERIAL_5O1 0x30
#define SERIAL_6O1 0x32
#define SERIAL_7O1 0x34
#define SERIAL_8O1 0x36
#define SERIAL_5O2 0x38
#define SERIAL_6O2 0x3A
#define SERIAL_7O2 0x3C
#define SERIAL_8O2 0x3E
At least this passes the "validate" step in the Energia-IDE:
void setup()
{
Serial.begin(9600, 0x00);
}

rs-485 USB to RS485 C code

#include <stdio.h>
#include <fcntl.h> /* File Control Definitions */
#include <termios.h> /* POSIX Terminal Control Definitions */
#include <unistd.h> /* UNIX Standard Definitions */
#include <errno.h> /* ERROR Number Definitions */
#include <sys/ioctl.h> /* ioctl() */
void main(void)
{
int fd;/*File Descriptor*/
printf("\n +----------------------------------+");
printf("\n | USB To RS485 Write |");
printf("\n +----------------------------------+");
/*------------------------------- Opening the Serial Port -------------------------------*/
/* Change /dev/ttyUSB0 to the one corresponding to your system */
fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY); /* ttyUSB0 is the FT232 based USB2SERIAL Converter */
/* O_RDWR Read/Write access to serial port */
/* O_NOCTTY - No terminal will control the process */
if(fd == -1) /* Error Checking */
printf("\n Error! in Opening ttyUSB0 ");
else
printf("\n ttyUSB0 Opened Successfully ");
/*---------- Setting the Attributes of the serial port using termios structure --------- */
struct termios SerialPortSettings; /* Create the structure */
tcgetattr(fd, &SerialPortSettings); /* Get the current attributes of the Serial port */
cfsetispeed(&SerialPortSettings,B4800); /* Set Read Speed as 9600 */
cfsetospeed(&SerialPortSettings,B4800); /* Set Write Speed as 9600 */
SerialPortSettings.c_cflag &= PARENB; /* Disables the Parity Enable bit(PARENB),So No Parity */
SerialPortSettings.c_cflag &= ~CSTOPB; /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */
SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the mask for setting the data size */
SerialPortSettings.c_cflag |= CS7; /* Set the data bits = 8 */
SerialPortSettings.c_cflag &= ~CRTSCTS; /* No Hardware flow Control */
SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Enable receiver,Ignore Modem Control lines */
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); /* Disable XON/XOFF flow control both i/p and o/p */
SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* Non Cannonical mode */
SerialPortSettings.c_oflag &= ~OPOST;/*No Output Processing*/
/* Setting Time outs */
SerialPortSettings.c_cc[VMIN] = 1; /* Read at least 10 character */
SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly */
if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) /* Set the attributes to the termios structure*/
printf("\n ERROR ! in Setting attributes");
else
printf("\n BaudRate = 9600 \n StopBits = 1 \n Parity = none");
/*---------------------------------------- Controlling RTS and DTR Pins --------------------------------------*/
/* ~RTS(USB2SERIAL) ---> ~RE(MAX485) */
/* ~DTR(USB2SERIAL) ---> DE(MAX485) */
/*------------------------- Putting MAX485 chip in USB2SERIAL in Transmit Mode ---------------------------*/
// //
// ----+ +-----------+ H +-----------+ //
// | | ~RTS| --------------> |~RE | //
// PC |==========>| FT232 | | MAX485 +(A,B)~~~~~~~~~~~~~~~>Data out(RS485) //
// | USB | ~DTR| --------------> | DE | Twisted Pair //
// ----+ +-----------+ H +-----------+ //
// //
//--------------------------------------------------------------------------------------------------------//
//TxMode - DE->High,~RE -> High
int RTS_flag,DTR_flag;
RTS_flag = TIOCM_RTS; /* Modem Constant for RTS pin */
DTR_flag = TIOCM_DTR; /* Modem Constant for DTR pin */
ioctl(fd,TIOCMBIC,&RTS_flag);/* ~RTS = 1,So ~RE pin of MAX485 is HIGH */
ioctl(fd,TIOCMBIC,&DTR_flag);/* ~DTR = 1,So DE pin of MAX485 is HIGH,Transmit Mode enabled */
/*------------------------------- Write data to serial port -----------------------------*/
tcflush(fd, TCIFLUSH); /* Discards old data in the rx buffer */
char write_buffer[] = "///?01!"; /* Buffer containing characters to write into port */
char write_buffer1[] = {'\r','\n'};
int bytes_written = 0; /* Value for storing the number of bytes written to the port */
bytes_written = write(fd,write_buffer,7);/* use write() to send data to port */
/* "fd" - file descriptor pointing to the opened serial port */
/* "write_buffer" - address of the buffer containing data */
/* "sizeof(write_buffer)" - No of bytes to write */
printf("\n %s written to ttyUSB0",write_buffer);
bytes_written = write(fd,write_buffer1,2);/* use write() to send data to port */
/* "fd" - file descriptor pointing to the opened serial port */
/* "write_buffer" - address of the buffer containing data */
/* "sizeof(write_buffer)" - No of bytes to write */
printf("\n %s written to ttyUSB0",write_buffer);
printf("\n %d Bytes written to ttyUSB0", bytes_written);
printf("\n +----------------------------------+\n\n");
char buf[20000];
sleep(2);
while(1)
{
printf("tring to read port\n");
int res = read(fd,buf,1);
sleep(1);
printf("%c :: %d\n",buf[0],res);
}
printf("\n +----------------------------------+\n\n\n");
close(fd); /* Close the serial port */
}
--------------------------------------------------------
Setting for running the code :BAUD RATE is 4800 Databits is 7 , Parity is EVEN, and Stopbit is 1.
If i compile the code and run it is not working. Before running the code if i open and close the minicom with BAUD B4800 & 7E1 then my code is working.
Can any one suggest where is the problem why it is not working.

Arduino send HEX values via XBee

I'm trying to send HEX values via XBee from a Arduino to digi's Xbee program XCTU. But i have some problems with some bytes that i cant send
The list of bytes i cant send:
0x11
0x13
0x7D
0x7E
0x81
0xEC
0xEE
If i use any other bytes i can see it in the XCTU console window and i get a response back to the Arduino
I have enabled API mode (AP=1) on both of the Xbee's. Im using the XBee lib linked from Arduino web site (https://www.arduino.cc/en/Reference/Libraries)
My code looks like this:
#define XBEE_RX_PIN 8
#define XBEE_TX_PIN 9
SoftwareSerial xbeeSerial(XBEE_RX_PIN, XBEE_TX_PIN);
void setup()
{
Serial.begin(9600);
xbeeSerial.begin(9600);
xbee.begin(xbeeSerial);
}
void loop()
{
Serial.println("Send next 255 msg:");
uint8_t testMsg[1] = { 0x0 };
for (uint8_t i = 0; i <= 0xFF; i++)
{
test2[0] = i;
Tx16Request testTx = Tx16Request(0xFFFF, testMsg, sizeof(testMsg));
xbee.send(testTx);
Serial.print("Message send: ");
Serial.println(test2[0], HEX);
if (xbee.readPacket(5000)) {
Serial.println("SUCCESS");
}
else if (xbee.getResponse().isError()) {
Serial.println("isError");
}
else {
Serial.println("No Response");
}
}
Serial.println("");
delay(3000);
}
With AP=1, you can't send some special bytes like:
0x7E (start of frame)
0x11/0x13 (XOn XOff)
...
As mentioned in the XBee documentation (XBee®XBee-PRO® ZB RF Modules manual 90000976_W.pdf, starting page 112), you should use AP=2 mode which allows you to transmit these special bytes by escaping them:
Escape characters.
When sending or receiving a UART data frame, specific data values must be
escaped (flagged) so they do not interfere with the data frame sequencing.
To escape an interfering data byte, insert 0x7D and follow it with the byte to be escaped XOR’d with 0x20.
Note that, if not escaped, 0x11 and 0x13 is sent as is.
Data bytes that need to be escaped:
- 0x7E – Frame Delimiter
- 0x7D – Escape
- 0x11 – XON
- 0x13 – XOFF
Example - Raw UART Data Frame (before escaping interfering bytes):
- 0x7E 0x00 0x02 0x23 0x11 0xCB
0x11 needs to be escaped which results in the following frame:
- 0x7E 0x00 0x02 0x23 0x7D 0x31 0xCB
Note In the above example, the length of the raw data (excluding the checksum) is 0x0002 and the checksum of the non-escaped data (excluding frame delimiter and length) is calculated as:
0xFF - (0x23 + 0x11) = (0xFF - 0x34) = 0xCB.
Hope this helps

libpcap fails to capture packets after upgrading to new linux ethernet driver

I have an old system running a custom 2.6.15 kernel that uses libpcap (version 1.1.1). Recently I've changed my network card with Intel 82575EB chipset that requires me to update the driver to igb.ko (was e1000.ko). After the update, libpcap stop capturing packets. I modified a sample test code from tcpdump website that captures 1 packet and print the header information, libpcap return header.len of 1358 and header.caplen of 42, whereas in e1000 case, both packet.len and packet.caplen returns 1358. I've tried disabling MSI/MSI-X and increase the MTU but nothing works. Is there any other options I need to set to get the igb driver to work with libpcap?
Here's the sample test program (courtesy of tcpdump/libpcap team):
#include <pcap.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
pcap_t *handle; /* Session handle */
char dev[20]; /* The device to sniff on */
char errbuf[PCAP_ERRBUF_SIZE]; /* Error string */
struct bpf_program fp; /* The compiled filter */
bpf_u_int32 mask; /* Our netmask */
bpf_u_int32 net; /* Our IP */
struct pcap_pkthdr header; /* The header that pcap gives us */
const u_char *packet; /* The actual packet */
if (argc <= 1) return(1);
strcpy(dev, argv[1]);
/* Find the properties for the device */
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Couldn't get netmask for device %s: %s\n", dev, errbuf);
net = 0;
mask = 0;
}
/* Open the session in promiscuous mode */
handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
return(2);
}
/* Grab a packet */
packet = pcap_next(handle, &header);
/* Print its length */
printf("packet length [%d]; captured length [%d]\n", header.len, header.caplen);
/* And close the session */
pcap_close(handle);
return(0);
}
Try libpcap 1.4.0, which is currently the most recent release; there's a bug in 1.1.1 that, as I remember, could cause a packet to be supplied with a too-short caplen even though you've supplied a sufficiently-large snapshot length argument to pcap_open_live() (which you have - BUFSIZ is typically somewhere between 1K and 4K, both of which are bigger than 42, and I think it's 4K on Linux).

Resources