- removed 'return 0', that causes exiting the script, from handle_subscribe and handl...
authorAnca Vamanu <anca@voice-system.ro>
Tue, 23 Oct 2007 13:06:55 +0000 (13:06 +0000)
committerAnca Vamanu <anca@voice-system.ro>
Tue, 23 Oct 2007 13:06:55 +0000 (13:06 +0000)
- modified to send stateless reply from the module in all cases (success or error)

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@2978 689a6050-402a-0410-94f2-e92a70836424

modules_k/presence/README
modules_k/presence/doc/presence_user.sgml
modules_k/presence/presentity.c
modules_k/presence/presentity.h
modules_k/presence/publish.c
modules_k/presence/subscribe.c
modules_k/presence/utils_func.c
modules_k/presence/utils_func.h

index 574a63d..3b42c55 100644 (file)
@@ -321,6 +321,13 @@ modparam("presence", "pres_htable_size", 11)
 
    This function can be used from REQUEST_ROUTE.
 
+   Return code:
+
+     * 1 - if success.
+     * -1 - if error.
+
+   The module sends an appropriate stateless reply in all cases.
+
    Example 1-14. handle_publish usage
 ...
         if(is_method("PUBLISH"))
@@ -343,6 +350,13 @@ modparam("presence", "pres_htable_size", 11)
 
    This function can be used from REQUEST_ROUTE.
 
+   Return code:
+
+     * 1 - if success.
+     * -1 - if error.
+
+   The module sends an appropriate stateless reply in all cases.
+
    Example 1-15. handle_subscribe usage
 ...
 if(method=="SUBSCRIBE")
index b6ac247..5652070 100644 (file)
@@ -361,6 +361,26 @@ modparam("presence", "pres_htable_size", 11)
                <para>
                This function can be used from REQUEST_ROUTE.
                </para>
+               <para>
+               <emphasis>Return code:</emphasis>
+               <itemizedlist>
+                       <listitem>
+                       <para>
+                               <emphasis> 1 - if success</emphasis>.
+                       </para>
+                       </listitem>
+                       <listitem>
+                       <para>
+                               <emphasis> -1 - if error</emphasis>.
+                       </para>
+                       </listitem>
+               </itemizedlist>
+               </para>
+               <para>
+                       The module sends an appropriate stateless reply
+                       in all cases.
+               </para>
+
                <example>
                <title><function>handle_publish</function> usage</title>
                <programlisting format="linespecific">
@@ -390,6 +410,26 @@ modparam("presence", "pres_htable_size", 11)
                <para>
                This function can be used from REQUEST_ROUTE.
                </para>
+               <para>
+               <emphasis>Return code:</emphasis>
+               <itemizedlist>
+                       <listitem>
+                       <para>
+                               <emphasis> 1 - if success</emphasis>.
+                       </para>
+                       </listitem>
+                       <listitem>
+                       <para>
+                               <emphasis> -1 - if error</emphasis>.
+                       </para>
+                       </listitem>
+               </itemizedlist>
+               </para>
+               <para>
+                       The module sends an appropriate stateless reply
+                       in all cases.
+               </para>
+
                <example>
                <title><function>handle_subscribe</function> usage</title>
                <programlisting format="linespecific">
index ae8ce55..d97b961 100644 (file)
@@ -227,7 +227,7 @@ error:
 }
 
 int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
-               int new_t)
+               int new_t, int* sent_reply)
 {
        db_key_t query_cols[11], update_keys[7], result_cols[5];
        db_op_t  query_ops[11];
@@ -240,6 +240,7 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
        str cur_etag= {0, 0};
        str* rules_doc= NULL;
 
+       *sent_reply= 0;
        if(presentity->event->req_auth)
        {
                /* get rules_document */
@@ -330,6 +331,7 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
                        LM_ERR("sending 200OK\n");
                        goto error;
                }
+               *sent_reply= 1;
                goto send_notify;
        }
        else
