File: functions.h

package info (click to toggle)
openser 1.1.0-9etch1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,828 kB
  • ctags: 11,809
  • sloc: ansic: 120,528; sh: 5,249; yacc: 1,716; makefile: 1,261; php: 656; perl: 205; sql: 190
file content (191 lines) | stat: -rw-r--r-- 5,965 bytes parent folder | download
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
/* $Id: functions.h,v 1.6 2006/02/03 18:04:22 bogdan_iancu Exp $
 *
 * Copyright (C) 2004 Dan Pascu
 * Copyright (C) 2003 Porta Software Ltd
 *
 * This file is part of openser, a free SIP server.
 *
 * openser 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
 *
 * openser 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
 *
 */

static void
pingClients(unsigned int ticks, void *param)
{
    static char pingbuf[4] = "\0\0\0\0";
    static int length = 256;
    struct socket_info* sock;
    struct hostent* hostent;
    union sockaddr_union to;
    unsigned int flags;
    struct sip_uri uri;
    void *buf, *ptr;
    str contact;
    int needed;
    int proto;

    buf = pkg_malloc(length);
    if (buf == NULL) {
        LOG(L_ERR, "error: mediaproxy/pingClients(): out of memory\n");
        return;
    }
    needed = userLocation.get_all_ucontacts(buf, length, FL_NAT);
    if (needed > 0) {
        // make sure we alloc more than actually we were told is missing
        // (some clients may register while we are making these calls)
        length = (length + needed) * 2;
        ptr = pkg_realloc(buf, length);
        if (ptr == NULL) {
            LOG(L_ERR, "error: mediaproxy/pingClients(): out of memory\n");
            pkg_free(buf);
            return;
        } else {
            buf = ptr;
        }
        // try again. we may fail again if _many_ clients register in between
        needed = userLocation.get_all_ucontacts(buf, length, FL_NAT);
        if (needed != 0) {
            pkg_free(buf);
            return;
        }
    }

    ptr = buf;
    while (1) {
        memcpy(&(contact.len), ptr, sizeof(contact.len));
        if (contact.len == 0)
            break;
        contact.s = (char*)ptr + sizeof(contact.len);
        ptr = contact.s + contact.len;
		memcpy( &sock, ptr, sizeof(sock));
		ptr += sizeof(sock);
		memcpy( &flags, ptr, sizeof(flags));
		ptr += sizeof(flags);
        if (parse_uri(contact.s, contact.len, &uri) < 0) {
            LOG(L_ERR, "error: mediaproxy/pingClients(): can't parse contact uri\n");
            continue;
        }
        if (uri.proto != PROTO_UDP && uri.proto != PROTO_NONE)
            continue;
        if (uri.port_no == 0)
            uri.port_no = SIP_PORT;
		proto = uri.proto;
        hostent = sip_resolvehost(&uri.host, &uri.port_no, &proto, 0);
        if (hostent == NULL){
            LOG(L_ERR, "error: mediaproxy/pingClients(): can't resolve host\n");
            continue;
        }
        hostent2su(&to, hostent, 0, uri.port_no);
		if (sock==0) {
			sock = get_send_socket(0, &to, PROTO_UDP);
			if (sock == NULL) {
				LOG(L_ERR, "error: mediaproxy/pingClients(): can't get "
					"sending socket\n");
				continue;
			}
        }
        udp_send(sock, pingbuf, sizeof(pingbuf), &to);
    }
    pkg_free(buf);
}


// Replace IP:Port in Contact field with the source address of the packet.
// Preserve port for SIP asymmetric clients
static int
FixContact(struct sip_msg* msg, char* str1, char* str2)
{
    str beforeHost, after, agent;
    contact_t* contact;
    struct lump* anchor;
    struct sip_uri uri;
    char *newip, *buf;
    int len, newiplen, offset;
    Bool asymmetric;

    if (!getContactURI(msg, &uri, &contact))
        return -1;

    newip = ip_addr2a(&msg->rcv.src_ip);
    newiplen = strlen(newip);

    /* Don't do anything if the IP's are the same. Return success. */
    if (newiplen==uri.host.len && memcmp(uri.host.s, newip, newiplen)==0) {
        return 1;
    }

    if (uri.port.len == 0)
        uri.port.s = uri.host.s + uri.host.len;

    agent = getUserAgent(msg);
    asymmetric = isSIPAsymmetric(agent);

    beforeHost.s   = contact->uri.s;
    beforeHost.len = uri.host.s - contact->uri.s;
    if (asymmetric) {
        // for asymmetrics we preserve the original port
        after.s   = uri.port.s;
        after.len = contact->uri.s + contact->uri.len - after.s;
    } else {
        after.s   = uri.port.s + uri.port.len;
        after.len = contact->uri.s + contact->uri.len - after.s;
    }

    len = beforeHost.len + newiplen + after.len + 20;

    // first try to alloc mem. if we fail we don't want to have the lump
    // deleted and not replaced. at least this way we keep the original.
    buf = pkg_malloc(len);
    if (buf == NULL) {
        LOG(L_ERR, "error: fix_contact(): out of memory\n");
        return -1;
    }

    offset = contact->uri.s - msg->buf;
    anchor = del_lump(msg, offset, contact->uri.len, HDR_CONTACT_F);

    if (!anchor) {
        pkg_free(buf);
        return -1;
    }

    if (asymmetric && uri.port.len==0) {
        len = sprintf(buf, "%.*s%s%.*s", beforeHost.len, beforeHost.s,
                      newip, after.len, after.s);
    } else if (asymmetric) {
        len = sprintf(buf, "%.*s%s:%.*s", beforeHost.len, beforeHost.s,
                      newip, after.len, after.s);
    } else {
        len = sprintf(buf, "%.*s%s:%d%.*s", beforeHost.len, beforeHost.s,
                      newip, msg->rcv.src_port, after.len, after.s);
    }

    if (insert_new_lump_after(anchor, buf, len, HDR_CONTACT_F) == 0) {
        pkg_free(buf);
        return -1;
    }

    contact->uri.s   = buf;
    contact->uri.len = len;

    if (asymmetric) {
        LOG(L_INFO, "info: fix_contact(): preserved port for SIP "
            "asymmetric client: `%.*s'\n", agent.len, agent.s);
    }

    return 1;
}