• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 654
  • Last Modified:

C lang-Solaris 8-64-bit Socket Program 128-Network unreachable error

I have a simple C socket program/function that works fine when compiled in 32-bit mode, but when compiled in 64-bit mode and then ran the connect function returns a 128-Network unreachable error all the time.  OS is Solaris 8.

The module is compiled with the command:  cc -xarch=v9 -c tcps.c -lnsl -lsocket
The compiler is Sun Forte
The output of the compile is:
"tcps.c", line 72: warning: implicit function declaration: readn
"tcps.c", line 180: warning: implicit function declaration: inet_addr
"tcps.c", line 207: warning: implicit function declaration: close

file tcps.o
tcps.o:         ELF 64-bit MSB relocatable SPARCV9 Version 1

The program using the tcps.o module is also a 64-bit.

An excerpt of the source with the includes and open socket (connect function is the one failing) is below.

#include <sys/utsname.h>
#include <netinet/in.h>
#include <netdb.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <inet/tcp.h>
#include <errno.h>

#ifndef INADDR_NONE
#define  INADDR_NONE    0xffffffff
#endif

#define MAX_PACKET  1500

#define HOSTNAMELEN     32

int tcp_client_open (char *host, char *service, int port)
{
  int fd;
  int on = 1;
  unsigned long inaddr;
  struct hostent tcp_host_info;
  struct hostent *hp;
  struct servent *sp;
  struct sockaddr_in tcp_srv_addr;
  struct servent tcp_serv_info;
  /*
   * Initialise the address structure for the server
   */
  memset ((char *) &tcp_srv_addr, '\0', sizeof (tcp_srv_addr));
  tcp_srv_addr.sin_family = AF_INET;
  /* determine port info */
  if (service != NULL)
  {
    if ((sp = getservbyname (service, "tcp")) == NULL)
    {
      fprintf (stderr, "Specified Service Undefined: %s\n", service);
      return (-1);
    }
    tcp_serv_info = *sp;
    if (port > 0)
    {
      tcp_srv_addr.sin_port = htons (port);
    }
    else
    {
      tcp_srv_addr.sin_port = sp->s_port;
    }
  }
  else
  {
    if (port <= 0)
    {
      fprintf (stderr, "tcp_open(): Must specify service or port no.\n");
      return (-1);
    }
    tcp_srv_addr.sin_port = htons (port);
  }
  /* determine host address */
  if ((inaddr = inet_addr (host)) != INADDR_NONE)
  {
    memcpy ((char *) &tcp_srv_addr.sin_addr, (char *) &inaddr, sizeof (inaddr));
    tcp_host_info.h_name = NULL;
  }
  else
  {
    if ((hp = gethostbyname (host)) == NULL)
    {
      fprintf (stderr, "Invalid Host Name: %s\n", host);
      return (-1);
    }
    tcp_host_info = *hp;
    memcpy ((char *) &tcp_srv_addr.sin_addr, hp->h_addr, hp->h_length);
  }
  /* connect to host/port */
  if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
  {
    fprintf (stderr, "Socket Open Failed: %d-(%i)%s\n",fd,errno,strerror(errno));
    return (-1);
  }
  if(connect (fd, (struct sockaddr *) &tcp_srv_addr, sizeof (tcp_srv_addr)) < 0)
  {
    fprintf (stderr, "Connection failed-(%i) %s\n",errno,strerror(errno));
    close (fd);
    return (-1);
  }
  return (fd);
}

Any help or suggestions would be great.
0
mjwest042400
Asked:
mjwest042400
  • 2
1 Solution
 
omarfaridCommented:
Hi,

check if your os supports 64 bits applications:

use isainfo -kv

Please see the link http://osdir.com/ml/solaris.managers.summaries/2002-04/msg00225.html

0
 
mjwest042400Author Commented:
Yes it does, the same program uses the 64-bit Oracle client and that part of the application is Ok as well.

isainfo -kv
64-bit sparcv9 kernel modules
0
 
Brian UtterbackPrinciple Software EngineerCommented:
Your problem is that you do not have all of the include files necessary. You need to pay heed
to compiler warnings, that's why they are there, to warn you.

You do not have the include file  <arpa/inet.h> which is needed for the inet_addr function.  The compiler
takes the default calling sequence since it does not know what the proper prototype is for the function.
In 32 bit mode the default happens to match the actual calling sequence, but in 64 bit it does not. This
means that when it returns a -1 indicating an error, the test against INADDR_NONE fails, and you end
up using the address ff.ff.ff.ff  for the destination. So when the connect call is made it rejects the 255.255.255.255
address with a network unreachable.
0
 
mjwest042400Author Commented:
Adding the arpa/inet.h include resolved the compiler warning, inet_addr still was not working properly though.  I did however end up removing the inet_addr usage as it was not strictly needed.  Thank you.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now