~ubuntu-branches/ubuntu/trusty/mpeg4ip/trusty

« back to all changes in this revision

Viewing changes to player/src/ip_port.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mario Limonciello
  • Date: 2008-01-12 15:59:56 UTC
  • Revision ID: james.westby@ubuntu.com-20080112155956-1vznw5njidvrh649
Tags: upstream-1.6dfsg
ImportĀ upstreamĀ versionĀ 1.6dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the Mozilla Public
 
3
 * License Version 1.1 (the "License"); you may not use this file
 
4
 * except in compliance with the License. You may obtain a copy of
 
5
 * the License at http://www.mozilla.org/MPL/
 
6
 * 
 
7
 * Software distributed under the License is distributed on an "AS
 
8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
9
 * implied. See the License for the specific language governing
 
10
 * rights and limitations under the License.
 
11
 * 
 
12
 * The Original Code is MPEG4IP.
 
13
 * 
 
14
 * The Initial Developer of the Original Code is Cisco Systems Inc.
 
15
 * Portions created by Cisco Systems Inc. are
 
16
 * Copyright (C) Cisco Systems Inc. 2000, 2001.  All Rights Reserved.
 
17
 * 
 
18
 * Contributor(s): 
 
19
 *              Bill May        wmay@cisco.com
 
20
 */
 
21
/*
 
22
 * ip_port.cpp - reserve IP port numbers for audio/video RTP streams
 
23
 */
 
24
#include "mpeg4ip.h"
 
25
#include "ip_port.h"
 
26
#include "player_util.h"
 
27
#include "our_config_file.h"
 
28
 
 
29
/*
 
30
 * CIpPort::CIpPort() - create ip socket, bind it to the local address
 
31
 * (which assigns it a port number), and figure out what the port number
 
32
 * is.
 
33
 */
 
34
CIpPort::CIpPort (in_port_t startport, in_port_t maxport)
 
35
{
 
36
  struct sockaddr_in saddr;
 
37
  
 
38
  m_next = NULL;
 
39
 
 
40
  for (m_sock = -1; m_sock == -1 && startport <= maxport; startport++) {
 
41
    // Start with min, go to max
 
42
    m_sock = socket(AF_INET, SOCK_DGRAM, 0);
 
43
    if (m_sock < 0) {
 
44
      return;
 
45
    }
 
46
    saddr.sin_family = AF_INET;
 
47
    saddr.sin_addr.s_addr = INADDR_ANY;
 
48
    saddr.sin_port = htons(startport);
 
49
    if (bind(m_sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
 
50
 
 
51
      startport++;
 
52
      closesocket(m_sock);
 
53
      m_sock = -1;
 
54
    }
 
55
  } 
 
56
 
 
57
  socklen_t socklen;
 
58
  socklen = sizeof(saddr);
 
59
  if (getsockname(m_sock, (struct sockaddr *)&saddr, &socklen) < 0) {
 
60
    closesocket(m_sock);
 
61
    m_sock = -1;
 
62
    return;
 
63
  }
 
64
 
 
65
  m_port_num = ntohs(saddr.sin_port);
 
66
}
 
67
 
 
68
/*
 
69
 * CIpPort::~CIpPort() - close the socket we opened.
 
70
 */
 
71
CIpPort::~CIpPort (void)
 
72
{
 
73
  if (m_sock != -1) {
 
74
    closesocket(m_sock);
 
75
    m_sock = -1;
 
76
  }
 
77
}
 
78
 
 
79
/*
 
80
 * C2ConsecIpPort::C2ConsecIpPort() - get 2 consecutive, even-odd, ip
 
81
 * port numbers
 
82
 */
 
83
C2ConsecIpPort::C2ConsecIpPort (CIpPort **global, in_port_t start_port)
 
84
{
 
85
  CIpPort *newone;
 
86
  m_first = m_second = NULL;
 
87
  in_port_t firstport, maxport;
 
88
 
 
89
  maxport = (in_port_t)~0;
 
90
  if (start_port == 0) {
 
91
    firstport = 1024;
 
92
    if (config.get_config_value(CONFIG_IPPORT_MIN) != UINT32_MAX) {
 
93
      firstport = config.get_config_value(CONFIG_IPPORT_MIN);
 
94
      if (config.get_config_value(CONFIG_IPPORT_MAX) != UINT32_MAX) {
 
95
        maxport = config.get_config_value(CONFIG_IPPORT_MAX);
 
96
        if (maxport <= firstport) {
 
97
          player_error_message("IP port configuration error - %u %u - using 65535 as max port value", 
 
98
                               firstport, maxport);
 
99
          maxport = (in_port_t)~0;
 
100
        }
 
101
      }
 
102
    }
 
103
  } else {
 
104
    firstport = start_port;
 
105
  }
 
106
  while (1) {
 
107
    // first, get an even port number.  If not even, save it in the
 
108
    // global queue.
 
109
    do {
 
110
      newone = new CIpPort(firstport, maxport);
 
111
      if (newone->valid() == 0)
 
112
        return;
 
113
      if ((newone->get_port_num() & 0x1) == 0x1) {
 
114
        newone->set_next(*global);
 
115
        *global = newone;
 
116
        firstport++;
 
117
      }
 
118
    } while ((newone->get_port_num() & 0x1) == 0x1);
 
119
 
 
120
    player_debug_message("First port is %d", newone->get_port_num());
 
121
 
 
122
    // Okay, save the first, get the 2nd.  If okay, just return
 
123
    m_first = newone;
 
124
    in_port_t next;
 
125
    next = m_first->get_port_num() + 1;
 
126
    m_second = new CIpPort(next, next);
 
127
    if ((m_second->valid() == 1) &&
 
128
        (m_second->get_port_num() == next)) {
 
129
      player_debug_message("Ip ports are %u %u", next - 1, next);
 
130
      return;
 
131
    } else {
 
132
      player_debug_message("Got port %d invalid %d", m_second->get_port_num(),
 
133
                           m_second->valid());
 
134
    }
 
135
    // Not okay - save both off in the global queue, and try again...
 
136
    m_first->set_next(*global);
 
137
    *global = m_first;
 
138
    m_first = NULL;
 
139
    firstport = m_second->get_port_num() + 1;
 
140
    m_second->set_next(*global);
 
141
    *global = m_second;
 
142
    m_second = NULL;
 
143
  }
 
144
}
 
145
 
 
146
/*
 
147
 * C2ConsecIpPort::~C2ConsecIpPort() - just delete the CIpPorts that
 
148
 * we've saved.
 
149
 */
 
150
C2ConsecIpPort::~C2ConsecIpPort (void)
 
151
{
 
152
  if (m_second) {
 
153
    delete m_second;
 
154
    m_second = NULL;
 
155
  }
 
156
  if (m_first) {
 
157
    delete m_first;
 
158
    m_first = NULL;
 
159
  }
 
160
 
 
161
}