May 20, 2015

cross compile openJDK for ARM

Source: http://mail.openjdk.java.net/pipermail/zero-dev/2014-December/000538.html

when I cross-compile OpenJDK 9 for ARM32 I pass the following configure 
options

configure
# these options tell openjdk to do a cross compile build.
--build=x86_64-unknown-linux-gnu
--host=arm-buildroot-linux-gnueabi
--target=arm-buildroot-linux-gnueabi

# these two options enable zero
--with-jvm-interpreter=cpp
--with-jvm-variants=zero

# specific options to make the build find the X and freetype headers and 
librarys found on the ARM32 root filesystem.
--disable-freetype-bundling
--with-freetype-include=/home/xranby/rpi-buildroot/output/host/usr/arm-buildroot-linux-gnueabi/sysroot/usr/include/freetype2
--with-freetype-lib=/home/xranby/rpi-buildroot/output/host/usr/arm-buildroot-linux-gnueabi/sysroot/usr/lib
--with-freetype=/home/xranby/rpi-buildroot/output/host/usr/arm-buildroot-linux-gnueabi/sysroot/usr/
--with-x=/home/xranby/rpi-buildroot/output/host/usr/arm-buildroot-linux-gnueabi/sysroot/usr/include

# the sysroot shall point to the ARM32 root file system, the build will 
use librarys inside the sys root during linking native libraries.
--with-sys-root=/home/xranby/rpi-buildroot/output/host/usr/arm-buildroot-linux-gnueabi/sysroot
# The tools dir contains binarys to run on the host x86 system, you may 
point this to your system root dir /
# in my case the tools i use have been compiled by buildroot thus I use 
the buildroot tools dir.
--with-tools-dir=/home/xranby/rpi-buildroot/output/host
# OpenJDK 9 require OpenJDK 8, i point with boot jdk to an OpenJDK 8 
image that can be run on the host x86 system.
--with-boot-jdk=/home/xranby/images-jdk8/j2sdk-image/

# some parts of the openjdk build still expect that the cross compile 
tools are found in the system PATH
# on my system i have to explicitly tell where the tools are located 
because my cross compile toolchain is not found on the path.
# i use gcc to do the linking instead of ld because the openjdk build 
passes -Xlinker -z
OBJCOPY=/home/xranby/rpi-buildroot/output/host/usr/bin/arm-buildroot-linux-gnueabi-objcopy
STRIP=/home/xranby/rpi-buildroot/output/host/usr/bin/arm-buildroot-linux-gnueabi-strip
CPP=/home/xranby/rpi-buildroot/output/host/usr/bin/arm-buildroot-linux-gnueabi-cpp
CXX=/home/xranby/rpi-buildroot/output/host/usr/bin/arm-buildroot-linux-gnueabi-g++
CC=/home/xranby/rpi-buildroot/output/host/usr/bin/arm-buildroot-linux-gnueabi-gcc
LD=/home/xranby/rpi-buildroot/output/host/usr/bin/arm-buildroot-linux-gnueabi-gcc
CPP_FLAGS=-lstdc++
CXX_FLAGS=-lstdc++


when running make i pass the following options to make
# BUILD_CC and BUILD_LD shall point to host x86 executables.
BUILD_CC=gcc
BUILD_LD=gcc
OBJCOPY=/home/xranby/rpi-buildroot/output/host/usr/bin/arm-buildroot-linux-gnueabi-objcopy
STRIP=/home/xranby/rpi-buildroot/output/host/usr/bin/arm-buildroot-linux-gnueabi-strip
CPP=/home/xranby/rpi-buildroot/output/host/usr/bin/arm-buildroot-linux-gnueabi-cpp
CXX=/home/xranby/rpi-buildroot/output/host/usr/bin/arm-buildroot-linux-gnueabi-g++
CC=/home/xranby/rpi-buildroot/output/host/usr/bin/arm-buildroot-linux-gnueabi-gcc
LD=/home/xranby/rpi-buildroot/output/host/usr/bin/arm-buildroot-linux-gnueabi-gcc
#and finally the target, this will build compact profiles, jre and jdk 
images.
all images profiles CONF=linux-arm-normal-zero-release


I have automated these configure and make options for openjdk 9 into 
buildroot build scripts
https://github.com/xranby/rpi-buildroot/tree/openjdk

>
> I've done Zero builds before but have never used OpenJDK's cross-compile
> feature to build it on ARMv7. There is an Arm32 bit JIT in IcedTea[1],
> but I don't know if that can be cross-compiled.

If you cross compile icedtea then you need to make sure that the 
bytecode generator tool is compiled by the host x86 toolchain.
openembedded meta-java contains buildscripts that can cross compile the 
Arm32 JIT found in IcedTea.
https://github.com/woglinde/meta-java

Cheers
Xerxes

May 6, 2015

hg change history

hg rebase, hg histedit, hg graft, hg strip, hg strip --keep, and hg commit --amend.

April 30, 2015

vim ctags tags path

This line in your ~/.vimrc should help:
set tags=./tags,tags;$HOME

