- exit(1);
- }
-
- /* drop root privs if running setuid */
- setuid(getuid());
-
- memset(&pingaddr, 0, sizeof(struct sockaddr_in));
- pingaddr.sin_family = AF_INET;
- if (!(h = gethostbyname(host))) {
- fprintf(stderr, "ping: unknown host %s\n", host);
- exit(1);
- }
-
- if (h->h_addrtype != AF_INET) {
- fprintf(stderr, "ping: unknown address type; only AF_INET is currently supported.\n");
- exit(1);
- }
-
- pingaddr.sin_family = AF_INET; /* h->h_addrtype */
- memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
- strncpy(buf, h->h_name, sizeof(buf)-1);
- hostname = buf;
-
- /* enable broadcast pings */
- sockopt = 1;
- setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *)&sockopt, sizeof(sockopt));
-
- /* set recv buf for broadcast pings */
- sockopt = 48 * 1024;
- setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *)&sockopt, sizeof(sockopt));
-
- printf("PING %s (%s): %d data bytes\n",
- hostname, inet_ntoa(*(struct in_addr *)&pingaddr.sin_addr.s_addr),
- DEFDATALEN);
-
- signal(SIGINT, pingstats);
-
- /* start the ping's going ... */
- sendping(0);
-
- /* listen for replies */
- while (1) {
- struct sockaddr_in from;
- size_t fromlen = sizeof(from);
- int c;
-
- if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
- (struct sockaddr *)&from, &fromlen)) < 0) {
- if (errno == EINTR) continue;
- perror("ping");
- continue;
- }
- unpack(packet, c, &from);
- if (pingcount > 0 && nreceived >= pingcount) break;
- }
- pingstats(0);