예제 #1
0
int ifc_reset_connections(const char *ifname, const int reset_mask)
{
#ifdef HAVE_ANDROID_OS
    int result = 0, success;
    in_addr_t myaddr = 0;
    struct ifreq ifr;
    struct in6_ifreq ifr6;
    int ctl_sock = -1;

    if (reset_mask & RESET_IPV4_ADDRESSES) {
        /* IPv4. Clear connections on the IP address. */
        ctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
        if (ctl_sock >= 0) {
            if (!(reset_mask & RESET_IGNORE_INTERFACE_ADDRESS)) {
                ifc_get_info(ifname, &myaddr, NULL, NULL);
            }
            ifc_init_ifr(ifname, &ifr);
            init_sockaddr_in(&ifr.ifr_addr, myaddr);
            result = ioctl(ctl_sock, SIOCKILLADDR,  &ifr);
            close(ctl_sock);
        } else {
            result = -1;
        }
    }

    if (reset_mask & RESET_IPV6_ADDRESSES) {
        /*
         * IPv6. On Linux, when an interface goes down it loses all its IPv6
         * addresses, so we don't know which connections belonged to that interface
         * So we clear all unused IPv6 connections on the device by specifying an
         * empty IPv6 address.
         */
        ctl_sock = socket(AF_INET6, SOCK_DGRAM, 0);
        // This implicitly specifies an address of ::, i.e., kill all IPv6 sockets.
        memset(&ifr6, 0, sizeof(ifr6));
        if (ctl_sock >= 0) {
            success = ioctl(ctl_sock, SIOCKILLADDR,  &ifr6);
            if (result == 0) {
                result = success;
            }
            close(ctl_sock);
        } else {
            result = -1;
        }
    }

    return result;
#else
    return 0;
#endif
}
예제 #2
0
/*
 * add_host_route
 *
 * Add an entry to the routing table through interface "name".
 */
int add_host_route(const char *name, in_addr_t addr)
{
	struct rtentry rt;
	int result;
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);

	memset(&rt, 0, sizeof(rt));

	rt.rt_dst.sa_family = AF_INET;
	rt.rt_flags = RTF_UP | RTF_HOST;
	rt.rt_dev = (void*) name;
	init_sockaddr_in(&rt.rt_dst, addr);
	init_sockaddr_in(&rt.rt_genmask, 0);
	init_sockaddr_in(&rt.rt_gateway, 0);

	result = ioctl(sockfd, SIOCADDRT, &rt);
	if (result < 0 && errno == EEXIST) {
		result = 0;
	}
	
	close(sockfd) ;
	return result;
}
예제 #3
0
int
localhost_socketpair(socket_t sv[2]) {
  socket_t socket_fd = INVALID_SOCKET;
  socket_t client_fd = INVALID_SOCKET;

  socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_fd == INVALID_SOCKET) goto fail;

  const port_t target_port = bind_random_free_listen_port(socket_fd,
                                                          LOCALHOST_IP,
                                                          PRIVATE_PORT_START,
                                                          PRIVATE_PORT_END);
  if (!target_port) goto fail;

  const int ret_listen = listen(socket_fd, 5);
  if (ret_listen) goto fail;

  client_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (client_fd == INVALID_SOCKET) goto fail;

  struct sockaddr_in connect_addr;
  init_sockaddr_in(&connect_addr, LOCALHOST_IP, target_port);

  const int ret_connect =
    connect(client_fd, (struct sockaddr *) &connect_addr,
            sizeof(connect_addr));
  if (ret_connect) goto fail;

  socklen_t filled = sizeof(connect_addr);
  const socket_t ret_accept =
    accept(socket_fd, (struct sockaddr *) &connect_addr, &filled);
  if (ret_accept == INVALID_SOCKET) goto fail;

  sv[0] = ret_accept;
  sv[1] = client_fd;

  closesocket(socket_fd);

  return 0;

 fail:
  // TODO: log if close() fails
  if (socket_fd != INVALID_SOCKET) closesocket(socket_fd);

  // TODO: log if close() fails
  if (client_fd != INVALID_SOCKET) closesocket(client_fd);

  return -1;
}
예제 #4
0
BOOL setaddr(int s, struct ifreq* ifr, const char* addr)
{
    int ret;
    init_sockaddr_in((struct sockaddr_in*) &ifr->ifr_addr, addr);
    RIL_LOG_INFO("setaddr - calling SIOCSIFADDR\r\n");
    errno = 0; // NOERROR
    ret = ioctl(s, SIOCSIFADDR, ifr);
    if (ret < 0)
    {
        RIL_LOG_CRITICAL("setaddr() : SIOCSIFADDR : %s\r\n",
            strerror(errno));
        return FALSE;
    }
    return TRUE;
}
예제 #5
0
/*
 * Removes the default route for the named interface.
 */
