def filter_uri(uri): reg_uri=copy_config.getConfig("filter", "uri") if not reg_uri: return True else: if not reg_match(reg_uri, uri): return False else: return True
def start(): if(len(sys.argv)<2): capture_nc=copy_config.getConfig("config", 'nc') port =copy_config.getConfig("config", 'port') if(capture_nc): print"Start copysever network card: %s" % (capture_nc) copy_log.log_info("Start copysever network card: %s" % (capture_nc)) if(port): capture_udp.capture(capture_nc,int(port)); else: capture_udp.capture(capture_nc); else: print "error not have network card info" copy_log.error_log("error not have network card info") sys.exit(1) else: capture_nc =sys.argv[1] print"Start copysever network card: %s" % (capture_nc) if(len(sys.argv)==3): port=sys.argv[2] capture_udp.capture(capture_nc,port); else: capture_udp.capture(capture_nc);
def send_brocast_UDP(data, port, ttl=255): time.sleep(0.001) ips = copy_config.getConfig('config', 'ip') if (ips): ip_list = ips.split(',') send_single_ips(data, port, ip_list) else: host = '<broadcast>' addr = (host, port) sock = socket(AF_INET, SOCK_DGRAM) sock.bind(('', 0)) #brocast ttl_bin = struct.pack('@i', ttl) sock.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, ttl_bin) sock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) #brocast sock.sendto(data, addr)
def send_groupcast_UDP(data, port, local_ip, group='224.1.1.1', ttl=255): if (copy_filter.filter(data)): time.sleep(0.001) ips = copy_config.getConfig('config', 'ip') if (ips): ip_list = ips.split(',') send_single_ips(data, port, ip_list) else: sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) sock.bind((local_ip, port)) # Set Time-to-live (optional) ttl_bin = struct.pack('@i', ttl) sock.setsockopt(IPPROTO_IP, IP_MULTICAST_TTL, ttl_bin) status = sock.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, inet_aton(group) + inet_aton(local_ip)) sock.sendto(data, (group, port)) else: pass