Example #1
0
int xsocket_udp(int type) {

    int s;
#ifdef HASIPV6
    int opt = 1;
#endif

    if (type == XSOCKET_V6) {
#ifdef HASIPV6
        s = socket(PF_INET6, SOCK_DGRAM, 0);
        if (s == -1) return -1;
        if (fcntl(s, F_SETFD, 1) == -1) { close(s); return -1; }
        if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof opt) == -1) { close(s); return -1; }
        blocking_disable(s);
        return s;
#endif
        errno = EPROTONOSUPPORT;
        return -1;
    }

    if (type == XSOCKET_V4) {
        s = socket(PF_INET, SOCK_DGRAM, 0);
        if (s == -1) return -1;
        if (fcntl(s, F_SETFD, 1) == -1) { close(s); return -1; }
        blocking_disable(s);
        return s;
    }

    errno = EPROTO;
    return -1;
}
Example #2
0
int
traceroute_set_proto(struct traceroute *t, const char *cp)
{
	struct protoent *pe;
	int on = 1;

	pe = getprotobyname(cp);
	if (pe) {
		if ((t->s = socket(AF_INET, SOCK_RAW, pe->p_proto)) < 0) {
			return -errno;
		} else if ((t->sndsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
			return -errno;
		}
	} else if (pe == NULL) {
		return -EINVAL;
	}

	/* sndsock options */
	if (t->options & SO_DEBUG)
		(void)setsockopt(t->s, SOL_SOCKET, SO_DEBUG, (char *)&on,
		    sizeof(on));
	if (t->options & SO_DONTROUTE)
		(void)setsockopt(t->s, SOL_SOCKET, SO_DONTROUTE, (char *)&on,
		    sizeof(on));

	/* sndsock options */
	if (t->options & SO_DEBUG)
		(void)setsockopt(t->sndsock, SOL_SOCKET, SO_DEBUG, (char *)&on,
		    sizeof(on));
	if (t->options & SO_DONTROUTE)
		(void)setsockopt(t->sndsock, SOL_SOCKET, SO_DONTROUTE, (char *)&on,
		    sizeof(on));

	return 0;
}
Example #3
0
int server_socket_init(renderd_config *sConfig) {
    struct sockaddr_un addrU;
    struct sockaddr_in6 addrI;
    mode_t old;
    int fd;

    if (sConfig->ipport > 0) {
        syslog(LOG_INFO, "Initialising TCP/IP server socket on %s:%i",
                sConfig->iphostname, sConfig->ipport);
        fd = socket(PF_INET6, SOCK_STREAM, 0);
        if (fd < 0) {
            fprintf(stderr, "failed to create IP socket\n");
            exit(2);
        }
        bzero(&addrI, sizeof(addrI));
        addrI.sin6_family = AF_INET6;
        addrI.sin6_addr = in6addr_any;
        addrI.sin6_port = htons(sConfig->ipport);
        if (bind(fd, (struct sockaddr *) &addrI, sizeof(addrI)) < 0) {
            fprintf(stderr, "socket bind failed for: %s:%i\n",
                    sConfig->iphostname, sConfig->ipport);
            close(fd);
            exit(3);
        }
    } else {
        syslog(LOG_INFO, "Initialising unix server socket on %s",
                sConfig->socketname);

        fd = socket(PF_UNIX, SOCK_STREAM, 0);
        if (fd < 0) {
            fprintf(stderr, "failed to create unix socket\n");
            exit(2);
        }

        bzero(&addrU, sizeof(addrU));
        addrU.sun_family = AF_UNIX;
        strncpy(addrU.sun_path, sConfig->socketname, sizeof(addrU.sun_path) - 1);

        unlink(addrU.sun_path);

        old = umask(0); // Need daemon socket to be writeable by apache
        if (bind(fd, (struct sockaddr *) &addrU, sizeof(addrU)) < 0) {
            fprintf(stderr, "socket bind failed for: %s\n", sConfig->socketname);
            close(fd);
            exit(3);
        }
        umask(old);
    }

    if (listen(fd, QUEUE_MAX) < 0) {
        fprintf(stderr, "socket listen failed for %d\n", QUEUE_MAX);
        close(fd);
        exit(4);
    }

    syslog(LOG_DEBUG, "Created server socket %i", fd);

    return fd;

}
Example #4
0
int
tc_raw_socket_in_init(int type)
{
    int        fd, recv_buf_opt, ret;
    socklen_t  opt_len;

    if (type == COPY_FROM_LINK_LAYER) {
        /* copy ip datagram from Link layer */
        fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
    } else {
        /* copy ip datagram from IP layer */
#if (TCPCOPY_UDP)
        fd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
#else
        fd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
#endif
    }

    if (fd == -1) {
        tc_log_info(LOG_ERR, errno, "Create raw socket to input failed");   
        return TC_INVALID_SOCKET;
    }

    recv_buf_opt = 67108864;
    opt_len = sizeof(int);

    ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &recv_buf_opt, opt_len);
    if (ret == -1) {
        tc_log_info(LOG_ERR, errno, "Set raw socket(%d)'s recv buffer failed");
        return TC_INVALID_SOCKET;
    }

    return fd;
}
bool Network::isConnected() const
{
    if (socket())
        return socket()->state() != QAbstractSocket::UnconnectedState;
    else
        return false;
}
Example #6
0
CBTConnection::CBTConnection(void)
{
    WSADATA wsaData;

    CollectionSize = 0;
    m_bInited = false;

    sockaddr_in Control, Report;

    Control.sin_family      = AF_INET;
    Control.sin_addr.s_addr = inet_addr("127.0.0.1");
    Control.sin_port        = htons(ControlPort);

    Report.sin_family      = AF_INET;
    Report.sin_addr.s_addr = inet_addr("127.0.0.1");
    Report.sin_port        = htons(ReportPort);

    for (int Index = 0; Index < 4; Index++)
    {
        memset(&(m_padState    [Index]), 0, sizeof(XINPUT_STATE));
        memset(&(m_padVibration[Index]), 0, sizeof(XINPUT_VIBRATION));
        memset(&(m_Extended    [Index]), 0, sizeof(SCP_EXTN));
    }

    if (WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR)
    {
        if ((m_Control = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
        {
            WSACleanup();
            return;
        }

        if (connect(m_Control, (SOCKADDR*) &Control, sizeof(Control)) == SOCKET_ERROR)
        {
            closesocket(m_Control);

            WSACleanup();
            return;
        }

        if ((m_Report = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
        {
            closesocket(m_Control);

            WSACleanup();
            return;
        }

        if (bind(m_Report, (SOCKADDR*) &Report, sizeof(Report)) == SOCKET_ERROR)
        {
            closesocket(m_Control);
            closesocket(m_Report);

            WSACleanup();
            return;
        }
    }

    m_bInited = true;
}
Example #7
0
ATF_TC_BODY(connect, tc)
{
	struct sockaddr_un sun;
	/* ATF's isolation mechanisms will guarantee uniqueness of this file */
	const char *path = "sock";
	int s, r, err, l, s2;

	s = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
	ATF_REQUIRE(s >= 0);

	bzero(&sun, sizeof(sun));
	sun.sun_family = AF_LOCAL;
	sun.sun_len = sizeof(sun);
	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
	r = bind(s, (struct sockaddr *)&sun, sizeof(sun));
	l = listen(s, -1);
	ATF_CHECK_EQ(0, r);
	ATF_CHECK_EQ(0, l);

	/* Create the other socket */
	s2 = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
	ATF_REQUIRE(s2 >= 0);
	err = connect(s2, (struct sockaddr*)&sun, sizeof(sun));
	if (err != 0) {
		perror("connect");
		atf_tc_fail("connect(2) failed");
	}
}
Example #8
0
int main(int argc, char** argv){
    if(argc == 1){//server case
        int socket_ofclient, listener;
        struct sockaddr_in addr;

        char buf[1024];
        int readres;
        listener = socket(AF_INET, SOCK_STREAM, 0);

        if(listener < 0) {
            perror("failded to create socket");
            return (1);
        }

        addr.sin_family = AF_INET;
        addr.sin_port = htons(1111);
        addr.sin_addr.s_addr = htonl(INADDR_ANY);

        if(bind(listener, (struct sockaddr*) &addr, sizeof(addr)) < 0){
            perror("can't bind");
            return (1);
        }

        listen(listener, 1);

        socket_ofclient = accept(listener, NULL, NULL);
        if(socket_ofclient < 0){
            perror("can't make connection");
            return (1);
        }

        int readed = read(socket_ofclient, buf, 1024);
        write(socket_ofclient, "got:\n", 5);
        write(socket_ofclient, buf, readed);
    }else{
        int sock;
        sock = socket(AF_INET, SOCK_STREAM, 0);
        if(sock < 0){
            perror("can't create sock");
            return (1);
        }

        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(1111);
        addr.sin_addr.s_addr = inet_aton(argv[1]);//htonl();
        //addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        
        if(connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0){
            perror("can't connect");
            return (1);
        }

        write(sock, "abc\n",4);
        char buf[1024];
        int got = read(sock, buf, 1024);

        write(1, buf, got);
    }
}
Example #9
0
File: test.c Project: ftc1718/Camp
int main(void)
{
//	int sockfd;
	int fds[2];
	socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
	socket(AF_UNIX, SOCK_STREAM, 0);
	if(fork() == 0)
	{
		int sockfd;
		close(fds[1]);
		recv_sockfd(fds[0], &sockfd);
		printf("%d in child\n", sockfd);
		exit(0);
	}
	else
	{
		int sockfd;
		close(fds[0]);
		signal(17, my_handle);
		sockfd = socket(AF_INET, SOCK_STREAM, 0);
		printf("%d in parent\n", sockfd);
		send_sockfd(fds[1], sockfd);
	}
	return 0;
}
Example #10
0
/// if host=0, creates a server otherwise creates a client (and automatically connects). Returns >=0 on success
int net_wiznet_createsocket(JsNetwork *net, unsigned long host, unsigned short port) {
  NOT_USED(net);
  int sckt = -1;
  if (host!=0) { // ------------------------------------------------- host (=client)

    //mgg1010 - added random source port - seems to solve problem of repeated GET failing
    
    sckt = socket(net_wiznet_getFreeSocket(), Sn_MR_TCP, (uint16_t)((rand() & 32767) + 2000), 0); // we set nonblocking later
     
    if (sckt<0) return sckt; // error

    int res = connect((uint8_t)sckt,(uint8_t*)&host, port);
    // now we set nonblocking - so that connect waited for the connection
    uint8_t ctl = SOCK_IO_NONBLOCK;
    ctlsocket((uint8_t)sckt, CS_SET_IOMODE, &ctl);

    if (res == SOCKET_ERROR) {
     jsError("Connect failed (err %d)\n", res );
    }
  } else { // ------------------------------------------------- no host (=server)
    sckt = socket(net_wiznet_getFreeSocket(), Sn_MR_TCP, port, SF_IO_NONBLOCK);
    listen((uint8_t)sckt);
  }
  wiznetSocketPorts[sckt&7] = port;
  //jsiConsolePrintf("Created socket %d\n", sckt);
  return sckt;
}
Example #11
0
static void do_client(char * addr)
{
    int s1;
    int s2;
    struct sockaddr_in saddr;

    /* connect to the server twice */
    bzero(&saddr, sizeof(saddr));
    saddr.sin_family = AF_INET;
    saddr.sin_port   = htons(20001);

    if( inet_pton(AF_INET, addr, &saddr.sin_addr) <= 0 ) {
        printf("Error invalid address (%s)\n", addr);
        return;
    }

    if( (s1 = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
        perror("Unable to create socket:");
    if( (s2 = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
        perror("Unable to create socket:");

    if( connect(s1, (struct sockaddr *) &saddr, sizeof(saddr)) < 0 )
        perror("First connect to failed\n");
    else if( connect(s2, (struct sockaddr *) &saddr, sizeof(saddr)) < 0 )
        perror("Second connect to failed\n");

    close(s1);
    close(s2);
}
Example #12
0
ne_socket *ne_sock_connect(const ne_inet_addr *addr, unsigned int portnum)
{
    int fd;
    int len, val;

#ifdef USE_GETADDRINFO
    /* use SOCK_STREAM rather than ai_socktype: some getaddrinfo
     * implementations do not set ai_socktype, e.g. RHL6.2. */
    fd = socket(addr->ai_family, SOCK_STREAM, addr->ai_protocol);
#else
    fd = socket(AF_INET, SOCK_STREAM, 0);
#endif
    if (fd < 0)
	return NULL;

    val = 1;
   if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &val, sizeof(val)) < 0){
       	perror("setsockopt() :");
	return -1;
   }


    if (raw_connect(fd, addr, ntohs(portnum))) {
	ne_close(fd);
	return NULL;
    }

    return create_sock(fd);
}
Example #13
0
static int listen_address(const char* str) {
	union {
		struct sockaddr_in in;
		struct sockaddr_un un;
	} addr;
	int fd;
	socklen_t length;
	if (!parse_inet_address(str, &addr.in)) {
		fill_un_addr(&addr.un, str);
		fd = socket(AF_UNIX, SOCK_STREAM, 0);
		length = sizeof(addr.un);
		if (addr.un.sun_path[0] != '\0') {
			unlink(addr.un.sun_path);
		}
	} else {
		length = sizeof(addr.in);
		fd = socket(AF_INET, SOCK_STREAM, 0);
		int one = 1;
		setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
	}
	if (bind(fd, (struct sockaddr*)&addr, length) != 0) {
		log_error("failed to bind address %s, %s", str, strerror(errno));
		return -1;
	}
	listen(fd, 10);
	return fd;
}
Example #14
0
bool Server::startServer() {
	sockAddrInfo.sin_family = AF_INET;
#ifdef _WIN32
	sockAddrInfo.sin_addr.S_un.S_addr = INADDR_ANY;
#else
	sockAddrInfo.sin_addr.s_addr = INADDR_ANY;
#endif
	sockAddrInfo.sin_port = htons(port);

	if(isTCP)
		serverSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	else
		serverSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	
	if(serverSock == INVALID_SOCKET)
		return false;

	if(bind(serverSock, (sockaddr *)&sockAddrInfo, sizeof(sockAddrInfo)) != 0)
		return false;
	
	if(isTCP)
		if(listen(serverSock, 128) != 0) //I don't care about backlog
			return false;

	isServerStopped = false;

	if(!isBlocking)
		return startClientHandler();
	
	return true;

}
int main(int argc, char *argv[])
{
	// 윈속 초기화
	WSADATA wsa;
	if(WSAStartup(MAKEWORD(2,2), &wsa) != 0)
		return 1;
	MessageBox(NULL, "윈속 초기화 성공", "알림", MB_OK);

	// socket()
	SOCKET tcp_sock = socket(AF_INET, SOCK_STREAM, 0);
	SOCKET udp_sock = socket(AF_INET, SOCK_DGRAM, 0);

	//print
	printf("tcp sockt num : %d \n", tcp_sock);
	printf("udp sockt num : %d \n", udp_sock);

	if(tcp_sock == INVALID_SOCKET) err_quit("socket()");
	MessageBox(NULL, "TCP 소켓 생성 성공", "알림", MB_OK);
	if(udp_sock == INVALID_SOCKET) err_quit("socket()");
	MessageBox(NULL, "UPD 소켓 생성 성공", "알림", MB_OK);
	
	// closesocket()
	closesocket(tcp_sock);
	closesocket(udp_sock);
	

	// 윈속 종료
	WSACleanup();
	return 0;
}
Example #16
0
File: core.c Project: dvv/libuv
/* Open a socket in non-blocking close-on-exec mode, atomically if possible. */
int uv__socket(int domain, int type, int protocol) {
  int sockfd;

#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
  sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);

  if (sockfd != -1)
    goto out;

  if (errno != EINVAL)
    goto out;
#endif

  sockfd = socket(domain, type, protocol);

  if (sockfd == -1)
    goto out;

  if (uv__nonblock(sockfd, 1) || uv__cloexec(sockfd, 1)) {
    close(sockfd);
    sockfd = -1;
  }

out:
  return sockfd;
}
Example #17
0
		int32_t SocketInit(int &sockDesc)
		{
			#ifdef __GNUWIN32__
			WSADATA wsaData;
			if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
			{
				return 0;
			}
			#endif

			#ifdef __NDS__
			if ( !Wifi_InitDefault(WFC_CONNECT) )
			{
				return 0;
			}
			if ( (sockDesc = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
			{
				return 0;
			}
			#elif defined (__GAMECUBE__) || defined (__WII__)
			if ( (sockDesc = net_socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 )
			{
				return 0;
			}
			#else
			if ( (sockDesc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 )
			{
				return 0;
			}
			#endif
			return 1;
		}
Example #18
0
// Read the packet from the client
void AuthSocket::OnRead()
{
    uint8 _cmd;
    while (1)
    {
        if (!socket().recv_soft((char *)&_cmd, 1))
            return;

        size_t i;

        // Circle through known commands and call the correct command handler
        for (i = 0; i < AUTH_TOTAL_COMMANDS; ++i)
        {
            if ((uint8)table[i].cmd == _cmd && (table[i].status == STATUS_CONNECTED || (_authed && table[i].status == STATUS_AUTHED)))
            {
                sLog->outStaticDebug("[Auth] got data for cmd %u recv length %u", (uint32)_cmd, (uint32)socket().recv_len());

                if (!(*this.*table[i].handler)())
                {
                    sLog->outStaticDebug("Command handler failed for cmd %u recv length %u", (uint32)_cmd, (uint32)socket().recv_len());
                    return;
                }
                break;
            }
        }

        // Report unknown packets in the error log
        if (i == AUTH_TOTAL_COMMANDS)
        {
            sLog->outError("[Auth] got unknown packet from '%s'", socket().getRemoteAddress().c_str());
            socket().shutdown();
            return;
        }
    }
}
Example #19
0
int CSocketOper::socket_init(const std::string& ip, const unsigned short& port, struct sockaddr_in& addr, int type)
{
    int reflag = 1;
    int sockfd = 0;

    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ip.c_str());
    //TCP初始化
    if (type == 0)
    {
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
    }
    //UDP初始化
    else
    {
        sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    }
    //设置地址复用
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reflag, sizeof(int)) == -1)
    {
        std::cout<<"reuse addr error."<<std::endl;
    }
    if (sockfd == -1)
    {
        std::cout<<"socket error:"<<ip.c_str()<<" port:"<<port<<" error!"<<std::endl;
    }

    return sockfd;
}
Example #20
0
/* Open a TCP/UDP client socket */
static int OS_Connect(u_int16_t _port, unsigned int protocol, const char *_ip, int ipv6)
{
    int ossock;
    struct sockaddr_in server;
#ifndef WIN32
    struct sockaddr_in6 server6;
#else
    ipv6 = 0;
#endif

    if (protocol == IPPROTO_TCP) {
        if ((ossock = socket(ipv6 == 1 ? PF_INET6 : PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
            return (OS_SOCKTERR);
        }
    } else if (protocol == IPPROTO_UDP) {
        if ((ossock = socket(ipv6 == 1 ? PF_INET6 : PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
            return (OS_SOCKTERR);
        }
    } else {
        return (OS_INVALID);
    }
#ifdef HPUX
    {
        int flags;
        flags = fcntl(ossock, F_GETFL, 0);
        fcntl(ossock, F_SETFL, flags | O_NONBLOCK);
    }
#endif

    if ((_ip == NULL) || (_ip[0] == '\0')) {
        OS_CloseSocket(ossock);
        return (OS_INVALID);
    }

    if (ipv6 == 1) {
#ifndef WIN32
        memset(&server6, 0, sizeof(server6));
        server6.sin6_family = AF_INET6;
        server6.sin6_port = htons( _port );
        inet_pton(AF_INET6, _ip, &server6.sin6_addr.s6_addr);

        if (connect(ossock, (struct sockaddr *)&server6, sizeof(server6)) < 0) {
            OS_CloseSocket(ossock);
            return (OS_SOCKTERR);
        }
#endif
    } else {
        memset(&server, 0, sizeof(server));
        server.sin_family = AF_INET;
        server.sin_port = htons( _port );
        server.sin_addr.s_addr = inet_addr(_ip);

        if (connect(ossock, (struct sockaddr *)&server, sizeof(server)) < 0) {
            OS_CloseSocket(ossock);
            return (OS_SOCKTERR);
        }
    }

    return (ossock);
}
Example #21
0
File: core.c Project: Kitware/CMake
/* Open a socket in non-blocking close-on-exec mode, atomically if possible. */
int uv__socket(int domain, int type, int protocol) {
  int sockfd;
  int err;

#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
  sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);
  if (sockfd != -1)
    return sockfd;

  if (errno != EINVAL)
    return UV__ERR(errno);
#endif

  sockfd = socket(domain, type, protocol);
  if (sockfd == -1)
    return UV__ERR(errno);

  err = uv__nonblock(sockfd, 1);
  if (err == 0)
    err = uv__cloexec(sockfd, 1);

  if (err) {
    uv__close(sockfd);
    return err;
  }

#if defined(SO_NOSIGPIPE)
  {
    int on = 1;
    setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
  }
#endif

  return sockfd;
}
Example #22
0
pid_t
start_server(struct sockaddr_in *ssin, struct sockaddr_un *ssun)
{
    pid_t pid;

    sfd = socket(PF_INET, SOCK_STREAM, 0);
    if (sfd < 0)
	return -1;
    if (bind(sfd, (struct sockaddr *)ssin, sizeof(*ssin)) < 0)
	return -1;
    if (listen(sfd, 10) < 0)
	return -1;

    /* set up UNIX-domain socket */
    ufd = socket(PF_UNIX, SOCK_STREAM, 0);
    if (ufd < 0)
	return -1;
    if (bind(ufd, (struct sockaddr *)ssun, sizeof(*ssun)))
	return -1;
    if (listen(ufd, 10) < 0)
	return -1;

    switch (pid = fork()) {
    case 0:		/* child */
	do_child();
	break;
    case -1:			/* fall through */
    default:			/* parent */
	(void)close(sfd);
	return pid;
    }

    return -1;
}
QString Network::errorString() const
{
    if (socket())
        return socket()->errorString();
    else
        return myerrorString;
}
Example #24
0
static int
priv_netinet_ipsec_policy_entrust_setup_af(int asroot, int injail,
    struct test *test, int af)
{

	entrustbuf = ipsec_set_policy(policy_entrust, sizeof(policy_entrust)-1);
	if (entrustbuf == NULL) {
		warn("%s: ipsec_set_policy(NULL)", __func__);
		return (-1);
	}
	switch (af) {
	case AF_INET:
		sd = socket(AF_INET, SOCK_DGRAM, 0);
		if (sd < 0) {
			warn("%s: socket4", __func__);
			return (-1);
		}
		break;
#ifdef INET6
	case AF_INET6:
		sd = socket(AF_INET6, SOCK_DGRAM, 0);
		if (sd < 0) {
			warn("%s: socket6", __func__);
			return (-1);
		}
		break;
#endif
	default:
		warnx("%s: unexpected address family", __func__);
		return (-1);
	}
	return (0);
}
int Network::error() const
{
    if (socket())
        return socket()->error();
    else
        return myerror;
}
Example #26
0
/* Return a pair of mutually connected sockets in sv[0] and sv[1] */
int
socketpair(
int af,
int type,
int protocol,
int sv[]
){
	struct usock *up0, *up1;
	if(sv == NULL){
		errno = EFAULT;
		return -1;
	}
	if(af != AF_LOCAL){
		errno = EAFNOSUPPORT;
		return -1;
	}
	if(type != SOCK_STREAM && type != SOCK_DGRAM){
		errno = ESOCKTNOSUPPORT;
		return -1;
	}
	if((sv[0] = socket(af,type,protocol)) == -1)
		return -1;
	if((sv[1] = socket(af,type,protocol)) == -1){
		close_s(sv[0]);
		return -1;
	}
	up0 = itop(sv[0]);
	up1 = itop(sv[1]);
	up0->cb.local->peer = up1;
	up1->cb.local->peer = up0;
	return sv[1];
}
Example #27
0
int main(){


	signal(SIGALRM, timer_handler);


	in_port_t server_portno = SERVER_PORT_NO;


	if( (main_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
		perror("Error: Creating the main socket");
		return EXIT_FAILURE;
	}

	// setting server_addr
	memset((char *)&server_addr, 0, sizeof(server_addr));// initialization
	// 0 init is important since the server_addr.zero[] must be zero
	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	server_addr.sin_port = htons(server_portno);

	// setting client_addr
	memset((char *) &client_addr, 0, client_addr_len);

	if ( (bind(main_sock, (struct sockaddr*) &server_addr, sizeof(server_addr)))  < 0){
		perror("ERROR on binding socket to server address");
		return EXIT_FAILURE;
	}

	int worker_pid;
	while (1) {

		// receive request
		// since we know the server the request message only contains the file path/name : my protocol my rules :P
		recvfrom(main_sock, file_name, sizeof(file_name), 0, (struct sockaddr*) &client_addr, &client_addr_len);
		printf("requested file is: %s\n",file_name);

		if ( (worker_pid = fork()) < 0) {
			perror("ERROR on forking a new worker process");
			continue;
		}

		if (!worker_pid) { // worker_pid = 0 then we are in the child
			// create the working socket
			close(main_sock);
			if ( (worker_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
				perror("Error: Creating the worker socket");
				return EXIT_FAILURE;
			}

			signal(SIGALRM, timer_handler);
			start_connection();
			exit(EXIT_SUCCESS);
		} else { // pid != 0 then we are in the parent;
			close(worker_sock);
		}

	}
	return EXIT_SUCCESS;
}
Example #28
0
int
sctp_getaddrlen(sa_family_t family)
{
	int ret, sd;
	socklen_t siz;
	struct sctp_assoc_value av;

	av.assoc_value = family;
	siz = sizeof(av);
#if defined(AF_INET)
	sd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
#elif defined(AF_INET6)
	sd = socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP);
#else
	sd = -1;
#endif
	if (sd == -1) {
		return (-1);
	}
	ret = getsockopt(sd, IPPROTO_SCTP, SCTP_GET_ADDR_LEN, &av, &siz);
	close(sd);
	if (ret == 0) {
		return ((int)av.assoc_value);
	} else {
		return (-1);
	}
}
Example #29
0
static int dns_init_sockets() {
    struct addrinfo hints;
    struct addrinfo *addr_ip;
    int r;

    local_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (0 != setnonblock(local_sock))
        return -1;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_DGRAM;
    if (0 != (r = getaddrinfo(listen_addr, listen_port, &hints, &addr_ip))) {
        VERR("%s:%s:%s\n", gai_strerror(r), listen_addr, listen_port);
        return -1;
    }
    if (0 != bind(local_sock, addr_ip->ai_addr, addr_ip->ai_addrlen)) {
        ERR("bind");
        VERR("Can't bind address %s:%s\n", listen_addr, listen_port);
        return -1;
    }
    freeaddrinfo(addr_ip);
    remote_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (0 != setnonblock(remote_sock))
        return -1;
    return 0;
}
Example #30
0
static krb5_socket_t
make_signal_socket (krb5_context context)
{
#ifndef NO_UNIX_SOCKETS
    struct sockaddr_un addr;
    const char *fn;
    krb5_socket_t fd;

    fn = kadm5_log_signal_socket(context);

    fd = socket (AF_UNIX, SOCK_DGRAM, 0);
    if (fd < 0)
	krb5_err (context, 1, errno, "socket AF_UNIX");
    memset (&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strlcpy (addr.sun_path, fn, sizeof(addr.sun_path));
    unlink (addr.sun_path);
    if (bind (fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
	krb5_err (context, 1, errno, "bind %s", addr.sun_path);
    return fd;
#else
    struct addrinfo *ai = NULL;
    krb5_socket_t fd;

    kadm5_log_signal_socket_info(context, 1, &ai);

    fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (rk_IS_BAD_SOCKET(fd))
	krb5_err (context, 1, rk_SOCK_ERRNO, "socket AF=%d", ai->ai_family);

    if (rk_IS_SOCKET_ERROR( bind (fd, ai->ai_addr, ai->ai_addrlen) ))
	krb5_err (context, 1, rk_SOCK_ERRNO, "bind");
    return fd;
#endif
}