Пример #1
0
void parent_process(int sock1, int sock2)
{
	int fd;
	char str[SIZE];
	int size, i;

	fd = open("./readme", O_RDWR);
	if(fd == -1) {
		perror("open th \"readme\" failed\n");
		exit(1);
	}

printf("The following is printed in parent 1st time\n");
    size = read(fd, &str[0], SIZE);
	for(i = 0; i < size; i++)
		printf("%c", str[i]);
	printf("\n");
printf("Parent finished, try to pass fd to child 1\n");
		
    if(ancil_send_fd(sock1, fd)) {
	  perror("ancil_send_fd");
	  exit(1);
    } else {
	  printf("Sent fd.\n\n");
    }
    sleep(5);

	wait(NULL); //Wait the finish of child 1. 
printf("\n\nThe following is printed in parent 2nd time\n");
    size = read(fd, &str[0], SIZE);
	for(i = 0; i < size; i++)
		printf("%c", str[i]);
	printf("\n");
  
    // Send fd to child 2 now. 
    if(ancil_send_fd(sock1, fd)) {
	  perror("ancil_send_fd");
	  exit(1);
    } else {
	  printf("Sent fd.\n\n");
    }
	sleep(5);

printf("\n\nThe following is printed in parent 3rd time\n");
    size = read(fd, &str[0], SIZE);
	for(i = 0; i < size; i++)
		printf("%c", str[i]);
	printf("\n");
	close(fd);
	printf("Parent finished\n");
}
Пример #2
0
void sfera_webserver::master_listener::_handleAccept(const b_sys::error_code &ec)
{
    if (ec) {
        BOOST_LOG(g_logger) << "accept error";
        _startAsyncAccept();
        return;
    }

    if (_checkWorkers()) {
        std::terminate();
    };

    auto remoteEndpoint =_acceptedSocket->remote_endpoint();
    BOOST_LOG(g_logger) << "accepted connection from " << remoteEndpoint.address().to_string() << ":"
        << remoteEndpoint.port();

    int newConnectionFd = _acceptedSocket->native_handle();

    auto worker = _workers.begin();
    std::advance(worker, rand() % _workers.size());
    ancil_send_fd(worker->second.communicationChannel[1], newConnectionFd);
    _acceptedSocket->close();

    _startAsyncAccept();
}
Пример #3
0
jint Java_com_github_shadowsocks_jnihelper_sendfd(JNIEnv *env, jobject thiz, jint tun_fd, jstring path) {
    int fd;
    struct sockaddr_un addr;
    const char *sock_str  = env->GetStringUTFChars(path, 0);

    if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
        LOGE("socket() failed: %s (socket fd = %d)\n", strerror(errno), fd);
        return (jint)-1;
    }

    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, sock_str, sizeof(addr.sun_path)-1);

    if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
        LOGE("connect() failed: %s (fd = %d)\n", strerror(errno), fd);
        close(fd);
        return (jint)-1;
    }

    if (ancil_send_fd(fd, tun_fd)) {
        LOGE("ancil_send_fd: %s", strerror(errno));
        close(fd);
        return (jint)-1;
    }

    close(fd);
    env->ReleaseStringUTFChars(path, sock_str);
    return 0;
}
Пример #4
0
    int
procket_pipe(PROCKET_STATE *ps)
{
    struct sockaddr_un sa = {0};
    int s = -1;


    (void)memcpy(sa.sun_path, ps->path, sizeof(sa.sun_path)-1);

    sa.sun_family = PF_LOCAL;

    if ( (s = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0)
        return -1;

    if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0)
        return -1;

    if (ancil_send_fd(s, ps->s) < 0)
        return -1;

    if (close(s) < 0)
        return -1;

    return 0;
}
Пример #5
0
jint Java_com_proxy_shadowsocksr_Jni_sendFd
        (JNIEnv *env, jobject thiz, jint tun_fd) {
    int fd;
    struct sockaddr_un addr;

    if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
        return (jint) - 1;
    }

    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, "/data/data/com.proxy.shadowsocksr/sock_path",
            sizeof(addr.sun_path) - 1);

    if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
        close(fd);
        return (jint) - 1;
    }

    if (ancil_send_fd(fd, tun_fd)) {
        close(fd);
        return (jint) - 1;
    }

    close(fd);
    return 0;
}
Пример #6
0
jint Java_com_github_shadowsocks_system_sendfd(JNIEnv *env, jobject thiz, jint tun_fd) {
    int fd;
    struct sockaddr_un addr;

    if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
        LOGE("socket() failed: %s (socket fd = %d)\n", strerror(errno), fd);
        return (jint)-1;
    }

    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, "#shadowsocks_tunfd", sizeof(addr.sun_path)-1);
    addr.sun_path[0] = 0;

    if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
        LOGE("connect() failed: %s (fd = %d)\n", strerror(errno), fd);
        close(fd);
        return (jint)-1;
    }

    if (ancil_send_fd(fd, tun_fd)) {
        LOGE("ancil_send_fd: %s", strerror(errno));
        close(fd);
        return (jint)-1;
    }

    close(fd);
    return 0;
}
Пример #7
0
/**
 * 安卓下 vpn 模式需要处理出 fd
 *
 *
 */
