30 October 2012

automatic proxy configuration for your pc

A proxy auto-config (PAC) file contains a JavaScript function "indProxyForURL(url, host)". This function returns a string with one or more access method specifications.

The URL of the PAC file is either configured manually or determined automatically by the Web Proxy Autodiscovery Protocol.

A very simple example of a PAC file is:
function FindProxyForURL(url, host)
{
   return "PROXY proxy.example.com:8080; DIRECT";
}

The Web Proxy Auto-Discovery Protocol (WPAD) is a method used by clients to locate a URL of a configuration file using DHCP and/or DNS discovery.

Before fetching its first page,
- a web browser sends the local DHCP server a DHCPINFORM query, and uses the URL from the WPAD option in the server's reply. [option 252 ("auto-proxy-config")]
- If the DHCP server does not provide the desired information, DNS is used. [A Web server must be configured to serve the WPAD file with a MIME type of "application/x-ns-proxy-autoconfig"]
c:\> nslookup wpad
On success check for "http://wpad/wpad.dat"

Reference:
http://en.wikipedia.org/wiki/Proxy_auto-config
http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol

23 October 2012

802.11n deployment strategie

To achieve maximum output, a pure 802.11n 5 GHz network is recommended. The 5 GHz band has substantial capacity due to many non-overlapping radio channels and less radio interference as compared to the 2.4 GHz band. An 802.11n-only network may be impractical for many users because they need to support legacy equipment that still is 802.11b/g only. Consequently, it may be more practical in the short term to operate a mixed 802.11b/g/n network until 802.11n hardware becomes more prevalent.

In a mixed-mode system, an optimal solution would be to use a dual-radio access point and place the 802.11b/g traffic on the 2.4 GHz radio and the 802.11n traffic on the 5 GHz radio. This setup assumes that all the 802.11n clients are 5 GHz capable. A technique called "band steering" is used to send 802.11n clients to the 5 GHz band, leaving the 2.4 GHz band for legacy clients. Band steering works by responding only to 5 GHz association requests and not the 2.4 GHz requests from dual-band clients.

802.11n protection mechanisms kick in as soon as an access point hears a legacy device transmitting on the same channel. The legacy device does not have to be associated to the 802.11n access point; the access point just needs to hear it on the same channel. 802.11n standardized support for increased data rate, short guard interval (800us to 400us), 40 MHz channels etc...

MIMO techniques:
- Spatial Multiplexing - this requires MIMO client capable of receiving and de-multiplexing N spatial streams. a 3x3:2 MIMO access point can transmit and receive 2 data streams on its 3 antennas.
- Maximal Ratio Combining (MRC) - MRC is a receive-side MIMO technique that takes RF signals from multiple receive antennas and combines them within the radio to effectively boost the signal strength.
- Transmit Beamforming (TxBF) - focus RF energy toward the target receiver (only supported for 802.11n clients). client send special frequency characteristics and channel response information to the access point. This information is used by the AP in calculating the phase adjustment for its next transmission.

Frame Aggregation Techniques:
- MSDU Aggregation (A-MSDU) - The maximum A-MSDU size allowed by 802.11n is 8192 bytes. The disadvantage of aggregating A-MSDU is that each frame is only protected by a single checksum and the overhead of having to retransmit the entire A-MSDU again.
- MPDU Aggregation with Block ACKs - MPDU (MAC Protocol Data Unit) aggregation gathers 802.11 frames, which each already have an 802.11 header for the same destination and transmits them as a single frame. One of the disadvantages of MPDU aggregation is that each 802.11 frame needs to be encrypted separately, adding encryption overhead.
- Reduced Interframe Spacing (RIFS)



Reference:
http://en.wikipedia.org/wiki/IEEE_802.11n-2009#Deployment_strategies
http://mcsindex.com/
http://www.motorola.com/web/Business/_Documents/White%20Paper/_Static%20files/802%2011nDEM_WP_v4_0209.pdf

21 October 2012

memory allocator

In C - dynamic memory allocation is done using malloc, realloc, calloc and free.

In linux kernel:
__get_free_pages() allocates memory from buddy system
-> /proc/buddyinfo
kmalloc() returns memory from slab caches
-> with slab allocation, memory chunks suitable to fit data objects of certain type or size are preallocated.
-> good for 32bytes to 32pages
-> /proc/slabinfo

In linux user space:
The GNU C library (glibc) uses an allocator based on dlmalloc ("Doug Lea's Malloc").
Allocated memory contains an 8 or 16 byte overhead for the size of the chunk and usage flags.

For requests below 256 bytes (a "smallbin" request), a simple two power best fit allocator is used. If there are no free blocks in that bin, a block from the next highest bin is split in two.

For requests of 256 bytes or above but below the mmap threshold, dlmalloc use an in-place bitwise trie algorithm.

For requests above the mmap threshold (a "largebin" request), the memory is always allocated using the mmap system call. The threshold is usually 256 KB.


Reference:
http://en.wikipedia.org/wiki/C_dynamic_memory_allocation#dlmalloc
http://www.win.tue.nl/~aeb/linux/lk/lk-9.html#ss9.3
http://en.wikipedia.org/wiki/Slab_allocation
http://en.wikipedia.org/wiki/Trie#Bitwise_tries

19 October 2012

