Ejemplo n.º 1
0
TcpClientIOPort::Socket::Socket(TcpClientIOPort *ioport)
    : ioport_m(ioport), sockfd_m(-1)
{
    // Reuse permanent socket?
    if(ioport->permanent_m)
    {
        sockfd_m = ioport->permanentSockfd_m;
    }

    // Allocate new socket?
    if(sockfd_m < 0)
    {
        // Allocate a new socket.
        sockfd_m = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd_m >= 0) {
            if (pth_connect(sockfd_m, (const struct sockaddr *)&ioport->addr_m, sizeof (ioport_m->addr_m)) < 0) {
                logger_m.errorStream() << "Unable to connect to server for ioport " << ioport->getID() << endlog;
            } else { 
        	ioport->onConnect(); 
    	    }
        }
        else {
            logger_m.errorStream() << "Unable to create  socket for ioport " << ioport_m->getID() << endlog;
        }
    }    

    // Keep socket?
    if (ioport->permanent_m)
    {
        ioport->permanentSockfd_m = sockfd_m;
    }
}
Ejemplo n.º 2
0
//
// will establish a connection, send the
// packet and block till a response is obtained.
// socket will be closed after the response
// is obtained..
//
// the buffer is used for the sent as well
// as the received data.
//
void send_packet_and_wait_for_response(char* buffer, int send_len, char* server_host_address, int server_port_number)
{
  int sockfd, n;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    {
      fprintf(stderr,"Error: could not open client socket\n");
      return;
    }
  
  server = gethostbyname(server_host_address);
  if (server == NULL) 
    {
      fprintf(stderr, "Error: server host %s not found..\n",server_host_address);
      close(sockfd);
      sockfd = -1;
    }
  else
    {
      bzero((char *) &serv_addr, sizeof(serv_addr));
      serv_addr.sin_family = AF_INET;
      bcopy((char *)server->h_addr,(char *) &serv_addr.sin_addr.s_addr,server->h_length);
      serv_addr.sin_port = htons(server_port_number);

#ifdef DEBUG
      fprintf(stderr, "Info: connecting to server %s on port %d .......... \n",
	      server_host_address,
	      server_port_number);
#endif

      n =-1;

      while(n == -1)
      {
#ifdef USE_GNUPTH
	n=pth_connect(sockfd,(struct sockaddr*) &serv_addr,sizeof(serv_addr));
#else
	n=connect(sockfd,(struct sockaddr*) &serv_addr,sizeof(serv_addr));
#endif
      }
      
#ifdef DEBUG
      fprintf(stderr, "Info: successfully connected to server %s on port %d .......... \n",
	      server_host_address,
	      server_port_number);
#endif


      while(1)
	{
	  if(can_write_to_socket(sockfd))
	    break;

	  __SLEEP__(1000);
	}

#ifdef USE_GNUPTH
      pth_send(sockfd,buffer,send_len,0);
#else
      send(sockfd,buffer,send_len,0);
#endif

#ifdef DEBUG
      char payload[4096];
      int pl = extract_payload(buffer,payload,send_len);
      fprintf(stderr, "Info: sent message %s to server (payload of %d bytes)\n", buffer, pl);
      if(pl > 0)
	print_payload(stderr,payload,8,pl);
#endif
      while( (n = receive_string(sockfd,buffer, MAX_BUF_SIZE)) <= 0)
	{
		__SLEEP__(1000);
	}
	  
#ifdef DEBUG
      fprintf(stderr, "Info: received message %s from server\n", buffer);	  
      print_payload(stderr,buffer,8,n);
#endif
      close(sockfd);
    }
}
Ejemplo n.º 3
0
int
ath_connect (int s, const struct sockaddr *addr, socklen_t length)
{
  return pth_connect (s, addr, length);
}
Ejemplo n.º 4
0
/*!
 * \if DeveloperDocs
 * \brief internal worker function to establishes connection with eibnetmux socket server
 * 
 * \param   hostname        name or ip address and port of eibnetmux server, format hostname:port
 * \param   clientid        user selectable identification of client
 * 
 * \return                  handle, <0: error
 * 
 * error codes:
 *      -1              resource problems
 *      -2              unknown server name
 *      -3              unable to establish connection (server not running, etc.)
 * \endif
 */
