X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=listati%2Fsockbind.c;fp=listati%2Fsockbind.c;h=ddf48d8962c7a4b4b286bb92e611f5b9a0c3570b;hp=0000000000000000000000000000000000000000;hb=8654ce33b450ae7bb34c3907835000a0760c2931;hpb=88d8a251947f948094d35de596feaf49e975f91b diff --git a/listati/sockbind.c b/listati/sockbind.c new file mode 100644 index 0000000..ddf48d8 --- /dev/null +++ b/listati/sockbind.c @@ -0,0 +1,32 @@ +int sockbind(char *host, char *serv, int prot, int type) +{ + struct addrinfo hint, *addr; + int res; + int sock; + /* initialize hint structure */ + memset(&hint, 0, sizeof(struct addrinfo)); + hint.ai_flags = AI_PASSIVE; /* address for binding */ + hint.ai_family = PF_UNSPEC; /* generic address (IPv4 or IPv6) */ + hint.ai_protocol = prot; /* protocol */ + hint.ai_socktype = type; /* socket type */ + res = getaddrinfo(host, serv, &hint, &addr); /* calling getaddrinfo */ + if (res != 0) { /* on error exit */ + printf("sockbind cannot resolve host %s, service %s, ", host, serv); + printf("protocol %d: %s\n", prot, gai_strerror(res)); + return -1; + } + /* get a socket */ + sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + if (sock < 0) { + printf("sockconn cannot create socket\n"); + return sock; + } + /* connect the socket */ + res = bind(sock, addr->ai_addr, addr->ai_addrlen); + if (res < 0) { + printf("sockconn cannot bind socket\n"); + return res; + } + freeaddrinfo(addr); /* done, release memory */ + return sock; +}