Example #1
0
void handleClient(int pSock, char *pPassword, char *pHWADDR)
{
  slog(LOG_DEBUG_VV, "In Handle Client\n");
  fflush(stdout);

  socklen_t len;
  struct sockaddr_storage addr;
  #ifdef AF_INET6
  unsigned char ipbin[INET6_ADDRSTRLEN];
  #else
  unsigned char ipbin[INET_ADDRSTRLEN];
  #endif
  unsigned int ipbinlen;
  int port;
  char ipstr[64];

  len = sizeof addr;
  getsockname(pSock, (struct sockaddr*)&addr, &len);

  // deal with both IPv4 and IPv6:
  if (addr.ss_family == AF_INET) {
      slog(LOG_DEBUG_V, "Constructing ipv4 address\n");
      struct sockaddr_in *s = (struct sockaddr_in *)&addr;
      port = ntohs(s->sin_port);
      inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr);
      memcpy(ipbin, &s->sin_addr, 4);
      ipbinlen = 4;
  } else { // AF_INET6
      struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr;
      port = ntohs(s->sin6_port);
      inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof ipstr);

      union {
        struct sockaddr_in6 s;
        unsigned char bin[sizeof(struct sockaddr_in6)];
      } addr;
      memcpy(&addr.s, &s->sin6_addr, sizeof(struct sockaddr_in6));

      if(memcmp(&addr.bin[0], "\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\xff\xff", 12) == 0)
      {
        // its ipv4...
        slog(LOG_DEBUG_V, "Constructing ipv4 from ipv6 address\n");
        memcpy(ipbin, &addr.bin[12], 4);
        ipbinlen = 4;
      }
      else
      {
        slog(LOG_DEBUG_V, "Constructing ipv6 address\n");
        memcpy(ipbin, &s->sin6_addr, 16);
        ipbinlen = 16;
      }
  }

  slog(LOG_DEBUG_V, "Peer IP address: %s\n", ipstr);
  slog(LOG_DEBUG_V, "Peer port      : %d\n", port);

  int tMoreDataNeeded = 1;
  struct keyring     tKeys;
  struct comms       tComms;
  struct connection  tConn;
  initConnection(&tConn, &tKeys, &tComms, pSock, pPassword);

  while(1)
  {
    tMoreDataNeeded = 1;

    initBuffer(&tConn.recv, 80); // Just a random, small size to seed the buffer with.
    initBuffer(&tConn.resp, 80);
    
    int tError = FALSE;
    while(1 == tMoreDataNeeded)
    {
      tError = readDataFromClient(pSock, &(tConn.recv));
      if(!tError && strlen(tConn.recv.data) > 0)
      {
        slog(LOG_DEBUG_VV, "Finished Reading some data from client\n");
        // parse client request
        tMoreDataNeeded = parseMessage(&tConn, ipbin, ipbinlen, pHWADDR);
        if(1 == tMoreDataNeeded)
        {
          slog(LOG_DEBUG_VV, "\n\nNeed to read more data\n");
        }
        else if(-1 == tMoreDataNeeded) // Forked process down below ended.
        {
          slog(LOG_DEBUG_V, "Forked Process ended...cleaning up\n");
          cleanup(&tConn);
          // pSock was already closed
          return;
        }
        // if more data needed,
      }
      else
      {
        slog(LOG_DEBUG, "Error reading from socket, closing client\n");
        // Error reading data....quit.
        cleanup(&tConn);
        return;
      }
    }
    slog(LOG_DEBUG_VV, "Writing: %d chars to socket\n", tConn.resp.current);
    //tConn->resp.data[tConn->resp.current-1] = '\0';
    writeDataToClient(pSock, &(tConn.resp));
   // Finished reading one message...
    cleanupBuffers(&tConn);
  }
  cleanup(&tConn);
  fflush(stdout);
}
Example #2
0
static void handleClient(int pSock, char *pPassword, char *pHWADDR)
{
  slog(LOG_DEBUG_VV, "In Handle Client\n");
  fflush(stdout);

  socklen_t len;
  struct sockaddr_storage addr;
  #ifdef AF_INET6
  unsigned char ipbin[INET6_ADDRSTRLEN];
  #else
  unsigned char ipbin[INET_ADDRSTRLEN];
  #endif
  unsigned int ipbinlen;
  int port = 0;
  char ipstr[64];

  len = sizeof addr;
  getsockname(pSock, (struct sockaddr*)&addr, &len);

  // deal with both IPv4 and IPv6:
  if (addr.ss_family == AF_INET) {
      slog(LOG_DEBUG_V, "Constructing ipv4 address\n");
      struct sockaddr_in *s = (struct sockaddr_in *)&addr;
      port = ntohs(s->sin_port);
      inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr);
      memcpy(ipbin, &s->sin_addr, 4);
      ipbinlen = 4;
  }

  slog(LOG_DEBUG_V, "Peer IP address: %s\n", ipstr);
  slog(LOG_DEBUG_V, "Peer port      : %d\n", port);

  int tMoreDataNeeded = 1;
  struct keyring     tKeys;
  struct comms       tComms;
  struct connection  tConn;
  fd_set rfds;
  int max_fd = pSock + 1;
  int session_started = 0;
  struct timeval recv_timeout;
  recv_timeout.tv_sec = GET_CONN_TIMEOUT(session_started);
  recv_timeout.tv_usec = 0;
  int selsock = 0;
  char rbuf[80];
  int rlen;
  unsigned long udp_pkc = 0, udp_pkc_new;
  FD_ZERO(&rfds);
  initConnection(&tConn, &tKeys, &tComms, pSock, pPassword);

  FD_SET(pSock, &rfds);
  while((selsock = select(max_fd, &rfds, 0, 0, &recv_timeout)) != -1)
  {
      if((selsock == 0) && (session_started)){
          if((tComms.in[1] > 2) && (tComms.out[0] > 2)){
              fprintf(stderr,"timeout\n");
              write(tComms.in[1], "status\n", 7);
              rlen = read(tComms.out[0], rbuf, sizeof(rbuf));
              rbuf[rlen] = 0;
              udp_pkc_new = strtoul(rbuf, NULL, 10);
              if(udp_pkc_new == udp_pkc) {
                  fprintf(stderr,"no traffic, disconn\n");
                  fprintf(stderr, "get timeout rtp packet\n");
                  slog(LOG_DEBUG_V, "Forked Process ended...cleaning up\n");
                  cleanup(&tConn);
                  // pSock was already closed
                  return;
              }
              fprintf(stderr,"ok\n");
              udp_pkc = udp_pkc_new;
          }
      } 
      if(FD_ISSET(pSock, &rfds)) {
          tMoreDataNeeded = 1;

          initBuffer(&tConn.recv, 80); // Just a random, small size to seed the buffer with.
          initBuffer(&tConn.resp, 80);

          int tError = FALSE;
          while(1 == tMoreDataNeeded)
          {
              tError = readDataFromClient(pSock, &(tConn.recv));
              if(!tError && strlen(tConn.recv.data) > 0)
              {
                  slog(LOG_DEBUG_VV, "Finished Reading some data from client\n");
                  // parse client request
                  tMoreDataNeeded = parseMessage(&tConn, ipbin, ipbinlen, pHWADDR);
                  if((tComms.in[0] > 0) || (tComms.in[2] > 0) || (tComms.out[0] > 0) || (tComms.out[1] > 0)) {
                      session_started = 1;
                  }
                  if(1 == tMoreDataNeeded)
                  {
                      slog(LOG_DEBUG_VV, "\n\nNeed to read more data\n");
                  }
                  else if(-1 == tMoreDataNeeded) // Forked process down below ended.
                  {
                      slog(LOG_DEBUG_V, "Forked Process ended...cleaning up\n");
                      cleanup(&tConn);
                      // pSock was already closed
                      return;
                  }
                  // if more data needed,
              }
              else
              {
                  slog(LOG_DEBUG, "Error reading from socket, closing client\n");
                  // Error reading data....quit.
                  cleanup(&tConn);
                  return;
              }
          }
          slog(LOG_DEBUG_VV, "Writing: %d chars to socket\n", tConn.resp.current);
          //tConn->resp.data[tConn->resp.current-1] = '\0';
          writeDataToClient(pSock, &(tConn.resp));
          // Finished reading one message...
          cleanupBuffers(&tConn);
      }
      FD_SET(pSock, &rfds);
      recv_timeout.tv_sec = GET_CONN_TIMEOUT(session_started);;
      recv_timeout.tv_usec = 0;
  }
  cleanup(&tConn);
  fflush(stdout);
}