Topic: Unable to create valid client side socket file descriptor

On page 129 of the WOLFSSL manual is the following:

Before the SSL_connect() can be issued, the user must supply wolfSSL with a valid socket file descriptor, sockfd in the example above. sockfd is typically the result of the TCP function socket() which is later established using TCP connect(). The following creates a valid client side socket descriptor for use with a local wolfSSL server on port 11111, error handling is omitted for simplicity.
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(11111);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
connect(sockfd, (const sockaddr*)&servaddr, sizeof(servaddr));

How do you issue a connect  without the server IP address? The code above supplies an  IP address of “127.0.0.1”. Well, what if you only have the common name of the  server? Is there a routine to get the ip address so that it can be plugged into inet_addr(“ “)?

I have been getting a return of -1 from connect and I can’t figure out what is going wrong.

Thanks

Share

2 (edited by Kaleb J. Himes 2016-10-27 14:36:41)

Re: Unable to create valid client side socket file descriptor

Hi will,


To get the IP of a server is quite simple and there are many online "checkers" if you just need to get the number once for example:
http://ipinfo.info/html/ip_checker.php

Or from a command line you can use the "dig" utility, just execute this command:

dig +short www.google.com

If your program needs to re-determine the IP at run time then please try the following:

#define BAD_HOST -1

/* in your function ... */

  struct hostent     *hostInfo;
  struct sockaddr_in  serveraddr;
  int                 socket;
  int ret;

  const char hostname[] = "www.google.com";

  /* convert to IP */
  if ( (hostInfo = gethostbyname(hostname) ) == NULL ) {
      return BAD_HOST;
  }

  memcpy(&server.sin_addr, hostInfo->h_addr_list[0], hostInfo->h_length);
  server.sin_family = AF_INET;
  server.sin_port = htons(443);

  /* Now do the call to "connect" */

Lastly you stated you are getting a return of negative 1. Is there a server running on your computer that is listening at 127.0.0.1 that you are trying to connect to? If there is no server listening then yes I would expect the client to get a failed connection every time.


Regards,

Kaleb