コード例 #1
0
ファイル: rktio_network.c プロジェクト: 97jaz/racket
rktio_addrinfo_lookup_t *rktio_start_addrinfo_lookup(rktio_t *rktio,
                                                     const char *hostname, int portno,
                                                     int family, int passive, int tcp)
{
  rktio_addrinfo_lookup_t *lookup;
  char buf[32], *service;
  struct rktio_addrinfo_t *hints;

  if (portno >= 0) {
    service = buf;
    sprintf(buf, "%d", portno);
  } else
    service = NULL;
  
  if (!hostname && !service) {
    set_racket_error(RKTIO_ERROR_HOST_AND_PORT_BOTH_UNSPECIFIED);
    return NULL;
  }

  hints = malloc(sizeof(rktio_addrinfo_t));
  memset(hints, 0, sizeof(struct rktio_addrinfo_t));
  RKTIO_AS_ADDRINFO(hints)->ai_family = ((family < 0) ? PF_UNSPEC : family);
  if (passive) {
    RKTIO_AS_ADDRINFO(hints)->ai_flags |= rktio_AI_PASSIVE;
  }
  if (tcp) {
    RKTIO_AS_ADDRINFO(hints)->ai_socktype = SOCK_STREAM;
# ifndef PROTOENT_IS_INT
    if (!proto) {
      proto = getprotobyname("tcp");
    }
# endif
    RKTIO_AS_ADDRINFO(hints)->ai_protocol= PROTO_P_PROTO;
  } else {
    RKTIO_AS_ADDRINFO(hints)->ai_socktype = SOCK_DGRAM;
  }

  lookup = malloc(sizeof(rktio_addrinfo_lookup_t));
  lookup->name = (hostname ? MSC_IZE(strdup)(hostname) : NULL);
  lookup->svc = (service ? MSC_IZE(strdup)(service) : NULL);
  lookup->hints = hints;
  init_lookup(lookup);
 
  return start_lookup(rktio, lookup);
}
コード例 #2
0
ファイル: ping.c プロジェクト: xiaoxuanzi/code
main(int argc,char *argv[])
{       struct hostent *host;
        struct protoent *protocol;
        unsigned long inaddr=0l;
        int waittime=MAX_WAIT_TIME;
        int size=50*1024;
        if(argc<2)
        {       printf("usage:%s hostname/IP address\n",argv[0]);
                exit(1);
        }
        if( (protocol=getprotobyname("icmp") )==NULL)
        {       perror("getprotobyname");
                exit(1);
        }
        /*生成使用ICMP的原始套接字,这种套接字只有root才能生成*/
        if( (sockfd=socket(AF_INET,SOCK_RAW,protocol->p_proto) )<0)
        {       perror("socket error");
                exit(1);
        }
        /* 回收root权限,设置当前用户权限*/
        setuid(getuid());
        /*扩大套接字接收缓冲区到50K这样做主要为了减小接收缓冲区溢出的
          的可能性,若无意中ping一个广播地址或多播地址,将会引来大量应答*/
        setsockopt(sockfd,SOL_SOCKET,SO_RCVBUF,&size,sizeof(size) );
        bzero(&dest_addr,sizeof(dest_addr));
        dest_addr.sin_family=AF_INET;
        /*判断是主机名还是ip地址*/
        if( inaddr=inet_addr(argv[1])==INADDR_NONE)
        {       if((host=gethostbyname(argv[1]) )==NULL) /*是主机名*/
                {       perror("gethostbyname error");
                        exit(1);
                }
                memcpy( (char *)&dest_addr.sin_addr,host->h_addr,host->h_length);
        }
        else    /*是ip地址*/
                memcpy( (char *)&dest_addr,(char *)&inaddr,host->h_length);
        /*获取main的进程id,用于设置ICMP的标志符*/
        pid=getpid();
        printf("PING %s(%s): %d bytes data in ICMP packets.\n",argv[1],
                        inet_ntoa(dest_addr.sin_addr),datalen);
        send_packet();  /*发送所有ICMP报文*/
        recv_packet();  /*接收所有ICMP报文*/
        statistics(SIGALRM); /*进行统计*/
        return 0;
}
コード例 #3
0
ファイル: core.c プロジェクト: codepongo/radare2
int gdbr_connect(libgdbr_t* g, const char* host, int port) {
	int	fd;
	int	connected;
	struct protoent		*protocol;
	struct hostent		*hostaddr;
	struct sockaddr_in	socketaddr;
	
	protocol = getprotobyname("tcp");
	if (!protocol) {
		printf("Error prot\n");
		//TODO Error here
		return -1;
	}

	fd = socket( PF_INET, SOCK_STREAM, protocol->p_proto);
	if (fd == -1) {
		printf("Error sock\n");
		//TODO Error here
		return -1;
	}
	memset(&socketaddr, 0, sizeof(socketaddr));
	socketaddr.sin_family = AF_INET;
	socketaddr.sin_port = htons(port);
	hostaddr = (struct hostent *)gethostbyname(host);

	if (!hostaddr) {
		printf("Error host\n");
		//TODO Error here
		return -1;
	}
	
	connected = connect(fd, (struct sockaddr *) &socketaddr, sizeof(socketaddr));
	if (connected == -1) {
		printf("error conn\n");
		//TODO Error here
		return -1;
	}
	g->fd = fd;
	g->connected = 1;
	// TODO add config possibility here
	char* message = "qSupported:multiprocess+;qRelocInsn+";
	send_command(g, message);
	read_packet(g);
	return handle_connect(g);
}
コード例 #4
0
ファイル: libping.c プロジェクト: 274914765/C
PING *ping_init (int type, int ident)
{
    int fd;

    struct protoent *proto;

    PING *p;

    /* Initialize raw ICMP socket */
    proto = getprotobyname ("icmp");
    if (!proto)
    {
        fprintf (stderr, "ping: unknown protocol icmp.\n");
        return NULL;
    }

    fd = socket (AF_INET, SOCK_RAW, proto->p_proto);
    if (fd < 0)
    {
        if (errno == EPERM)
            fprintf (stderr, "ping: ping must run as root\n");
        return NULL;
    }

    /* Allocate PING structure and initialize it to default values */
    p = malloc (sizeof (*p));
    if (!p)
    {
        close (fd);
        return p;
    }

    memset (p, 0, sizeof (*p));

    p->ping_fd = fd;
    p->ping_type = type;
    p->ping_count = 0;
    p->ping_interval = PING_DEFAULT_INTERVAL;
    p->ping_datalen = sizeof (icmphdr_t);
    /* Make sure we use only 16 bits in this field, id for icmp is a u_short.  */
    p->ping_ident = ident & 0xFFFF;
    p->ping_cktab_size = PING_CKTABSIZE;
    gettimeofday (&p->ping_start_time, NULL);
    return p;
}
コード例 #5
0
ファイル: create_socket.c プロジェクト: 42MrPiou42/Ft_p
int						create_socket(t_serv *server)
{
	int					ret;
	struct protoent		*proto;
	struct sockaddr_in	sin;

	if (!(proto = getprotobyname("tcp")))
		return (-1);
	server->sock = socket(PF_INET, SOCK_STREAM, proto->p_proto);
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = INADDR_ANY;
	sin.sin_port = htons(server->port);
	ret = bind(server->sock, (const struct sockaddr *)&sin, sizeof(sin));
	if (ret == -1)
		return (-1);
	listen(server->sock, 128);
	return (1);
}
コード例 #6
0
ファイル: Client.cpp プロジェクト: jonquach/zappy
void Client::createClient()
{
  struct protoent *proto;
  struct sockaddr_in sin;

  if ((proto = getprotobyname("TCP")) == 0)
    throw NException("Error : getprotobyname failed");
  if ((_socket = socket(AF_INET, SOCK_STREAM, proto->p_proto)) == -1)
    throw NException("Error : socket failed");

  sin.sin_family = AF_INET;
  sin.sin_port = htons(_port);

  if ((sin.sin_addr.s_addr = inet_addr(_ip.c_str())) == INADDR_NONE)
    throw NException("Error : inet_addr failed");
  if (connect(_socket, (const struct sockaddr *)&sin, sizeof(sin)) == -1)
    throw NException("Error : connect failed");
}
static int set_default_protos(void)
{
	struct protoent *prot;
	const char *pname;
	size_t pindex;

	init_protos(default_numprotos);

	for (pindex = 0; pindex < default_numprotos; pindex++) {
		pname = default_protos[pindex];
		prot = getprotobyname(pname);
		if (prot == NULL)
			err(1, "getprotobyname: %s", pname);
		protos[pindex] = prot->p_proto;
	}
	numprotos = pindex;
	return (pindex);
}
コード例 #8
0
ファイル: getprotbyname.c プロジェクト: jes5199/mythryl
/* _lib7_NetDB_getprotbyname : String -> (String * String list * int) option
 */
