src/tap-bridge/model/tap-creator.cc
author Vedran Miletić <rivanvx@gmail.com>
Sat, 01 Sep 2012 20:57:21 +0200
changeset 9063 32755d0516f4
parent 7256 b04ba6772f8c
permissions -rw-r--r--
Bug 1237 - code cleanups related to includes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     2
/*
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     3
 * Copyright (c) 2009 University of Washington
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     4
 *
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License version 2 as
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     7
 * published by the Free Software Foundation;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     8
 *
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    12
 * GNU General Public License for more details.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    13
 *
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
 */
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
#include <unistd.h>
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
#include <stdint.h>
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
#include <string>
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7256
diff changeset
    22
#include <cstring> // for strerror
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
#include <iostream>
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
#include <iomanip>
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
#include <sstream>
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7256
diff changeset
    26
#include <cstdlib>
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7256
diff changeset
    27
#include <cerrno>
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
#include <fcntl.h>
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
#include <sys/ioctl.h>
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
#include <sys/types.h>
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
#include <sys/socket.h>
4186
f1d57182d491 tighten up configuration reqs for tap and emu
Craig Dowell <craigdo@ee.washington.edu>
parents: 4179
diff changeset
    32
#include <sys/un.h>
f1d57182d491 tighten up configuration reqs for tap and emu
Craig Dowell <craigdo@ee.washington.edu>
parents: 4179
diff changeset
    33
#include <net/if.h>
f1d57182d491 tighten up configuration reqs for tap and emu
Craig Dowell <craigdo@ee.washington.edu>
parents: 4179
diff changeset
    34
#include <linux/if_tun.h>
f1d57182d491 tighten up configuration reqs for tap and emu
Craig Dowell <craigdo@ee.washington.edu>
parents: 4179
diff changeset
    35
#include <net/route.h>
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
#include <netinet/in.h>
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
#include "tap-encode-decode.h"
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
#define TAP_MAGIC 95549
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
4290
af8a40d5c2cb test out the tap-bridge modes and update the example
Craig Dowell <craigdo@ee.washington.edu>
parents: 4289
diff changeset
    42
static int gVerbose = 0; // Set to true to turn on logging messages.
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
#define LOG(msg) \
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
  if (gVerbose) \
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
    { \
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
      std::cout << __FUNCTION__ << "(): " << msg << std::endl;   \
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
    }
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
#define ABORT(msg, printErrno) \
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
  std::cout << __FILE__ << ": fatal error at line " << __LINE__ << ": " << __FUNCTION__ << "(): " << msg << std::endl; \
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
  if (printErrno) \
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
    { \
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7256
diff changeset
    54
      std::cout << "    errno = " << errno << " (" << std::strerror (errno) << ")" << std::endl; \
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
    } \
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7256
diff changeset
    56
  std::exit (-1); 
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
#define ABORT_IF(cond, msg, printErrno) \
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
  if (cond) \
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
    { \
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7190
diff changeset
    61
      ABORT (msg, printErrno); \
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
    }
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
//
4176
0246ffdff112 cleanup, doxygen, prepare for review
Craig Dowell <craigdo@ee.washington.edu>
parents: 4170
diff changeset
    65