@@ -361,6 +363,7 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
                                        LM_ERR("sending 200OK reply\n");
                                        goto error;
                                }
+                               *sent_reply= 1;
                                if( publ_notify( presentity, body, &presentity->etag, rules_doc)< 0 )
                                {
                                        LM_ERR("while sending notify\n");
@@ -476,6 +479,8 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
                                        pkg_free(etag.s);
                                return -1;
                        }
+                       *sent_reply= 1;
+                       
                        if(etag.s)
                                pkg_free(etag.s);
                        etag.s= NULL;
@@ -495,6 +500,7 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
                                LM_ERR("sending '412 Conditional request failed' reply\n");
                                goto error;
                        }
+                       *sent_reply= 1;
                }
        }
 
index 6073799..90cfd2d 100644 (file)
@@ -59,7 +59,8 @@ presentity_t* new_presentity( str* domain,str* user,int expires,
                pres_ev_t* event, str* etag, str* sender);
 
 /* update presentity in database */
-int update_presentity(struct sip_msg* msg,presentity_t* p,str* body,int t_new);
+int update_presentity(struct sip_msg* msg,presentity_t* p,str* body,int t_new,
+               int* sent_reply);
 
 /* free memory */
 void free_presentity(presentity_t* p);
index b0b581f..57d69af 100644 (file)
@@ -53,6 +53,8 @@ extern int startup_time;
 
 static str pu_400a_rpl = str_init("Bad request");
 static str pu_400b_rpl = str_init("Invalid request");
+static str pu_500_rpl  = str_init("Server Internal Error");
+static str pu_489_rpl  = str_init("Bad Event");
 
 void msg_presentity_clean(unsigned int ticks,void *param)
 {
@@ -270,7 +272,6 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
        struct hdr_field* hdr;
        int found= 0, etag_gen = 0;
        str etag={0, 0};
-       int error_ret = -1; 
        str* sender= NULL;
        static char buf[256];
        int buf_len= 255;
@@ -278,16 +279,20 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
        str pres_user;
        str pres_domain;
        struct sip_uri pres_uri;
+       int reply_code;
+       str reply_str;
+       int sent_reply= 0;
+
+       reply_code= 500;
+       reply_str= pu_500_rpl;
 
        counter++;
        if ( parse_headers(msg,HDR_EOH_F, 0)==-1 )
        {
                LM_ERR("parsing headers\n");
-               if (slb.reply(msg, 400, &pu_400a_rpl) == -1)
-                       LM_ERR("Error while sending 400 reply\n");
-               else
-                       error_ret = 0;
-               return error_ret;
+               reply_code= 400;
+               reply_str= pu_400a_rpl;
+               goto error;
        }
        memset(&body, 0, sizeof(str));
        
@@ -298,6 +303,8 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
                if (!msg->event->parsed && (parse_event(msg->event) < 0))
                {
                        LM_ERR("cannot parse Event header\n");
+                       reply_code= 400;
+                       reply_str= pu_400a_rpl;
                        goto error;
                }
                if(((event_t*)msg->event->parsed)->parsed & EVENT_OTHER)
@@ -334,7 +341,7 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
                if(etag.s == NULL)
                {
                        LM_ERR("when generating etag\n");
-                       return -1;
+                       goto error;
                }
                etag.len=(strlen(etag.s));
                etag_gen=1;
@@ -379,6 +386,8 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
                                msg->first_line.u.request.uri.len, &pres_uri)< 0)
        {
                LM_ERR("parsing Request URI\n");
+               reply_code= 400; 
+               reply_str= pu_400a_rpl;
                goto error;
        }
        pres_user= pres_uri.user;
@@ -387,6 +396,8 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
        if (!msg->content_length) 
        {
                LM_ERR("no Content-Length header found!\n");
+               reply_code= 400; 
+               reply_str= pu_400a_rpl;
                goto error;
        }       
 
