int mcp_tcp_client_connect (struct ppp_mcp_sock *peer)
{
	unsigned int ifindex = 0;

	if(peer->fd > 0)
	{
		close(peer->fd);
		peer->fd = -1;
	}
	
	/* Make socket for the peer. */
	peer->fd = sockunion_socket (&peer->su);

	if (peer->fd < 0)
		return -1;

	/* If we can get socket for the peer, adjest TTL and make connection. */
	sockopt_ttl (peer->su.sa.sa_family, peer->fd, 512);

	sockopt_reuseaddr (peer->fd);
	sockopt_reuseport (peer->fd);

	PPPD_DEBUG_TCP("%s [Event] Connect start to %s fd %d",
	            peer->hostname, peer->hostname, peer->fd);

	/* Connect to the remote peer. */
	return sockunion_connect (peer->fd, &peer->su, htons(peer->port), ifindex);
}
/* BGP try to connect to the peer.  */
int
bgp_connect (struct peer *peer)
{
  unsigned int ifindex = 0;

  /* Make socket for the peer. */
  peer->fd = sockunion_socket (&peer->su);
  if (peer->fd < 0)
    return -1;

  set_nonblocking (peer->fd);

  /* Set socket send buffer size */
  bgp_update_sock_send_buffer_size(peer->fd);

  bgp_set_socket_ttl (peer, peer->fd);

  sockopt_reuseaddr (peer->fd);
  sockopt_reuseport (peer->fd);
  
#ifdef IPTOS_PREC_INTERNETCONTROL
  if (bgpd_privs.change (ZPRIVS_RAISE))
    zlog_err ("%s: could not raise privs", __func__);
  if (sockunion_family (&peer->su) == AF_INET)
    setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
# ifdef HAVE_IPV6
  else if (sockunion_family (&peer->su) == AF_INET6)
    setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL);
# endif
  if (bgpd_privs.change (ZPRIVS_LOWER))
    zlog_err ("%s: could not lower privs", __func__);
#endif

  if (peer->password)
    bgp_md5_set_connect (peer->fd, &peer->su, peer->password);

  /* Bind socket. */
  bgp_bind (peer);

  /* Update source bind. */
  bgp_update_source (peer);

#ifdef HAVE_IPV6
  if (peer->ifname)
    ifindex = if_nametoindex (peer->ifname);
#endif /* HAVE_IPV6 */

  if (BGP_DEBUG (events, EVENTS))
    plog_debug (peer->log, "%s [Event] Connect start to %s fd %d",
	       peer->host, peer->host, peer->fd);

  /* Connect to the remote peer. */
  return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
}
/* BGP try to connect to the peer.  */
int
bgp_connect (struct peer *peer)
{
  unsigned int ifindex = 0;

  /* Make socket for the peer. */
  peer->fd = sockunion_socket (&peer->su);
  if (peer->fd < 0)
    return -1;

  /* If we can get socket for the peer, adjest TTL and make connection. */
  if (peer->sort == BGP_PEER_EBGP) {
    sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
    if (peer->gtsm_hops)
      sockopt_minttl (peer->su.sa.sa_family, peer->fd, MAXTTL + 1 - peer->gtsm_hops);
  }

  sockopt_reuseaddr (peer->fd);
  sockopt_reuseport (peer->fd);
  
#ifdef IPTOS_PREC_INTERNETCONTROL
  if (bgpd_privs.change (ZPRIVS_RAISE))
    zlog_err ("%s: could not raise privs", __func__);
  if (sockunion_family (&peer->su) == AF_INET)
    setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
# ifdef HAVE_IPV6
  else if (sockunion_family (&peer->su) == AF_INET6)
    setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL);
# endif
  if (bgpd_privs.change (ZPRIVS_LOWER))
    zlog_err ("%s: could not lower privs", __func__);
#endif

  if (peer->password)
    bgp_md5_set_connect (peer->fd, &peer->su, peer->password);

  /* Bind socket. */
  bgp_bind (peer);

  /* Update source bind. */
  bgp_update_source (peer);

#ifdef HAVE_IPV6
  if (peer->ifname)
    ifindex = if_nametoindex (peer->ifname);