// Lots of the following helper code taken from corresponding functions in src/node.
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
//
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
#define ASCII_DOT (0x2e)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
#define ASCII_ZERO (0x30)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
#define ASCII_a (0x41)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
#define ASCII_z (0x5a)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
#define ASCII_A (0x61)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
#define ASCII_Z (0x7a)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
#define ASCII_COLON (0x3a)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
#define ASCII_ZERO (0x30)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
static char
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
AsciiToLowCase (char c)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
{
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
  if (c >= ASCII_a && c <= ASCII_z) {
7190
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    80
      return c;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    81
    } else if (c >= ASCII_A && c <= ASCII_Z) {
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    82
      return c + (ASCII_a - ASCII_A);
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    83
    } else {
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    84
      return c;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    85
    }
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
}
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
static uint32_t 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
AsciiToIpv4 (const char *address)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
{
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
  uint32_t host = 0;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
  while (true) {
7190
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    93
      uint8_t byte = 0;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    94
      while (*address != ASCII_DOT &&
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    95
             *address != 0) {
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    96
          byte *= 10;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    97
          byte += *address - ASCII_ZERO;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    98
          address++;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
    99
        }
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   100
      host <<= 8;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   101
      host |= byte;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   102
      if (*address == 0) {
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   103
          break;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   104
        }
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
      address++;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
    }
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
  return host;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
}
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
static void 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
AsciiToMac48 (const char *str, uint8_t addr[6])
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
{
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
  int i = 0;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
  while (*str != 0 && i < 6) 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
    {
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
      uint8_t byte = 0;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
      while (*str != ASCII_COLON && *str != 0) 
7190
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   118
        {
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   119
          byte <<= 4;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   120
          char low = AsciiToLowCase (*str);
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   121
          if (low >= ASCII_a)
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   122
            {
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   123
              byte |= low - ASCII_a + 10;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   124
            }
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   125
          else
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   126
            {
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   127
              byte |= low - ASCII_ZERO;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   128
            }
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   129
          str++;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   130
        }
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
      addr[i] = byte;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   132
      i++;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   133
      if (*str == 0) 
7190
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   134
        {
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   135
          break;
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   136
        }
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   137
      str++;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   138
    }
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   139
}
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   140
4353
596eb8034443 bug 552: build with gcc 4.4
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4290
diff changeset
   141
static sockaddr
596eb8034443 bug 552: build with gcc 4.4
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4290
diff changeset
   142
