Session Initiation Protocol (SIP)
voip.sip
Session Initiation Protocol (SIP) implementation of RFC 3261.
CallerID
Bases: str
SIP From/To header value with structured access and privacy-safe repr.
Behaves as a plain str so it is wire-format compatible and can be
stored in header dicts unchanged. repr() returns a short anonymized
form that shows only the last four characters of the user part and the
carrier domain — useful for log messages.
Examples:
>>> str(CallerID('"015114455910" <sip:015114455910@telefonica.de>;tag=abc'))
'"015114455910" <sip:015114455910@telefonica.de>;tag=abc'
>>> repr(CallerID('"015114455910" <sip:015114455910@telefonica.de>;tag=abc'))
'****5910@telefonica.de'
>>> repr(CallerID('sip:alice@example.com'))
'*lice@example.com'
Source code in voip/sip/types.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
display_name
property
Display name from the From/To header, if present.
host
property
Carrier domain extracted from the SIP URI.
tag
property
Dialog tag parameter value, if present.
user
property
SIP user part (phone number or username).
__repr__()
Anonymized label: last 4 chars of user + carrier domain.
Source code in voip/sip/types.py
52 53 54 55 56 57 | |
Message
dataclass
Bases: ByteSerializableObject, ABC
A SIP message RFC 3261 §7.
Source code in voip/sip/messages.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
Request
dataclass
Bases: Message
A SIP request message RFC 3261 §7.1.
Source code in voip/sip/messages.py
87 88 89 90 91 92 93 94 95 96 97 98 99 | |
Response
dataclass
Bases: Message
A SIP response message RFC 3261 §7.2.
Source code in voip/sip/messages.py
102 103 104 105 106 107 108 109 110 111 112 113 114 | |
SessionInitiationProtocol
dataclass
Bases: Protocol
SIP User Agent Client (UAC) over TLS/TCP RFC 3261.
Handles incoming calls and, optionally, carrier registration with digest authentication RFC 3261 §22. All signalling is sent over a single persistent TLS/TCP connection.
RFC 3261 topology overview
Outbound proxy (§8.1.2): the SIP server this UA sends all requests to. It may be a carrier edge proxy whose address differs from the registrar.
Registrar (§10): the server that maintains location bindings for a
domain. Its URI is derived automatically from the aor by
stripping the user part (e.g. sips:alice@example.com →
sips:example.com). When no outbound_proxy is configured,
the UA is expected to connect directly to the registrar server.
When an outbound_proxy is configured it acts as the first SIP
hop and may differ from the registrar domain — for example when a carrier
provides a dedicated proxy at proxy.carrier.com while the AOR domain
(and thus the registrar Request-URI) is carrier.com.
Subclass and override call_received to handle incoming calls:
class MySession(SessionInitiationProtocol):
def call_received(self, request: Request) -> None:
self.answer(request=request, call_class=MyCall)
To register with a carrier on startup, pass the registration parameters:
session = SessionInitiationProtocol(
aor="sips:alice@example.com",
username="alice",
password="secret",
# Optional: connect via a separate outbound proxy
# outbound_proxy=("proxy.carrier.com", 5061),
)
Source code in voip/sip/protocol.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 | |
registrar_uri
property
Registrar Request-URI derived from the AOR, preserving its scheme.
The scheme (sip: or sips:) is taken directly from aor
so the client honours whatever security contract the administrator has
configured. The user part is stripped; only the host (and optional
port) is kept, per RFC 3261 §10.2.
Examples:
sip:alice@example.com → sip:example.com
sips:alice@example.com → sips:example.com
ack_received(request)
Handle an ACK confirming dialog establishment.
Override in subclasses to react to the ACK.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The SIP ACK request. |
required |
Source code in voip/sip/protocol.py
463 464 465 466 467 468 469 470 | |
answer(request, *, call_class)
async
Answer an incoming call by setting up RTP and sending 200 OK with SDP.
This coroutine can be awaited directly or wrapped in a task:
# inside a sync call_received:
asyncio.create_task(self.answer(request=request, call_class=MyCall))
# inside an async call_received:
await self.answer(request=request, call_class=MyCall)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The SIP INVITE request (from |
required |
call_class
|
type[RTPCall]
|
A |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
When |
Source code in voip/sip/protocol.py
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
bye_received(request)
Handle a BYE terminating a dialog.
Override in subclasses to tear down the call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The SIP BYE request. |
required |
Source code in voip/sip/protocol.py
472 473 474 475 476 477 478 479 | |
call_received(request)
Handle an incoming call.
Override in subclasses to accept or reject the call:
def call_received(self, request: Request) -> None:
self.answer(request=request, call_class=MyCall)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The SIP INVITE request. |
required |
Source code in voip/sip/protocol.py
449 450 451 452 453 454 455 456 457 458 459 460 461 | |
cancel_received(request)
Handle a CANCEL request for a pending INVITE.
Override in subclasses to react to caller cancellation before the call is answered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The SIP CANCEL request. |
required |
Source code in voip/sip/protocol.py
481 482 483 484 485 486 487 488 489 | |
close()
Close the TLS/TCP transport and the RTP mux.
Source code in voip/sip/protocol.py
248 249 250 251 252 253 | |
connection_lost(exc)
Handle a lost TLS/TCP connection.
Source code in voip/sip/protocol.py
927 928 929 930 931 | |
connection_made(transport)
Store the TLS/TCP transport and start RTP mux + carrier registration.
Source code in voip/sip/protocol.py
170 171 172 173 174 175 176 177 178 179 180 | |
data_received(data)
Buffer incoming bytes and dispatch complete SIP messages.
SIP over TCP uses the Content-Length header to frame messages
(RFC 3261 §18.3). Partial datagrams are accumulated until a full
message is available.
Source code in voip/sip/protocol.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
digest_response(*, username, password, realm, nonce, method, uri, algorithm=DigestAlgorithm.SHA_256, qop=None, nc='00000001', cnonce=None)
classmethod
Compute a SIP digest response per RFC 3261 §22 and RFC 8760.
RFC 8760 deprecates MD5 and mandates support for SHA-256 and
SHA-512-256. The algorithm parameter selects the hash function;
it defaults to SHA-256.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in voip/sip/protocol.py
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 | |
packet_received(data, addr)
Handle RFC 5626 keepalive pings, then dispatch SIP messages.
Source code in voip/sip/protocol.py
229 230 231 232 233 234 235 236 237 238 239 240 | |
parse_auth_challenge(header)
staticmethod
Parse Digest challenge parameters from a WWW-Authenticate/Proxy-Authenticate header.
Source code in voip/sip/protocol.py
862 863 864 865 866 867 868 869 870 871 | |
register(authorization=None, proxy_authorization=None)
async
Send a REGISTER request to the registrar, optionally with credentials.
The REGISTER Request-URI is the registrar URI derived from aor
(RFC 3261 §10.2). When an outbound_proxy is configured, the
request is sent over the existing TLS/TCP connection to that proxy,
which routes it to the registrar on our behalf.
Source code in voip/sip/protocol.py
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 | |
registered()
Handle a confirmed carrier registration. Override to react.
Source code in voip/sip/protocol.py
859 860 | |
reject(request, status_code=Status['Busy Here'], reason=Status['Busy Here'].name)
Reject an incoming call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The SIP INVITE request (from |
required |
status_code
|
int
|
SIP response status code (default: 486 Busy Here). |
Status['Busy Here']
|
reason
|
str
|
SIP response reason phrase. |
name
|
Source code in voip/sip/protocol.py
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | |
request_received(request, addr)
Dispatch a received SIP request to the appropriate handler.
Source code in voip/sip/protocol.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
response_received(response, addr)
Handle REGISTER responses including digest auth challenges (RFC 3261 §22).
Only processes responses when registration parameters are configured.
Source code in voip/sip/protocol.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
ringing(request)
Send a 180 Ringing provisional response to the caller.
Call this from call_received before answering to indicate
that the call is being processed (e.g. while a user is alerted).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The SIP INVITE request (from |
required |
Source code in voip/sip/protocol.py
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 | |
send(message)
Serialize and send a SIP message over the TLS/TCP connection.
Source code in voip/sip/protocol.py
242 243 244 245 246 | |