It tells Vim to look for a tags file in the directory of the current file, in the current directory and up and up until your $HOME (that's the meaning of the semicolon), stopping on the first hit.

April 27, 2015

golang win32 UI

https://github.com/lxn/walk

OR

https://github.com/AllenDang/gform

OR

https://github.com/andlabs/ui

April 23, 2015

USB terms

In the USB world, the most basic data transfer is done in packet, but to understand the on-the-wire protocol, there are other things to understand.

Packet
USB Packet types: (Source: http://www.usbmadesimple.co.uk/ums_3.htm)

PID Type PID Name PID<3:0>*
Token OUT 0001b
IN 1001b
SOF 0101b
SETUP 1101b
Data DATA0 0011b
DATA1 1011b
DATA2 0111b
MDATA 1111b
Handshake ACK 0010b
NAK 1010b
STALL 1110b
NYET 0110b
Special PRE 1100b
ERR 1100b
SPLIT 1000b
PING 0100b
Reserved 0000b

Transaction: 
   
One transaction has 3 packets: Token + Data + Handshake for a successful transaction. (*). Nak and Stall error condition could happen after Token when no data is available.

There are 3 types of transactions: IN, OUT, SETUP

 *: For IN and OUT transactions used for isochronous transfers, there are only 2 packets; there is no handshake packet.

Transfer:
There are 4 types of transfer:

  • Control 
  • Interrupt
  • Bulk
  • Isochronous (such as audio data)
HID device only uses Control and Interrupt

Control transfer = 3 transactions: SETUP + IN/OUT DATA + STATUS(IN/OUT transaction)
Other transfers has just one transaction: IN/OUT data.

In general, you want to pay attention to Transfer or Transaction level, not packet level. 

April 22, 2015

prevent ssh client timeout

Add this in /etc/ssh/ssh_config for Linux and ~/.ssh/config for Mac:


Host *
ServerAliveInterval 120

linux cp without overwrite

You can use "-n" option.

If you are on a embedded linux box with busybox, it does not have that option. You can this instead:

false | cp -i source destination 2>/dev/null

April 16, 2015

Vim show C function name

Put this in your .vimrc file, and use key "f" to show current function name:

fun! ShowFuncName()
  let lnum = line(".")
  let col = col(".")
  echohl ModeMsg
  echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bW'))
  echohl None
  call search("\\%" . lnum . "l" . "\\%" . col . "c")
endfun
map f :call ShowFuncName() <CR>

April 15, 2015

golang combine static data (css/js/images/) with binary file

https://github.com/tiebingzhang/statik (zip the contents and compiled it into go source code, very small code base, this is a github fork with the slash fix)

https://github.com/jteeuwen/go-bindata

https://github.com/GeertJohan/go.rice (can either compile zip into go source, or append the zip to existing go EXE, more flexible but code base is a little larger)

April 14, 2015

script to detected unused C files

If you have a large project with a lot of C files, once you compiled them, you will end up with a lot of .o files. Then you can use the .o files to find out which .c files is not used (conditionally compiled/not compiled). To do this, follow the steps below:

1. cd subdir
2. ls -1 > ../1.out
3. gawk -f lsnu.awk ../1.out

The lsnu.awk script
{
        if ($1 ~ /\.c$/){
                cfiles[$1]=1;
                print $1;
                next;
        }
        if ($1 ~ /\.o$/){
                ofiles[$1]=1;
                #printf("%s\n",$1);
                next;
        }

}

END{
        printf("-------------\n");
        for ( i in  cfiles){
                f=i;
                gsub(/\.c$/,".o",i);
                if (ofiles[i]!=1){
                        printf("%s ",f);
                        }
        }
        printf("\n");
}

March 12, 2015

cmake add include path and library path

Some tips for cMake beginners. This is done on Linux.

How to use cMake

cmake generates a bunch of intermediate files, including the final Makefile. To make things clean, make a separate directory for the build process:

mkdir build
cd build
cmake ..

To re-run cmake after changing the CMakeLists.txt, do:

cd build
rm -rf *
cmake ..

Cross Compile to ARM

To cross compile to an embedded target, add the following lines to you CMakeLists.txt at the very top (after VERSION)
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_C_COMPILER arm-none-linux-gnueabi-gcc)

Add include paths

include_directories("${PROJECT_SOURCE_DIR}/../" "${PROJECT_SOURCE_DIR}/build/" "${PROJECT_SOURCE_DIR}")

Add a library

FIND_LIBRARY(json json-c json)

1st parameter: output,  The variable that will hold the results of FIND_LIBRARY
2nd parameter: the library to look for (in this case, libjson-c.so)
3rd parameter: the path to look, starting with ${PROJECT_SOURCE_DIR}

The returned varilable can be used in the following statement to add the library to link time:
TARGET_LINK_LIBRARIES(${json})

Assign a variable

SET(LIBS ${ubox} ${blobmsg_json} ${json})

assigns the concatenated value of ubox, blobmsg_json and json to the variable LIBS



March 11, 2015

AT&T Data plan web page

https://buyasession.att.com

Linux running 4G LTE modem

The modem I have is Sierra Wireless Air Prime MC7453 Mini PCI Express Module.  The carrier I tested is AT&T.

Before running on Linux, I had to install the card on a regular Laptop that supports 4G card, and run Windows 7 on the laptop, install all the drivers and the Sierra Wireless Connect Manager to try out the card. This confirmed that the card has the latest firmware, and the carrier setting is correct, etc.

Next I run it on Linux. Instead of using the Gobi driver from Sierra Wireless, I used the qmi_wwan driver in the latest Linux (after kernel version 3.14).  An user land utility is also needed. libqmi seems to be popular, but it depends on a fairly new version of glib, which itself depends on a bunch of libraries. It becomes a hassle to compile everything. Then I discovered uqmi, a small utility developed and used by the openwrt project. It's much more lightweight, and seems to be pretty mature. I was able to get my modem to connect and ping with the following commands.

device="/dev/cdc-wdm1"
modes="lte"
uqmi -s -d "$device" --get-pin-status
uqmi -s -d "$device" --get-serving-system
uqmi -s -d "$device" --set-data-format 802.3
uqmi -s -d "$device" --wda-set-data-format 802.3
uqmi -s -d "$device" --set-network-modes "$modes"
wds=`uqmi -s -d "$device" --get-client-id wds`
uqmi -s -d "$device" --set-client-id wds,"$wds" --start-network "ISP.CINGULAR" --autoconnect
uqmi -s -d "$device" --get-signal-info
uqmi -s -d "$device" --get-data-status
dhclient wwan1

Now I can ping

To disconnect:
uqmi -s -d "$device" --stop-network 0xffffffff    --autoconnect > /dev/null


[1] The home page of uqmi:  http://nbd.name/gitweb.cgi?p=uqmi.git;a=summary
[2] A list of useful commands of how to use uqmi: http://trac.gateworks.com/wiki/modem
[3] The official Openwrt qmi script that uses uqmi:  https://dev.openwrt.org/browser/trunk/package/network/utils/uqmi/files/lib/netifd/proto/qmi.sh
[4] Another useful link: https://lists.openwrt.org/pipermail/openwrt-devel/2014-April/025046.html

March 5, 2015

How to get Linux USB Report Descriptor when it says UNAVAILABLE

Start by plugging in your USB device and running dmesg to get the kernel output for the event. It will look something like this:

input: Sony PLAYSTATION(R)3 Controller as /devices/pci0000:00/0000:00:04.1/usb2/2-4/2-4.2/2-4.2:1.0/input/input16

The first line input: ... is what we want. It contains the full device path and in this case we are interested in third last component of the path: 2-4.2:1.0.

To get the USB report Descriptor with "lsusb -v", we need to unbind the device first (as root).
$ sudo bash -c "echo -n 2-4.2:1.0 >/sys/bus/usb/drivers/usbhid/unbind"

Now run lsusb again for the device and you should see the report descriptor in the output!

Patch Linux kernel to support Esterline Medigenic keyboard

The Esterline Medigenic keyboard does not work under stock Linux kernel (as of kernel version 4.0RC). This is because the HID Report Descriptor of the keyboard is wrong for Linux (and Mac OS). Under Linux kernel tree driver/hid there are a bunch of "special" drivers for different hid devices, and we can create one for the medigenic keyboard to fix up the HID report descriptor so that it works under Linux.

1. Create a file under driver/hid called hid-medigenic.c with the following content:
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 */

#include <linux/device.h>
#include <linux/hid.h>
#include <linux/module.h>

#include "hid-ids.h"

/* the fixups that need to be done:
 *   - change keyboard Usage Mininum from 1 to 0
 */
static __u8 *medigenic_report_fixup(struct hid_device *hdev, __u8 *rdesc,
                unsigned int *rsize)
{
        if (*rsize >= 59 &&
                rdesc[24] == 0x19 && rdesc[25] == 0x01 &&
                rdesc[26] == 0x29 && rdesc[27] == 0x65 ) {
                dev_info(&hdev->dev, "fixing up medigenic keyboard report descriptor\n");
                rdesc[25] = 0x00;
        }
        return rdesc;
}

static const struct hid_device_id medigenic_devices[] = {
        { HID_USB_DEVICE(USB_VENDOR_ID_ESTERLINE, USB_DEVICE_ID_MEDIGENIC) },
        { }
};
MODULE_DEVICE_TABLE(hid, medigenic_devices);

static struct hid_driver medigenic_driver = {
        .name = "medigenic",
        .id_table = medigenic_devices,
        .report_fixup = medigenic_report_fixup,
};

static int __init medigenic_init(void)
{
        return hid_register_driver(&medigenic_driver);
}

static void __exit medigenic_exit(void)
{
        hid_unregister_driver(&medigenic_driver);
}

module_init(medigenic_init);
module_exit(medigenic_exit);
MODULE_LICENSE("GPL");

2. Edit file driver/hid/hid-ids.h to add the following two lines:
#define USB_VENDOR_ID_ESTERLINE 0x059d
#define USB_DEVICE_ID_MEDIGENIC 0x0708


3. Edit file driver/hid/Kconfig to add the following lines:
config HID_MEDIGENIC
    tristate "Medigenic Keyboard Support" if EMBEDDED
    depends on USB_HID
    default !EMBEDDED
    ---help---
    Support for Medigenic keyboard.

4. Edit file driver/hid/Makefile to add the following line:
obj-$(CONFIG_HID_MEDIGENIC)     += hid-medigenic.o

Then in top directory of kernel, do make menuconfig, and exit, and save (somehow medigenic is automatically set to Y when you do this). Check .config to make sure you have the following line:
CONFIG_HID_MEDIGENIC=y

The build your kernel and the keyboard should work now.

memdump function in kernel

void memdump(unsigned char *rdesc, int *rsize){
    int i,pos=0;
    unsigned char outbuf[256];
    printk(KERN_WARNING "rsize=%d\n",*rsize);
    for (i=0;i<*rsize;i++){
        pos+=sprintf(outbuf+pos,"%02x ",rdesc[i]);
        if (i>0 && ((i+1)%16)==0){
            printk(KERN_WARNING "%04x dump %s\n",(i/16)*16,outbuf);
            pos=0;
        }
    }
    if (pos>0){
        printk(KERN_WARNING "%04x dump %s\n",(i/16)*16,outbuf);
    }
}

March 3, 2015

replace systemd with a simple busybox inittab

1. install busybox
2. create file /etc/inittab with the following content:
# Start "rc init" on boot
::sysinit:/opt/rc init

# Set up the TTY's 1 through 4
tty1::askfirst:/sbin/agetty -8 -s 38400 tty1 linux
tty2::respawn:/sbin/agetty -8 -s 38400 tty2 linux

# Stop all services on shutdown
::shutdown:/opt/rc shutdown

# Killing everything on shutdown
::shutdown:echo :: sending SIGTERM to all
::shutdown:/bin/kill -s TERM -1
::shutdown:sleep 1
::shutdown:echo :: sending SIGKILL to all
::shutdown:/bin/kill -s KILL -1

# Unmount everything on shutdown
::shutdown:echo :: unmounting everything
::shutdown:/bin/umount -a -r
::shutdown:/bin/mount -o remount,ro /

3. ln -s /bin/busybox /opt/init
4. create /opt/rc with the following content:
#!/bin/sh
on_boot() {
    #===================
    # mount the API filesystem
    # /proc, /sys, /run, /dev, /run/lock, /dev/pts, /dev/shm
    echo 3 mounting API filesystem...
    mountpoint -q /proc    || mount -t proc proc /proc -o nosuid,noexec,nodev
    mountpoint -q /sys     || mount -t sysfs sys /sys -o nosuid,noexec,nodev
    mountpoint -q /run     || mount -t tmpfs run /run -o mode=0755,nosuid,nodev
    mountpoint -q /dev     || mount -t devtmpfs dev /dev -o mode=0755,nosuid
    mkdir -p /dev/pts /dev/shm
    mountpoint -q /dev/pts || mount -t devpts devpts /dev/pts -o mode=0620,gid=5,nosuid,noexec
    mountpoint -q /dev/shm || mount -t tmpfs shm /dev/shm -o mode=1777,nosuid,nodev

    #===================
    # initialize system
    echo 3 setting up loopback device...
    /usr/sbin/ip link set up dev lo

    echo 3 initializing udev...
        busybox mdev -s
        echo /sbin/mdev > /proc/sys/kernel/hotplug

    echo 3 setting hostname...
    cat /etc/hostname >| /proc/sys/kernel/hostname

    echo 3 mounting...
    mount -a
    mount -o remount,rw /

        dhclient eth0&
        /etc/init.d/ssh start&
}

on_shutdown() {
    #===================
    echo 3 shutting down udev...
        killall busybox
        killall mdev

    #===================
    # umount the API filesystem
    echo 3 unmounting API filesystem...
    umount -r /run
}

#===================
# handle arguments
case "$1" in
init)
    on_boot;;