#endif /* HAVE_IPV6 */

  if (BGP_DEBUG (events, EVENTS))
    plog_debug (peer->log, "%s [Event] Connect start to %s fd %d",
	       peer->host, peer->host, peer->fd);

  /* Connect to the remote peer. */
  return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
}
Exemple #4
0
/* BGP try to connect to the peer.  */
int
bgp_connect (struct peer *peer)
{
  unsigned int ifindex = 0;

  /* Make socket for the peer. */
  peer->fd = sockunion_socket (&peer->su);
  if (peer->fd < 0)
    return -1;

  /* If we can get socket for the peer, adjest TTL and make connection. */
  if (peer_sort (peer) == BGP_PEER_EBGP)
    sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);

  sockopt_reuseaddr (peer->fd);
  sockopt_reuseport (peer->fd);

  /* Bind socket. */
  bgp_bind (peer);

  /* Update source bind. */
  bgp_update_source (peer);

#ifdef HAVE_IPV6
  if (peer->ifname)
    ifindex = if_nametoindex (peer->ifname);
#endif /* HAVE_IPV6 */

  if (BGP_DEBUG (events, EVENTS))
    plog_info (peer->log, "%s [Event] Connect start to %s fd %d",
	       peer->host, peer->host, peer->fd);

#ifdef HAVE_TCP_SIGNATURE
  if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSWORD))
    bgp_tcpsig_set (peer->fd, peer);
