c - 绑定(bind) : Address family not supported by protocol

标签 c proxy tunnel

此代码适用于我的其他 vps,但不适用于 linode。

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <time.h>
#include <string.h>

#ifdef STRERROR
extern char *sys_errlist[];
extern int sys_nerr;
char *undef = "Undefined error";

char *strerror(error)
int error;
{
    if (error > sys_nerr)
        return undef;
    return sys_errlist[error];
}
#endif
#define CIAO_PS "bfi_2"

main(argc, argv)
    int argc;
    char **argv;
{
    int lsock, csock, osock;
    FILE *cfile;
    char buf[4096];
    struct sockaddr_in laddr, caddr, oaddr;
    int caddrlen = sizeof(caddr);
    fd_set fdsr, fdse;
    struct hostent *h;
    struct servent *s;
    int nbyt;
    unsigned long a;
    unsigned short oport;
    int i, j, argvlen;
    char *bfiargv[argc+1];
    char *fintops = CIAO_PS ;

    if( argc < 4 )
    {
        fprintf(stderr,"Usage: %s localport remoteport remotehost fakeps\n",argv[0]);
        return 30;
    }

    for( i = 0; i < argc; i++ )
    {
        bfiargv[i] = malloc(strlen(argv[i]) + 1);
        strncpy(bfiargv[i], argv[i], strlen(argv[i]) + 1);
    }

    bfiargv[argc] = NULL;
    argvlen = strlen(argv[0]);

    if( argvlen < strlen(CIAO_PS) )
    {
        printf("Se vuoi fregare davvero ps vedi di lanciarmi almeno come superFunkyDataPipe !\n") ;
        abort();
    }

    if(bfiargv[4]) fintops=bfiargv[4] ;
    strncpy(argv[0], fintops, strlen(fintops));

    for( i = strlen(fintops); i < argvlen; i++ )
        argv[0][i] = '\0';

    for( i = 1; i < argc; i++ )
    {
        argvlen = strlen(argv[i]);
        for(j=0; j <= argvlen; j++)
            argv[i][j] = '\0';
    }

    a = inet_addr(argv[3]);

    if( !(h = gethostbyname(bfiargv[3])) && !(h = gethostbyaddr(&a, 4, AF_INET)) )
    {
        perror(bfiargv[3]);
        return 25;
    }

    oport = atol(bfiargv[2]);
    laddr.sin_port = htons((unsigned short)(atol(bfiargv[1])));

    if( (lsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1 )
    {
        perror("socket");
        return 20;
    }

    laddr.sin_family = htons(AF_INET);
    // laddr.sin_addr.s_addr = htonl(0);
    laddr.sin_addr.s_addr = inet_addr("ip address here");

    if( bind(lsock, &laddr, sizeof(laddr)) )
    {
        perror("bind");
        return 20;
    }

    if( listen(lsock, 1) )
    {
        perror("listen");
        return 20;
    }

    if( (nbyt = fork()) == -1 )
    {
        perror("fork");
        return 20;
    }

    if (nbyt > 0)
        return 0;

    setsid();
    while( (csock = accept(lsock, &caddr, &caddrlen)) != -1 )
    {
        cfile = fdopen(csock,"r+");
        if( (nbyt = fork()) == -1 )
        {
            fprintf(cfile, "500 fork: %s\n", strerror(errno));
            shutdown(csock,2);
            fclose(cfile);
            continue;
        }

        if (nbyt == 0)
            goto gotsock;

        fclose(cfile);
        while (waitpid(-1, NULL, WNOHANG) > 0);
    }

    return 20;

gotsock:
    if( (osock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1 )
    {
        fprintf(cfile, "500 socket: %s\n", strerror(errno));
        goto quit1;
    }

    oaddr.sin_family = h->h_addrtype;
    oaddr.sin_port = htons(oport);
    memcpy(&oaddr.sin_addr, h->h_addr, h->h_length);
    if( connect(osock, &oaddr, sizeof(oaddr)) )
    {
        fprintf(cfile, "500 connect: %s\n", strerror(errno));
        goto quit1;
    }

    while( 1 )
    {
        FD_ZERO(&fdsr);
        FD_ZERO(&fdse);
        FD_SET(csock,&fdsr);
        FD_SET(csock,&fdse);
        FD_SET(osock,&fdsr);
        FD_SET(osock,&fdse);

        if( select(20, &fdsr, NULL, &fdse, NULL) == -1 )
        {
            fprintf(cfile, "500 select: %s\n", strerror(errno));
            goto quit2;
        }

        if( FD_ISSET(csock,&fdsr) || FD_ISSET(csock,&fdse) )
        {
            if ((nbyt = read(csock,buf,4096)) <= 0)
                goto quit2;
            if ((write(osock,buf,nbyt)) <= 0)
                goto quit2;
        }
        else if( FD_ISSET(osock,&fdsr) || FD_ISSET(osock,&fdse) )
        {
            if ((nbyt = read(osock,buf,4096)) <= 0)
                goto quit2;
            if ((write(csock,buf,nbyt)) <= 0)
                goto quit2;
        }
    }

quit2:
    shutdown(osock,2);
    close(osock);

quit1:
    fflush(cfile);
    shutdown(csock,2);

quit0:
    fclose(cfile);
    return 0;
}

最佳答案

使用AF_INET代替htons(AF_INET)来初始化sin_family

关于c - 绑定(bind) : Address family not supported by protocol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14860648/

相关文章:

在 Mac 上编译 Unix 代码

mysql - 在C中将MySQL连接保留在函数中的静态变量中(避免全局变量)

c - 按照 Beej 的网络编程指南实现聊天客户端

java - 实现 java.lang.reflect.Proxy 的注释背后的基本原理是什么?

ruby-on-rails - 在显示 iFrame 之前验证用户

python - 如何正确运行 localtunnel v2

android - 如何使用开放 VPN 绕过受阻网络中的 sip 或 rtp voip 流量?

c - 构建 GCC 使 : *** [all] Error 2

ruby-on-rails - Net::HTTP::Proxy 请求...你是怎么做到的?

ruby-on-rails - localhost 应用程序的公共(public) URL,用于 Facebook