#!/usr/bin/env python3
"""
Generate invalid-content-length.pcap with SIP messages containing
non-numeric Content-Length headers to trigger invalid_SIP_request_body_len
and invalid_SIP_response_body_len weirds.

Generated by Claude Opus 4.6 (Anthropic).
"""

from pathlib import Path

from scapy.all import IP, UDP, Ether, Raw, wrpcap

CLIENT = "10.0.0.1"
SERVER = "10.0.0.2"
SIP_PORT = 5060


def sip_invite_request(content_length="bad"):
    """INVITE request with an invalid Content-Length header."""
    msg = (
        f"INVITE sip:bob@{SERVER} SIP/2.0\r\n"
        f"Via: SIP/2.0/UDP {CLIENT}:5060;branch=z9hG4bK776\r\n"
        f"From: <sip:alice@{CLIENT}>;tag=1928301774\r\n"
        f"To: <sip:bob@{SERVER}>\r\n"
        f"Call-ID: a84b4c76e66710@{CLIENT}\r\n"
        f"CSeq: 1 INVITE\r\n"
        f"Content-Length: {content_length}\r\n"
        f"\r\n"
    )
    return (
        Ether()
        / IP(src=CLIENT, dst=SERVER)
        / UDP(sport=5060, dport=SIP_PORT)
        / Raw(load=msg.encode())
    )


def sip_200_response(content_length="xyz"):
    """200 OK response with an invalid Content-Length header."""
    msg = (
        f"SIP/2.0 200 OK\r\n"
        f"Via: SIP/2.0/UDP {CLIENT}:5060;branch=z9hG4bK776\r\n"
        f"From: <sip:alice@{CLIENT}>;tag=1928301774\r\n"
        f"To: <sip:bob@{SERVER}>;tag=abc123\r\n"
        f"Call-ID: a84b4c76e66710@{CLIENT}\r\n"
        f"CSeq: 1 INVITE\r\n"
        f"Content-Length: {content_length}\r\n"
        f"\r\n"
    )
    return (
        Ether()
        / IP(src=SERVER, dst=CLIENT)
        / UDP(sport=SIP_PORT, dport=5060)
        / Raw(load=msg.encode())
    )


def main():
    packets = [
        sip_invite_request(content_length="bad"),
        sip_200_response(content_length="xyz"),
    ]

    out = Path(__file__).parent / "invalid-content-length.pcap"
    wrpcap(str(out), packets)
    print(f"Wrote {len(packets)} packets to {out}")


if __name__ == "__main__":
    main()