@@ -397,10 +408,8 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
                if (etag_gen)
                {
                        LM_ERR("No E-Tag and no body found\n");
-                       if (slb.reply(msg, 400, &pu_400b_rpl) == -1)
-                               LM_ERR("sending 400 Invalid request reply\n");
-                       else
-                               error_ret = 0;
+                       reply_code= 400;
+                       reply_str= pu_400b_rpl;
                        goto error;
                }
        }
@@ -410,6 +419,8 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
                if (body.s== NULL) 
                {
                        LM_ERR("cannot extract body\n");
+                       reply_code= 400; 
+                       reply_str= pu_400a_rpl;
                        goto error;
                }
                body.len= get_content_length( msg );
@@ -430,8 +441,11 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
                if(parse_uri(buf, buf_len, &puri)!=0)
                {
                        LM_ERR("bad owner SIP address!\n");
+                       reply_code= 400; 
+                       reply_str= pu_400a_rpl;
                        goto error;
-               } else 
+               } 
+               else 
                {
                        LM_DBG("using user id [%.*s]\n",buf_len,buf);
                }
@@ -460,7 +474,7 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
        }
 
        /* querry the database and update or insert */
-       if(update_presentity(msg, presentity, &body, etag_gen) <0)
+       if(update_presentity(msg, presentity, &body, etag_gen, &sent_reply) <0)
        {
                LM_ERR("when updating presentity\n");
                goto error;
@@ -475,7 +489,24 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
 
        return 1;
 
+unsupported_event:
+       
+       LM_ERR("Missing or unsupported event header field value\n");
+               
+       if(msg->event && msg->event->body.s && msg->event->body.len>0)
+               LM_ERR("\tevent=[%.*s]\n", msg->event->body.len, msg->event->body.s);
+
+       reply_code= 489;
+       reply_str=      pu_489_rpl; 
+
 error:
+       if(sent_reply== 0)
+       {
+               if(send_error_reply(msg, reply_code, reply_str)< 0)
+               {
+                       LM_ERR("failed to send error reply\n");
+               }
+       }
        
        if(presentity)
                pkg_free(presentity);
@@ -483,20 +514,8 @@ error:
                pkg_free(etag.s);
        if(sender)
                pkg_free(sender);
-       
-       return error_ret;
-
-unsupported_event:
-       
-       LM_ERR("Missing or unsupported event header field value\n");
-               
-       if(msg->event && msg->event->body.s && msg->event->body.len>0)
-               LM_ERR("\tevent=[%.*s]\n", msg->event->body.len, msg->event->body.s);
-       
-       if(reply_bad_event(msg)< 0)
-               return -1;
 
-       return 0;
+       return -1;
 
 }
 
index d7ab6d6..e18ed2c 100644 (file)
@@ -38,8 +38,8 @@
 #include "notify.h"
 #include "../pua/hash.h"
 
-int get_stored_info(struct sip_msg* msg, subs_t* subs, int* error_ret);
-int get_database_info(struct sip_msg* msg, subs_t* subs, int* error_ret);
+int get_stored_info(struct sip_msg* msg, subs_t* subs, int* error_ret, str* reply_str);
+int get_database_info(struct sip_msg* msg, subs_t* subs, int* error_ret, str* reply_str);
 int refresh_watcher(str* pres_uri, str* watcher_uri, str* event, 
                int status, str* reason);
 
@@ -49,8 +49,12 @@ int insert_db_subs_auth(subs_t* subs);
 static str su_200_rpl  = str_init("OK");
 static str pu_481_rpl  = str_init("Subscription does not exist");
 static str pu_400_rpl  = str_init("Bad request");
+static str pu_500_rpl  = str_init("Server Internal Error");
+static str pu_489_rpl  = str_init("Bad Event");
 
