3333#include "asterisk/channel.h"
3434#include "asterisk/module.h"
3535#include "asterisk/callerid.h"
36+ #include "asterisk/conversions.h"
3637
3738/*!
3839 * \internal
@@ -119,6 +120,58 @@ static pjsip_fromto_hdr *get_id_header(pjsip_rx_data *rdata, const pj_str_t *hea
119120 return parsed_hdr ;
120121}
121122
123+ /*!
124+ * \internal
125+ * \brief Set an ANI2 integer based on OLI data in a From header
126+ *
127+ * This uses the contents of a From header in order to set Originating Line information.
128+ *
129+ * \param rdata The incoming message
130+ * \param ani2 The ANI2 field to set
131+ * \retval 0 Successfully parsed OLI
132+ * \retval non-zero Could not parse OLI
133+ */
134+ static int set_id_from_oli (pjsip_rx_data * rdata , int * ani2 )
135+ {
136+ char fromhdr [AST_CHANNEL_NAME ];
137+ const char * s = NULL ;
138+ pjsip_sip_uri * uri ;
139+ pjsip_name_addr * id_name_addr ;
140+
141+ pjsip_fromto_hdr * from = pjsip_msg_find_hdr (rdata -> msg_info .msg ,
142+ PJSIP_H_FROM , rdata -> msg_info .msg -> hdr .next );
143+ id_name_addr = (pjsip_name_addr * ) from -> uri ;
144+
145+ if (!from ) {
146+ /* This had better not happen */
147+ return -1 ;
148+ }
149+
150+ uri = pjsip_uri_get_uri (id_name_addr );
151+ ast_copy_pj_str (fromhdr , & uri -> user , sizeof (fromhdr ));
152+
153+ /* Look for the possible OLI tags. */
154+ if ((s = strcasestr (fromhdr , ";isup-oli=" ))) {
155+ s += 10 ;
156+ } else if ((s = strcasestr (fromhdr , ";ss7-oli=" ))) {
157+ s += 9 ;
158+ } else if ((s = strcasestr (fromhdr , ";oli=" ))) {
159+ s += 5 ;
160+ }
161+
162+ if (ast_strlen_zero (s )) {
163+ /* OLI tag is missing, or present with nothing following the '=' sign */
164+ return -1 ;
165+ }
166+
167+ /* just in case OLI is quoted */
168+ if (* s == '\"' ) {
169+ s ++ ;
170+ }
171+
172+ return ast_str_to_int (s , ani2 );
173+ }
174+
122175/*!
123176 * \internal
124177 * \brief Set an ast_party_id structure based on data in a P-Asserted-Identity header
@@ -371,6 +424,7 @@ static void update_incoming_connected_line(struct ast_sip_session *session, pjsi
371424static int caller_id_incoming_request (struct ast_sip_session * session , pjsip_rx_data * rdata )
372425{
373426 if (!session -> channel ) {
427+ int ani2 ;
374428 /*
375429 * Since we have no channel this must be the initial inbound
376430 * INVITE. Set the session ID directly because the channel
@@ -387,6 +441,11 @@ static int caller_id_incoming_request(struct ast_sip_session *session, pjsip_rx_
387441 if (!session -> endpoint -> id .self .number .valid ) {
388442 set_id_from_from (rdata , & session -> id );
389443 }
444+ if (!set_id_from_oli (rdata , & ani2 )) {
445+ session -> ani2 = ani2 ;
446+ } else {
447+ session -> ani2 = 0 ;
448+ }
390449 } else {
391450 /*
392451 * ReINVITE or UPDATE. Check for changes to the ID and queue
0 commit comments