分享
 
 
 

UTL_INADDR包获取ip地址的内部原理

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

今天有朋友在MSN上问我如何获得已经连接用户的IP地址。

我们知道,通过SYS_CONTEXT函数可以获得这部分信息,当前用户的ip等信息可以通过如下命令轻易获取:

SQL> select sys_context('userenv','host') from dual;

SYS_CONTEXT('USERENV','HOST')

--------------------------------------------------------------------------------

WORKGROUP\GQGAI

SQL> select sys_context('userenv','ip_address') from dual;

SYS_CONTEXT('USERENV','IP_ADDR

--------------------------------------------------------------------------------

172.16.34.20

可是假如我们希望获取其它session的地址信息等,通过SYS_CONTEXT函数就只能通过LOGON触发器来完成。

而假如没有触发器记录,则我们可以通过UTL_INADDR Package来实现。

我们看一下UTL_INADDR包获取ip等信息的实现原理。

我们在数据库中进行如下查询:

[Oracle@jumper oracle]$ sqlplus "/ as sysdba"

SQL*Plus: Release 9.2.0.4.0 - ProdUCtion on Wed Oct 25 11:24:22 2006

Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.

Connected to:

Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production

With the Partitioning option

JServer Release 9.2.0.4.0 - Production

SQL> !

[oracle@jumper oracle]$ ps -efgrep sql

oracle 14700 14663 1 11:24 pts/0 00:00:00 sqlplus

oracle 14732 14702 0 11:24 pts/0 00:00:00 grep sql

[oracle@jumper oracle]$ ps -efgrep LO

oracle 14701 14700 0 11:24 ? 00:00:00 oracleeygle (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))

oracle 14734 14702 0 11:24 pts/0 00:00:00 grep LO

[oracle@jumper oracle]$ exit

exit

SQL> SELECT UTL_INADDR.get_host_address('www.anysql.net') from dual;

UTL_INADDR.GET_HOST_ADDRESS('WWW.ANYSQL.NET')

---------------------------------------------------------------------

208.113.151.109

在Linux中我们通过strace跟踪这个进程,可以得到以下堆栈信息:

[oracle@jumper oracle]$ strace -p 14701

Process 14701 attached - interrupt to quit

read(7, "\0\313\0\0\6\0\0\0\0\0\3^\10a\200\0\0\0\0\0\0@\342\22\10"..., 2064) = 203

gettimeofday({1161746697, 269895}, NULL) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 30000}, ru_stime={0, 10000}, ...}) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0

gettimeofday({1161746697, 270542}, NULL) = 0

gettimeofday({1161746697, 270670}, NULL) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0

gettimeofday({1161746697, 271614}, NULL) = 0

gettimeofday({1161746697, 271748}, NULL) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0

gettimeofday({1161746697, 272347}, NULL) = 0

gettimeofday({1161746697, 272699}, NULL) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0

gettimeofday({1161746697, 272989}, NULL) = 0

gettimeofday({1161746697, 273140}, NULL) = 0

gettimeofday({1161746697, 273273}, NULL) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 10000}, ...}) = 0

gettimeofday({1161746697, 273771}, NULL) = 0

gettimeofday({1161746697, 275526}, NULL) = 0

getpid() = 14701

open("/etc/resolv.conf", O_RDONLY) = 12

fstat64(12, {st_mode=S_IFREG0644, st_size=46, ...}) = 0

mmap2(NULL, 4096, PROT_READPROT_WRITE, MAP_PRIVATEMAP_ANONYMOUS, -1, 0) = 0xb6fba000

read(12, "search hurray.com.cn\nnameserver "..., 4096) = 46

read(12, "", 4096) = 0

close(12) = 0

munmap(0xb6fba000, 4096) = 0

socket(PF_UNIX, SOCK_STREAM, 0) = 12

connect(12, {sa_family=AF_UNIX, path="/var/run/.nscd_socket"}, 110) = -1 ENOENT (No such file or Directory)

close(12) = 0

open("/etc/host.conf", O_RDONLY) = 12

fstat64(12, {st_mode=S_IFREG0644, st_size=17, ...}) = 0

mmap2(NULL, 4096, PROT_READPROT_WRITE, MAP_PRIVATEMAP_ANONYMOUS, -1, 0) = 0xb6fba000

read(12, "order hosts,bind\n", 4096) = 17

read(12, "", 4096) = 0

close(12) = 0

munmap(0xb6fba000, 4096) = 0

futex(0xb71a1a20, FUTEX_WAKE, 2147483647) = 0

open("/etc/hosts", O_RDONLY) = 12

fcntl64(12, F_GETFD) = 0

fcntl64(12, F_SETFD, FD_CLOEXEC) = 0

fstat64(12, {st_mode=S_IFREG0644, st_size=175, ...}) = 0

mmap2(NULL, 4096, PROT_READPROT_WRITE, MAP_PRIVATEMAP_ANONYMOUS, -1, 0) = 0xb6fba000

read(12, "# Do not remove the following li"..., 4096) = 175

read(12, "", 4096) = 0

close(12) = 0

