/* stringtoaddress.c for X3, 24, X4 with usage example SJ */ int main(int argc, char *argv[]) { /* ... */ if (argc == 4) join(argv[3]); /* ... */ } // main() /* join another node, a lot of side effetct */ int join(char *hostport) { char buf[MAXBUFLEN]; int i; struct sockaddr_in their_addr; /* connector's address information */ printf("join: %s\n", hostport); stringtoaddress(hostport, &their_addr); for (i = 0; i < knownpeers; i++) { if (sameaddr(their_addr, addresses[i])) return 0; } addresses[knownpeers++] = their_addr; sprintf(buf, "%s %s %s", COMM[JOIN], user, NL); ssendto(sockfd, buf, &their_addr); printf("Sent join request to %s:%d\n", inet_ntoa(their_addr.sin_addr), ntohs(their_addr.sin_port)); return 1; } // host:port to struct void stringtoaddress(char *address, struct sockaddr_in *addrstruct) { struct hostent *he; char *colon, *p = address; int port = 0; while (*p && isspace(*p)) p++; // skip space colon = strchr(p, ':'); if (colon) { *colon = '\0'; port = atoi(colon+1); } if ((he=gethostbyname(p)) == NULL) { perror("gethostbyname"); return; } addrstruct->sin_family = AF_INET; addrstruct->sin_port = htons(port); addrstruct->sin_addr = *((struct in_addr *)he->h_addr); memset(&(addrstruct->sin_zero), '\0', 8); if (colon) *colon = ':'; } // stringtoaddress