long protect_socket(int fd) {
#ifdef ANDROID
    long l_fd;
    if ((l_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
        return -1;
    struct timeval tv = {1};
    setsockopt(l_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
    setsockopt(l_fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
    
    struct sockaddr_un sin;
    memset(&sin, 0, sizeof(struct sockaddr_un));
    sin.sun_family = AF_UNIX;
    strncpy(sin.sun_path, PROTECTPATH, sizeof(sin.sun_path) - 1);
    
    char b = 0;
    if (connect(l_fd, (struct sockaddr *) &sin, sizeof(struct sockaddr_un)) < 0 || ancil_send_fd(l_fd, fd) || recv(l_fd, &b, 1, 0) < 0) {
        closesocket(l_fd);
        return -1;
    }
    closesocket(l_fd);
    return b;
#else
    return 0;
#endif
}
Пример #8
0
/** report \e fd to Android GUI to be protected (i.e. not to be routed via TUN) */
bool fastd_android_protect_socket(int fd) {
	if (!conf.android_integration) {
		/* rooted/non-GUI mode */
		return true;
	}

	pr_debug("sending fd to protect");
	if (ancil_send_fd(ctx.android_ctrl_sock_fd, fd) == -1) {
		exit_errno("could not send handle to Android for protecting");
	}

	char buf[20];
	if (read(ctx.android_ctrl_sock_fd, buf, sizeof(buf)) == -1) {
		exit_errno("read ack");
	}
	return buf[0] == PROTECT_OK;
}
Пример #9
0
int protect_socket(int fd)
{
    int sock;
    struct sockaddr_un addr;

    if ( (sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
        LOGE("[android] socket() failed: %s (socket fd = %d)\n", strerror(errno), sock);
        return -1;
    }

    // Set timeout to 1s
    struct timeval tv;
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
    setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));

    const char path[] = "/data/data/com.github.shadowsocks/protect_path";

    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);

    if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
        LOGE("[android] connect() failed: %s (socket fd = %d)\n", strerror(errno), sock);
        close(sock);
        return -1;
    }

    if (ancil_send_fd(sock, fd)) {
        ERROR("[android] ancil_send_fd");
        close(sock);
        return -1;
    }

    char ret = 0;

    if (recv(sock, &ret, 1, 0) == -1) {
        ERROR("[android] recv");
        close(sock);
        return -1;
    }

    close(sock);
    return ret;
}
Пример #10
0
void parent_process(int sock)
{
    int fds[2] = { 1, 2 };

    if(ancil_send_fd(sock, 1)) {
	perror("ancil_send_fd");
	exit(1);
    } else {
	printf("Sent fd.\n");
    }
    sleep(1);

    if(ancil_send_fds(sock, fds, 2)) {
	perror("ancil_send_fds");
	exit(1);
    } else {
	printf("Sent two fds.\n");
    }
}
Пример #11
0
void parent_process(int sock)
{
	int fd;
	char str[25];
	int size, i;
   	int child;

	child = fork(); 
	switch(child) {
	case 0:
	    break;
	case -1:
	    perror("fork");
	    exit(1);
	default:
	    wait(NULL);
	    break;
    }

	/* child process. */	
	fd = open("./API", O_RDWR);
	if(fd == -1) {
		perror("open th API failed\n");
		exit(1);
	}
printf("The following is printed in parent\n");
    size = read(fd, &str[0], 25);
	for(i = 0; i < size; i++)
		printf("%c", str[i]);
	printf("\n");
	printf("Parent finished\n");
		
    if(ancil_send_fd(sock, fd)) {
	  perror("ancil_send_fd");
	  exit(1);
    } else {
	  printf("Sent fd.\n");
    }
}