Modification of opening socket

2007-06-11
2013-04-22
  • With the actual code, this impossible to open a socket with just this IP address, you must put the dns address.
    I have modify this part for opening ip or dns address.
    You can use my code for the project

    In nanohttp-socket.c :
    <code>
    herror_t
    hsocket_open(hsocket_t * dsock, const char *hostname, int port, int ssl)
    {
      struct sockaddr_in serverSockAddr;
      struct hostent *serverHostEnt;
      unsigned long    hostAddr;
      char *ip;

    /* Initialise a zero serverSockAddr */
      bzero((void *)&serverSockAddr,sizeof(serverSockAddr));
      /* Converti l'adresse ip en entier long */
      hostAddr = inet_addr(hostname);
      if ((long)hostAddr != (long)-1)
      {
        bcopy((void *)&hostAddr,(void *)&serverSockAddr.sin_addr,sizeof(hostAddr));
      }
      else /* Si on a donné un nom ou une adresse DNS */
      {
        serverHostEnt = gethostbyname(hostname);
        if (serverHostEnt == NULL)
           return herror_new("hsocket_open", HSOCKET_ERROR_GET_HOSTNAME,
                          "Socket error get hostname(%s)", strerror(errno));
        
        bcopy((void *)serverHostEnt->h_addr,(void *)&serverSockAddr.sin_addr,serverHostEnt->h_length);
      }

      /* set server addresss */
      serverSockAddr.sin_family = AF_INET;
      serverSockAddr.sin_port = htons((unsigned short) port);

    /* Creation de la socket et retourne un descripteur de fichier
      sur lequel on pourra lire les flux lié à la socket */
      if ((dsock->sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        return herror_new("hsocket_open", HSOCKET_ERROR_CREATE,
                          "Socket error create(%s)", strerror(errno));

      log_verbose4("Opening %s://%s:%i", ssl ? "https" : "http", hostname, port);

      /* connect to the server */
      if (connect(dsock->sock, (struct sockaddr *) &serverSockAddr, sizeof(serverSockAddr)) !=
          0)
        return herror_new("hsocket_open", HSOCKET_ERROR_CONNECT,
                          "Socket error (%s)", strerror(errno));

      if (ssl)
      {
        herror_t status;

        if ((status = hssl_client_ssl(dsock)) != H_OK)
        {
          log_error2("hssl_client_ssl failed (%s)", herror_message(status));
          return status;
        }
      }

      return H_OK;
    }
    </code>