CreateInetAddress (uint32_t networkOrder)
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   143
{
4353
596eb8034443 bug 552: build with gcc 4.4
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4290
diff changeset
   144
  union {
596eb8034443 bug 552: build with gcc 4.4
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4290
diff changeset
   145
    struct sockaddr any_socket;
596eb8034443 bug 552: build with gcc 4.4
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4290
diff changeset
   146
    struct sockaddr_in si;
596eb8034443 bug 552: build with gcc 4.4
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4290
diff changeset
   147
  } s;
596eb8034443 bug 552: build with gcc 4.4
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4290
diff changeset
   148
  s.si.sin_family = AF_INET;
596eb8034443 bug 552: build with gcc 4.4
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4290
diff changeset
   149
  s.si.sin_port = 0; // unused
596eb8034443 bug 552: build with gcc 4.4
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4290
diff changeset
   150
  s.si.sin_addr.s_addr = htonl (networkOrder);
596eb8034443 bug 552: build with gcc 4.4
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4290
diff changeset
   151
  return s.any_socket;
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   152
}
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
7190
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   154
static void
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
SendSocket (const char *path, int fd)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   156
{
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   157
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   158
  // Open a Unix (local interprocess) socket to call back to the tap bridge
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   160
  LOG ("Create Unix socket");
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   161
  int sock = socket (PF_UNIX, SOCK_DGRAM, 0);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   162
  ABORT_IF (sock == -1, "Unable to open socket", 1);
7190
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   163
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   165
  // We have this string called path, which is really a hex representation
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   166
  // of the endpoint that the tap bridge created.  It used a forward encoding
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   167
  // method (TapBufferToString) to take the sockaddr_un it made and passed 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
  // the resulting string to us.  So we need to take the inverse method
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   169
  // (TapStringToBuffer) and build the same sockaddr_un over here.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   170
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   171
  socklen_t clientAddrLen;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   172
  struct sockaddr_un clientAddr;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   173
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   174
  LOG ("Decode address " << path);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   175
  bool rc = ns3::TapStringToBuffer (path, (uint8_t *)&clientAddr, &clientAddrLen);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   176
  ABORT_IF (rc == false, "Unable to decode path", 0);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   177
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   178
  LOG ("Connect");
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   179
  int status = connect (sock, (struct sockaddr*)&clientAddr, clientAddrLen);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   180
  ABORT_IF (status == -1, "Unable to connect to tap bridge", 1);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   181
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   182
  LOG ("Connected");
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   183
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   184
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   185
  // This is arcane enough that a few words are worthwhile to explain what's 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   186
  // going on here.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   187
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   188
  // The interesting information (the socket FD) is going to go back to the
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   189
  // tap bridge as an integer of ancillary data.  Ancillary data is bits 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   190
  // that are not a part a socket payload (out-of-band data).  We're also 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   191
  // going to send one integer back.  It's just initialized to a magic number
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
  // we use to make sure that the tap bridge is talking to the tap socket 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   193
  // creator and not some other creator process (emu, specifically)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   194
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   195
  // The struct iovec below is part of a scatter-gather list.  It describes a
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
  // buffer.  In this case, it describes a buffer (an integer) containing the
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   197
  // data that we're going to send back to the tap bridge (that magic number).
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   198
  // 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   199
  struct iovec iov;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   200
  uint32_t magic = TAP_MAGIC;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   201
  iov.iov_base = &magic;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   202
  iov.iov_len = sizeof(magic);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   203
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   204
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   205
  // The CMSG macros you'll see below are used to create and access control 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   206
  // messages (which is another name for ancillary data).  The ancillary 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   207
  // data is made up of pairs of struct cmsghdr structures and associated
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   208
  // data arrays.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
  // 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   210
  // First, we're going to allocate a buffer on the stack to contain our 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   211
  // data array (that contains the socket).  Sometimes you'll see this called
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   212
  // an "ancillary element" but the msghdr uses the control message termimology
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   213
  // so we call it "control."
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   214
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   215
  size_t msg_size = sizeof(int);
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7190
diff changeset
   216
  char control[CMSG_SPACE (msg_size)];
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   217
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   218
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   219
  // There is a msghdr that is used to minimize the number of parameters
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   220
  // passed to sendmsg (which we will use to send our ancillary data).  This
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   221
  // structure uses terminology corresponding to control messages, so you'll
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   222
  // see msg_control, which is the pointer to the ancillary data and controllen
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   223
  // which is the size of the ancillary data array.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   224
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   225
  // So, initialize the message header that describes our ancillary/control data
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   226
  // and point it to the control message/ancillary data we just allocated space
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   227
  // for.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   228
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   229
  struct msghdr msg;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   230
  msg.msg_name = 0;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   231
  msg.msg_namelen = 0;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   232
  msg.msg_iov = &iov;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   233
  msg.msg_iovlen = 1;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   234
  msg.msg_control = control;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   235
  msg.msg_controllen = sizeof (control);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   236
  msg.msg_flags = 0;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   237
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   238
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   239
  // A cmsghdr contains a length field that is the length of the header and
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   240
  // the data.  It has a cmsg_level field corresponding to the originating 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   241
  // protocol.  This takes values which are legal levels for getsockopt and
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   242
  // setsockopt (here SOL_SOCKET).  We're going to use the SCM_RIGHTS type of 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   243
  // cmsg, that indicates that the ancillary data array contains access rights 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   244
  // that we are sending back to the tap bridge.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   245
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   246
  // We have to put together the first (and only) cmsghdr that will describe
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   247
  // the whole package we're sending.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   248
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   249
  struct cmsghdr *cmsg;
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7190
diff changeset
   250
  cmsg = CMSG_FIRSTHDR (&msg);
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   251
  cmsg->cmsg_level = SOL_SOCKET;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   252
  cmsg->cmsg_type = SCM_RIGHTS;
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7190
diff changeset
   253
  cmsg->cmsg_len = CMSG_LEN (msg_size);
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   254
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   255
  // We also have to update the controllen in case other stuff is actually
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   256
  // in there we may not be aware of (due to macros).
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   257
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   258
  msg.msg_controllen = cmsg->cmsg_len;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   259
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   260
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   261
  // Finally, we get a pointer to the start of the ancillary data array and
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   262
  // put our file descriptor in.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   263
  //
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7190
diff changeset
   264
  int *fdptr = (int*)(CMSG_DATA (cmsg));
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   265
  *fdptr = fd; // 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   266
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   267
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   268
  // Actually send the file descriptor back to the tap bridge.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   269
  //
7256
b04ba6772f8c rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents: 7190
diff changeset
   270
  ssize_t len = sendmsg (sock, &msg, 0);
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   271
  ABORT_IF (len == -1, "Could not send socket back to tap bridge", 1);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   272
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   273
  LOG ("sendmsg complete");
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   274
}
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   275
7190
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   276
static int
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   277
CreateTap (const char *dev, const char *gw, const char *ip, const char *mac, const char *mode, const char *netmask)
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   278
{
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   279
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   280
  // Creation and management of Tap devices is done via the tun device
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   281
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   282
  int tap = open ("/dev/net/tun", O_RDWR);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   283
  ABORT_IF (tap == -1, "Could not open /dev/net/tun", true);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   284
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   285
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   286
  // Allocate a tap device, making sure that it will not send the tun_pi header.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   287
  // If we provide a null name to the ifr.ifr_name, we tell the kernel to pick
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   288
  // a name for us (i.e., tapn where n = 0..255.
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   289
  //
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   290
  // If the device does not already exist, the system will create one.
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   291
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   292
  struct ifreq ifr;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   293
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   294
  strcpy (ifr.ifr_name, dev);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   295
  int status = ioctl (tap, TUNSETIFF, (void *) &ifr);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   296
  ABORT_IF (status == -1, "Could not allocate tap device", true);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   297
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   298
  std::string tapDeviceName = (char *)ifr.ifr_name;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   299
  LOG ("Allocated TAP device " << tapDeviceName);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   300
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   301
  //
7190
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   302
  // Operating mode "2" corresponds to USE_LOCAL and "3" to USE_BRIDGE mode.
4290
af8a40d5c2cb test out the tap-bridge modes and update the example
Craig Dowell <craigdo@ee.washington.edu>
parents: 4289
diff changeset
   303
  // This means that we expect that the user will have named, created and 
af8a40d5c2cb test out the tap-bridge modes and update the example
Craig Dowell <craigdo@ee.washington.edu>
parents: 4289
diff changeset
   304
  // configured a network tap that we are just going to use.  So don't mess 
af8a40d5c2cb test out the tap-bridge modes and update the example
Craig Dowell <craigdo@ee.washington.edu>
parents: 4289
diff changeset
   305
  // up his hard work by changing anything, just return the tap fd.
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   306
  //
4289
2f5b0ec50102 initial modifications to tap-bridge
core@samcore1.nettech.boeing.com
parents: 4285
diff changeset
   307
  if (strcmp (mode, "2") == 0 || strcmp (mode, "3") == 0)
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   308
    {
4289
2f5b0ec50102 initial modifications to tap-bridge
core@samcore1.nettech.boeing.com
parents: 4285
diff changeset
   309
      LOG ("Returning precreated tap ");
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   310
      return tap;
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   311
    }
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   312
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   313
  //
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   314
  // Set the hardware (MAC) address of the new device
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   315
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   316
  ifr.ifr_hwaddr.sa_family = 1; // this is ARPHRD_ETHER from if_arp.h
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   317
  AsciiToMac48 (mac, (uint8_t*)ifr.ifr_hwaddr.sa_data);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   318
  status = ioctl (tap, SIOCSIFHWADDR, &ifr);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   319
  ABORT_IF (status == -1, "Could not set MAC address", true);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   320
  LOG ("Set device MAC address to " << mac);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   321
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   322
  int fd = socket (AF_INET, SOCK_DGRAM, 0);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   323
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   324
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   325
  // Bring the interface up.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   326
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   327
  status = ioctl (fd, SIOCGIFFLAGS, &ifr);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   328
  ABORT_IF (status == -1, "Could not get flags for interface", true);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   329
  ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   330
  status = ioctl (fd, SIOCSIFFLAGS, &ifr);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   331
  ABORT_IF (status == -1, "Could not bring interface up", true);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   332
  LOG ("Device is up");
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   333
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   334
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   335
  // Set the IP address of the new interface/device.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   336
  //
4353
596eb8034443 bug 552: build with gcc 4.4
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4290
diff changeset
   337
  ifr.ifr_addr = CreateInetAddress (AsciiToIpv4 (ip));
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   338
  status = ioctl (fd, SIOCSIFADDR, &ifr);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   339
  ABORT_IF (status == -1, "Could not set IP address", true);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   340
  LOG ("Set device IP address to " << ip);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   341
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   342
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   343
  // Set the net mask of the new interface/device
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   344
  //
4353
596eb8034443 bug 552: build with gcc 4.4
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4290
diff changeset
   345
  ifr.ifr_netmask = CreateInetAddress (AsciiToIpv4 (netmask));
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   346
  status = ioctl (fd, SIOCSIFNETMASK, &ifr);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   347
  ABORT_IF (status == -1, "Could not set net mask", true);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   348
  LOG ("Set device Net Mask to " << netmask);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   349
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   350
  return tap;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   351
}
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   352
7190
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   353
int
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   354
main (int argc, char *argv[])
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   355
{
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   356
  int c;
4164
1f6ae48061a9 checkpoint tap bridge
Craig Dowell <craigdo@ee.washington.edu>
parents: 4163
diff changeset
   357
  char *dev = (char *)"";
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   358
  char *gw = NULL;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   359
  char *ip = NULL;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   360
  char *mac = NULL;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   361
  char *netmask = NULL;
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   362
  char *operatingMode = NULL;
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   363
  char *path = NULL;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   364
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   365
  opterr = 0;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   366
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   367
  while ((c = getopt (argc, argv, "vd:g:i:m:n:o:p:")) != -1)
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   368
    {
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   369
      switch (c)
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   370
        {
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   371
        case 'd':
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   372
          dev = optarg;           // name of the new tap device
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   373
          break;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   374
        case 'g':
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   375
          gw = optarg;            // gateway address for the new device
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   376
          break;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   377
        case 'i':
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   378
          ip = optarg;            // ip address of the new device
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   379
          break;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   380
        case 'm':
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   381
          mac = optarg;           // mac address of the new device
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   382
          break;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   383
        case 'n':
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   384
          netmask = optarg;       // net mask for the new device
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   385
          break;
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   386
        case 'o':
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   387
          operatingMode = optarg; // operating mode of tap bridge
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   388
          break;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   389
        case 'p':
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   390
          path = optarg;          // path back to the tap bridge
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   391
          break;
4168
3d8f0d101f79 last connections on tap-bridge, start flowing packets
Craig Dowell <craigdo@ee.washington.edu>
parents: 4164
diff changeset
   392
        case 'v':
3d8f0d101f79 last connections on tap-bridge, start flowing packets
Craig Dowell <craigdo@ee.washington.edu>
parents: 4164
diff changeset
   393
          gVerbose = true;
3d8f0d101f79 last connections on tap-bridge, start flowing packets
Craig Dowell <craigdo@ee.washington.edu>
parents: 4164
diff changeset
   394
          break;
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   395
        }
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   396
    }
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   397
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   398
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   399
  // We have got to be able to coordinate the name of the tap device we are
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   400
  // going to create and or open with the device that an external Linux host
4164
1f6ae48061a9 checkpoint tap bridge
Craig Dowell <craigdo@ee.washington.edu>
parents: 4163
diff changeset
   401
  // will use.  If this name is provided we use it.  If not we let the system
1f6ae48061a9 checkpoint tap bridge
Craig Dowell <craigdo@ee.washington.edu>
parents: 4163
diff changeset
   402
  // create the device for us.  This name is given in dev
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   403
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   404
  LOG ("Provided Device Name is \"" << dev << "\"");
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   405
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   406
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   407
  // We have got to be able to provide a gateway to the external Linux host 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   408
  // so it can talk to the ns-3 network.  This ip address is provided in 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   409
  // gw.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   410
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   411
  ABORT_IF (gw == NULL, "Gateway Address is a required argument", 0);
4164
1f6ae48061a9 checkpoint tap bridge
Craig Dowell <craigdo@ee.washington.edu>
parents: 4163
diff changeset
   412
  LOG ("Provided Gateway Address is \"" << gw << "\"");
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   413
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   414
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   415
  // We have got to be able to assign an IP address to the tap device we are
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   416
  // allocating.  This address is allocated in the simulation and assigned to
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   417
  // the tap bridge.  This address is given in ip.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   418
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   419
  ABORT_IF (ip == NULL, "IP Address is a required argument", 0);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   420
  LOG ("Provided IP Address is \"" << ip << "\"");
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   421
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   422
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   423
  // We have got to be able to assign a Mac address to the tap device we are
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   424
  // allocating.  This address is allocated in the simulation and assigned to
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   425
  // the bridged device.  This allows packets addressed to the bridged device
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   426
  // to appear in the Linux host as if they were received there.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   427
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   428
  ABORT_IF (mac == NULL, "MAC Address is a required argument", 0);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   429
  LOG ("Provided MAC Address is \"" << mac << "\"");
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   430
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   431
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   432
  // We have got to be able to assign a net mask to the tap device we are
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   433
  // allocating.  This mask is allocated in the simulation and given to
7190
c57fc687add6 tap-bridge coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6852
diff changeset
   434
  // the bridged device.
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   435
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   436
  ABORT_IF (netmask == NULL, "Net Mask is a required argument", 0);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   437
  LOG ("Provided Net Mask is \"" << netmask << "\"");
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   438
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   439
  //
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   440
  // We have got to know whether or not to create the TAP.
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   441
  //
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   442
  ABORT_IF (operatingMode == NULL, "Operating Mode is a required argument", 0);
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   443
  LOG ("Provided Operating Mode is \"" << operatingMode << "\"");
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   444
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   445
  //
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   446
  // This program is spawned by a tap bridge running in a simulation.  It
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   447
  // wants to create a socket as described below.  We are going to do the
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   448
  // work here since we're running suid root.  Once we create the socket,
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   449
  // we have to send it back to the tap bridge.  We do that over a Unix
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   450
  // (local interprocess) socket.  The tap bridge created a socket to 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   451
  // listen for our response on, and it is expected to have encoded the address
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   452
  // information as a string and to have passed that string as an argument to
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   453
  // us.  We see it here as the "path" string.  We can't do anything useful 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   454
  // unless we have that string.
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   455
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   456
  ABORT_IF (path == NULL, "path is a required argument", 0);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   457
  LOG ("Provided path is \"" << path << "\"");
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   458
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   459
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   460
  // The whole reason for all of the hoops we went through to call out to this
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   461
  // program will pay off here.  We created this program to run as suid root
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   462
  // in order to keep the main simulation program from having to be run with
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   463
  // root privileges.  We need root privileges to be able to futz with the 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   464
  // Tap device underlying all of this.  So all of these hoops are to allow 
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   465
  // us to exeucte the following code:
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   466
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   467
  LOG ("Creating Tap");
4285
2b197a6b4460 first cut at enabling OpenVZ scenario
Craig Dowell <craigdo@ee.washington.edu>
parents: 4186
diff changeset
   468
  int sock = CreateTap (dev, gw, ip, mac, operatingMode, netmask);
4163
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   469
  ABORT_IF (sock == -1, "main(): Unable to create tap socket", 1);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   470
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   471
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   472
  // Send the socket back to the tap net device so it can go about its business
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   473
  //
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   474
  SendSocket (path, sock);
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   475
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   476
  return 0;
8c48682b3d42 checkpoint tap development
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   477
}