lib7_val_t _lib7_NetDB_getprotbyname (lib7_state_t *lib7_state, lib7_val_t arg)
{
    lib7_val_t	    name, aliases, res;
    struct protoent *pentry;

    pentry = getprotobyname (STR_LIB7toC(arg));

    if (pentry == NULL)
	return OPTION_NONE;
    else {
	name = LIB7_CString (lib7_state, pentry->p_name);
	aliases = LIB7_CStringList (lib7_state, pentry->p_aliases);
	REC_ALLOC3 (lib7_state, res, name, aliases, INT_CtoLib7(pentry->p_proto));
	OPTION_SOME (lib7_state, res, res);
	return res;
    }

} /* end of _lib7_NetDB_getprotbyname */
コード例 #9
0
ファイル: client.c プロジェクト: ajulien42/ft_p
int		create_client(char *addr, int port)
{
	t_client	clt;

	if (!(clt.proto = getprotobyname("tcp")))
		return (-1);
	clt.sock = socket(PF_INET, SOCK_STREAM, clt.proto->p_proto);
	clt.sin.sin_family = AF_INET;
	clt.sin.sin_port = htons(port);
	clt.sin.sin_addr.s_addr = inet_addr(addr);
	if (connect(clt.sock, (const struct sockaddr *)&clt.sin,
									sizeof(clt.sin)) == -1)
	{
		printf("connect error\n");
		exit(2);
	}
	return (clt.sock);
}
コード例 #10
0
ファイル: resolver.c プロジェクト: 0utsider/filtergen
void resolve_protocol_argument(struct protocol_argument_s * n) {
    struct protoent * p;

    if (n->proto) {
	if ((p = getprotobyname(n->proto))) {
	    free(n->proto);
	    asprintf(&n->proto, "%d", p->p_proto);
	} else {
	    /* check that the proto is a number if we can't resolve it */
	    char * e;

	    strtol(n->proto, &e, 10);
	    if (*e) {
		fprintf(stderr, "warning: suspicious protocol name encountered: %s\n", n->proto);
	    }
	}
    }
}
コード例 #11
0
ファイル: main.c プロジェクト: jdG-/zap
static int				create_server(t_game game)
{
	struct protoent		*proto;
	struct sockaddr_in	sin;

	if ((proto = getprotobyname("tcp")) == 0)
		ft_error("ERROR getprotobyname.");
	if ((game.sock = socket(PF_INET, SOCK_STREAM, proto->p_proto)) == -1)
		ft_error("ERROR socket.");
	sin.sin_family = AF_INET;
	sin.sin_port = htons(game.port);
	sin.sin_addr.s_addr = htonl(INADDR_ANY);
	if (bind(game.sock, (const struct sockaddr*)&sin, sizeof(sin)) == -1)
		ft_error("ERROR bind.");
	if (listen(game.sock, MAX_CLI) == -1)
		ft_error("ERROR listen.");
	return (game.sock);
}
コード例 #12
0
ファイル: test.c プロジェクト: ChrisJefferson/io
int main(void)
{
    int fd;
    int len,len2;
    int guck;
    struct sockaddr_in sa;
    struct hostent *he;
    fd = socket(PF_INET,SOCK_STREAM,getprotobyname("tcp")->p_proto);
    sa.sin_family = AF_INET;
    sa.sin_port = htons(22);
    he = gethostbyname("antares.math.rwth-aachen.de");
    memcpy(&(sa.sin_addr.s_addr),he->h_addr_list[0],he->h_length);
    len = he->h_length;
    len2 = sizeof(struct sockaddr_in);
    guck = connect(fd,(struct sockaddr *) (&sa),len2);
    printf("%d %d %d %d %d\n",fd,len,guck,len2,errno);
    return 0;
}
コード例 #13
0
ファイル: net.c プロジェクト: moul/junk
int	lnc_init_connect(METHOD_ARGS)
{
  int	opt;

  if ((THIS->he = gethostbyname(THIS->hostname)) == NULL)
    return (0);
  THIS->pe = getprotobyname("tcp");
  THIS->sin.sin_family = AF_INET;
  THIS->sin.sin_port = htons(THIS->port);
  memcpy(&THIS->sin.sin_addr, THIS->he->h_addr_list[0], THIS->he->h_length);
  opt = 1;
  if ((THIS->fd = socket(PF_INET, SOCK_STREAM, THIS->pe->p_proto))
      == -1 || -1 ==
      setsockopt(THIS->fd, SOL_SOCKET, SO_REUSEADDR,
		 (void *)&opt, sizeof(opt)))
    return (0);
  return (1);
}
コード例 #14
0
ファイル: ft_server.c プロジェクト: Melissasat/ft_p
int		ft_create_server(int port, char *str)
{
	int					sock;
	struct protoent		*proto;
	struct sockaddr_in	sina;

	proto = getprotobyname("tcp");
	if (proto == NULL)
		ft_error(str, 1);
	sock = socket(PF_INET, SOCK_STREAM, proto->p_proto);
	sina.sin_family = AF_INET;
	sina.sin_port = htons(port);
	sina.sin_addr.s_addr = htonl(INADDR_ANY);
	if (bind(sock, (const struct sockaddr *)&sina, sizeof(sina)) == -1)
		ft_error(str, 1);
	listen(sock, 42);
	return (sock);
}
コード例 #15
0
ファイル: Socket.cpp プロジェクト: Kafei59/epitech-projects
Socket::Socket(std::string const &serv, int port)
{
  struct protoent	*pe;
  struct sockaddr_in	sockaddr;
  socklen_t		socksize;
  int			sock;
  int			okay;

  pe = getprotobyname("TCP");
  socksize = sizeof(struct sockaddr);
  if ((sock = socket(AF_INET, SOCK_STREAM, pe->p_proto)) == -1)
    {
      std::cerr << "Socket creation failed." << std::endl;
      exit(2);
    }
  sockaddr.sin_family = AF_INET;
  sockaddr.sin_port = htons(port);
  sockaddr.sin_addr.s_addr = inet_addr(serv.c_str());
  if ((connect(sock, reinterpret_cast<const struct sockaddr *>(&sockaddr), socksize)) != 0)
    {
      std::cerr << "Socket connection failed." << std::endl;
      exit(3);
    }
  m_sock = sock;
  m_fds.fd = m_sock;
  m_fds.events = POLLIN;
  okay = 0;
  if (toRead())
    {
      okay = 1;
      if (receiveMessage() != "BIENVENUE")
	okay = 0;
    }
  if (okay)
    {
      m_okay = true;
      sendMessage("GRAPHIC");
    }
  else
    {
      m_okay = false;
      std::cerr << "\'BIENVENUE\' not received..." << std::endl << "Exiting ..." << std::endl;
    }
}
コード例 #16
0
// Passive Sock
int
passiveSock(const char *service, const char *transport, int qlen) {
	struct servent *pserv ;
	struct protoent *pproto ;
	struct sockaddr_in sin ;
	int sd, type;

	/* Initialize sock address struct to 0 */
	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = INADDR_ANY ;    //Get connection from any IP address

	/* Map the input service to port number */
	if (pserv = getservbyname(service, transport)) {
		sin.sin_port = (unsigned short)pserv->s_port ;
	}
	else if ( (sin.sin_port = htons( (unsigned short) atoi(service))) == 0) {
		printf("Error in getting service port number.\n");
	}

	/* Map the transport protocol type to port number */
	if ((pproto = getprotobyname(transport)) == 0)
		printf("Error in getting protocol port number. \n");

	/* Use protocol to choose socket type */
	if (strcmp(transport, "udp"))
		type = SOCK_DGRAM;
	else
		type = SOCK_STREAM;

	/* Allocate a socket */
	sd = socket(PF_INET, type, pproto->p_proto);
	if(sd < 0)
		printf("Error in allocating a socket \n");

	/* Bind a socket */
	if (bind(sd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
		printf("Error in binding the socket to port %s\n", service);

	if (type == SOCK_STREAM && listen(sd, qlen) < 0)
		printf("Can't listen to port \n");
	
	return sd;
}
コード例 #17
0
int clientsock(const char* host, const char* service, const char* transport, struct sockaddr_in* fsin)
{
    struct servent *pse;
    struct protoent *ppe;
    struct sockaddr_in *sin;
    if (fsin == NULL)
        // Service address
        sin = malloc(sizeof(struct sockaddr_in));  
    else
        sin = fsin;
    int s, type;
    memset(sin, 0, sizeof(*sin));
    // assign sin_family
    sin->sin_family = AF_INET;                   
    // assign sin_addr
    sin->sin_addr.s_addr = inet_addr(host);      
    //echo("[CLIENT] Debug: service host : %lu (%s) \n", (unsigned long)sin->sin_addr.s_addr, host);
    // assign sin_port
    if (pse = getservbyname(service, transport))
        sin->sin_port = htons(ntohs((unsigned short)pse->s_port) + portbase);
    else if((sin->sin_port = htons((unsigned short)atoi(service))) == 0)
        errexit("can't get \"%s\" service entry\n", service);
    //echo("[CLIENT] Debug: service port : %u (%s) \n", sin->sin_port, service);
    // protocol
    if ((ppe = getprotobyname(transport)) == 0)
        errexit("can't get \"%s\" protocol entry\n", transport);
    if (strcmp(transport, "udp") == 0 || strcmp(transport, "UDP") == 0)
        type = SOCK_DGRAM;
    else
        type = SOCK_STREAM;
    s = socket(PF_INET, type, ppe->p_proto);
    if (s < 0)
        errexit("can't create socket: %s\n", strerror(errno));
    if (type == SOCK_STREAM)
    {
        //echo("[CLIENT] Debug: connecting to service %s.. \n", service);
        if (connect(s, (struct sockaddr*)sin, sizeof(*sin)) != 0)
            errexit("can't connect to service: %s\n", strerror(errno));
        //echo("[CLIENT] Debug: service %s connected. \n", service);
    }
    if (fsin == NULL)
        free(sin);
    return s;
}
コード例 #18
0
ファイル: tcp_nt.c プロジェクト: Distrotech/pine
int tcp_socket_open (int family,void *adr,size_t adrlen,unsigned short port,
		     char *tmp,char *hst)
{
  int sock;
  size_t len;
  char *s;
  struct protoent *pt = getprotobyname ("tcp");
  struct sockaddr *sadr = ip_sockaddr (family,adr,adrlen,port,&len);
  sprintf (tmp,"Trying IP address [%s]",ip_sockaddrtostring (sadr));
  mm_log (tmp,NIL);
				/* get a TCP stream */
  if ((sock = socket (sadr->sa_family,SOCK_STREAM,pt ? pt->p_proto : 0)) ==
      INVALID_SOCKET)
    sprintf (tmp,"Unable to create TCP socket (%d)",WSAGetLastError ());
  else {
    wsa_sock_open++;		/* count this socket as open */
				/* open connection */
    if (connect (sock,sadr,len) == SOCKET_ERROR) {
      switch (WSAGetLastError ()) {
      case WSAECONNREFUSED:
	s = "Refused";
	break;
      case WSAENOBUFS:
	s = "Insufficient system resources";
	break;
      case WSAETIMEDOUT:
	s = "Timed out";
	break;
      case WSAEHOSTUNREACH:
	s = "Host unreachable";
	break;
      default:
	s = "Unknown error";
	break;
      }
      sprintf (tmp,"Can't connect to %.80s,%ld: %s (%d)",hst,port,s,
	       WSAGetLastError ());
      tcp_abort (&sock);	/* flush socket */
      sock = INVALID_SOCKET;
    }
  }
  fs_give ((void **) &sadr);
  return sock;			/* return the socket */
}
コード例 #19
0
ファイル: online_detect.c プロジェクト: Jeansfirm/glexer-ipc
int init_online_detect()
{
    struct protoent *protocol;

    int size=3*1024;

    nsend=0;
    nreceived=0;

    if((protocol=getprotobyname("icmp") )==NULL)
    {
        perror("getprotobyname");
        //exit(1);
        return -1;
    }
    /*生成使用ICMP的原始套接字,这种套接字只有root才能生成*/
    if((sockfd=socket(AF_INET,SOCK_RAW,protocol->p_proto))<0)
    {
        perror("socket error");
        close(sockfd);
        //exit(1);
        return -1;

    }

#if 0
    //设置阻塞接收超时
    struct timeval tv_out;
    tv_out.tv_sec = 2; //超时2秒没有收到ICMP报文就返回
    tv_out.tv_usec = 0;
    setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv_out, sizeof(tv_out));
    setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv_out, sizeof(tv_out));
#endif

    /* 回收root权限,设置当前用户权限*/
    //setuid(getuid());
    /*扩大套接字接收缓冲区到50K这样做主要为了减小接收缓冲区溢出的
    的可能性,若无意中ping一个广播地址或多播地址,将会引来大量应答*/
    setsockopt(sockfd,SOL_SOCKET,SO_RCVBUF,&size,sizeof(size));
    bzero(&dest_addr,sizeof(dest_addr));
    dest_addr.sin_family=AF_INET;
    //dest_addr.sin_addr.s_addr = inet_addr("192.168.1.184");
    return 0;
}
コード例 #20
0
ファイル: s_socket.c プロジェクト: elenys/ft_p
int		create_server(int port)
{
	int					sock;
	struct protoent		*protocole;
	struct sockaddr_in	adr_serv;

	protocole = getprotobyname("tcp");
	if (protocole == 0)
		s_error("to get protocole");
	sock = socket(PF_INET, SOCK_STREAM, protocole->p_proto);
	adr_serv.sin_family = AF_INET;
	adr_serv.sin_port = htons(port);
	adr_serv.sin_addr.s_addr = htonl(INADDR_ANY);
	if (bind(sock, (const struct sockaddr *)&adr_serv, sizeof(adr_serv)) == -1)
		s_error("to bind the socket");
	if (listen(sock, 10) == -1)
		s_error("to listen the socket");
	return (sock);
}
コード例 #21
0
int		create_clt(char *addr, int port)
{
	int					sock;
	struct protoent		*prt;
	struct sockaddr_in	clt;

	clt.sin_family = AF_INET;
	clt.sin_port = htons(port);
	clt.sin_addr.s_addr = inet_addr(addr);
	prt = getprotobyname("TCP");
	if (prt == 0)
		return (-1);
	if ((sock = socket(PF_INET, SOCK_STREAM, prt->p_proto)) == -1)
		return (my_error('s'));
	if (connect(sock, (const struct sockaddr *)&clt, sizeof(clt)) == -1)
		return (my_error('b'));
	printf("Client OK\n");
	return (sock);
}
コード例 #22
0
static int
make_tcp_socket(void)
{
	struct protoent *protop;
	int sockfd;

	protop = getprotobyname("tcp");
	if (protop == NULL) {
		warnx("protocol not found: \"tcp\"");
		return (-1);
	}

	sockfd = socket(PF_INET, SOCK_STREAM, protop->p_proto);
	if (sockfd < 0) {
		warn("socket");
	}

	return (sockfd);
}
コード例 #23
0
ファイル: ft_connect.c プロジェクト: Julow/IRC
int				ft_connect(char const *addr, char const *port)
{
	struct addrinfo	hints;
	struct addrinfo	*res;
	struct protoent	*proto;
	int				fd;

	if ((proto = getprotobyname("tcp")) == NULL)
		return (-1);
	ft_bzero(&hints, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = proto->p_proto;
	if (getaddrinfo(addr, port, &hints, &res) != 0)
		return (-1);
	fd = try_connect(res);
	freeaddrinfo(res);
	return (fd);
}
コード例 #24
0
PROTO_R_RETURN
getprotobyname_r(const char *name, struct protoent *pptr, PROTO_R_ARGS) {
	struct protoent *pe = getprotobyname(name);
#ifdef PROTO_R_SETANSWER
	int n = 0;

	if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
		*answerp = NULL;
	else
		*answerp = pptr;
	
	return (n);
#else
	if (pe == NULL)
		return (PROTO_R_BAD);

	return (copy_protoent(pe, pptr, PROTO_R_COPY));
#endif
}
コード例 #25
0
ファイル: server.c プロジェクト: tolsac/zappy
int			fillstruct(struct sockaddr_in *sin, t_init *data)
{
  struct protoent	*s_proto;

  if ((s_proto = getprotobyname("TCP")) == NULL)
    return (merror("getprotobyname"));
  if ((data->socket = socket(AF_INET, SOCK_STREAM,
			     s_proto->p_proto)) == -1)
    return (merror("socket"));
  sin->sin_family = AF_INET;
  sin->sin_addr.s_addr = INADDR_ANY;
  sin->sin_port = htons(data->port);
  data->head = NULL;
  data->cmd = xmalloc(sizeof(char *) * (NB_FCT + 1));
  data->time = xmalloc(sizeof(float) * (NB_FCT + 1));
  if (data->cmd == NULL || data->time == NULL)
    return (-1);
  return (1);
}
コード例 #26
0
int mksock( char *host, char * service, char * proto, struct sockaddr_in *sin)
{
struct hostent *hptr;
struct servent *sptr;
struct protoent *pptr;
int sd=0, type;

int ret, len;
struct linger sling;

    memset( sin, 0, sizeof( *sin));
    sin->sin_family = AF_INET;
    
    if( hptr = gethostbyname( host))
	memcpy( & sin->sin_addr, hptr->h_addr, hptr->h_length);
    else
	return -1;

    if ( ! ( pptr = getprotobyname( proto)) )
	return -1;

    if( sptr = getservbyname( service, proto))
	sin->sin_port = sptr->s_port;
    else 
	if( (sin->sin_port = htons(( unsigned short) atoi (service))) == 0)
	    return -1;

    if ( strcmp( proto, "udp") == 0)
	type = SOCK_DGRAM;
    else if (strcmp( proto, "sctp") == 0)
	type = SOCK_SEQPACKET;
    else
	type = SOCK_STREAM;


    if ( (sd = socket( PF_INET, type, pptr->p_proto)) < 0)
    {
	perror( "Ошибка при распределении сокета");
	return -1;
    }

	return sd;
}
コード例 #27
0
ファイル: connect_server.c プロジェクト: nott0ny/zappy2010
int	connect_server(t_client *cl)
{
  if ((cl->proto = getprotobyname(PROTO)) == NULL)
    return (error("getprotobyname"));
  if ((cl->sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    return (error("socket"));
  cl->sin.sin_family = AF_INET;
  cl->sin.sin_port = htons(cl->port);
  bcopy(cl->host->h_addr_list[0], &cl->sin.sin_addr.s_addr,
	sizeof(struct in_addr));
  cl->addrlen = sizeof(cl->sin);
  if (fprintf(stdout, "Initializing client: \033[32mOK\033[00m\n") == -1)
    return (error("fprintf"));
  printf("Try connecting... %s:%d\n", inet_ntoa(cl->sin.sin_addr), cl->port);
  if (connect(cl->sock, (struct sockaddr*)&cl->sin, cl->addrlen) == -1)
    return (error("connect"));
  printf("Client-fx connect successfullly\n\n");
  return (0);
}
コード例 #28
0
ファイル: LinSocket.cpp プロジェクト: CedLaPute/cpp_babel
LinSocket::LinSocket(short port, const char *protocol)
{
  struct protoent *pe;
  std::string s((char *)protocol);

  if ((pe = getprotobyname(protocol)) == NULL)
	return;
  if (s.compare("TCP") == 0)
  {
    this->_fd = socket(AF_INET, SOCK_STREAM, pe->p_proto);
  }
  else
  {
    this->_fd = socket(PF_INET, SOCK_DGRAM, pe->p_proto);
  };
  this->_port = port;
  if (this->_fd == -1)
	throw "socket failed";
}
コード例 #29
0
ファイル: UNetwork.cpp プロジェクト: ekersale/RType
bool			Network::createSocket(std::string proto, int &type)
{
  struct protoent       *pe;
  ClientInfo		*current;

  if (_connected.find(0) != _connected.end())
    return (false);
  if (!(current = (new ClientInfo(_len))))
    return (false);
  if (!(pe = getprotobyname(proto.c_str())))
    return (false);
  if ((current->get_socket() = socket(_family, type, pe->p_proto)) == -1)
    {
      delete current;
      return (false);
    }
  _connected[++_id] = current; //stockage de la socket
  return (true);
}
コード例 #30
0
void nps_flightgear_init(const char* host,  unsigned int port, unsigned int time_offset) {
  int so_reuseaddr = 1;
  struct protoent * pte = getprotobyname("UDP");
  flightgear.socket = socket( PF_INET, SOCK_DGRAM, pte->p_proto);
  setsockopt(flightgear.socket, SOL_SOCKET, SO_REUSEADDR,
         &so_reuseaddr, sizeof(so_reuseaddr));
  flightgear.addr.sin_family = PF_INET;
  flightgear.addr.sin_port = htons(port);
  flightgear.addr.sin_addr.s_addr = inet_addr(host);

  // get current time to use as inital when computing cur_time for FG
  //struct timeval t;
  //gettimeofday(&t, NULL);
  time_t t = time(NULL);
  //struct tm tm;
  //localtime_r(t, &tm);
  flightgear.initial_time = t;
  flightgear.time_offset = time_offset;
}