shutdown)
    on_shutdown;;
esac

5. reboot and add the following parameter to your kernel command line on grub:
init=/opt/init

Enjoy

February 26, 2015

Golang parse xml simple example

package main

import (
        "encoding/xml"
        "fmt"
)

func main() {
        type Email struct {
                Where string `xml:",attr"`
                Addr  string
        }
        type Result struct {
                Email   []Email `xml:"email"`
        }
        v := Result{}

        data := `
        <person>
        <email where="home">
        <Addr>gre@example.com</Addr>
        </email>
        <email where='work'>
        <Addr>gre@work.com</Addr>
        </email>
        </person>
        `
        err := xml.Unmarshal([]byte(data), &v)
        if err != nil {
                fmt.Printf("error: %v", err)
                return
        }
        fmt.Printf("v: %#v\n", v)
}

February 23, 2015

Asynchronous PHP port scanner on Windows

The following PHP code scans IP range 192.168.204.200 to 192.168.204.254 port 443 in 5 seconds. It's tested on Windows with PHP 5.3, with php_socket extension enabled. 


<?php
$port = "443";
$timeout = 5;  //timeout in seconds

$write=array();
for ($i=200;$i<255;$i++){
$host="192.168.204.$i";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Unable to create socket\n");
socket_set_nonblock($socket) or die("Unable to set nonblock on socket\n");
$connected=@socket_connect($socket, $host, $port);
if (!$connected) {
$error = socket_last_error($socket);
if ($error != 10035 && $error != SOCKET_EINPROGRESS && $error != SOCKET_EALREADY) {
socket_close($socket);
}else{
$write[]=$socket;
}
}
}

