2013-06-03 51 views
0

我想在TCI中使用原始套接字实现,因为它在Python和C中使用,这可能吗? TCL套接字库是否支持原始套接字?TCL中的原始套接字

Python原始套接字实例:

#create a raw socket 
try: 
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) 
except socket.error , msg: 
    print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] 
    sys.exit() 

# tell kernel not to put in headers, since we are providing it, when using IPPROTO_RAW this is not necessary 
# s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) 

# now start constructing the packet 
packet = ''; 

source_ip = '192.168.1.101' 
dest_ip = '127.0.0.1' # or socket.gethostbyname('www.google.com') 

# ip header fields 
ip_ihl = 5 
ip_ver = 4 
ip_tos = 0 
ip_tot_len = 0 # kernel will fill the correct total length 
ip_id = 54321 #Id of this packet 
ip_frag_off = 0 
ip_ttl = 255 
ip_proto = socket.IPPROTO_TCP 
ip_check = 0 # kernel will fill the correct checksum 
ip_saddr = socket.inet_aton (source_ip) #Spoof the source ip address if you want to 
ip_daddr = socket.inet_aton (dest_ip) 

ip_ihl_ver = (ip_ver << 4) + ip_ihl 

# the ! in the pack format string means network order 
ip_header = pack('!BBHHHBBH4s4s' , ip_ihl_ver, ip_tos, ip_tot_len, ip_id, ip_frag_off, ip_ttl, ip_proto, ip_check, ip_saddr, ip_daddr) 

# tcp header fields 
tcp_source = 1234 # source port 
tcp_dest = 80 # destination port 
tcp_seq = 454 
tcp_ack_seq = 0 
tcp_doff = 5 #4 bit field, size of tcp header, 5 * 4 = 20 bytes 
#tcp flags 
tcp_fin = 0 
tcp_syn = 1 
tcp_rst = 0 
tcp_psh = 0 
tcp_ack = 0 
tcp_urg = 0 
tcp_window = socket.htons (5840) # maximum allowed window size 
tcp_check = 0 
tcp_urg_ptr = 0 

tcp_offset_res = (tcp_doff << 4) + 0 
tcp_flags = tcp_fin + (tcp_syn << 1) + (tcp_rst << 2) + (tcp_psh <<3) + (tcp_ack << 4) + (tcp_urg << 5) 

# the ! in the pack format string means network order 
tcp_header = pack('!HHLLBBHHH' , tcp_source, tcp_dest, tcp_seq, tcp_ack_seq, tcp_offset_res, tcp_flags, tcp_window, tcp_check, tcp_urg_ptr) 

user_data = 'Hello, how are you' 

我切出一些地方,但你的想法。这可能吗?

+2

请参阅http://wiki.tcl.tk/16733和http://community.activestate.com/forum/newbie-tcl-how-program-using-sockets。 – user1929959

+0

非常感谢! – Blackdragon1400

+0

Tcl没有内置的支持任何类型的面向数据报的套接字,这对于做RAW套接字是必要的。 (另外,RAW套接字通常仅限于Unix上的“受信任”进程;它们非常可滥用,唉。)在[TIP#409]之前不可能改变(http://www.tcl.tk/cgi-bin/ tct/tip/409.html),但[Scotty](http://wiki.tcl.tk/691)可能是相关的。不确定... –

回答