network.c 6.9 KB
Newer Older
Benoit Steiner's avatar
Benoit Steiner committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* VideoLAN VLANbridge: network operations
 *
 * Benoit Steiner, ECP, <benny@via.ecp.fr>
 * Regis Duchesne, ECP, <hpreg@via.ecp.fr>
*/


#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <netdb.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "debug.h"
#include "log.h"
#include "network.h"

#define MOD_NAME MOD_VLANBRIDGE


/***************************************************************************/
/* Initialize a descriptor                                                 */
/***************************************************************************/
void InitDescriptor(struct sockaddr_in* psaDescriptor) 
{
  ASSERT(psaDescriptor);

  /* fill it with zero */
Benoit Steiner's avatar
Benoit Steiner committed
33
  memset(psaDescriptor, 0, sizeof(*psaDescriptor));
Benoit Steiner's avatar
Benoit Steiner committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

  /* set the protocol family */
  psaDescriptor->sin_family = AF_INET;
}


/***************************************************************************/
/* Fill in a descriptor with a given port                                  */
/***************************************************************************/
int SetPort(struct sockaddr_in* psaDescriptor, const char* strPort)
{
  int iPort;
  struct servent* sp;

  ASSERT(psaDescriptor);
  ASSERT(strPort);

  if ((iPort = atoi(strPort)))
  {
    /* It's given by its number */
    psaDescriptor->sin_port = htons(iPort);
  }
  else
  {
    /* It's should be given by its service name */
    sp = getservbyname(strPort, "tcp");
    if (sp == NULL)
    {
      Log(LOG_WARN, MOD_NAME, "Unknown port: %s/tcp", strPort);
      return -1;
    }
    else
      psaDescriptor->sin_port = sp->s_port;
  }
  return 0;
}


/***************************************************************************/
/* Fill in a descriptor with a given address                               */
/***************************************************************************/
int SetHost(struct sockaddr_in* psaDescriptor, const char* strHost)
{
  unsigned long inaddr;
  struct hostent *hp;
        
  if ((inaddr = inet_addr(strHost)) != -1)
  {  
    /* it's dotted-decimal */
Benoit Steiner's avatar
Benoit Steiner committed
83
    memcpy(&psaDescriptor->sin_addr, &inaddr, sizeof(inaddr));
Benoit Steiner's avatar
Benoit Steiner committed
84 85 86 87 88 89 90 91 92 93 94
  }
  else
  {
    /* It should be given by it's host name */
    hp = gethostbyname(strHost);
    if (hp == NULL)
    {
      Log(LOG_WARN, MOD_NAME, "Unknown host: %s", strHost);
      return -1;
    }
    else
Benoit Steiner's avatar
Benoit Steiner committed
95
      memcpy(&psaDescriptor->sin_addr, hp->h_addr, hp->h_length);
Benoit Steiner's avatar
Benoit Steiner committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  }
  return 0;
}

/***************************************************************************/
/* Connect to the network                                                  */
/***************************************************************************/
//int OpenSocket(char *port)
//{
//  struct sockaddr_in local_addr;
//  int fd;
//  int sockopt;

  /* open a socket for UDP/IP */
//  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
//    log(LOG_ARG, "socket() error");

//  sockopt = 1;
//  init_descriptor(&local_addr);
  /* receive on all addresses */
//  local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
//  set_port(&local_addr, port);

  /* allow to re-bind() a busy port */
//  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)) < 0)
//    log(LOG_ARG, "SO_REUSEADDR setsockopt error");

  /* tell the system about the local port and IP address */
//  if (bind(fd, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0)
//      log(LOG_ARG, "bind() error");

//  return fd;
//}


/***************************************************************************/
/* Receive on a connected socket with a timeout                            */
/***************************************************************************/
int RecvReq(int iSockFd, char* strBuff, int iLen, int iTimeout)
{
  fd_set rfds;
  struct timeval tv;
  int iRc;

  ASSERT(strBuff);

  /* Watch the given fd to see when it has input */
  FD_ZERO(&rfds);
  FD_SET(iSockFd, &rfds);
  
  /* Wait up to timeout seconds. */
  tv.tv_sec = iTimeout;
  tv.tv_usec = 0;

  /* Select return with error EINTR when a signal is caught: detect it
     and retry a select when this occurs */
  do
  {
    iRc = select(iSockFd+1, &rfds, NULL, NULL, &tv);

    if (iRc > 0)
    {
      /* Data were received before timeout */
      iRc = recv(iSockFd, strBuff, iLen, 0);
    }
//    else if (iRc == 0)
//    {
      /* Host didn't respond */
//      Log(LOG_WARN, MOD_NAME, "Timeout");
//    }
//    else
//    {
//      /* Select error */
//      Log(LOG_ERROR, MOD_NAME, "Select error: %s", strerror(errno));

//      printf("AOB recv ?: %d\n", recv( iSockFd, strBuff, iLen, MSG_OOB));
//    }
  }
  while (iRc == -1 && errno == EINTR);

  return iRc;
}


/***************************************************************************/
/* Receive a command from a telnet client                                  */
/* We must receive characters one by one in order to support windows       */
/* client which send characters one at a time                              */
/***************************************************************************/
int RecvCmd(int iSockFd, char* strBuff, int iBuffLen, int iTimeout)
{
  struct sockaddr_in saPeerName;
  int iNameLen = sizeof(saPeerName);
  int iPos = 0;
  int iRc = 1;

  ASSERT(strBuff);

  getpeername(iSockFd, &saPeerName, &iNameLen);

  while (iRc > 0)
  {
    /* Recv next char with a 60 seconds timeout */
    iRc = RecvReq(iSockFd, strBuff+iPos, 1, iTimeout);
//    iRc = recv(iSockFd, strBuff+iPos, 1,0);
    
    /* Check reception was ok */
    if (iRc < 0)
    {
      Log(LOG_WARN, MOD_NAME, "Unable to receive data from %s: %s",
          inet_ntoa(saPeerName.sin_addr), strerror(errno));
      break;
    }
    else if (iRc == 0)
    {
       Log(LOG_WARN, MOD_NAME, "Connection with %s lost or timed out",
           inet_ntoa(saPeerName.sin_addr));
       break;
    }
    /* Parse buffer looking for a termination char */
    else if (strBuff[iPos] == '\n' || strBuff[iPos] == '\0')
    {
      /* Add a '\0' char at the end of the buff to be able to deal with it as
         with a string */
      strBuff[iPos+1] = '\0';
      iRc = strlen(strBuff)+1;
      break;
    }
    /* Avoid buffer overflow */
    else if (iPos >= iBuffLen-1)
    {
      Log(LOG_WARN, MOD_NAME, "Message from %s too long: aborting",
          inet_ntoa(saPeerName.sin_addr));      
      iRc = -1;
      break;
    }
    /* Msg not complete: looping */
    else
      iPos++;
  }

  return iRc;
}


/***************************************************************************/
/* Send an answer on a connected socket                                    */
/***************************************************************************/
int SendResult(int iSockFd, char* strMsg, ...)
{
  va_list ap;
  char* strAnswer;
  int iRc = 0;
  
  ASSERT(strMsg);

  va_start(ap, strMsg);
  vasprintf(&strAnswer, strMsg, ap);
  va_end(ap);

  iRc = send(iSockFd, strAnswer, strlen(strAnswer), 0);

  if (iRc < 0)
    Log(LOG_ERROR, MOD_NAME, "Send error: %s", strerror(errno));
  
  return iRc;
}