TLB

Why Virtual Memory?
 - to handle shortage of memory
   - more active process than physical memory can hold
 - to handle excess memory
   - 32bit processor could support 64GB of RAM
 - to support multiprogramming
   - memory protection
   - memory sharing

Advantages of paging
 - allocation of memory is easy and cheap
 - no external fragmentation
Disadvantages of paging
 - mapping table overhead
 - internal fragmentation

A TLB is part of the chip’s memory-management unit (MMU), and is simply a hardware cache of popular virtual-to-physical address translations. A typical TLB might have 32, 64, or 128 entries.

The MIPS R4000 supports a 32-bit address space with 4KB pages. The VPN translates to up to a 24-bit PFN, and hence can support systems with up to 64GB of (physical) main memory (2^24 4KB pages).
- global bit (G), globally-shared page
- address space identifier (ASID)
- Coherence (C) bits, how a page is cached by the hardware
- a valid bit, a valid translation present in the entry.
- a page mask, for multiple page sizes




if the number of pages a program accesses in a short period of time exceeds the number of pages that fit into the TLB, the program will generate a large number of TLB misses, and thus run quite a bit more slowly.
- database management system (a DBMS), which have certain data structures that are both large and randomly-accessed.

Reference:
http://pages.cs.wisc.edu/~remzi/OSFEP/vm-tlbs.pdf
http://cs.nyu.edu/~gottlieb/courses/2000-01-fall/arch/lectures/lecture-23.html
http://www.slideshare.net/vitlic/linux-memory

11 October 2012

encrypt/decrypt with openssl API -2

The symmetric cipher commands allow data to be encrypted or decrypted using various block and stream ciphers using keys based on passwords.

int AES_set_encrypt_key(const uchar *userKey, const int bits, 
    AES_KEY *key);

void AES_cbc_encrypt(const uchar *in, uchar *out, const ulong length, 
    const AES_KEY *key, uchar *ivec, const int enc);
# ./encrypt password1
cb3d7f690989f7237fd4485a582ac5b2fe98be8ebd6e37c50bb4bdd734a95631
# ./decrypt cb3d7f690989f7237fd4485a582ac5b2fe98be8ebd6e37c50bb4bdd734a95631
password1
download encrypt.c from here
download decrypt.c from here
# echo -n "password1" > plain.txt
# openssl enc -aes-256-cbc -nosalt -k "1234567890abcdef" -iv 0 -in plain.txt -out enc.txt
# hexdump -C enc.txt 
00000000  aa 3b 81 85 64 bf 6d b9  35 de 59 c3 36 41 0e f2  |.;..d.m.5.Y.6A..|
# openssl enc -d -aes-256-cbc -nosalt -k "1234567890abcdef" -iv 0 -in enc.txt
password1


Reference:
http://saju.net.in/blog/?p=36
http://stackoverflow.com/questions/9889492/how-to-do-encryption-using-aes-in-openssl
http://www.128bitstudios.com/2010/05/31/file-encryption-with-openssl/

07 October 2012

Single precision floating point variable

Single-precision floating-point format is a computer number format that occupies 4 bytes (32 bits) in computer memory, known as float in C.

IEEE 754 is a technical standard for floating-point computation.


Converting from decimal to single-precision binary:
26.25

.25 x 2 = 0.50 -> 0
.50 x 2 = 1.00 -> 1

26.25 = 1A.25 = 0001 1010 . 01 = 1.101001 x 2^4

0 (127+4) 101001 = 0 (131) 101001
0 10000011 101001 = 0100 0001 1101 0010 = 0x41D20000

Converting from single-precision binary to decimal:
471BCF90 = 0100 0111 0001 1011 1100 1111 1001 0000

0 10001110 00110111100111110010000 = 0 (142-127) XYZ

1 . 00110111100111110010000 x 2^15 = 1001 1011 1100 1111 . 1001

1x2^-1 + 1x2^-4 =  .5 + .0625 = .5625

9BCF . 1001 = 39887 . 5625 = 39887.5625

Reference:
http://en.wikipedia.org/wiki/Single_precision
http://sandbox.mc.edu/~bennet/cs110/flt/dtof.html
http://www.exploringbinary.com/pi-and-e-in-binary/

05 October 2012

A script to check PNR status

# cat << EOF > pnrdata
lccp_pnrno1=MYPNRNO
EOF

# cat pnrdata
lccp_pnrno1=MYPNRNO

# url=http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi
# lynx -post_data $url < ./pnrdata | 
  grep -A32 PNR | grep "Passenger [0-9]"
   Passenger 1 W/L 114,GNWL W/L 21

wget --user-agent="Myscript 1.0;" \
--header="Content-Type: application/x-www-form-urlencoded" \
--post-data="lccp_pnrno1=MYPNRNO" \
"http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi" -O- 2>/dev/null \
| grep -A2 "Passenger 1"
<TD class="table_border_both"><B>Passenger 1</B></TD>
<TD class="table_border_both"><B>W/L  114,GNWL  </B></TD>
<TD class="table_border_both"><B>W/L   21</B></TD>


Reference:
http://wiki.linux-delhi.org/cgi-bin/twiki/view/Main/LinuxTricks
http://akhileshss.blogspot.com/2008/09/script-to-periodically-check-pnr-status.html