root/rat/trunk/net.c @ 1990

Revision 1990, 5.4 KB (checked in by ucaccsp, 16 years ago)

Fix compilation on Irix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * FILE:    net.c
3 * PROGRAM: rat - Version 3.0
4 *
5 * Modified by Isidor Kouvelas from NT source.
6 *
7 * $Revision$ $Date$
8 *
9 * Modified by V.J.Hardman 2/3/95 All the network specific routines have been
10 * moved to this module
11 *
12 * Add queues functionality to simplify interface to rest of program
13 *
14 * 21/3/95 Isidor fixed the network select to work for Solaris
15 *
16 * Copyright (c) 1995,1996 University College London
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, is permitted, for non-commercial use only, provided
21 * that the following conditions are met:
22 * 1. Redistributions of source code must retain the above copyright
23 *    notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 *    notice, this list of conditions and the following disclaimer in the
26 *    documentation and/or other materials provided with the distribution.
27 * 3. All advertising materials mentioning features or use of this software
28 *    must display the following acknowledgement:
29 *      This product includes software developed by the Computer Science
30 *      Department at University College London
31 * 4. Neither the name of the University nor of the Department may be used
32 *    to endorse or promote products derived from this software without
33 *    specific prior written permission.
34 * Use of this software for commercial purposes is explicitly forbidden
35 * unless prior written permission is obtained from the authors.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 */
49
50#include "config_unix.h"
51#include "config_win32.h"
52#include "assert.h"
53#include "net_udp.h"
54#include "net.h"
55#include "crypt.h"
56#include "session.h"
57#include "interfaces.h"
58#include "audio.h"
59#include "util.h"
60#include "mbus.h"
61#include "mbus_ui.h"
62#include "mbus_engine.h"
63#include "ui.h"
64
65#ifndef INADDR_NONE
66#define INADDR_NONE 0xffffffff
67#endif
68
69int
70net_write(socket_udp *s, unsigned char *msg, int msglen, int type)
71{
72        unsigned char   *encrypted_msg = NULL;
73
74        assert(type == PACKET_RTP || type == PACKET_RTCP);
75
76        if (Null_Key()) {
77                encrypted_msg = msg;
78        } else {
79                switch (type) {
80                case PACKET_RTP  : encrypted_msg=Encrypt(msg, &msglen);
81                                   break;
82                case PACKET_RTCP : encrypted_msg=Encrypt_Ctrl(msg, &msglen);
83                                   break;
84                }
85        }
86        return udp_send(s, (char *) encrypted_msg, msglen);
87}
88
89#define MAXPACKETSIZE    (1500-28)
90int net_write_iov(socket_udp *s, struct iovec *iov, int len, int type)
91{
92        unsigned char  wrkbuf[MAXPACKETSIZE];
93        unsigned char *cp;
94        unsigned char *ep;
95
96        for (cp = wrkbuf, ep = wrkbuf + MAXPACKETSIZE; --len >= 0; ++iov) {
97                int plen = iov->iov_len;
98                if (cp + plen >= ep) {
99                        errno = E2BIG;
100                        return -1;
101                }
102                memcpy(cp, iov->iov_base, plen);
103                cp += plen;
104        }
105        return net_write(s, wrkbuf, cp - wrkbuf, type);
106}
107
108void 
109network_init(session_struct *sp)
110{
111        sp->rtp_socket  = udp_init(sp->asc_address, sp->rtp_port,  sp->ttl);
112        sp->rtcp_socket = udp_init(sp->asc_address, sp->rtcp_port, sp->ttl);
113}
114
115void
116read_and_enqueue(socket_udp *s, u_int32 cur_time, pckt_queue_struct *queue, int type)
117{
118        unsigned char                   *data_in, *data_out, *tmp_data;
119        int                              read_len;
120        pckt_queue_element_struct       *pckt;
121
122        assert(type == PACKET_RTP || type == PACKET_RTCP);
123        while (1) {
124                data_in  = block_alloc(PACKET_LENGTH);
125                data_out = block_alloc(PACKET_LENGTH);
126                read_len = udp_recv(s, (char *) data_in, PACKET_LENGTH);
127                if (read_len > 0) {
128                        if (Null_Key()) {
129                                tmp_data      = data_out;
130                                data_out      = data_in;
131                                data_in       = tmp_data;
132                        } else {
133                                switch (type) {
134                                        case PACKET_RTP:  Decrypt(data_in, data_out, &read_len);
135                                                          break;
136                                        case PACKET_RTCP: Decrypt_Ctrl(data_in, data_out, &read_len);
137                                                          break;
138                                }
139                        }
140                        pckt = (pckt_queue_element_struct *) block_alloc(sizeof(pckt_queue_element_struct));
141                        pckt->len               = read_len;
142                        pckt->pckt_ptr          = data_out;
143                        pckt->next_pckt_ptr     = NULL;
144                        pckt->prev_pckt_ptr     = NULL;
145                        pckt->arrival_timestamp = cur_time;
146                        put_on_pckt_queue(pckt, queue);
147                        block_free(data_in, PACKET_LENGTH);
148                } else {
149                        block_free(data_in, PACKET_LENGTH);
150                        block_free(data_out, PACKET_LENGTH);
151                        return;
152                }
153        }
154}
155
156void network_process_mbus(session_struct *sp)
157{
158        /* Process outstanding Mbus messages. */
159        int     rc;
160
161        do {
162                mbus_send(sp->mbus_ui_base);
163                rc  = mbus_recv(sp->mbus_engine_base, (void *) sp);
164                mbus_send(sp->mbus_engine_base);
165                rc |= mbus_recv(sp->mbus_ui_base    , (void *) sp);
166                if (sp->mbus_channel != 0) {
167                        mbus_send(sp->mbus_ui_conf);
168                        rc |= mbus_recv(sp->mbus_engine_conf, (void *) sp);
169                        mbus_send(sp->mbus_engine_conf);
170                        rc |= mbus_recv(sp->mbus_ui_conf    , (void *) sp);
171                }
172        } while (rc);
173}
Note: See TracBrowser for help on using the browser.