#endif /* HAVE_TCP_SIGNATURE */

  /* Connect to the remote peer. */
  return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
}
int ppp_client_connection_mcp (struct thread *thread)
{
	struct ppp_mcp_sock *peer;
	//struct listnode *nn, *nm;
	unsigned int ifindex = 0;
	int ret;
	
    peer = THREAD_ARG (thread);
	peer->t_connect_admin = NULL;

    MCP_TIMER_ON (peer->t_connect_admin, ppp_client_connection_mcp, peer, MCP_TRANS_TCP_RECONNECT_ADMIN_TIME);
    
    if(peer->connect_status == CLIENT_CONNECT_SERVER_OK)
        return 0;

    if(peer->fd > 0) {
		close(peer->fd);
		peer->fd = -1;
	}
	
	peer->fd = sockunion_socket (&peer->su);
	if (peer->fd < 0) {
        PPPD_DEBUG_TCP("ppp client connect server peer:%s error", peer->hostname);
		return -1;
    }
    sockopt_reuseaddr (peer->fd);
	sockopt_reuseport (peer->fd);
	ret = sockunion_connect(peer->fd, &peer->su, htons(peer->port), ifindex);
	if(ret == connect_error) {
        PPPD_DEBUG_TCP("client connect server err");
        return -1;
	}
        
	MCP_WRITE_ON (peer->t_check, ppp_tcp_client_connect_check, peer, peer->fd);

	return 0;
}
Exemple #6
0
/* active peer socket setup */
int pim_msdp_sock_connect(struct pim_msdp_peer *mp)
{
	int rc;

	if (PIM_DEBUG_MSDP_INTERNAL) {
		zlog_debug("MSDP peer %s attempt connect%s", mp->key_str,
			   mp->fd < 0 ? "" : "(dup)");
	}

	/* if we have an existing connection we need to kill that one
	 * with this one */
	if (mp->fd >= 0) {
		if (PIM_DEBUG_MSDP_EVENTS) {
			zlog_notice(
				"msdp duplicate connect to %s nuke old connection",
				mp->key_str);
		}
		pim_msdp_peer_stop_tcp_conn(mp, false /* chg_state */);
	}

	/* Make socket for the peer. */
	mp->fd = sockunion_socket(&mp->su_peer);
	if (mp->fd < 0) {
		flog_err_sys(LIB_ERR_SOCKET,
			     "pim_msdp_socket socket failure: %s",
			     safe_strerror(errno));
		return -1;
	}

	if (mp->pim->vrf_id != VRF_DEFAULT) {
		struct interface *ifp =
			if_lookup_by_name(mp->pim->vrf->name, mp->pim->vrf_id);
		if (!ifp) {
			flog_err(LIB_ERR_INTERFACE,
				  "%s: Unable to lookup vrf interface: %s",
				  __PRETTY_FUNCTION__, mp->pim->vrf->name);
			return -1;
		}
		if (pim_socket_bind(mp->fd, ifp)) {
			flog_err_sys(LIB_ERR_SOCKET,
				     "%s: Unable to bind to socket: %s",
				     __PRETTY_FUNCTION__, safe_strerror(errno));
			close(mp->fd);
			mp->fd = -1;
			return -1;
		}
	}

	set_nonblocking(mp->fd);

	/* Set socket send buffer size */
	pim_msdp_update_sock_send_buffer_size(mp->fd);
	sockopt_reuseaddr(mp->fd);
	sockopt_reuseport(mp->fd);

	/* source bind */
	rc = sockunion_bind(mp->fd, &mp->su_local, 0, &mp->su_local);
	if (rc < 0) {
		flog_err_sys(LIB_ERR_SOCKET,
			     "pim_msdp_socket connect bind failure: %s",
			     safe_strerror(errno));
		close(mp->fd);
		mp->fd = -1;
		return rc;
	}

	/* Connect to the remote mp. */
	return (sockunion_connect(mp->fd, &mp->su_peer,
				  htons(PIM_MSDP_TCP_PORT), 0));
}
Exemple #7
0
/* BGP try to connect to the peer. */
int bgp_connect (struct peer *peer)
{
printf("\n BGP CONNECT: I am in BGP Connect\n");
unsigned int ifindex = 0;
/*setting up TLS for a second*/


printf("\n BGP CONNECT: I am just about to initialise SSL\n");

ssl_init();		//initialise the library, method, contact of ssl session, returns nothing

if(BGPTLS.psCTX==NULL)
	{
		printf("\n BGP CONNECT: There is no entry in the .psCTX pointer \n");
	}
else 
	{
		printf("\n BGP CONNECT: There is an entry in the .psCTX pointer, it is %i \n", BGPTLS_sess_server.psCTX);
	}

printf("\n BGP CONNECT: There is an entry in the .psCTX pointer: ");
printf("%i \n", BGPTLS.psCTX);

/*if (SSL_CTX_use_certificate_chain_file(BGPTLS_sess_server.psCTX,"/usr/home/dugald/subcert.pem")!=1)
{
	printf("Error loading certificate from file");
}
else
{
	printf("Certificate has loaded correctly");
} */

/* Make socket for the peer. */
printf("BGP CONNECT: Here's sockets");
peer->fd = sockunion_socket (&peer->su);

SSL_connect(BGPTLS->ssl);


if (peer->fd < 0)
	return -1;


	/* If we can get socket for the peer, adjest TTL and make connection. */
if (peer->sort == BGP_PEER_EBGP) 
{
	sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
	if (peer->gtsm_hops)
		sockopt_minttl (peer->su.sa.sa_family, peer->fd, MAXTTL + 1 - peer->gtsm_hops);
}

sockopt_reuseaddr (peer->fd);
sockopt_reuseport (peer->fd);

#ifdef IPTOS_PREC_INTERNETCONTROL

if (bgpd_privs.change (ZPRIVS_RAISE))
	zlog_err ("%s: could not raise privs", __func__);
if (sockunion_family (&peer->su) == AF_INET)
	setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);

# ifdef HAVE_IPV6
else if (sockunion_family (&peer->su) == AF_INET6)
	setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL);
# endif

if (bgpd_privs.change (ZPRIVS_LOWER))
	zlog_err ("%s: could not lower privs", __func__);
#endif

if (peer->password)
	bgp_md5_set_connect (peer->fd, &peer->su, peer->password);

/* Bind socket. */
//bgp_bind (peer);

/* Update source bind. */
//bgp_update_source (peer);

#ifdef HAVE_IPV6
if (peer->ifname)
	ifindex = if_nametoindex (peer->ifname);
#endif /* HAVE_IPV6 */


if (BGP_DEBUG (events, EVENTS))
	plog_debug (peer->log, "%s [Event] Connect start to %s fd %d", peer->host, peer->host, peer->fd);


/* Connect to the remote peer. */
return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
}