Monthly Archives: March 2016

iptable nf_inet_hooks

Netfilter places

从上网络包发送接受流程图中看出,可以在不同的地方注册Nefilter的hook函数.由如下定义:

enum nf_inet_hooks {
NF_INET_PRE_ROUTING, //0
NF_INET_LOCAL_IN,
NF_INET_FORWARD,
NF_INET_LOCAL_OUT,
NF_INET_POST_ROUTING, //4
NF_INET_NUMHOOKS
};
NF_INET_PRE_ROUTING: incoming packets pass this hook in the ip_rcv() (linux/net/ipv4/ip_input.c) function before they are processed by the routing code.
NF_INET_LOCAL_IN: all incoming packets addressed to the local computer pass this hook in the function ip_local_deliver().
NF_INET_FORWARD: incoming packets are passed this hook in the function ip_forwared().
NF_INET_LOCAL_OUT: all outgoing packets created in the local computer pass this hook in the function ip_build_and_send_pkt().
NF_INET_POST_ROUTING: this hook in the ipfinishoutput() function before they leave the computer.

C++ std map

Declare

std::map first;

Add
first[‘a’]=10;
first[‘b’]=30;
first[‘c’]=50;
first[‘d’]=70;

Test key exist:
if ( first.find(“f”) == first.end() ) {
// not found
} else {
// found
}

it = mymap.begin();
while (it != mymap.end()) {
   if (something)
      mymap.erase(it++);
   else
      it++;
}

C++ 11

std::map<K, V>::iterator itr = myMap.begin();
while (itr != myMap.end()) {
    if (ShouldDelete(*itr)) {
       itr = myMap.erase(itr);
    } else {
       ++itr;
    }
}

Compare Two Java byte Arrays Example

/*
Compare Two Java byte Arrays Example
This java example shows how to compare two byte arrays for equality using
Arrays.equals method.
*/

import java.util.Arrays;

public class CompareByteArraysExample {

public static void main(String[] args) {
//create byte arrays
byte[] byteArray1 = new byte[]{7,25,12};
byte[] byteArray2 = new byte[]{7,25,12};

/*
To compare two byte arrays use,
static boolean equals(byte array1[], byte array2[]) method of Arrays class.

It returns true if both arrays are equal. Arrays are considered as equal
if they contain same elements in same order.
*/

boolean blnResult = Arrays.equals(byteArray1,byteArray2);
System.out.println(“Are two byte arrays equal ? : ” + blnResult);

/*
Please note that two byte array references pointing to null are
considered as equal.
*/

}
}

/*
Output of the program would be
Are two byte arrays equal ? : true
*/