KernelTcp.cc 3.09 KB
Newer Older
1 2 3
// KernelTcp.cc

#include "lib.h"
4
#include "log.h"
5 6
#include "KernelTcp.h"

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
using namespace std;

void kernelTcp_init(void)
{
  int error = 0;
  // Set up the peerAccept socket
  address.sin_family = AF_INET;
  address.sin_port = htons(SENDER_PORT);
  address.sin_arrd.s_addr = INADDR_ANY;
  error = bind(global::peerAccept,
               reinterpret_cast<struct sockaddr *>(address),
               sizeof(struct sockaddr))
  // Set up the connectionModelExemplar
  // Set up packet capture
}

23 24 25 26 27 28 29
void kernelTcp_addNewPeer(fd_set * readable)
{
  if (global::peerAccept != -1
      && FD_ISSET(global::peerAccept, readable))
  {
    struct sockaddr_in remoteAddress;
    socklen_t addressSize = sizeof(remoteAddress);
30 31 32
    int fd = accept(global::peerAccept,
                    reinterpret_cast<struct sockaddr *>(&remoteAddress),
                    &addressSize);
33 34 35
    if (fd != -1)
    {
      // Add the peer.
36
      int flags = fcntl(fd, F_GETFL);
37 38
      if (flags != -1)
      {
39
        int error = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
40 41 42 43 44 45 46
        if (error != -1)
        {
          global::peers.push_back(
            make_pair(fd, ipToString(remoteAddress.sin_addr.s_addr)));
          addDescriptor(fd);
          logWrite(PEER_CYCLE,
                   "Peer connection %d from %s was accepted normally.",
47 48
                   global::peers.back().first,
                   global::peers.back().second.c_str());
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        }
        else
        {
          logWrite(EXCEPTION, "fctl(F_SETFL) failed: %s", strerror(errno));
          close(fd);
        }
      }
      else
      {
        logWrite(EXCEPTION, "fctl(F_GETFL) failed: %s", strerror(errno));
        close(fd);
      }
    }
    else
    {
      logWrite(EXCEPTION, "accept() called on a peer connection failed: %s",
               strerror(errno));
    }
  }
}

void kernelTcp_readFromPeers(fd_set * readable)
{
72 73
  list< pair<int, string> >::iterator pos = global::peers.begin();
  while (pos != global::peers.end())
74 75 76 77 78 79 80 81 82 83
  {
    if (FD_ISSET(pos->first, readable))
    {
      static const int bufferSize = 8096;
      static char buffer[bufferSize];
      int size = read(pos->first, buffer, bufferSize);
      if (size == 0)
      {
        logWrite(PEER_CYCLE,
                 "Peer connection %d from %s is closing normally.",
84
                 pos->first, pos->second.c_str());
85
        close(pos->first);
86
        list< pair<int, string> >::iterator temp = pos;
87 88 89 90 91 92 93
        ++pos;
        global::peers.erase(temp);
      }
      else if (size == -1 && errno != EAGAIN && errno != EINTR)
      {
        logWrite(EXCEPTION,
                 "Failed to read peer connection %d from %s so "
94
                 "I'm shutting it down: %s", pos->first, pos->second.c_str(),
95 96
                 strerror(errno));
        close(pos->first);
97
        list< pair<int, string> >::iterator temp = pos;
98 99 100 101 102 103 104 105 106 107 108 109 110 111
        ++pos;
        global::peers.erase(temp);
      }
      else
      {
        ++pos;
      }
    }
    else
    {
      ++pos;
    }
  }
}
112 113 114 115

void kernelTcp_packetCapture(fd_set * readable)
{
}