FreeCalypso > hg > themwi-system-sw
comparison sip-manual-out/uac.c @ 71:d74b545a3c2a
sip-manual-out: new test program
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Tue, 20 Sep 2022 10:14:18 -0800 |
| parents | |
| children | d7b6b8973a83 |
comparison
equal
deleted
inserted
replaced
| 70:47976db01894 | 71:d74b545a3c2a |
|---|---|
| 1 /* | |
| 2 * Here we implement processing of SIP responses to the requests we sent out. | |
| 3 */ | |
| 4 | |
| 5 #include <sys/types.h> | |
| 6 #include <sys/socket.h> | |
| 7 #include <netinet/in.h> | |
| 8 #include <arpa/inet.h> | |
| 9 #include <stdio.h> | |
| 10 #include <stdlib.h> | |
| 11 #include <string.h> | |
| 12 #include <strings.h> | |
| 13 #include "../libsip/parse.h" | |
| 14 #include "../libsip/out_msg.h" | |
| 15 | |
| 16 #define MAX_TO_TAG 63 | |
| 17 | |
| 18 extern char *get_single_header(); | |
| 19 | |
| 20 extern struct in_addr sip_bind_ip; | |
| 21 extern unsigned sip_bind_port; | |
| 22 extern char call_id[], from_uri[], to_uri[]; | |
| 23 | |
| 24 char to_tag[MAX_TO_TAG+1]; | |
| 25 | |
| 26 add_req_boilerplate(msg, cseq) | |
| 27 struct sip_msg_out *msg; | |
| 28 char *cseq; | |
| 29 { | |
| 30 char strbuf[256]; | |
| 31 int rc; | |
| 32 | |
| 33 sprintf(strbuf, "SIP/2.0/UDP %s:%u", | |
| 34 inet_ntoa(sip_bind_ip), sip_bind_port); | |
| 35 rc = out_msg_add_header(msg, "Via", strbuf); | |
| 36 if (rc < 0) | |
| 37 return rc; | |
| 38 rc = out_msg_add_header(msg, "From", from_uri); | |
| 39 if (rc < 0) | |
| 40 return rc; | |
| 41 if (to_tag[0]) { | |
| 42 sprintf(strbuf, "<%s>;tag=%s", to_uri, to_tag); | |
| 43 rc = out_msg_add_header(msg, "To", strbuf); | |
| 44 } else | |
| 45 rc = out_msg_add_header(msg, "To", to_uri); | |
| 46 if (rc < 0) | |
| 47 return rc; | |
| 48 rc = out_msg_add_header(msg, "Call-ID", call_id); | |
| 49 if (rc < 0) | |
| 50 return rc; | |
| 51 rc = out_msg_add_header(msg, "CSeq", cseq); | |
| 52 if (rc < 0) | |
| 53 return rc; | |
| 54 return out_msg_add_header(msg, "Max-Forwards", "70"); | |
| 55 } | |
| 56 | |
| 57 add_contact_header(msg) | |
| 58 struct sip_msg_out *msg; | |
| 59 { | |
| 60 char strbuf[80]; | |
| 61 | |
| 62 sprintf(strbuf, "<sip:%s:%u;transport=udp>", | |
| 63 inet_ntoa(sip_bind_ip), sip_bind_port); | |
| 64 return out_msg_add_header(msg, "Contact", strbuf); | |
| 65 } | |
| 66 | |
| 67 static void | |
| 68 send_ack(sin) | |
| 69 struct sockaddr_in *sin; | |
| 70 { | |
| 71 struct sip_msg_out msg; | |
| 72 int rc; | |
| 73 | |
| 74 rc = start_request_out_msg(&msg, "ACK", to_uri); | |
| 75 if (rc < 0) { | |
| 76 msg_size_err: fprintf(stderr, "composing ACK message: size error\n"); | |
| 77 return; | |
| 78 } | |
| 79 rc = add_req_boilerplate(&msg, "1 ACK"); | |
| 80 if (rc < 0) | |
| 81 goto msg_size_err; | |
| 82 out_msg_finish(&msg); | |
| 83 sip_tx_packet(&msg, sin); | |
| 84 } | |
| 85 | |
| 86 static void | |
| 87 handle_invite_response(msg, sin) | |
| 88 struct sip_pkt_rx *msg; | |
| 89 struct sockaddr_in *sin; | |
| 90 { | |
| 91 printf("Response to INVITE: %s\n", msg->status_str); | |
| 92 if (msg->status_code >= 200) { | |
| 93 printf("Sending ACK\n"); | |
| 94 send_ack(sin); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 void | |
| 99 process_sip_response(msg, sin) | |
| 100 struct sip_pkt_rx *msg; | |
| 101 struct sockaddr_in *sin; | |
| 102 { | |
| 103 char *call_id_hdr, *cseq_hdr; | |
| 104 | |
| 105 call_id_hdr = get_single_header(msg, "Call-ID", "i", (int *) 0); | |
| 106 if (!call_id_hdr) { | |
| 107 printf("Got SIP response w/o Call-ID header\n"); | |
| 108 return; | |
| 109 } | |
| 110 if (strcmp(call_id_hdr, call_id)) { | |
| 111 printf("Got SIP response with wrong Call-ID\n"); | |
| 112 return; | |
| 113 } | |
| 114 cseq_hdr = get_single_header(msg, "CSeq", (char *) 0, (int *) 0); | |
| 115 if (!cseq_hdr) { | |
| 116 printf("Got SIP response w/o CSeq header\n"); | |
| 117 return; | |
| 118 } | |
| 119 if (!strcmp(cseq_hdr, "1 INVITE")) | |
| 120 handle_invite_response(msg, sin); | |
| 121 else | |
| 122 printf("Got SIP resp for our Call-ID with unknown CSeq %s\n", | |
| 123 cseq_hdr); | |
| 124 } |
