smoltcp-rs / smoltcp

a smol tcp/ip stack
BSD Zero Clause License
3.64k stars 404 forks source link

how to change source ip address? #819

Closed ryanjackgit closed 11 months ago

ryanjackgit commented 11 months ago

I'm new to smoltcp, I want to change the ipv4 packet src_addr if it is 10. 107. 1. 7. Below is right? Thanks.

                        let msg=<ipv4 bytes>
                         let mut head_copy;
                        {
                        let  packet=Ipv4Packet::new_checked(msg.as_bytes()).expect("ipv4 packet into bytes");
                        let  header_parsed = Ipv4Repr::parse(&packet, &ChecksumCapabilities::default()) .expect("malformed packet");
                        head_copy=header_parsed.clone();

                        if  head_copy.src_addr==Ipv4Address::new(10, 107, 1, 7) {
                            head_copy.src_addr=Ipv4Address::new(10, 107, 1, 8);
                            }

                        }

                        let mut bytes=msg.into_bytes();
                        let  mut packet=Ipv4Packet::new_unchecked(&mut bytes);

                        head_copy.emit(&mut packet, &ChecksumCapabilities::default());