存档
DNS报文格式
| Query Identifier(16) | QR | OPCodes | Flags | Reserved | RCodes |
| QDCount(16) | ANCount(16) | ||||
| NSCount(16) | ARCount(16) | ||||
| Question Section(32) | |||||
| Answer Section(32) | |||||
| Authority Section(32) | |||||
| Additional Records Section(32) | |||||
以下为各栏位的简要说明:
QID
DNS 查询封包编号,作为确认依据。
QR
查询封包为 0 ﹔回应为 1 。长度为 1 byte 。
OPCodes
封包类别(QUERY, IQUERY, STATUS, Reserved)。长度为 4 bytes。
Flags
共 4 bytes ,各表示:AA(Authoritative Answer)、TC(Truncation)、RD(Recursion Desired)、RA(Recursion Avalable)。
Reserved
保留未用。
RCodes
回应讯息,长 4 bytes ,除 0 及 6-15 保留未用外,1-5 分别为:Format Error、Server Failure、Name Error、Not Implemented、Refused。
Question Section、Answer Section、Authority Section、Additional Records Section
每一 Section 分为 NAME、TYPE、CLASS 三个子栏位,分别作为查询、应答、授权、额外记录等封包之资讯,及各自长度。
好了﹐关于 DNS 协定的讨论﹐暂时介绍到这里﹐相信上面说的这些也够您消化的了。
DNS 协定之 RFC 文件
RFC-822﹑RFC-883﹑RFC-920﹑RFC-973﹑RFC-974﹑RFC-1032﹑RFC-1033﹑RFC-1034﹑RFC-1035﹑RFC-1101﹑RFC-1296
libevent的一些知识
1、什么动作会触发EV_READ, 什么动作触发EV_WRITE?
2、为什么connection_accept()的eventflag有EV_PERSIST,而connection_time的没有?
通过查资料,终于搞明白了!
在《UNIX网络编程第1卷(第2版)》第6章 “I/O 复用:select和poll函数”(129页),有解释:
===============
描述字符在什么条件下准备好?
1、下列的4个条件中的任何一个满足时,socket准备好读(即有EV_READ):
a)套接口接收缓冲区中的数据字节大于等于套接口接收缓冲区低潮限度的当前值(可以设定,默认为1)
b)连接的读这一半关闭(即收到了FIN的TCP连接)。(也就是读到结尾了)
c)套接口是一个接听套接口且已完成的连接数为非0。
d)有一个套接口错误待处理。
2、下列的3个条件中的任何一个满足时,socket准备好写(即有EV_WRITE):
a)套接口发送缓冲区中的数据字节大于等于套接口发送缓冲区低潮限度的当前值(可以设定,默认为2048),且:
aa)套接口已连接 或 bb)套接口不需要连接(如UDP)
b)连接的写这一半关闭。
c)有一个套接口错误待处理。
===============
根 据刚才的time server中的例程,当client有socket连上来,应该是EV_READ,因为满足1中的c“套接口是一个接听套接口且已完成的连接数为非 0”,当该连接被acept以后,就有EV_WRITE了,因为它满足2中的a“套接口发送缓冲区中的数据字节大于等于套接口发送缓冲区低潮限度的当前 值,且套接口已连接”。所以,event_set的时候,EV_READ是有人连上来,回调函数是connection_accept(),而 EV_WRITE是在accept以后。
那EV_PERSIST呢?
EV_PERSIST,是在event发生了以后,不从队列中拿开,就是下次再有这个消息的时候,继续调那个回调函数,知道程序主动调用了event_del后,才从队列中删除--就算有那个消息也不回调函数。
connection_accept函数,只要有连接上来,就要调用的,所以它需要设置为EV_PERSIST。
但 connection_time函数,用途是在client连上来以后,给client一个返回,然后就断开连接。那它必须在连接连上了以后,只做一次, 所以不需要设置为EV_PERSIST,而且必须在accept中设置--如果没有accept就算有EV_WRITE也不应该调用它啊。
正则表达示全集,包括验证汉字在内(转)
(“/^[\xa1-\xff]+$/”这是一个纯中文字符串
正则表达式用于字符串处理、表单验证等场合,实用高效。现将一些常用的表达式 收集于此,以备不时之需。
匹配中文字符的正则表达式: [\u4e00-\u9fa5]
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了
匹配双字节字符(包括汉字在内):[^\x00-\xff]
评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计 1)
匹配空白行的正则表达式:\n\s*\r
评注:可以用来删除空白行
匹配HTML标记的正则表达式:<(\S*?)[^>]*>.*?|<.*? />
评注:网上流传的版本太 糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力
匹配首尾空白字符的正则表达式:^\s*|\s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表 达式
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
评注:表单验 证时很实用
匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
评注: 表单验证时很实用
匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7}
评注:匹配形式如 0511-4405222 或 021-87888822
匹配腾讯QQ号:[1-9][0-9]{4,}
评注:腾讯QQ号从10000开始
匹配中国邮政编码:[1-9]\d{5}(?!\d)
评注:中国邮政编码为6位数字
匹配身份证:\d{15}|\d{18}
评注:中国的身份证为15位或18位
匹配ip地址:\d+\.\d+\.\d+\.\d+
评注:提取ip地址时有用
匹配特定数字:
^[1-9]\d*$ //匹配正整数
^-[1-9]\d*$ //匹配负整数
^-?[1-9]\d*$ //匹配整数
^[1-9]\d*|0$ //匹配非负整数(正整数 + 0)
^-[1-9]\d*|0$ //匹配非正整数(负整数 + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ //匹配正浮点数
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //匹配负浮点数
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ //匹配浮点数
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ //匹配非负浮点数(正浮点数 + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ //匹配非正浮点数(负浮点数 + 0)
评注:处理大量数据时有用,具体应用时注意修正
匹配特定字符串:
^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
^[A-Z]+$ //匹配由26个英文字母 的大写组成的字符串
^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$ //匹配由数字和 26个英文字母组成的字符串
^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
评注:最基本也是最常用的一些表达式
使用enca转换文件编码
安装enca:
# cd /usr/ports/converters/enca
# make install clean
对一个目录下的文件进行转换编码:
# find . -type f | grep -E -v “.svn|.gif|.jpg|.png” | xargs enca -L zh_CN -x UTF-8
php-excel,用PHP导出excel文件
php-excel是一个PHP导出excel文件的类库。导出的是XML文件,用office2003,openoffice3都可以正常打开。
官方网站:
简单的例子:
<?php
// load library
require ‘php-excel.class.php’;// create a simple 2-dimensional array
$data = array(
1 => array (‘Name’, ‘呵呵’),
array(‘Schwarz’, ‘Oliver’),
array(‘Test’, ‘Peter’)
);// generate file (constructor parameters are optional)
$xls = new Excel_XML(‘GB2312′, true, ‘My Test Sheet’);
$xls->addArray($data);
$xls->generateXML(‘my-test’);?>
使用rxvt-unicode
在新笔记本,迁移到UTF-8环境下,mrxvt不支持UTF-8,中文显示乱码。只有换成rxvt-unicode了。支持UTF-8,中文输入。
配置文件:
$ cat ~/.Xresources
Xft.dpi:96
! Color setting
!URxvt.geometry: 200×53
URxvt.cursorColor:green
URxvt.foreground:black
URxvt.background:lightyellow
! transparent setting
URxvt.inheritPixmap:false
URxvt.tintColor:lightyellow
URxvt.shading:-80
! normal setting
URxvt.termName:rxvt
URxvt.cursorBlink:true
URxvt.saveLines:65535
URxvt.scrollBar_right:true
URxvt.scrollTtyKeypress:true
URxvt.scrollWithBuffer:false
!URxvt.font:xft:serif:pixelsize=13:antialias=false
!URxvt.font: 9x15bold,\
! -misc-fixed-bold-r-normal–15-140-75-75-c-90-iso10646-1,\
! -misc-fixed-medium-r-normal–15-140-75-75-c-90-iso10646-1,\
! [codeset=cp936]xft:serif:antialias=false, \
! xft:simsun:antialias=falseURxvt.font: -*-fixed-medium-r-*–14-*-*-*-*-*-iso10646-1,\
xft:simsun:pixelsize=14:antialias=false! test modify ALT key value
!URxvt.insecure:true
!URxvt.modifier:alt
!URxvt.meta8:true
!XTerm*VT100*metaSendsEscape:true
urxvt*font:xft:SimSun:pixelsize=16
urxvt*imFont: -misc-simsun-medium-r-normal-0-0-0-0-p-0-iso10646-1
urxvt*inputMethod:SCIM
urxvt*preeditType: OverTheSpot
urxvt*multichar_encoding:noenc
shell脚本编译
今天发现一个好玩的东东,可以将shell脚本编译一下。
http://www.datsi.fi.upm.es/~frosal/sources/shc.html
文档:
Manpage for shc(1)
NAME
shc - Generic shell script compilerSYNOPSIS
shc [ -e date ] [ -m addr ] [ -i iopt ] [ -x cmnd ] [ -l lopt ] [ -ACDhTv ] -f scriptDESCRIPTION
shc creates a stripped binary executable version of the script specified with -f on the command line. The binary version will get a .x extension appended and will usually be a bit larger in size than the original ascii code. Generated C source code is saved in a file with the extension .x.c If you supply an expiration date with the -e option the com- piled binary will refuse to run after the date specified. The message "Please contact your provider" will be displayed instead. This message can be changed with the -m option. You can compile any kind of shell script, but you need to supply valid -i, -x and -l options. The compiled binary will still be dependent on the shell specified in the first line of the shell code (i.e. #!/bin/sh), thus shc does not create completely independent binaries. shc itself is not a compiler such as cc, it rather encodes and encrypts a shell script and generates C source code with the added expiration capability. It then uses the system compiler to compile a stripped binary which behaves exactly like the original script. Upon execution, the compiled binary will decrypt and execute the code with the shell -c option. Unfortunatelly, it will not give you any speed improvement as a real C program would. shc's main purpose is to protect your shell scripts from modification or inspection. You can use it if you wish to distribute your scripts but don't want them to be easily readable by other people.OPTIONS
The command line options are: -e date Expiration date in dd/mm/yyyy format [none] -m message message to display upon expiration ["Please contact your provider"] -f script_name File name of the script to compile -i inline_option Inline option for the shell interpreter i.e: -e -x comand eXec command, as a printf format i.e: exec(\\'%s\\',@ARGV); -l last_option Last shell option i.e: -- -r Relax security. Make a redistributable binary which executes on different systems running the same operat- ing system. -v Verbose compilation -D Switch on debug exec calls -T Allow binary to be traceable (using strace, ptrace, truss, etc.) -C Display license and exit -A Display abstract and exit -h Display help and exitENVIRONMENT VARIABLES
CC C compiler command [cc] CFLAGS C compiler flags [none]EXAMPLES
Compile a script which can be run on other systems with the trace option enabled: example% shc -v -r -T -f myscriptBUGS
The maximum size of the script that could be executed once com� piled is limited by the operating system configuration parameter _SC_ARG_MAX (see sysconf(2))AUTHOR
Francisco Rosales <frosal@fi.upm.es>REPORT BUGS TO
the author.
一颗璀璨的月光宝石–Lua(转)
1993年在巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro in Brazil)诞生了一门编程语言,发明者是该校的三位研究人员,他们给这门语言取了个浪漫的名字——Lua,在葡萄牙语里代表美丽的月亮。事实证明她没 有糟蹋这个优美的单词,Lua语言正如它名字所预示的那样成长为一门简洁、优雅且富有乐趣的语言。
Lua从一开始就是作为一门方便嵌入(其它应用程序)并可扩展的轻量级脚本语言来设计的,因此她一直遵从着简单、小巧、可移植、快速的原则,官 方实现完全采用ANSI C编写,能以C程序库的形式嵌入到宿主程序中。Lua的每个版本都保持着开放源码的传统,不过各版采用的许可协议并不相同,自5.0版(最新版是5.1) 开始她采用的是著名的MIT许可协议。正由于上述特点,所以Lua在游戏开发、机器人控制、分布式应用、图像处理、生物信息学等各种各样的领域中得到了越 来越广泛的应用。其中尤以游戏开发为最,许多著名的游戏,比如Escape from Monkey Island、World of Warcraft、大话西游,都采用了Lua来配合引擎完成数据描述、配置管理和逻辑控制等任务。
作为一门过程型动态语言,Lua有着如下的特性:
1、变量名没有类型,值才有类型,变量名在运行时可与任何类型的值绑定;
2、语言只提供唯一一种数据结构,称为表(table),它类似key-value关联数组,可以用任何类型的值作为key和value。提供 了一致且富有表达力的表构造语法,使得Lua很适合描述复杂的数据;
3、函数是一等类型,支持匿名函数和正则尾递归(proper tail recursion);
4、支持词法定界(lexical scoping)和闭包(closure);
5、提供thread类型和结构化的协程(coroutine)机制,在此基础上可方便实现协作式多任务;
6、运行期能编译字符串形式的程序文本并载入虚拟机执行;
7、通过元表(metatable)和元方法(metamethod)提供动态元机制(dynamic meta-mechanism),从而允许程序运行时根据需要改变或扩充语法设施的内定语义;
8、能方便地利用表和动态元机制实现基于原型(prototype-based)的面向对象模型;
9、从5.1版开始提供了完善的模块机制,从而更好地支持开发大型的应用程序;
Lua的语法类似PASCAL和Modula但更加简洁,所有的语法产生式规则(EBNF)不过才60几个。熟悉C和PASCAL的程序员一般 只需半个小时便可将其完全掌握。而在语义上Lua则与Scheme极为相似,她们完全共享上述的1、3、4、6点特性, Scheme的continuation与协程也基本相同只是自由度更高。最引人注目的是,两种语言都只提供唯一一种数据结构:Lua的表和Scheme 的列表(list)。正因为如此,有人甚至称Lua为“只用表的Scheme”。 阅读全文…






最新评论