$count=0;
$write0=$write;
$mynil=NULL;
$timeout_us=0;
$endtime=microtime(true)+$timeout;
$address="";
while(true){
$ret=socket_select($mynil,$write,$mynil,$timeout,$timeout_us);
if ($ret==0){
die("Done. Total $count found.\n");
}
if ($ret>0){
foreach ($write as $sock){
socket_getpeername($sock,$address);
echo "$address\n";
$count++;
socket_close($sock);
}
$write=array_diff($write0,$write);
$write0=$write;
$newtimeout=$endtime-microtime(true);
$timeout=floor($newtimeout);
$timeout_us=$newtimeout-$timeout;
}
}

PHP asynchronous host scanner on Windows

The following PHP code scans IP range 192.168.5.1-192.168.5.254 in 3 seconds and returns the reachable hosts.

<?php
$timeout=3;

function ping($host, $timeout = 1) {
/* ICMP ping packet with a pre-calculated checksum */
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
$socket  = socket_create(AF_INET, SOCK_RAW, 1);
socket_connect($socket, $host, null);

$ts = microtime(true);
socket_send($socket, $package, strLen($package), 0);
return $socket;
}

$read=array();
for ($i=1;$i<255;$i++){
$host="192.168.5.$i";
$socket=ping($host);
$read[]=$socket;
}