-int send_202ok(struct sip_msg * msg, int lexpire, str *rtag, str* local_contact)
+
+int send_2XX_reply(struct sip_msg * msg, int reply_code, int lexpire,
+               str *rtag, str* local_contact)
 {
        static str hdr_append;
        
@@ -78,7 +82,7 @@ int send_202ok(struct sip_msg * msg, int lexpire, str *rtag, str* local_contact)
                goto error;
        }
 
-       if( slb.reply_dlg( msg, 202, &su_200_rpl, rtag)== -1)
+       if( slb.reply_dlg( msg, reply_code, &su_200_rpl, rtag)== -1)
        {
                LM_ERR("sending reply\n");
                goto error;
@@ -93,47 +97,6 @@ error:
        return -1;
 }
 
-int send_200ok(struct sip_msg * msg, int lexpire, str *rtag, str* local_contact)
-{
-       static str hdr_append;  
-
-       hdr_append.s = (char *)pkg_malloc( sizeof(char)*(local_contact->len+ 128));
-       if(hdr_append.s == NULL)
-       {
-               LM_ERR("unable to add lump_rl\n");
-               return -1;
-       }
-       hdr_append.len = sprintf(hdr_append.s, "Expires: %d\r\n", lexpire);
-       strncpy(hdr_append.s+hdr_append.len ,"Contact: <", 10);
-       hdr_append.len += 10;
-       strncpy(hdr_append.s+hdr_append.len, local_contact->s, local_contact->len);
-       hdr_append.len+= local_contact->len;
-       strncpy(hdr_append.s+hdr_append.len, ">", 1);
-       hdr_append.len += 1;
-       strncpy(hdr_append.s+hdr_append.len, CRLF, CRLF_LEN);
-       hdr_append.len += CRLF_LEN;
-
-       hdr_append.s[hdr_append.len]= '\0';
-
-       if (add_lump_rpl( msg, hdr_append.s, hdr_append.len, LUMP_RPL_HDR)==0 )
-       {
-               LM_ERR("unable to add lump_rl\n");
-               goto error;
-       }
-
-       if( slb.reply_dlg( msg, 200, &su_200_rpl, rtag)== -1)
-       {
-               LM_ERR("sending 200OK reply\n");
-               goto error;
-       }
-       
-       pkg_free(hdr_append.s);
-       return 0;
-error:
-       pkg_free(hdr_append.s);
-       return -1;
-
-}
 
 int delete_db_subs(str pres_uri, str ev_stored_name, str to_tag)
 {
@@ -288,12 +251,15 @@ int update_subs_db(subs_t* subs, int type)
        return 0;
 }
 