int ifc_remove_default_route(const char *ifname)
{
    struct rtentry rt;
    int result;

    ifc_init();
    memset(&rt, 0, sizeof(rt));
    rt.rt_dev = (void *)ifname;
    rt.rt_flags = RTF_UP|RTF_GATEWAY;
    init_sockaddr_in(&rt.rt_dst, 0);
    if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) {
        ALOGD("failed to remove default route for %s: %s", ifname, strerror(errno));
    }
    ifc_close();
    return result;
}
예제 #6
0
/*
 * Class:     es_tid_rocksaw_net_RawSocket
 * Method:    __query_routing_interface
 * Signature: (II[B[B)I
 */
JNIEXPORT jint JNICALL
Java_es_tid_rocksaw_net_RawSocket__1_1query_1routing_1interface
(JNIEnv *env, jclass cls, jint socket, jint family,
 jbyteArray destination, jbyteArray source)
{
  int result = 0;
#if defined(_WIN32)
  struct sockaddr_storage ifc;
  DWORD size;
  struct sockaddr *saddr;
  socklen_t socklen;
  union {
    struct sockaddr_in sin;
    struct sockaddr_in6 sin6;
  } sin;

  if(family == PF_INET) {
    socklen = sizeof(sin.sin);
    saddr = init_sockaddr_in(env, &sin.sin, destination);
  } else if(family == PF_INET6) {
    socklen = sizeof(sin.sin6);
    saddr = init_sockaddr_in6(env, &sin.sin6, destination);
  } else
    return -1;

  result =
    WSAIoctl(socket, SIO_ROUTING_INTERFACE_QUERY, saddr, socklen,
             (struct sockaddr *)&ifc, sizeof(ifc), &size, NULL, NULL);

  if(result != SOCKET_ERROR) {
    jbyte *buf = (*env)->GetByteArrayElements(env, source, NULL);

    if(ifc.ss_family == PF_INET6) {
      memcpy(buf, &((struct sockaddr_in6*)&ifc)->sin6_addr,
             sizeof(struct in6_addr));
    } else {
      memcpy(buf, &((struct sockaddr_in*)&ifc)->sin_addr,
             sizeof(struct in_addr));
    }

    (*env)->ReleaseByteArrayElements(env, source, buf, 0);
  }

#endif
  return result;
}
예제 #7
0
int ifc_reset_connections(const char *ifname)
{
#ifdef HAVE_ANDROID_OS
    int result;
    in_addr_t myaddr;
    struct ifreq ifr;

    ifc_init();
    ifc_get_info(ifname, &myaddr, NULL, NULL);
    ifc_init_ifr(ifname, &ifr);
    init_sockaddr_in(&ifr.ifr_addr, myaddr);
    result = ioctl(ifc_ctl_sock, SIOCKILLADDR,  &ifr);
    ifc_close();
    
    return result;
#else
    return 0;
#endif
}
예제 #8
0
/*
 * Class:     es_tid_rocksaw_net_RawSocket
 * Method:    __bind
 * Signature: (II[B)I
 */