$read0=$read;
$mynil=NULL;
$timeout_us=0;
$endtime=microtime(true)+$timeout;
while(true){
$ret=socket_select($read,$mynil,$mynil,$timeout,$timeout_us);
if ($ret==0){
die("Done\n");
}
if ($ret>0){
foreach ($read as $sock){
$address="";
socket_getpeername($sock,$address);
echo "$address\n";
}
$read=array_diff($read0,$read);
$read0=$read;
$newtimeout=$endtime-microtime(true);
$timeout=floor($newtimeout);
$timeout_us=$newtimeout-$timeout;
}
}

February 18, 2015

use stunnel for ssl proxy

stunnel.conf: (this setup one server and one client instance)

debug = 3
#foreground = yes
pid =
[server]
client = no
cert= ./server.pem
accept = 127.0.0.1:443
connect = 127.0.0.1:4434
[client]
client = yes
accept = 127.0.0.1:4434
connect = api.opscode.com:443

February 14, 2015

How to run an X program on a headless Linux server



# apt-get install xvfb

# Xvfb -shmem -screen 0 1280x1024x24

 To test it you can run a following command:

# DISPLAY=:0 xdpyinfo


February 4, 2015

snmp manager for Windows and Mac

In addition to the paid version (iReasonsing mibbrowser) and agentpp's mib explorer, there is a free and open source version called snmpb: http://snmpb.sf.net. I have tried it out for a short time and it worked well for me. It includes walk/get/set/table-view and also an trap receiver.

wireshark display filters

by Joke Snelders

Display Filters

To show just traffic from/to a specific station, use

wlan.addr==00:01:02:03:04:05

or wlan.ta , wlan.ra, wlan.sa, wlan.da

  • Show only the beacon frames:
    wlan.fc.type_subtype == 0x08
  • Show everything except the beacon frames:
    !wlan.fc.type_subtype == 0x08
  • Show only beacon frames and ack frames:
    (wlan.fc.type_subtype == 0x08) || (wlan.fc.type_subtype == 0x1d) 
  • Show everything except the beacon and ack frames
    (!wlan.fc.type_subtype == 0x08) && (!wlan.fc.type_subtype == 0x1d)
You will find more information in the Wireshark User's Guide and in the Wireshark Wiki.

In the Wireshark Display Filter Reference you will find an overview of the field names.
On the website Will Hack For Sushi you can find a cheat sheet, the 802.11 Pocket Reference Guide, with the type codes you can use in combination with wlan.fc.type and wlan.fc.type_subtype.

You can download the 802.11 Pocket Reference Guide here.



Here are some examples of the Display Filter Fields and next you will learn how to use them as a display filter:
Frame typeFilter
Management frameswlan.fc.type eq 0
Control frameswlan.fc.type eq 1
Data frameswlan.fc.type eq 2

Frame subtypeFilter
Association requestwlan.fc.type_subtype eq 0
Association responsewlan.fc.type_subtype eq 1
Probe requestwlan.fc.type_subtype eq 4
Probe responsewlan.fc.type_subtype eq 5
Beaconwlan.fc.type_subtype eq 8
Authenticationwlan.fc.type_subtype eq 11
Deauthenticationwlan.fc.type_subtype eq 12

Display Filters
  • Show beacons:
    wlan.fc.type_subtype eq 8
  • Show everything except the beacons:
    not wlan.fc.type_subtype eq 8
  • Show probe requests or probe responses:
    wlan.fc.type_subtype eq 4 or wlan.fc.type_subtype eq 5
  • Show everything except the beacons, probe requests or probe responses:
    not wlan.fc.type_subtype eq 4 and not wlan.fc.type_subtype eq 5 and not wlan.fc.type_subtype eq 8


