Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets Lua原始套接字示例_Sockets_Network Programming_Lua - Fatal编程技术网

Sockets Lua原始套接字示例

Sockets Lua原始套接字示例,sockets,network-programming,lua,Sockets,Network Programming,Lua,我很难在Lua中找到一些原始套接字的示例代码。理想情况下,它们应该与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]) + ' Mes

我很难在Lua中找到一些原始套接字的示例代码。理想情况下,它们应该与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'
#创建原始套接字
尝试:
s=socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_RAW)
除socket.error外,消息:
无法创建“打印”套接字。错误代码:'+str(消息[0])+'消息'+msg[1]
sys.exit()
#告诉内核不要放入头文件,因为我们提供了头文件,当使用IPPROTO_RAW时,这是不必要的
#s.setsockopt(插座IP协议,插座IP协议,1)
#现在开始构建数据包
数据包=“”;
source_ip='192.168.1.101'
dest_ip='127.0.0.1'或socket.gethostbyname('www.google.com'))
#ip头字段
国际人道主义法=5
ip_ver=4
ip_tos=0
ip_tot_len=0#内核将填充正确的总长度
ip_id=此数据包的54321#id
ip_frag_off=0
ip_ttl=255
ip_proto=socket.IPPROTO_TCP
ip_check=0#内核将填充正确的校验和
ip_saddr=socket.inet_aton(source_ip)#如果您想欺骗源ip地址
ip_daddr=socket.inet_aton(dest_ip)

国际人道主义法版本=(ip\u verLuaSocket不是为这个而设计的。也许看看和?

你到底想做什么?你可以检查一下,看看这是否满足你的需要。LuaSocket在解析或创建头方面没有太多功能。我需要能够创建我自己的ip/TCP/UDP头来进行测试。我希望有人知道是什么原因e为Lua的原始套接字找到一个库,或者是否有一些方法可以使用现有的Lua套接字库来实现。关于Luasocket的文档非常简单,没有太多细节。