static ENMX_HANDLE _enmx_open( int mode, char *hostname, char *myname )
{
    sConnectionInfo         *connInfo;
    sENMX_Server            *server_list;
    sENMX_Server            *server_entry;
    int                     sock_con;
    struct sockaddr_in      server, client;
    struct protoent         *proto_entry;
    struct hostent          *h;
    uint32_t                *addr;
    uint32_t                target_address;
    char                    *target_name = NULL;    // stupid, but keeps compiler happy
    char                    *ptr;
    SOCKET_CMD_HEAD         cmd_head;
    SOCKET_RSP_HEAD         rsp_head;
    int                     ecode;
    
    // library initialised?
    if( enmx_mode != ENMX_LIB_INITIALISED ) {
        return( ENMX_E_NOT_INITIALISED );
    }
    
    // client name must be specified
    if( myname == NULL || strlen( myname ) == 0 ) {
        return( ENMX_E_NOCLIENTID );
    }
    
    // get host's ip address
    //   if no hostname specified, search for eibnetmux servers
    //   if exactly one is found, use it, otherwise return with error
    ptr = NULL;
    if( hostname != NULL ) {
        ptr = strchr( hostname, ':' );
        if( ptr != NULL ) {
            *ptr++ = '\0';
        }
        
        h = gethostbyname( hostname );
        if( !h ) {
            return( ENMX_E_HOST_NOTFOUND );
        }
        
        addr = (uint32_t *)(h->h_addr_list[0]);
        target_address = *addr;
        target_name = strdup( hostname );
    } else {
        target_address = 0;
        server_list = enmx_getservers( 1 );
        for( server_entry = server_list; server_entry != NULL; server_entry = server_entry->next ) {
            if( server_entry->eibnetmux == 1 ) {
                if( target_address != 0 ) {
                    return( ENMX_E_SEARCH );
                }
                target_address = server_entry->ip;
                target_name = strdup( server_entry->hostname );
            } else {
                // all eibnetmux server are first in the list
                break;
            }
        }
        if( target_address == 0 ) {
            return( ENMX_E_SEARCH );
        }
    }
    
    // establish connection with eibnetmux
    proto_entry = getprotobyname( "tcp" );
    sock_con = socket( PF_INET, SOCK_STREAM, proto_entry->p_proto );
    if( sock_con < 0 ) {
        return( ENMX_E_RESOURCE );
    }
    bzero( (void *)&client, sizeof( client ));
    client.sin_family = AF_INET;
    client.sin_addr.s_addr = INADDR_ANY;
    client.sin_port = htons( 0 );
    if( bind( sock_con, (struct sockaddr *)&client, sizeof(struct sockaddr_in) ) == -1 ) {
        close( sock_con );
        return( ENMX_E_RESOURCE );
    }
    bzero( (void *)&server, sizeof( server ));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = target_address;
    if( ptr != NULL ) {
        server.sin_port = htons( atoi( ptr ));
    } else {
        server.sin_port = htons( SOCKET_TCP_PORT );
    }
    
    switch( mode ) {
        case ENMX_MODE_STANDARD:
            if( connect( sock_con, (struct sockaddr *)&server, sizeof( struct sockaddr_in )) != 0 ) {
                close( sock_con );
                return( ENMX_E_SERVER_NOTRUNNING );
            }
            break;
        case ENMX_MODE_PTH:
            if( pth_connect( sock_con, (struct sockaddr *)&server, sizeof( struct sockaddr_in )) != 0 ) {
                close( sock_con );
                return( ENMX_E_SERVER_NOTRUNNING );
            }
            break;
    }
    
    // sanitize name
    if( strlen( myname ) >= SOCKET_NAME_MAX_LENGTH ) {
        myname[SOCKET_NAME_MAX_LENGTH] = '\0';
    }
    
    /*
     * update connection info
     */
    connInfo = malloc( sizeof( sConnectionInfo ));
    connInfo->socket    = sock_con;
    connInfo->errorcode = 0;
    connInfo->next      = enmx_connections;
    connInfo->state     = stateUnused;
    connInfo->hostname  = target_name;      // strdup'ed above
    connInfo->name      = strdup( myname );
    connInfo->mode      = mode;
    connInfo->L7connection = 0;
    connInfo->L7sequence_id = 0;
    switch( mode ) {
        case ENMX_MODE_STANDARD:
            connInfo->send = _enmx_send;
            connInfo->recv = _enmx_receive;
            connInfo->wait = _enmx_wait;
            break;
        case ENMX_MODE_PTH:
            connInfo->send = _enmx_pth_send;
            connInfo->recv = _enmx_pth_receive;
            connInfo->wait = _enmx_pth_wait;
    }
    enmx_connections    = connInfo;
    
    // register clientid
    cmd_head.cmd = SOCKET_CMD_NAME;
    cmd_head.address = htons( strlen( myname ));
    ecode = connInfo->send( sock_con, (unsigned char *)&cmd_head, sizeof( cmd_head ));
    if( ecode < 0 ) {
        enmx_close( sock_con );
        return( ENMX_E_REGISTER_CLIENT );
    }
    
    // append name
    ecode = connInfo->send( sock_con, (unsigned char *)myname, strlen( myname ));
    if( ecode < 0 ) {
        enmx_close( sock_con );
        return( ENMX_E_REGISTER_CLIENT );
    }
    
    // get acknowledgement
    ecode = connInfo->recv( sock_con, (unsigned char *)&rsp_head, sizeof( rsp_head ), 1 );
    if( ecode < 0 ) {
        enmx_close( sock_con );
        return( ENMX_E_REGISTER_CLIENT );
    }
    if( rsp_head.status != SOCKET_STAT_NAME ) {
        enmx_close( sock_con );
        return( ENMX_E_REGISTER_CLIENT );
    }
    
    return( sock_con );
}