Capture filters
When you use a capture filter only the packets that match the filter are dumped  to a file. This will reduce the amount of data to be captured.

Capture filters have a different syntax than display filters.

You enter the capture filters into the Filter field of the Wireshark Capture Options dialog box and hit the Start button.

Here are some examples:

  • Capture only beacon frames:
    wlan[0] == 0x80
  • Capture everything except beacon frames:
    wlan[0] != 0x80
  • Capture only beacon frames and ack frames:
    wlan[0] == 0xd4
  • Capture everything except beacon frames and ack  frames:
    wlan[0] != 0x80 and wlan[0] != 0xd4
You can use a wlan type or a wlan subtype as a capture filter.
Let me give you some capture filter samples.

WLAN type
Valid wlan types are mgt, ctl and data.

Capture filter examples
  • Capture only management frames:
    type mgt
  • Capture everything except control frames:
    not type ctl
  • Capture data frames to/from mac address 04:1e:64:ea:c3:ef
    wlan host 04:1e:64:ea:c3:ef and type data

WLAN subtype
Management frames
Valid subtypes are:
assocreq,  assocresp,  reassocreq,  reassocresp,  probereq, probresp, beacon, atim, disassoc, auth and deauth

Control frames
Valid subtypes are:
ps-poll, rts, cts, ack, cf-end and cf-end-ack

Data frames
Valid subtypes are:
data,  data-cf-ack,  data-cf-poll, data-cf-ack-poll, null, cf-ack, cf-poll, cf-ack-poll,  qos-data,  qos-data-cf-ack,  qos-data-cf-poll, qos-data-cf-ack-poll, qos, qos-cf-poll and qos-cf-ack-poll

Capture filters examples
  • Capture only beacons:
    subtype beacon
  • Capture everything except beacons:
    not subtype beacon
  • Capture beacons, probe requests and probe responses:
    subtype beacon or subtype probereq or subtype proberesp
  • Capture all frames except beacons, probe requests and probe responses:
    not subtype beacon and not subtype probereq and not subtype proberesp
  • Capture beacons, probe requests and probe responses to/from host 00:0c:f6:69:f8:69:
    (wlan host 00:0c:f6:69:f8:69 and subtype beacon) or (wlan host 00:0c:f6:69:f8:69 and subtype probereq) or (wlan host 00:0c:f6:69:f8:69 and subtype proberesp)

    You can also use this capture filter:

    wlan host 00:0c:f6:69:f8:69 and (subtype beacon or subtype probereq or subtype proberesp)
  • Capture probe requests from wlan host 00:0c:f6:69:f8:69 and probe responses from wlan host: 00:24:2c:69:f8:69
    (wlan host 00:0c:f6:69:f8:69 and subtype probereq) or (wlan host 00:24:2c:69:f8:69 and subtype proberesp)
  • Capture beacons, probe requests and probe responses to/from host 00:0c:f6:69:f8:69 or to/from host 00:24:2c:69:f8:69:
    (wlan host 00:0c:f6:69:f8:69 or wlan host 00:24:2c:69:f8:69) and (subtype beacon or subtype probereq or subtype proberesp)
  • Capture all packets from wlan src 00:24:2c:69:f8:69 except beacons, probe requests and probe responses:
    wlan src 00:24:2c:69:f8:69 and not subtype beacon and not subtype probereq and not subtype proberesp
  • Capture all association requests/responses, reassociation requests/responses, disassociation and (de)authentication frames and all eapols:
    (subtype assocreq or subtype assocresp or subtype reassocreq or subtype reassocresp or subtype disassoc or subtype auth or subtype deauth) or (ether proto 0x888e)
  • Capture all eapols, association requests/responses, reassociation requests/responses, disassociation and (de)authentication frames to/from wlan host 00:0c:f6:69:f8:69 or wlan host 00:24:2c:69:f8:69:
    (wlan host 00:0c:f6:69:f8:69 or wlan host 00:24:2c:69:f8:69) and (ether proto 0x888e or subtype assocreq or subtype assocresp or subtype reassocreq or subtype reassocresp or subtype disassoc or subtype auth or subtype deauth)
  • Capture all frames to/from wlan host 00:0c:f6:69:f8:69 or wlan host 00:24:2c:69:f8:69:
    wlan host 00:0c:f6:69:f8:69 or wlan host 00:24:2c:69:f8:69

Interesting links:
Understanding 802.11 Frame Types by Jim Geier
Ubuntu manual
Wireless Communications by Martin Land
WildPackets: Wireless LAN Overview
Packetstan: A blog about packets, tools, and bacon

  
Save the display and capture filters to file for future use
File dfilters
To save the display filters for future use you can modify the file dfilters. 
In Windows XP the file dfilters is located at:
C:\Documents and Settings\<user>\Application Data\Wireshark\dfilters
In Windows 7 or Windows Server 2008 at:
C:\Users\<user>\AppData\Roaming\Wireshark\dfilters
Notes:
  • You have to turn on "Show Hidden Files, Folders, and drives" to see the AppData folder:
    go to Control Panel\All Control Panel Items -> Folder Options -> View -> Show Hidden Files, Folders, and drives.
  • If there is no file dfilters at this location, you can copy and paste the file from C:\Program Files\Wireshark\dfilters to C:\Users\<user>\AppData\Roaming\Wireshark\dfilters.
  • The file dfilters has no extension.