munmap(0xb6fba000, 4096) = 0

open("/opt/oracle/product/9.2.0/lib/libnss_dns.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)

open("/lib/tls/libnss_dns.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)

open("/lib/i686/libnss_dns.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)

open("/lib/libnss_dns.so.2", O_RDONLY) = 12

read(12, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\240\16"..., 512) = 512

fstat64(12, {st_mode=S_IFREG0755, st_size=18632, ...}) = 0

old_mmap(NULL, 17100, PROT_READPROT_EXEC, MAP_PRIVATE, 12, 0) = 0xb6fb6000

old_mmap(0xb6fba000, 4096, PROT_READPROT_WRITE, MAP_PRIVATEMAP_FIXED, 12, 0x3000) = 0xb6fba000

close(12) = 0

open("/opt/oracle/product/9.2.0/lib/libresolv.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)

open("/lib/tls/libresolv.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)

open("/lib/i686/libresolv.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)

open("/lib/libresolv.so.2", O_RDONLY) = 12

read(12, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\320(\0"..., 512) = 512

fstat64(12, {st_mode=S_IFREG0755, st_size=76508, ...}) = 0

old_mmap(NULL, 73604, PROT_READPROT_EXEC, MAP_PRIVATE, 12, 0) = 0xb6fa4000

old_mmap(0xb6fb3000, 4096, PROT_READPROT_WRITE, MAP_PRIVATEMAP_FIXED, 12, 0xf000) = 0xb6fb3000

old_mmap(0xb6fb4000, 8068, PROT_READPROT_WRITE, MAP_PRIVATEMAP_FIXEDMAP_ANONYMOUS, -1, 0) = 0xb6fb4000

close(12) = 0

socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 12

connect(12, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("208.113.151.109")}, 28) = 0

send(12, "\324#\1\0\0\1\0\0\0\0\0\0\3www\5anysql\3com\0\0\1\0\1", 31, 0) = 31

gettimeofday({1161746697, 286025}, NULL) = 0

poll([{fd=12, events=POLLIN, revents=POLLIN}], 1, 5000) = 1

ioctl(12, FIONREAD, [74]) = 0

recvfrom(12, "\324#\201\200\0\1\0\1\0\1\0\0\3www\5anysql\3com\0\0\1\0"..., 1024, 0,

{sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("208.113.151.109")}, [16]) = 74

close(12) = 0

gettimeofday({1161746697, 290245}, NULL) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 20000}, ...}) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 20000}, ...}) = 0

gettimeofday({1161746697, 291553}, NULL) = 0

write(10, "\2\275\0\0\6\0\0\0\0\0\20\31\266\344\217\3700\320\341S"..., 701) = 701

read(7, "\0\215\0\0\6\0\0\0\0\0\3^\t@\0\0\0\1\0\0\0\0\0\0\0\0\0"..., 2064) = 141

gettimeofday({1161746697, 294898}, NULL) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 20000}, ...}) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 20000}, ...}) = 0

gettimeofday({1161746697, 295496}, NULL) = 0

getrusage(RUSAGE_SELF, {ru_utime={0, 40000}, ru_stime={0, 20000}, ...}) = 0

gettimeofday({1161746697, 295847}, NULL) = 0

gettimeofday({1161746697, 295981}, NULL) = 0

lseek(9, 1024, SEEK_SET) = 1024

read(9, "\30\0$\0007\0@\0J\0V\0`\0i\0t\0~\0\232\0\245\0\320\0\330"..., 512) = 512

lseek(9, 47104, SEEK_SET) = 47104

read(9, "\f\0^\5\0\0P\0x\5\0\0\214\0y\5\0\0\250\0z\5\0\0\313\0{"..., 512) = 512

gettimeofday({1161746697, 297024}, NULL) = 0

write(10, "\0\202\0\0\6\0\0\0\0\0\4\1\0\0\0\1\1\0\0\0{\5\0\0\0\0\1"..., 130) = 130

read(7,

Process 14701 detached

在这个信息中,我们注重到Oracle顺序访问了如下文件来完成地址定位:

open("/etc/resolv.conf", O_RDONLY) = 12

open("/etc/host.conf", O_RDONLY) = 12

open("/etc/hosts", O_RDONLY) = 12

首先获取域名解析服务器,在根据host.conf文件确定解析顺序,因为缺省hosts文件优先,又继续读取/etc/hosts文件。

假如hosts文件存在解析关系,则返回信息;假如不存在,则继续问询DNS服务器,获得解析地址,假如不能解析,则会出错:

SQL> select UTL_INADDR.get_host_address('www.a.com') from dual;

select UTL_INADDR.get_host_address('www.a.com') from dual

*

ERROR at line 1:

ORA-29257: host www.a.com unknown

ORA-06512: at "SYS.UTL_INADDR", line 35

ORA-06512: at "SYS.UTL_INADDR", line 40

ORA-06512: at line 1

也就是说,UTL_INADDR的数据获取已经不依靠于数据库信息了,而SYS_CONTEXT的信息获取仍然来自数据库内部。

这就是UTL_INADDR包的工作原理。

-The End-

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有