2015年4月26日 星期日

Self define chroot environment

1. Create new required root directory.
2. Create all needed directories. ($newroot $newroot/dev $newroot/bin $newroot/home $newroot/lib $newroot/etc $newroot/usr/lib $newroot/usr/bin $newroot/proc $newroot/dev/pts)
3. Collect needed execution files and libraries. Copy these needed files . The destination location will be on ${newroot}/bin and ${newroot}/lib
4, Copy required configuration files. (/etc files:   group  passwd  profile)
5. Do chroot command. (In user SSH login session, or define in inittab to chroot during sysinit boot time)

2015年4月20日 星期一

影像取樣與擷取

取樣(Sampling) - 連續函數數位化的過程

奈奎斯特定理 (Nyquist Theorem)
奈奎斯特定理說明必須以高於受測訊號的最高頻率兩倍以上的速度進行取樣,才能正確地重建波型

數位影像(digital image)和一般(類比)照片的不同之處在於其xyf(x,y) 的值都是不連續(或離散)的。
數位影像可以看成一個連續影像經過取樣而成的大型陣列。這些點就是所謂的像素pixels),組成了數位影像。

2015年4月16日 星期四

sqlite lacks some libraries.

# which sqlite3
/bin/sqlite3

# sqlite3
sqlite3: error while loading shared libraries: libreadline.so.5: cannot open shared object file: No such file or directory

Get package "readline-5.2.tar.gz" from gnu, cross compile , install libreadline.so.5.2 on /lib

# cd /lib
# ln -s libreadline.so.5.2 libreadline.so.5
# ln -s libreadline.so.5.2 libreadline.so

# sqlite3
sqlite3: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

Get package "ncurses-5.9.tar.gz" from gun, cross compile , install libtinfo.so.5.9, libncurses.so.5.9 on /lib

# ln -s libncurses.so.5.9 libncurses.so.5
# ln -s libncurses.so.5.9 libncurses.so

# sqlite3
SQLite version 3.8.5 2014-06-04 14:06:34
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .quit



2015年4月9日 星期四

socket programming

Connection-oriented socket (TCP)
connection-oriented




Connectionless socket (UDP)
connectionless

The way to get local IP address in Linux

Kernel Routing tables

open "/proc/net/route" then parse content

$ cat /proc/net/routeIface   Destination     Gateway         Flags   RefCnt  Use     Metric  Mask            MTU     Window  IRTTeth1    0000A8C0        00000000        0001    0       0       0       00FFFFFF        0       0       0eth0    005111AC        00000000        0001    0       0       1       00FFFFFF        0       0       0eth1    0000FEA9        00000000        0001    0       0       1000    0000FFFF        0       0       0eth0    00000000        FE5111AC        0003    0       0       0       00000000        0       0       0


00000000   is default interface

Then get this interface ip address.

Using getsockname with an external connection


new connection outside then use this socket value to check own ip address.

/*
 * Find local ip used as source ip in ip packets.
 * Use getsockname and a udp connection
 */
 
#include<stdio.h> //printf
#include<string.h>    //memset
#include<errno.h> //errno
#include<sys/socket.h>    //socket
#include<netinet/in.h> //sockaddr_in
#include<arpa/inet.h> //getsockname
#include<unistd.h>    //close
 
int main ( int argc , char *argv[] )
{
    const char* google_dns_server = "8.8.8.8";
    int dns_port = 53;
     
    struct sockaddr_in serv;
     
    int sock = socket ( AF_INET, SOCK_DGRAM, 0);
     
    //Socket could not be created
    if(sock < 0)
    {
        perror("Socket error");
    }
     
    memset( &serv, 0, sizeof(serv) );
    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = inet_addr( google_dns_server );
    serv.sin_port = htons( dns_port );
 
    int err = connect( sock , (const struct sockaddr*) &serv , sizeof(serv) );
     
    struct sockaddr_in name;
    socklen_t namelen = sizeof(name);
    err = getsockname(sock, (struct sockaddr*) &name, &namelen);
         
    char buffer[100];
    const char* p = inet_ntop(AF_INET, &name.sin_addr, buffer, 100);
         
    if(p != NULL)
    {
        printf("Local ip is : %s \n" , buffer);
    }
    else
    {
        //Some error
        printf ("Error number : %d . Error message : %s \n" , errno , strerror(errno));
    }
 
    close(sock);
     
    return 0;
}