Open the file dfilters with Notepad.
Copy and paste the following text to dfilters:
"WLAN DISPLAY FILTERS" HEADER
"    Beacon Frames" wlan.fc.type_subtype == 0x08
"    No Beacon Frames" !wlan.fc.type_subtype == 0x08
"    Beacon Frames or Ack's" (wlan.fc.type_subtype == 0x08) || (wlan.fc.type_subtype == 0x1d)
"    No Beacon Frames and No Ack" (!wlan.fc.type_subtype == 0x08) && (!wlan.fc.type_subtype == 0x1d)

Make sure to end the file with an empty line, otherwise you won't see the last filter.

File cfilters
Repeat the steps above to modify the file cfilters.

Copy and paste the following text to cfilters:
"WLAN CAPTURE FILTERS" HEADER
"    Capture only Ethernet type EAPOL" ether proto 0x888e
"    Beacon Frames" wlan[0] == 0x80
"    No Beacon Frames" wlan[0] != 0x80
"    Probe Requests" wlan[0] == 0x40
"    No Probe Requests" wlan[0] != 0x40
"    Probe Response" wlan[0] == 0x50
"    No Probe Response" wlan[0] != 0x50
"    Ack" wlan[0] == 0xd4
"    No Ack" wlan[0] != 0xd4
"    CF-End" wlan[0] == 0xe4
"    No CF-End" wlan[0] != 0xe4
"    Clear-to-send" wlan[0] == 0xc4
"    No Clear-to-send" wlan[0] != 0xc4
"    Beacon Frames - Probe Response/Request - Ack" wlan[0] == 0x80 or wlan[0] == 0x50 or wlan[0] == 0x40 or wlan[0] == 0xd4
"    No Beacon Frames - No Probe Response/Request - No Ack" wlan[0] != 0x80 and wlan[0] != 0x50 and wlan[0] != 0x40 and wlan[0] != 0xd4
"    Beacon Frames-Probe Resp/Req-Ack-CF-End-Clear-to-send" wlan[0] == 0x80 or wlan[0] == 0x50 or wlan[0] == 0x40 or wlan[0] == 0xd4 or wlan[0] == 0xe4 or wlan[0] == 0xc4
"    No Beacon Frames-Probe Resp/Req-Ack-CF-End-Clear-to-send" wlan[0] != 0x80 and wlan[0] != 0x50 and wlan[0] != 0x40 and wlan[0] != 0xd4 and wlan[0] != 0xe4 and wlan[0] != 0xc4

After you have edited the files and restarted Wireshark the new filters show up in the "Display Filters" and "Capture Filters" dialog boxes.

The original post: http://www.lovemytool.com/blog/2010/07/wireshark-wireless-display-and-capture-filters-samples-part-2-by-joke-snelders.html


January 30, 2015

wifi channel and frequency list

Channel Frequency(MHz)
1 2412
2 2417
3 2422
4 2427
5 2432
6 2437
7 2442
8 2447
9 2452
10 2457
11 2462
12 2467
13 2472
14 2484
36 5180
40 5200
44 5220
48 5240
52 5260
56 5280
60 5300
64 5320
100 5500
104 5520
108 5540
112 5560
116 5580
120 5600
124 5620
128 5640
132 5660
136 5680
140 5700
149 5745
153 5765
157 5785
161 5805
165 5825

5Ghz WiFi Channels (US)


Source: http://www.revolutionwifi.net/blog/?month=april-2014&view=calendar

convert pdf to png gray

gs -sDEVICE=pnggray -sOutputFile=page-%03d.png -r600x600   -f test-ecg.pdf

December 18, 2014

Install a minimal X on Linux

apt-get install xinit i3 rxvt

i3 is the lightweight window manager
rxvt is the terminal
xinit gives you X windows and the famous "startx" command

To make i3 use your full screen resolution, create the file ~/.xinitrc and put the following there

xrandr --output Virtual1 --mode 1680x1050
exec i3


You may need to change the name "Virtual1" to something else. Use xrandr to list all the known windows (you first need to have X windows  running though)

December 9, 2014

p11tool, gnutls and PIV CAC card

p11tool --list-all-certs

p11tool --login --export "pkcs11:model=PKCS%2315%20emulated;manufacturer=piv_II;serial=36889385781093f6;token=PIV_II%20%28PIV%20Card%20Holder%20pin%29;id=%02;object=Certificate%20for%20Digital%20Signature;object-type=cert" > /tmp/02.cert

p11tool --list-all-privkeys --login



x509 certificate subject name and OID

In a X509 certificate, there is always a subject name like the following:

$ openssl x509 -in user-cert.pem -text -noout
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 1373122324 (0x51d82f14)
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: CN=CA
        Validity
            Not Before: Jul  6 14:52:05 2013 GMT
            Not After : May 15 14:52:05 2023 GMT
        Subject: UID=test,CN=A user

