Blame view

3rdparty/libexosip2-5.3.0/src/jnotify.c 5.4 KB
73ef4ff3   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
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
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
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
  /*
    eXosip - This is the eXtended osip library.
    Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com
  
    eXosip is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
  
    eXosip is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
  
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  
    In addition, as a special exception, the copyright holders give
    permission to link the code of portions of this program with the
    OpenSSL library under certain conditions as described in each
    individual source file, and distribute linked combinations
    including the two.
    You must obey the GNU General Public License in all respects
    for all of the code used other than OpenSSL.  If you modify
    file(s) with this exception, you may extend this exception to your
    version of the file(s), but you are not obligated to do so.  If you
    do not wish to do so, delete this exception statement from your
    version.  If you delete this exception statement from all source
    files in the program, then also delete it here.
  */
  
  #include "eXosip2.h"
  
  #ifndef MINISIZE
  
  osip_transaction_t *_eXosip_find_last_inc_subscribe(eXosip_notify_t *jn, eXosip_dialog_t *jd) {
    osip_transaction_t *inc_tr;
  
    inc_tr = NULL;
  
    if (jd != NULL) {
      osip_list_iterator_t it;
  
      inc_tr = (osip_transaction_t *) osip_list_get_first(jd->d_inc_trs, &it);
  
      while (inc_tr != NULL) {
        if (0 == strcmp(inc_tr->cseq->method, "SUBSCRIBE"))
          break;
  
        else if (0 == strcmp(inc_tr->cseq->method, "REFER"))
          break;
  
        inc_tr = (osip_transaction_t *) osip_list_get_next(&it);
      }
    }
  
    if (inc_tr == NULL)
      return jn->n_inc_tr; /* can be NULL */
  
    return inc_tr;
  }
  
  osip_transaction_t *_eXosip_find_last_out_notify(eXosip_dialog_t *jd) {
    osip_transaction_t *out_tr;
  
    out_tr = NULL;
  
    if (jd != NULL) {
      osip_list_iterator_t it;
  
      out_tr = (osip_transaction_t *) osip_list_get_first(jd->d_out_trs, &it);
  
      while (out_tr != OSIP_SUCCESS) {
        if (0 == strcmp(out_tr->cseq->method, "NOTIFY"))
          return out_tr;
  
        out_tr = (osip_transaction_t *) osip_list_get_next(&it);
      }
    }
  
    return NULL;
  }
  
  int _eXosip_notify_init(struct eXosip_t *excontext, eXosip_notify_t **jn, osip_message_t *inc_subscribe) {
    osip_contact_t *co;
  
    *jn = NULL;
    co = (osip_contact_t *) osip_list_get(&inc_subscribe->contacts, 0);
  
    if (co == NULL || co->url == NULL)
      return -1;
  
    *jn = (eXosip_notify_t *) osip_malloc(sizeof(eXosip_notify_t));
  
    if (*jn == NULL)
      return OSIP_NOMEM;
  
    memset(*jn, 0, sizeof(eXosip_notify_t));
  
  #ifndef MINISIZE
    {
      struct timeval now;
  
      excontext->statistics.allocated_insubscriptions++;
      osip_gettimeofday(&now, NULL);
      _eXosip_counters_update(&excontext->average_insubscriptions, 1, &now);
    }
  #endif
    return OSIP_SUCCESS;
  }
  
  void _eXosip_notify_free(struct eXosip_t *excontext, eXosip_notify_t *jn) {
    /* ... */
  
    eXosip_dialog_t *jd;
  
    if (jn->n_inc_tr != NULL && jn->n_inc_tr->orig_request != NULL && jn->n_inc_tr->orig_request->call_id != NULL && jn->n_inc_tr->orig_request->call_id->number != NULL)
      _eXosip_delete_nonce(excontext, jn->n_inc_tr->orig_request->call_id->number);
  
    else if (jn->n_out_tr != NULL && jn->n_out_tr->orig_request != NULL && jn->n_out_tr->orig_request->call_id != NULL && jn->n_out_tr->orig_request->call_id->number != NULL)
      _eXosip_delete_nonce(excontext, jn->n_out_tr->orig_request->call_id->number);
  
    for (jd = jn->n_dialogs; jd != NULL; jd = jn->n_dialogs) {
      REMOVE_ELEMENT(jn->n_dialogs, jd);
      _eXosip_dialog_free(excontext, jd);
    }
  
    _eXosip_delete_reserved(jn->n_inc_tr);
    _eXosip_delete_reserved(jn->n_out_tr);
  
    if (jn->n_inc_tr != NULL)
      osip_list_add(&excontext->j_transactions, jn->n_inc_tr, 0);
  
    if (jn->n_out_tr != NULL)
      osip_list_add(&excontext->j_transactions, jn->n_out_tr, 0);
  
    osip_free(jn);
  
  #ifndef MINISIZE
    excontext->statistics.allocated_insubscriptions--;
  #endif
  }
  
  int _eXosip_notify_set_refresh_interval(eXosip_notify_t *jn, osip_message_t *inc_subscribe) {
    osip_header_t *exp;
    time_t now;
    int default_expires = 600;
  
    now = osip_getsystemtime(NULL);
  
    if (jn == NULL || inc_subscribe == NULL)
      return -1;
  
    if (MSG_IS_REFER(inc_subscribe))
      default_expires = 120;
  
    osip_message_get_expires(inc_subscribe, 0, &exp);
  
    if (exp == NULL || exp->hvalue == NULL)
      jn->n_ss_expires = now + default_expires;
  
    else {
      jn->n_ss_expires = osip_atoi(exp->hvalue);
  
      if (jn->n_ss_expires != -1)
        jn->n_ss_expires = now + jn->n_ss_expires;
  
      else /* on error, set it to default */
        jn->n_ss_expires = now + default_expires;
    }
  
    return OSIP_SUCCESS;
  }
  
  void _eXosip_notify_add_expires_in_2XX_for_subscribe(eXosip_notify_t *jn, osip_message_t *answer) {
    char tmp[20];
    time_t now;
  
    now = osip_getsystemtime(NULL);
  
    if (jn->n_ss_expires - now < 0) {
      tmp[0] = '0';
      tmp[1] = '\0';
  
    } else {
      snprintf(tmp, 20, "%li", (long) (jn->n_ss_expires - now));
    }
  
    osip_message_set_expires(answer, tmp);
  }
  
  #endif