def standard_arp_request(dst_ipaddr): try: sock = rawsocket.socket(EthernetHeader.TYPE_ARP) except IOError: print "Login as root, or you won't be able to make a raw socket" raise IOError a = ArpPacket() eth = a.previous eth.dst_mac = 0xFFFFFFFFFFFFL eth.src_mac = my_macaddr eth.packet_type = EthernetHeader.TYPE_ARP a.arp_hw = ArpPacket.HW_ETHER a.ethernet_type = EthernetHeader.TYPE_IP a.arp_opcode = ArpPacket.OPCODE_REQUEST a.src_hw_addr = my_macaddr a.src_prot_addr = my_ipaddr a.dst_hw_addr = 0xFFFFFFFFFFFFL a.dst_prot_addr = dst_ipaddr rawsocket.sendto(sock, a.bytes()) x = rawsocket.recvfrom(sock) x = a.extract(x) arptable[dst_ipaddr] = a.src_hw_addr
def test_udp(byte): if not arptable.has_key(bd_ipaddr): standard_arp_request(bd_ipaddr) try: sock = rawsocket.socket(EthernetHeader.TYPE_IP) except IOError: print "Login as root, or you won't be able to make a raw socket" raise IOError cmd = "\xff" + ((byte and "\x01") or "\x00") u = UdpPacket(my_ipaddr, 43700, bd_ipaddr, 5000, cmd) print "IP COMMAND TO BOARD" u.dump() rawsocket.sendto(sock, u.bytes()) x = rawsocket.recvfrom(sock) oldtype = u.previous.previous.packet_type x = u.extract(x) assert u.previous.previous.packet_type == oldtype print "\nIP RESPONSE FROM BOARD" u.dump() if x: print len(x), "bytes left over"
def test_arp(): try: sock = rawsocket.socket(EthernetHeader.TYPE_ARP) except IOError: print "Login as root, or you won't be able to make a raw socket" raise IOError a = ArpPacket() eth = a.previous eth.dst_mac = bd_macaddr eth.src_mac = my_macaddr eth.packet_type = EthernetHeader.TYPE_ARP a.arp_hw = ArpPacket.HW_ETHER a.ethernet_type = EthernetHeader.TYPE_IP a.arp_opcode = ArpPacket.OPCODE_REQUEST a.src_hw_addr = my_macaddr a.src_prot_addr = my_ipaddr a.dst_hw_addr = bd_macaddr a.dst_prot_addr = bd_ipaddr print "ARP REQUEST" a.dump() # Send ARP request, receive ARP reply rawsocket.sendto(sock, a.bytes()) x = rawsocket.recvfrom(sock) print "\nARP REPLY" x = a.extract(x) a.dump() if x: print len(x), "bytes left over"
try: import rawsocket as socket except: import socket CONTENT = """\ HTTP/1.0 200 OK Hello #{} from MicroPython! """ s = socket.socket() ai = socket.getaddrinfo("127.0.0.1", 8080) print("Bind address info:", ai) addr = ai[0][4] s.bind(addr) s.listen(5) print("Listening, connect your browser to http://127.0.0.1:8080/") counter = 0 while True: res = s.accept() client_s = res[0] client_addr = res[1] print("Client address:", client_addr) print("Client socket:", client_s) print("Request:") if 0:
from ops import ltb import rawsocket import data from layer2 import * from layer3 import * from layer4 import * import tcp from packet import Packet MAC = 0xf8b156fe741e IP = ltb([192, 168, 1, 226]) ROUTER = ltb([192, 168, 1, 1]) ROUTER_MAC = 0x58ef6870d132 socket = rawsocket.socket('Realtek') eth = EthernetFrame(ROUTER_MAC, MAC, data.EtherType.ipv4) ip = IPv4( data.IGNORE, 5, 0b000000, 0b00, 30, # packet len in bytes 0, 0b010, # fragmentation might be cool 0, # frag offset 255, # ttl data.IPPROTO.udp, data.IGNORE, IP, ROUTER)
try: import rawsocket as _socket except: import _socket s = _socket.socket() if 1: ai = _socket.getaddrinfo("google.com", 80) print("Address infos:", ai) addr = ai[0][4] else: # Deprecated ways to construct connection address addr = _socket.sockaddr_in() addr.sin_family = 2 #addr.sin_addr = (0x0100 << 16) + 0x007f #addr.sin_addr = (0x7f00 << 16) + 0x0001 #addr.sin_addr = _socket.inet_aton("127.0.0.1") addr.sin_addr = _socket.gethostbyname("google.com") addr.sin_port = _socket.htons(80) print("Connect address:", addr) s.connect(addr) if 0: # MicroPython rawsocket module supports file interface directly s.write("GET / HTTP/1.0\n\n") print(s.readall()) else: s.send(b"GET / HTTP/1.0\n\n")
def __init__(self, device): self.socket = socket(device) self.wbuffer = [] self.rbuffer = []