JNIEXPORT jint JNICALL
Java_es_tid_rocksaw_net_RawSocket__1_1bind
(JNIEnv *env, jclass cls, jint socket, jint family, jbyteArray address)
{
  struct sockaddr *saddr;
  socklen_t socklen;
  union {
    struct sockaddr_in sin;
    struct sockaddr_in6 sin6;
  } sin;

  if(family == PF_INET) {
    socklen = sizeof(sin.sin);
    saddr = init_sockaddr_in(env, &sin.sin, address);
  } else if(family == PF_INET6) {
    socklen = sizeof(sin.sin6);
    saddr = init_sockaddr_in6(env, &sin.sin6, address);
  } else
    return -1;

  return bind(socket, saddr, socklen);
}
예제 #9
0
파일: RawSocket.c 프로젝트: aj04/Project_4
/*
 * Class:     com_savarese_rocksaw_net_RawSocket
 * Method:    __sendto
 * Signature: (I[BIII[B)I
 */
JNIEXPORT jint JNICALL
Java_com_savarese_rocksaw_net_RawSocket__1_1sendto
(JNIEnv *env, jclass cls, jint socket,
 jbyteArray data, jint offset, jint len, jint family, jbyteArray address,
 jint scope_id)
{
  int result;
  jbyte *buf;
  union {
    struct sockaddr_in sin;
    struct sockaddr_in6 sin6;
  } sin;
  struct sockaddr *saddr;
  socklen_t socklen;

  if(family == PF_INET) {
    socklen = sizeof(sin.sin);
    saddr = init_sockaddr_in(env, &sin.sin, address);
  } else if(family == PF_INET6) {
    socklen = sizeof(sin.sin6);
    saddr = init_sockaddr_in6(env, &sin.sin6, address, scope_id);
  } else {
    errno = EINVAL;
    return errno;
  }

  buf = (*env)->GetByteArrayElements(env, data, NULL);

  result = sendto(socket, buf+offset, len, 0, saddr, socklen);

  (*env)->ReleaseByteArrayElements(env, data, buf, JNI_ABORT);

#if defined(_WIN32)
  if(result < 0)
    errno = WSAGetLastError();
#endif

  return result;
}
예제 #10
0
파일: netsource.c 프로젝트: jackaudio/jack2
int
main (int argc, char *argv[])
{
    /* Some startup related basics */
    char *client_name, *server_name = NULL, *peer_ip;
    int peer_port = 3000;
    jack_options_t options = JackNullOption;
    jack_status_t status;
#ifdef WIN32
    WSADATA wsa;
    int rc = WSAStartup(MAKEWORD(2, 0), &wsa);
#endif
    /* Torben's famous state variables, aka "the reporting API" ! */
    /* heh ? these are only the copies of them ;)                 */
    int statecopy_connected, statecopy_latency, statecopy_netxruns;
    jack_nframes_t net_period;
    /* Argument parsing stuff */
    extern char *optarg;
    extern int optind, optopt;
    int errflg = 0, c;

    if (argc < 3) {
        printUsage ();
        return 1;
    }

    client_name = (char *) malloc (sizeof (char) * 10);
    peer_ip = (char *) malloc (sizeof (char) * 10);
    sprintf(client_name, "netjack");
    sprintf(peer_ip, "localhost");

    while ((c = getopt (argc, argv, ":h:H:o:i:O:I:n:p:r:B:b:c:m:R:e:N:s:P:")) != -1) {
        switch (c) {
            case 'h':
                printUsage();
                exit (0);
                break;
            case 'H':
                free(peer_ip);
                peer_ip = (char *) malloc (sizeof (char) * strlen (optarg) + 1);
                strcpy (peer_ip, optarg);
                break;
            case 'o':
                playback_channels_audio = atoi (optarg);
                break;
            case 'i':
                capture_channels_audio = atoi (optarg);
                break;
            case 'O':
                playback_channels_midi = atoi (optarg);
                break;
            case 'I':
                capture_channels_midi = atoi (optarg);
                break;
            case 'n':
                latency = atoi (optarg);
                break;
            case 'p':
                peer_port = atoi (optarg);
                break;
            case 'r':
                reply_port = atoi (optarg);
                break;
            case 'B':
                bind_port = atoi (optarg);
                break;
            case 'f':
                factor = atoi (optarg);
                printf("This feature is deprecated and will be removed in future netjack versions. CELT offers a superiour way to conserve bandwidth");
                break;
            case 'b':
                bitdepth = atoi (optarg);
                break;
            case 'c':
#if HAVE_CELT
                bitdepth = 1000;
                factor = atoi (optarg);
#else
                printf( "not built with celt support\n" );
                exit(10);
#endif
                break;
            case 'P':
#if HAVE_OPUS
                bitdepth = 999;
                factor = atoi (optarg);
#else
                printf( "not built with opus support\n" );
                exit(10);
#endif
                break;
            case 'm':
                mtu = atoi (optarg);
                break;
            case 'R':
                redundancy = atoi (optarg);
                break;
            case 'e':
                dont_htonl_floats = 1;
                break;
            case 'N':
                free(client_name);
                client_name = (char *) malloc (sizeof (char) * strlen (optarg) + 1);
                strcpy (client_name, optarg);
                break;
            case 's':
                server_name = (char *) malloc (sizeof (char) * strlen (optarg) + 1);
                strcpy (server_name, optarg);
                options |= JackServerName;
                break;
            case ':':
                fprintf (stderr, "Option -%c requires an operand\n", optopt);
                errflg++;
                break;
            case '?':
                fprintf (stderr, "Unrecognized option: -%c\n", optopt);
                errflg++;
        }
    }
    if (errflg) {
        printUsage ();
        exit (2);
    }

    capture_channels = capture_channels_audio + capture_channels_midi;
    playback_channels = playback_channels_audio + playback_channels_midi;

    outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
    insockfd = socket (AF_INET, SOCK_DGRAM, 0);

    if ((outsockfd == -1) || (insockfd == -1)) {
        fprintf (stderr, "cant open sockets\n" );
        return 1;
    }

    init_sockaddr_in ((struct sockaddr_in *) &destaddr, peer_ip, peer_port);
    if (bind_port) {
        init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, bind_port);
        if( bind (outsockfd, &bindaddr, sizeof (bindaddr)) ) {
            fprintf (stderr, "bind failure\n" );
        }
    }
    if (reply_port) {
        init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, reply_port);
        if( bind (insockfd, &bindaddr, sizeof (bindaddr)) ) {
            fprintf (stderr, "bind failure\n" );
        }
    }

    /* try to become a client of the JACK server */
    client = jack_client_open (client_name, options, &status, server_name);
    if (client == NULL) {
        fprintf (stderr, "jack_client_open() failed, status = 0x%2.0x\n"
                 "Is the JACK server running ?\n", status);
        return 1;
    }

    /* Set up jack callbacks */
    jack_set_process_callback (client, process, 0);
    jack_set_sync_callback (client, sync_cb, 0);
    jack_set_freewheel_callback (client, freewheel_cb, 0);
    jack_on_shutdown (client, jack_shutdown, 0);

    alloc_ports (capture_channels_audio, playback_channels_audio, capture_channels_midi, playback_channels_midi);

    if( bitdepth == 1000 || bitdepth == 999)
        net_period = (factor * jack_get_buffer_size(client) * 1024 / jack_get_sample_rate(client) / 8) & (~1) ;
    else
        net_period = ceilf((float) jack_get_buffer_size (client) / (float) factor);

    int rx_bufsize =  get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
    packcache = packet_cache_new (latency + 50, rx_bufsize, mtu);

    /* tell the JACK server that we are ready to roll */
    if (jack_activate (client)) {
        fprintf (stderr, "Cannot activate client");
        return 1;
    }

    /* Now sleep forever... and evaluate the state_ vars */

    signal( SIGTERM, sigterm_handler );
    signal( SIGINT, sigterm_handler );

    statecopy_connected = 2; // make it report unconnected on start.
    statecopy_latency = state_latency;
    statecopy_netxruns = state_netxruns;

    while ( !quit ) {
#ifdef WIN32
        Sleep (1000);
#else
        sleep(1);
#endif
        if (statecopy_connected != state_connected) {
            statecopy_connected = state_connected;
            if (statecopy_connected) {
                state_netxruns = 1; // We want to reset the netxrun count on each new connection
                printf ("Connected :-)\n");
            } else
                printf ("Not Connected\n");

            fflush(stdout);
        }

        if (statecopy_connected) {
            if (statecopy_netxruns != state_netxruns) {
                statecopy_netxruns = state_netxruns;
                printf ("%s: at frame %06d -> total netxruns %d  (%d%%) queue time= %d\n",
                        client_name,
                        state_currentframe,
                        statecopy_netxruns,
                        100 * statecopy_netxruns / state_currentframe,
                        state_recv_packet_queue_time);

                fflush(stdout);
            }
        } else {
            if (statecopy_latency != state_latency) {
                statecopy_latency = state_latency;
                if (statecopy_latency > 1)
                    printf ("current latency %d\n", statecopy_latency);
                fflush(stdout);
            }
        }
    }

    jack_client_close (client);
    packet_cache_free (packcache);
    exit (0);
}
예제 #11
0
int
client_open(int timeout)
{
    int			sin_size = sizeof (struct sockaddr_in);
    struct sockaddr_in	sin_local;
    struct sockaddr_in	sin_dst;
    int			sock_fd;
    const char	       *dst_host;

    init_sockaddr_in(&sin_local, 0, 0);

    /*
     * create our local socket descriptor.  pay attention:
     *
     * SVR4 STREAMS-based socket implementations check permissions
     * on binds to privileged ports relative to the credentials of
     * the socket creator.  thus, if we want to bind to a privileged
     * port, we should become root before creating the socket.
     */

    if (rlpr_client->proxyhost == 0 && rlpr_client->no_bind == 0) {

	/*
	 * become root, but even if we can't become root (perhaps
	 * because we're not setuid), keep on going anyway, since
	 * some lpd implementations will allow connections from
	 * non-privileged ports.
	 */

	toggle_root();
	if (geteuid() != 0)
	    msg(R_WARNING, 0, "cannot bind to privileged port: lpd may reject");
    }

    sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (sock_fd == -1) {
	if (geteuid() == 0)
	    toggle_root();
	msg(R_ERROR, errno, "socket create for connection to lpd");
	return -1;
    }

    if (geteuid() == 0 && rlpr_client->no_bind == 0) {

	if (bind_try_range(&sin_local, sock_fd, R_LPD_SRC_PORT_LOW,
			   R_LPD_SRC_PORT_HIGH) == 0) {
	    toggle_root();
	    msg(R_FATAL, errno, "privileged bind failed (no ports left?)");
	    return -1;
	}
	toggle_root();
    }

    /*
     * connect to the remote endpoint.  for debugging purposes,
     * grab our local identity too.
     */

    dst_host = rlpr_client->proxyhost ? rlpr_client->proxyhost :
	       rlpr_client->printhost;
    if (init_sockaddr_in(&sin_dst, dst_host, rlpr_client->dst_port) == 0)
	return -1;

    if (connect_timed(sock_fd, &sin_dst, timeout) == -1) {
	msg(R_ERROR, errno, "connect to %s:%hi", dst_host,
	    rlpr_client->dst_port);
	return -1;
    }

    if (getsockname(sock_fd, (struct sockaddr *)&sin_local, &sin_size) == -1) {
	msg(R_ERROR, errno, "getsockname on connection to lpd");
	return -1;
    }

    rlpr_client->src_port = ntohs(sin_local.sin_port);
    msg(R_DEBUG, 0, "connected localhost:%hu -> %s:%hu", rlpr_client->src_port,
	dst_host, rlpr_client->dst_port);

    if (rlpr_client->proxyhost != 0) {

	size_t	printhost_len = strlen(rlpr_client->printhost);

	msg(R_DEBUG, 0, "pointing proxy to %s...", rlpr_client->printhost);

	if (full_write(sock_fd, rlpr_client->printhost, printhost_len, 0)
	    == 0 || full_write(sock_fd, "\n", 1, 0) == 0) {
	    msg(R_ERROR, 0, "unable to send printhost name to proxyhost");
	    return -1;
	}
    }

    return sock_fd;
}