C:我可以使用libssh转发端口以供外部应用程序使用吗? [英] C: can I forward port for external application with libssh?

查看:124
本文介绍了C:我可以使用libssh转发端口以供外部应用程序使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序实现Xen VM的VNC连接.为了进行连接,我必须转发端口,因为XenServer仅接受本地连接.我这样做是这样的:

I'm implementing VNC connection for xen VM in my app. In order to connect I have to forward the port as XenServer accept only local connection. I do it like this:

ssh -L 5903:localhost:5903 root@192.168.1.4

之后,可以使用相应的端口将我的VNC连接到localhost.但是我必须经常重新连接到不同的主机,并且使用bash也不是一个好主意,因为我也有Windows构建.并非总是可以安装ssh-client.
我阅读了 http://api.libssh.org/stable/libssh_tutor_forwarding.html 并尝试了进行测试.

After it a can connect my VNC to localhost with corresponding port. But I have to frequently reconnect to different hosts, and using bash is not a good idea as I have a windows build also. Installing ssh-client is not always possible.
I read http://api.libssh.org/stable/libssh_tutor_forwarding.html and tried to test it.

 ssh_channel forwarding_channel;        
    forwarding_channel = ssh_channel_new(session);    
    int rc = channel_open_forward(forwarding_channel,
                              "192.168.1.4", 5903,
                              "localhost", 5903);
    if (rc != SSH_OK)
    {
        ssh_channel_free(forwarding_channel);
        return rc;
    }    
    for(;;)
    {
        usleep(100000);
    }       

根据状态创建隧道本身.但是我看不到通过netstat监听的端口.我在做错什么,有可能吗?

The tunnel itself is created, according to status. But I can see no ports listening via netstat. What I'm doing wrong and is it possible at all?

更新:

这里的结果代码似乎可以使用libssh正常工作

Here is resulted code that seems to work properly using libssh

int32_t forward_port (ssh_session session, char *remote_host, int32_t remote_port, int32_t local_port)
{   
    int32_t server_sock = 0;    
    int32_t client_sock = -1;
    struct sockaddr_in client_name;
    socklen_t client_name_len = sizeof client_name;
    char buf[4096] = {0};
    server_sock = server_startup(local_port);
    client_sock = accept(server_sock,
                         (struct sockaddr *)&client_name,
                         &client_name_len);
    if (client_sock == -1)
    {
        perror("Error on accept");
        return SSH_ERROR;
    }
    int32_t client_port = ntohs(client_name.sin_port);  
    int32_t size_recv, nwritten, nread = 0;
    uint8_t data[4096];
    fcntl(client_sock, F_SETFL, O_NONBLOCK);
    /*  */
    ssh_channel forwarding_channel;
    forwarding_channel = ssh_channel_new(session);
    int rc = channel_open_forward(forwarding_channel,
                                  remote_host, remote_port,
                                  "127.0.0.1", client_port);
    if (rc != SSH_OK)
    {
        ssh_channel_free(forwarding_channel);
        close(client_sock);
        close(server_sock);
        return rc;
    }
    while(!ssh_channel_is_eof (forwarding_channel)) 
    {
        if((size_recv = recv(client_sock, data, sizeof data, MSG_DONTWAIT) ) < 0)
        {
            if((nread = ssh_channel_read_nonblocking(forwarding_channel, data, sizeof data, 0))>0)
            {
                if(write(client_sock,data,nread)<0)
                {                   
                    perror("Error writing to socket");
                    close(client_sock);
                    close(server_sock);
                    ssh_channel_free(forwarding_channel);
                    return SSH_ERROR;
                }               
            }

        }       
        else if (!size_recv)
        {
            puts("Local client disconnected, exiting");
            goto exit;  
        }
        nwritten = channel_write(forwarding_channel, data, size_recv);
            if (size_recv != nwritten)
            {
                ssh_channel_free(forwarding_channel);
                return SSH_ERROR;
            }
    }
exit:
    close(client_sock);
    close(server_sock);
    ssh_channel_free(forwarding_channel);
    return SSH_OK;
}

推荐答案

ssh_channel_open_forward文档

ssh_channel_open_forward documentation

## Warning ##
This function does not bind the local port and does not automatically forward the content of a socket to the channel. You still have to use channel_read and channel_write for this. 

这篇关于C:我可以使用libssh转发端口以供外部应用程序使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