The library functions inet_ntop() and inet_pton() transform human-readable IP addresses into their binary form, and vice-versa.
CFLAGS=-g TARGETSOURCES=client-rdv.c server-wiki-rdv.c server-udp-rdv.c \ client-udp-rdv.c client-udp-wiki.c LIBRARIES=mylib.a LIBSOURCES=mylib.c TESTSOURCES=mylibtest.c gai.c HEADERFILES=prototypes.h LIBOBJS=$(subst .c,.o,$(LIBSOURCES)) ALLSOURCES=$(LIBSOURCES) $(TESTSOURCES) $(TARGETSOURCES) TESTBINARIES=$(subst .c,,$(TESTSOURCES)) TARGETBINARIES=$(subst .c,,$(TARGETSOURCES)) ALLBINARIES=$(TARGETBINARIES) $(TESTBINARIES) all: $(ALLBINARIES) server-wiki-rdv: server-wiki-rdv.c $(LIBRARIES) $(CC) $(CFLAGS) -o server-wiki-rdv server-wiki-rdv.c $(LIBRARIES) client-rdv: client-rdv.c $(LIBRARIES) $(CC) $(CFLAGS) -o client-rdv client-rdv.c $(LIBRARIES) server-udp-rdv: server-udp-rdv.c $(LIBRARIES) $(CC) $(CFLAGS) -o server-udp-rdv server-udp-rdv.c $(LIBRARIES) client-udp-wiki: client-udp-wiki.c $(LIBRARIES) $(CC) $(CFLAGS) -o client-udp-wiki client-udp-wiki.c $(LIBRARIES) gai: gai.c $(LIBRARIES) $(CC) $(CFLAGS) -o gai gai.c $(LIBRARIES) mylib.a: $(LIBOBJS) ar r mylib.a $(LIBOBJS) clean: rm -f *.o *.a *.d $(ALLBINARIES) TAGS # automatically generated dependency files include $(subst .c,.d,$(ALLSOURCES)) # rules for generating dependency files %.d: %.c $(CC) -M $(CFLAGS) $< > $@.$$$$; \ sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ rm -f $@.$$$$
int get_stream(char *host, char *service){ int error, fd; struct addrinfo *ai, *ai2, hints; char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; memset((void *)&hints, 0, sizeof(struct addrinfo)); hints.ai_socktype = SOCK_STREAM; hints.ai_family |= PF_UNSPEC; // hints.ai_flags |= AI_NUMERICSERV; if (error = getaddrinfo(host, service, &hints, &ai)) { fprintf(stderr, "getaddrinfo(%s, %s, ...): %s (%d)\n", host, service, gai_strerror(error), error); exit(-1); } for (ai2 = ai; ai; ai = ai->ai_next) { if (error = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) { fprintf(stderr, "getnameinfo(%p, %d, %p, %d, %p, %d, %d): %s(%d)\n", ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV, gai_strerror(error), error); continue; } fprintf(stdout, "Trying %s port %s...\n", hbuf, sbuf); if ((fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) < 0) { fprintf(stderr, "socket(%d, %d, %d): ", ai->ai_family, ai->ai_socktype, ai->ai_protocol); perror("get_stream"); continue; } if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) { fprintf(stderr, "connect(%d, %p, %d): \n", fd, ai->ai_addr, ai->ai_addrlen); perror("get_stream"); close(fd); continue; } freeaddrinfo(ai2); return fd; } freeaddrinfo(ai2); fprintf(stderr, "No connections result.\n"); return -1; }