@@ -119,7 +119,7 @@ def create_raw_udp_packet(
119119
120120
121121def send_packet (
122- src_ip : str ,
122+ src_ip : Optional [ str ] ,
123123 src_port : Optional [int ],
124124 dst_ip : str ,
125125 dst_port : int ,
@@ -128,21 +128,26 @@ def send_packet(
128128 ip_id : Optional [int ] = None ,
129129) -> None :
130130 """
131- Sends a UDP packet with the specified source IP, destination IP, destination port, and data payload.
132-
133- Ensure that the source IP address is valid and that you have permission to send packets with spoofed addresses.
131+ Sends a UDP packet with a spoofed source IP address.
134132
135133 Args:
136- src_ip (str): The source IP address.
134+ src_ip (Optional[ str] ): The source IP address to spoof. If None, spoofing is not performed .
137135 src_port (Optional[int]): The source port number. If None, a random port will be used.
138136 dst_ip (str): The destination IP address.
139137 dst_port (int): The destination port number.
140138 payload (bytes): The data payload to include in the packet.
139+ ip_id (Optional[int]): The IP identification number. If None, a random ID will be used.
141140 """
142- client = socket .socket (socket .AF_INET , socket .SOCK_RAW , socket .IPPROTO_RAW )
143- client .setsockopt (socket .IPPROTO_IP , socket .IP_HDRINCL , 1 )
144- packet = create_raw_udp_packet (src_ip , src_port , dst_ip , dst_port , payload , ip_id = ip_id )
145- client .sendto (packet , (dst_ip , dst_port ))
141+ if src_ip is None :
142+ if src_port is not None or ip_id is not None :
143+ raise ValueError ("If src_ip is None, src_port and ip_id must also be None." )
144+ client = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
145+ client .sendto (payload , (dst_ip , dst_port ))
146+ else :
147+ client = socket .socket (socket .AF_INET , socket .SOCK_RAW , socket .IPPROTO_RAW )
148+ client .setsockopt (socket .IPPROTO_IP , socket .IP_HDRINCL , 1 )
149+ packet = create_raw_udp_packet (src_ip , src_port , dst_ip , dst_port , payload , ip_id = ip_id )
150+ client .sendto (packet , (dst_ip , dst_port ))
146151
147152
148153def broadcast_packet (
0 commit comments