-int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen)
+int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen,
+               int* sent_reply)
 {      
        unsigned int hash_code;
-
+       
        printf_subs(subs);      
        
+       *sent_reply= 0;
+
        hash_code= core_hash(&subs->pres_uri, &subs->event->name, shtable_size);
 
        if( to_tag_gen ==0) /*if a SUBSCRIBE within a dialog */
@@ -315,12 +281,13 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen)
                
                        if(subs->event->type & PUBL_TYPE)
                        {       
-                               if( send_202ok(msg, subs->expires, &subs->to_tag, &subs->local_contact) <0)
+                               if( send_2XX_reply(msg, 202, subs->expires, &subs->to_tag,
+                                                       &subs->local_contact) <0)
                                {
                                        LM_ERR("sending 202 OK\n");
                                        goto error;
                                }
-                               
+                               *sent_reply= 1;
                                if(subs->event->wipeer)
                                {
                                        if(query_db_notify(&subs->pres_uri,
@@ -334,11 +301,13 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen)
                        }       
                        else /* if unsubscribe for winfo */
                        {
-                               if( send_200ok(msg, subs->expires, &subs->to_tag, &subs->local_contact) <0)
+                               if( send_2XX_reply(msg, 200, subs->expires, &subs->to_tag,
+                                                       &subs->local_contact) <0)
                                {
                                        LM_ERR("sending 202 OK reply\n");
                                        goto error;
                                }
+                               *sent_reply= 1;
                        }
                
                        subs->status= TERMINATED_STATUS;
@@ -388,11 +357,13 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen)
 
        if(subs->event->type & PUBL_TYPE)
        {       
-               if(send_202ok(msg,subs->expires,&subs->to_tag,&subs->local_contact)<0)
+               if(send_2XX_reply(msg, 202, subs->expires,&subs->to_tag,
+                                       &subs->local_contact)<0)
                {
                        LM_ERR("sending 202 OK reply\n");
                        goto error;
                }
+               *sent_reply= 1;
                
                if(subs->expires!= 0 && subs->event->wipeer)
                {       
@@ -423,11 +394,14 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen)
        }
        else 
        {
-               if( send_200ok(msg,subs->expires,&subs->to_tag,&subs->local_contact)<0)
+               if( send_2XX_reply(msg, 200, subs->expires, &subs->to_tag,
+                                       &subs->local_contact)<0)
                {
                        LM_ERR("sending 202 OK reply\n");
                        goto error;
                }               
+               *sent_reply= 1;
+               
                if(notify(subs, NULL, NULL, 0 )< 0)
                {
                        LM_ERR("sending notify request\n");
@@ -501,22 +475,23 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
        int found;
        str reason= {0, 0};
        struct sip_uri uri;
-       int error_ret= -1;
+       int reply_code;
+       str reply_str;
+       int sent_reply= 0;
 
        /* ??? rename to avoid collisions with other symbols */
        counter++;
 
        memset(&subs, 0, sizeof(subs_t));
+       
+       reply_code= 500;
+       reply_str= pu_500_rpl;
 
        if( parse_headers(msg,HDR_EOH_F, 0)==-1 )
        {
                LM_ERR("parsing headers\n");
-
-               if (slb.reply(msg, 400, &pu_400_rpl) == -1)
-               {
-                       LM_ERR("sending 400 reply\n");
-               }
-               error_ret = 0;
+               reply_code= 400;
+               reply_str= pu_400_rpl;
                goto error;
        }
        
@@ -525,7 +500,8 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
        {
                if (!msg->event->parsed && (parse_event(msg->event) < 0))
                {
-                       LM_ERR("cannot parse Event header\n");
+                       reply_code= 400;
+                       reply_str= pu_400_rpl;
                        goto error;
                }
                if(((event_t*)msg->event->parsed)->parsed & EVENT_OTHER)
@@ -575,7 +551,7 @@ int handle_subscribe(struct sip_msg* msg, char* str1, char* str2)
        }
        else
        {
-               if(get_stored_info(msg, &subs, &error_ret )< 0)
+               if(get_stored_info(msg, &subs, &reply_code, &reply_str )< 0)
                {
                        LM_ERR("getting stored info\n");
                        goto error;
@@ -666,7 +642,7 @@ after_status:
                goto error;
        }
        
-       if( update_subscription(msg, &subs, to_tag_gen) <0 )
+       if( update_subscription(msg, &subs, to_tag_gen, &sent_reply) <0 )
        {       
                LM_ERR("in update_subscription\n");
                goto error;
@@ -698,12 +674,18 @@ bad_event:
        if(parsed_event)
                LM_ERR("\tevent= %.*s\n",parsed_event->text.len,parsed_event->text.s);
        
-       if(reply_bad_event(msg)< 0)
-               goto error;
-
-       error_ret = 0;
+       reply_code= 489;
+       reply_str= pu_489_rpl;
 
 error:
+       
+       if(sent_reply== 0)
+       {
+               if(send_error_reply(msg, reply_code, reply_str)< 0)
+               {
+                       LM_ERR("failed to send reply on error case\n");
+               }
+       }
 
        if(subs.pres_uri.s)
                pkg_free(subs.pres_uri.s);
@@ -724,7 +706,7 @@ error:
        if(subs.record_route.s)
                pkg_free(subs.record_route.s);
 
-       return error_ret;
+       return -1;
 
 }
 
@@ -947,15 +929,14 @@ error:
 }
 
 
-int get_stored_info(struct sip_msg* msg, subs_t* subs, int* error_ret)
+int get_stored_info(struct sip_msg* msg, subs_t* subs, int* reply_code,
+               str* reply_str)
 {      
        str pres_uri= {0, 0}, reason={0, 0};
        subs_t* s;
        int i;
        unsigned int hash_code;
 
-       *error_ret= -1;
-
        /* first try to_user== pres_user and to_domain== pres_domain */
 
        uandd_to_uri(subs->to_user, subs->to_domain, &pres_uri);
@@ -1000,16 +981,13 @@ int get_stored_info(struct sip_msg* msg, subs_t* subs, int* error_ret)
 
        if(fallback2db)
        {
-               return get_database_info(msg, subs, error_ret); 
+               return get_database_info(msg, subs, reply_code, reply_str);     
        }
 
        LM_ERR("record not found in hash_table\n");
-       if (slb.reply(msg, 481, &pu_481_rpl) == -1)
-       {
-               LM_ERR("sending '481 Subscription does not exist' reply\n");
-               return -1;
-       }
-       *error_ret= 0;
+       *reply_code= 481;
+       *reply_str= pu_481_rpl;
+
        return -1;
 
 found_rec:
@@ -1047,12 +1025,10 @@ found_rec:
        {
                LM_ERR("wrong sequence number;received: %d - stored: %d\n",
                                subs->remote_cseq, s->remote_cseq);
-               if (slb.reply(msg, 400, &pu_400_rpl) == -1)
-               {
-                       LM_ERR("sending '400 Bad request' reply\n");
-               }
-               else
-                       *error_ret= 0;
+               
+               *reply_code= 400;
+               *reply_str= pu_400_rpl;
+
                lock_release(&subs_htable[i].lock);
                goto error;
        }       
@@ -1070,7 +1046,7 @@ error:
        return -1;
 }
 
-int get_database_info(struct sip_msg* msg, subs_t* subs, int* error_ret)
+int get_database_info(struct sip_msg* msg, subs_t* subs, int* reply_code, str* reply_str)
 {      
        db_key_t query_cols[10];
        db_val_t query_vals[10];
@@ -1087,8 +1063,6 @@ int get_database_info(struct sip_msg* msg, subs_t* subs, int* error_ret)
        str pres_uri, record_route;
        str reason;
 
-       *error_ret= -1;
-
        query_cols[n_query_cols] = "to_user";
        query_vals[n_query_cols].type = DB_STR;
        query_vals[n_query_cols].nul = 0;
@@ -1178,14 +1152,10 @@ int get_database_info(struct sip_msg* msg, subs_t* subs, int* error_ret)
        {
                LM_ERR("No matching subscription dialog found in database\n");
                
-               if (slb.reply(msg, 481, &pu_481_rpl) == -1)
-               {
-                       LM_ERR("sending '481 Subscription does not exist' reply\n");
-                       pa_dbf.free_result(pa_db, result);
-                       return -1;
-               }
                pa_dbf.free_result(pa_db, result);
-               *error_ret= 0;
+               *reply_code= 481;
+               *reply_str= pu_481_rpl;
+
                return -1;
        }
 
@@ -1197,12 +1167,8 @@ int get_database_info(struct sip_msg* msg, subs_t* subs, int* error_ret)
        {
                LM_ERR("wrong sequence number received: %d - stored: %d\n",
                                subs->remote_cseq, remote_cseq);
-
-               if (slb.reply(msg, 400, &pu_400_rpl) == -1)
-                       LM_ERR("sending '400 Bad request' reply\n");
-               else
-                       *error_ret= 0;
-
+               *reply_code= 400;
+               *reply_str= pu_400_rpl;
                pa_dbf.free_result(pa_db, result);
                return -1;
        }
index 219142c..2df2930 100644 (file)
@@ -38,7 +38,6 @@
 #include "event_list.h"
 #include "presence.h"
 
-static str pu_489_rpl  = str_init("Bad Event");
 
 static const char base64digits[] =
 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@@ -95,48 +94,52 @@ int a_to_i (char *s,int len)
        return n;
 }
 
-int reply_bad_event(struct sip_msg * msg)
+int send_error_reply(struct sip_msg* msg, int reply_code, str reply_str)
 {
-       str hdr_append;
-       char buffer[256];
-       int i;
-       pres_ev_t* ev= EvList->events;
-
-       hdr_append.s = buffer;
-       hdr_append.s[0]='\0';
-       hdr_append.len = sprintf(hdr_append.s, "Allow-Events: ");
-       if(hdr_append.len < 0)
+       if(reply_code== 481)
        {
-               LM_ERR("unsuccessful sprintf\n");
-               return -1;
-       }
+               str hdr_append;
+               char buffer[256];
+               int i;
+               pres_ev_t* ev= EvList->events;
 
-       for(i= 0; i< EvList->ev_count; i++)
-       {
-               if(i> 0)
+               hdr_append.s = buffer;
+               hdr_append.s[0]='\0';
+               hdr_append.len = sprintf(hdr_append.s, "Allow-Events: ");
+               if(hdr_append.len < 0)
                {
-                       memcpy(hdr_append.s+ hdr_append.len, ", ", 2);
-                       hdr_append.len+= 2;
-               }       
-               memcpy(hdr_append.s+ hdr_append.len, ev->name.s, ev->name.len );
-               hdr_append.len+= ev->name.len ;
-               ev= ev->next;
-       }
-       memcpy(hdr_append.s+ hdr_append.len, CRLF, CRLF_LEN);
-       hdr_append.len+=  CRLF_LEN;
-       hdr_append.s[hdr_append.len]= '\0';
+                       LM_ERR("unsuccessful sprintf\n");
+                       return -1;
+               }
+
+               for(i= 0; i< EvList->ev_count; i++)
+               {
+                       if(i> 0)
+                       {
+                               memcpy(hdr_append.s+ hdr_append.len, ", ", 2);
+                               hdr_append.len+= 2;
+                       }       
+                       memcpy(hdr_append.s+ hdr_append.len, ev->name.s, ev->name.len );
+                       hdr_append.len+= ev->name.len ;
+                       ev= ev->next;
+               }
+               memcpy(hdr_append.s+ hdr_append.len, CRLF, CRLF_LEN);
+               hdr_append.len+=  CRLF_LEN;
+               hdr_append.s[hdr_append.len]= '\0';
                
-       if (add_lump_rpl( msg, hdr_append.s, hdr_append.len, LUMP_RPL_HDR)==0 )
-       {
-               LM_ERR("unable to add lump_rl\n");
-               return -1;
+               if (add_lump_rpl( msg, hdr_append.s, hdr_append.len, LUMP_RPL_HDR)==0 )
+               {
+                       LM_ERR("unable to add lump_rl\n");
+                       return -1;
+               }
        }
 
-       if (slb.reply(msg, 489, &pu_489_rpl) == -1)
+       if (slb.reply(msg, reply_code, &reply_str) == -1)
        {
-               LM_ERR("sending '489 Bad Event' reply\n");
+               LM_ERR("sending %d %.*s reply\n", reply_code, reply_str.len, reply_str.s);
                return -1;
        }
        return 0;
+
 }
 
index 4db92ed..01ce61f 100644 (file)
@@ -157,7 +157,8 @@ static inline str* get_local_contact(struct sip_msg* msg)
 int a_to_i (char *s,int len);
 
 void to64frombits(unsigned char *out, const unsigned char *in, int inlen);
-int reply_bad_event(struct sip_msg * msg);
+
+int send_error_reply(struct sip_msg* msg, int reply_code, str reply_str);
 
 #endif