Inside the subject line, there can be multiple subparts, such as CN=xxx, DC=xxxx, UID=xxx, OU=xxx, C=xxx, ... Each subpart is represented in the certificate as an OID that is globally unique and registred with IETF. For example, the OID of CN is 2.5.4.3, and the OID of UID is 0.9.2342.19200300.100.1.1. How are we supposed to find out the OID? Openssl provides a command option for just.  

openssl x509 -in user-cert.pem -text -noout -nameopt RFC2253,oid

This command will print out the cert with the OID=xxx instead of CN=xxx.

December 8, 2014

cross compile openconnect

Openconnect is a nice open source SSL VPN client for Cisco AnyConnect, and also for the open source SSL vpn server ocserv (hosted on the same website as openconnect). Below are some tips on how to cross compile openconnect for ARM, with GnuTLS

Openconnect works with both Openssl and GnuTLS. However, to use hardware token (smart card, etc), you will need GnuTLS.

Dependencies:

Openconnect depends on GnuTLS (3.3.9)
GnuTLS depends on libnettle, and libhogweed 2.7.1 (both in the nettle package)
libnettle depends on gnu GMP (libgmp, version 6.0.0)

To use hardware token, GnuTLS also depends on p11-kit (version 0.22.1) and pcsc-lite (version 1.8.11), and opensc (0.14.0), which depends on pcsc-lite.

All of these packages support autoconfig so that one can run "configure" to generate the makefile(s).  We use the --prefix "/opt/ncs-install" to install all packages. Below are the customized "configure" scripts for each package:

p11-kit-0.22.1:
CC=arm-none-linux-gnueabi-gcc CXX=arm-none-linux-gnueabi-g++ ./configure --host=arm-linux --prefix=/opt/ncs-install \
        --without-libffi --without-libtasn1

gmp-6.0.0:
CC=arm-none-linux-gnueabi-gcc CXX=arm-none-linux-gnueabi-g++ ./configure --host=arm-linux --prefix=/opt/ncs-install

nettle-2.7.1:
CFLAGS=-I/opt/ncs-install/include LDFLAGS=-L/opt/ncs-install/lib CC=arm-none-linux-gnueabi-gcc CXX=arm-none-linux-gnueabi-g++ ./configure --host=arm-linux --prefix=/opt/ncs-install

pcsc-lite-1.8.11:
CFLAGS="-I/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/include/"
CC=arm-none-linux-gnueabi-gcc ./configure -host=arm-linux  --disable-libudev --enable-libusb \
        LIBUSB_CFLAGS="-I/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/include/libusb-1.0/ -L/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/lib"  \
        LIBUSB_LIBS="-lusb-1.0"

/opensc-0.14.0:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export LTLIB_LIBS="-L/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/lib/ -lltdl"
export PCSC_CFLAGS="-I$DIR/../pcsc-lite-1.8.11/src/PCSC"
export LIBTOOL_SYSROOT_PATH=/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/
export CFLAGS="-I/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/include -L/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/lib"
export LDFLAGS="-lcrypto"
export CC=arm-none-linux-gnueabi-gcc
./configure -host=arm-linux  -v

gnutls-3.3.9:
CFLAGS=-I/opt/ncs-install/include LDFLAGS=-L/opt/ncs-install/lib \
ZLIB_CFLAGS=-I/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/include/ \
ZLIB_LIBS="-L/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/lib -lz" \
CC=arm-none-linux-gnueabi-gcc CXX=arm-none-linux-gnueabi-g++ ./configure --host=arm-linux --prefix=/opt/ncs-install \
 --with-nettle-mini --disable-crywrap \
 --with-p11-kit \
 --with-default-trust-store-file=/etc/ssl/certs/ca-certificates.crt


openconnect-7.00:
LIBPCSCLITE_CFLAGS=-I/opt/ncs-install/include/PCSC/ \
LIBPCSCLITE_LIBS="-L/opt/ncs-install/lib -lpcsclite" \
LIBXML2_CFLAGS=-I/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/include/libxml2/ \
LIBXML2_LIBS="-L/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/lib -lxml2" \
CFLAGS=-I/opt/ncs-install/include LDFLAGS=-L/opt/ncs-install/lib  \
LDFLAGS="-L/opt/ncs-install/lib -lp11-kit -lnettle -lhogweed -lgmp" \
ZLIB_CFLAGS=-I/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/include/ \
ZLIB_LIBS="-L/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/lib -lz" \
CC=arm-none-linux-gnueabi-gcc ./configure --prefix=/opt/install --disable-nls --host=arm-linux --without-openssl --with-gnutls


December 4, 2014

how to verify certificate signed by intermediate CA

 openssl verify -untrusted intermediate-ca.pem your-cert.pem

Put the list of intermediate CA (in PEM format, concatenated ) in intermediate-ca.pem, and use the "-untrusted" option. That name tricked me initially, and that's the one to use. 

The above command is to use the system CA list to verify the cert. If you have your own CA, just use the option "-CAfile your-ca.pem".