2014/08/15

簡單 Struct 清單的使用

這是一個簡單 Struct 的使用,資料餵入的方法只用單純的插入,暫時也還沒有遇到需求,所以就不寫搜尋跟刪除的功能了。

/**********
 * Define *
 * ********/

#define SLIST_ENTRY(s_type) struct s_type *next;

#define SLIST_IS_EMPTY(head) (head == NULL)

#define SLIST_NOT_EMPTY(head) (head != NULL)

#define SLIST_FOREACH(var, c_type, head) \
for (var=head; var!=NULL; var=var->next)

#define SLIST_INSERT(var, c_type, head) \
{
c_type *tail = head; \
var->next = NULL; \
if (NULL == tail) \
head = var; \
else \
{ \
while (tail && tail->next) \
tail = tail->next; \
if (tail) \
tail->next = var; \
} \
}

#define SLIST_FREE(c_type, head) \
if (head) \
{ \
c_type *this=head, *next; \
while (NULL != this) \
{ \
next = this->next; \
free(this); \
this = next; \
}; \
head = NULL; \
}

/***********
 * Example *
 * *********/

// Define Struct
typedef struct S_MyType
{
char FirstName[30];
char LastName[30];
SLIST_ENTRY(S_MyType);
} C_MyType;

// Define variable
C_MyType *Users;

// Insert Element
C_MyType *ptr = (C_MyType *)malloc(sizeof(C_MyType));
if (ptr)
{
ENSURE_ZERO(ptr, sizeof(C_MyType));
sprintf(ptr->FirstName, "Aim");
sprintf(ptr->LastName, "Wang");
SLIST_INSERT(ptr, C_MyType, Users);
}

// Read all data
C_MyType *ptr;
int i = 0;

SLIST_FOREACH(ptr, C_MyType, Users)
{
printf("User No.%2d: %s %s", i++, ptr->FirstName, ptr->LastName);
}

// release memory
SLIST_FREE(C_MyType, Users);

2014/08/12

find_pid_by_cmdline

要寫一個送 kill 到指定程式的 code,所以先要弄出這個根據 command line 找到 pid 的 function
#include <dirent.h>

static int find_pid_by_cmdline(const char *cmdline)
{
DIR *dir;
struct dirent *entry;
char *name, *p;
char status[32];
char buf[1024];
FILE *fp;
int pid, i;

dir = opendir("/proc");
if (!dir)
return 0;

while (entry = readdir(dir)) {
name = entry->d_name;
if (!(*name >= '0' && *name <= '9'))
continue;

pid = atoi(name);
sprintf(status, "/proc/%d/cmdline", pid);
if ((fp = fopen(status, "r")) == NULL)
continue;

name = fgets(buf, sizeof(buf), fp);
fclose(fp);
if (name == NULL)
continue;

for (i=0; i<1024; i++) {
if (buf[i] == 0) {
if (buf[i+1] == 0) {
i = 1024;
} else {
buf[i] = ' ';
}
}
}

if (!strcmp(buf, cmdline)) {
closedir(dir);
return pid;
}
}
closedir(dir);

return 0;
}

2014/07/17

Debian 7 安裝 Asterisk

說來慚愧,在自稱台灣第一大的 VoIP Gateway 製造商待了兩年多的時間,剛開始一年多整天玩網路,最近幾個月被螃蟹夾到手,光是搞定他家的 VLAN  就頭昏腦脹了,一直都沒有機會去真正瞭解 VoIP 這東西。今天抽個時間先把 Asterisk 裝起來,再來慢慢玩好了。

以下是安裝過程的筆記,只安裝主系統跟弄出 WEB 介面而已,其他東西以後有空再說

1. 安裝 Debian 7 amd64 (依照習慣不安裝視窗介面,節省記憶體跟硬碟空間)
套件只勾選 shell system 跟 ssh server

2. 更新系統
#apt-get update
#apt-get upgrade

3. 安裝 Asterisk
#apt-get install asterisk
(會有一堆相依套件被安裝,能拒絕嗎?當然不)
ITU-T telephone code: 886 (參考 http://en.wikipedia.org/wiki/List_of_country_calling_codes)

4. 安裝 Asterisk GUI  (因為 Debian 套件沒有內建 GUI,所以要下載原始碼自己 Make,原始碼採用 svn 方式下載)
#apt-get install subversion
#cd /usr/src
#svn co http://svn.digium.com/svn/asterisk-gui/trunk asterisk-gui
#cd asterisk-gui
#./configure
#make
#make install

*** GUI 會被安裝在 /var/lib/asterisk/static-http,而 asterisk 實際會用 /usr/share/asterisk/static-http

#rm -rf /usr/share/asterisk/static-http/
#ln -s /var/lib/asterisk/static-http/ /usr/share/asterisk/
#cd /var/lib/asterisk
#chown -R asterisk.asterisk static-http
#chown -R asterisk.asterisk scripts
#chown -R asterisk.asterisk gui_backups

修改 /etc/asterisk/http.conf
[general]
enabled=yes
bindaddr=your_ip
bindport=8088
prefix=asterisk
enablestatic=yes
redirect = / /asterisk/static/config/index.html

修改 /etc/asterisk/manager.conf
[general]
webenabled = yes
[admin]
secret = your_password
read = system,call,log,verbose,command,agent,config
write = system,call,log,verbose,command,agent,config

#/etc/init.d/asterisk restart

接著就可以用瀏覽器瀏覽 http://your_ip:8088/ 進入管理介面

2014/04/03

[linux shell script] findgrep

[ "2" != "$#" ] && {
        echo "syntax:"
        echo "  $0 "'"filename" "greptext"'
        echo '          filename: like *.c or *.cpp'
        echo '          greptext: text you want to search.'
        echo "example:"
        echo "  $0 "'"*.c" "TODO:"'
} || {
        echo '[exec] find . -name "'$1'" | xargs grep "'$2'"'
        find . -name "$1" | xargs grep "$2"
}

2014/03/13

Freeradius 設定作 eap 認證

eap 認證

eap 認證有兩點要注意
  1. freeradius 必須有伺服器憑證
  2. 被認證端的根憑證必須在 trusted ca

信任的 ca 設定

/etc/raddb/eap.conf
eap{
    tls {
        CA_file = ${cadir}/ca.pem
    }
}

測試

安裝 eapol_test 需用 root 權限
wget http://hostap.epitest.fi/releases/wpa_supplicant-0.5.10.tar.gz
tar xvf wpa_supplicant-0.5.10.tar.gz
cd wpa_supplicant-0.5.10/
cp defconfig .config
make eapol_test
cp eaplo_test /usr/bin
測試用 config 檔 /tmp/eapol_test.conf.peap
network={
  eap=PEAP
  eapol_flags=0
  key_mgmt=IEEE8021X
  identity="myid"
  password="mypw"
  ca_cert="/etc/raddb/certs/ca.pem"
  phase2="auth=MSCHAPV2"
  anonymous_identity="anonymous"
}
執行測試
eapol_test -c /tmp/eapol_test.conf.peap -a127.0.0.1 -p1812 -stesting123 -r1
傳回結果,看到 SUCCESS 表示認證連結成功
... ... ... ... 省略 ... ... ... ...
EAP: deinitialize previously used EAP method (25, PEAP) at EAP deinit
ENGINE: engine deinit
MPPE keys OK: 1  mismatch: 0
SUCCESS
標籤: (Edit tags)