def test_attach_kwargs(self):
     """Ensure attach takes kwargs as advertised"""
     mem = sysv_ipc.SharedMemory(None, sysv_ipc.IPC_CREX)
     mem.write('hello world')
     mem.detach()
     mem2 = sysv_ipc.attach(mem.id, flags=0)
     mem2.detach()
     mem.remove()
def main():
	

	
	raw_sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003)) # 0x0003 = ETH_P_ALL
	gtp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	
	shm_addr = sysv_ipc.SharedMemory(9487, sysv_ipc.IPC_CREAT , 0666 , 4096)
	TEID_IP_mapping = sysv_ipc.attach(shm_addr.id)

	eth_length = 14
	ip_length = 20
	seq = 0
	header_length = eth_length + ip_length
	cnt = 0
	#print 'start'	
	while True :
		packet = raw_sock.recvfrom(8192)[0]
		#print "start time: ", datetime.now().microsecond/1000.0
		header = struct.unpack("!6s6s2sBBHHHBBH4s4s", packet[:header_length])
		#if header[0][0:6] == b'\x9c\x5c\x8e\xbb\x7d\x06':   # link layer MAC address
		if 1:
			src_ip = socket.inet_ntoa(header[11])
			des_ip = socket.inet_ntoa(header[12])
			if(des_ip[0:7] == "192.188"):                   # UE subnet prefix
				tunnel = find_TEID(TEID_IP_mapping, des_ip)
					#print tunnel
				if tunnel == -1:
					print "tunnel not found"
				else:
					print ("From: {0} -> To {1}".format(src_ip, des_ip))
					ip_packet = packet[eth_length:]
						#print "start time: ", datetime.now().microsecond/1000.0, ", ", datetime.now(), ", len=", len(packet) 
					isSup, new_packet, protocol = src_ip_spoof(ip_packet)
					if isSup :
						print "Add GTP header, protocol: {0}".format(protocol)
							#gtp_header = struct.pack("!BBHL",56,255,len(new_packet),3396329693)
						gtp_header = struct.pack("!BBHL",56,255,len(new_packet),int(tunnel))
						packet = gtp_header + new_packet
							#print "end time: ", datetime.now().microsecond/1000.0, ", ", datetime.now(), ", len=", len(packet)
						gtp_sock.sendto( packet,("10.0.0.4", 2152))
							#print "Return to user..."
					else:
						print "not Sup"
						pass

			else:
				pass
Exemple #3
0
def main():
    LOCAL_IP = "10.0.1.3"
    eNB_IP = "10.0.0.4"
    eNB_PORT = 2152
    gtp_length = 8
    ip_length = 20

    #recive the traffic
    ListenSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    ListenSock.bind((LOCAL_IP, 2152))
    #send the traffic
    ForwardSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # allocate shm
    shm_addr = sysv_ipc.SharedMemory(9487, sysv_ipc.IPC_CREAT, 0666, 4096)
    TEID_ip_mapping = sysv_ipc.attach(shm_addr.id)

    #sysv_ipc.remove_shared_memory()

    while True:
        #print 'start'
        gtp_pkg, addr = ListenSock.recvfrom(2048)
        gtp_header = struct.unpack("!BBHL", gtp_pkg[:gtp_length])
        ip_packet = gtp_pkg[gtp_length:]
        ip_header = struct.unpack("BBHHHBBH4s4s", ip_packet[:ip_length])
        TEID = str(gtp_header[3])
        dst_ip = socket.inet_ntoa(ip_header[9])
        src_ip = socket.inet_ntoa(ip_header[8])
        #print "{0} -> {1}".format(src_ip,dst_ip)
        '''if 'application/octet-stream' in gtp_pkg:#'HTTP/1.1 200 OK':
			print "mpd start forward time: {0}".format(datetime.now())
		if '</MPD>' in gtp_pkg:
			print "mpd end   forward time: {0}".format(datetime.now())
		if 'video/x-m4v' in gtp_pkg:
			print "x-m4v forward time: {0}".format(datetime.now())
		if 'audio/x-m4a' in gtp_pkg:
			print "x-m4a forward time: {0}".format(datetime.now())'''
        # write to shm on downlink traffic
        res = write_dl_mapping_list(TEID_ip_mapping, TEID, dst_ip)
        #print res
        ForwardSocket.sendto(gtp_pkg, (eNB_IP, eNB_PORT))
    def test_attach(self):
        """Exercise attach()"""
        # Create memory, write something to it, then detach
        mem = sysv_ipc.SharedMemory(None, sysv_ipc.IPC_CREX)
        mem.write('hello world')
        mem.detach()
        self.assertFalse(mem.attached)
        self.assertEqual(mem.number_attached, 0)

        # Reattach memory via a different SharedMemory instance
        mem2 = sysv_ipc.attach(mem.id)
        self.assertFalse(mem.attached)
        self.assertTrue(mem2.attached)
        self.assertEqual(mem.number_attached, 1)
        self.assertEqual(mem2.number_attached, 1)

        self.assertEqual(mem2.read(len('hello world')), b'hello world')

        mem2.detach()

        mem.remove()

        self.assertRaises(sysv_ipc.ExistentialError, sysv_ipc.SharedMemory, mem.key)
Exemple #5
0
def attach_afl_map_shm(shm_env_var=None):
    shm_env_var = shm_env_var or DEFAULT_SHM_ENV_VAR
    shm = sysv_ipc.attach(int(os.environ[shm_env_var]))
    shm.write(b"\x01", offset=0)
    return shm
        if (self.airtemp(plane_index) < -100
                or self.airtemp(plane_index) > 50):
            #print "Fail on temp", self.airtemp(plane_index)
            return False
        #49 < lat < 54 [deg] ##CHANGE PER CURRENT LOCATION
        #0.0 < lon < 10 [deg] ##CHANGE PER CURRENT LOCATION
        if (self.planes[plane_index].lat < -180
                or self.planes[plane_index].lon < -90):
            return False

        return True


with open('mds02.log') as f:
    for line in f:
        if 'SHM_ID=' in line:
            shm_id = int(line[18:-4])
            #print 'Ident, Latitude, Longitude, Altitude / ft, T / °C'
            shm = sysv_ipc.attach(shm_id)
            at = AircraftTemp(shm)
            for idx, ac in enumerate(at.getPlanes()):
                if at.filter(idx):
                    with open('mds02.csv', 'a') as c:
                        csv.writer(c).writerow([
                            datetime.utcnow().isoformat(), ac.acident, ac.lat,
                            ac.lon, ac.alt,
                            at.airtemp(idx), ac.hdg, ac.ias, ac.tas, ac.sogc,
                            ac.bds.mach_60, ac.bds.hdg_60, ac.bds.ias_60,
                            ac.bds.tas_50
                        ])