int main(int argc, char **argv) { int c; struct addrinfo *ai; char *h; opterr = 0; /* don't want getopt() writing to stderr */ while ( (c = getopt(argc, argv, "m:v")) != -1) { switch (c) { case 'm': if ( (max_ttl = atoi(optarg)) <= 1) err_quit("invalid -m value"); break; case 'v': verbose++; break; case '?': err_quit("unrecognized option: %c", c); } } if (optind != argc-1) err_quit("usage: traceroute [ -m <maxttl> -v ] <hostname>"); host = argv[optind]; pid = getpid(); Signal(SIGALRM, sig_alrm); ai = Host_serv(host, NULL, 0, 0); h = Sock_ntop_host(ai->ai_addr, ai->ai_addrlen); printf("traceroute to %s (%s): %d hops max, %d data bytes\n", ai->ai_canonname ? ai->ai_canonname : h, h, max_ttl, datalen); /* initialize according to protocol */ if (ai->ai_family == AF_INET) { pr = &proto_v4; #ifdef IPV6 } else if (ai->ai_family == AF_INET6) { pr = &proto_v6; if (IN6_IS_ADDR_V4MAPPED(&(((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr))) err_quit("cannot traceroute IPv4-mapped IPv6 address"); #endif } else err_quit("unknown address family %d", ai->ai_family); pr->sasend = ai->ai_addr; /* contains destination address */ pr->sarecv = Calloc(1, ai->ai_addrlen); pr->salast = Calloc(1, ai->ai_addrlen); pr->sabind = Calloc(1, ai->ai_addrlen); pr->salen = ai->ai_addrlen; traceloop(); exit(0); }
void start_connect(struct file *fptr) { int fd, flags, n; struct addrinfo *ai; ai = Host_serv(fptr->f_host, SERV, 0, SOCK_STREAM); fd = Socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); fptr->f_fd = fd; printf("start_connect for %s, fd %d\n", fptr->f_name, fd); /* 4Set socket nonblocking */ flags = Fcntl(fd, F_GETFL, 0); Fcntl(fd, F_SETFL, flags | O_NONBLOCK); /* 4Initiate nonblocking connect to the server. */ if ( (n = connect(fd, ai->ai_addr, ai->ai_addrlen)) < 0) { if (errno != EINPROGRESS) err_sys("nonblocking connect error"); fptr->f_flags = F_CONNECTING; FD_SET(fd, &rset); /* select for reading and writing */ FD_SET(fd, &wset); if (fd > maxfd) maxfd = fd; } else if (n >= 0) /* connect is already done */ write_get_cmd(fptr); /* write() the GET command */ }
void start_connect(struct file *fptr) { int fd, flags, n; struct addrinfo *ai; ai = Host_serv(fptr -> f_host, SERV, 0, SOCK_STREAM); fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); fptr->f_fd = fd; printf("start_connect for %s, fd %d\n", fptr->f_name, fd); flags = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, flags | O_NONBLOCK); if((n = connect(fd, ai->ai_addr, ai->ai_addrlen)) < 0) { if(errno != EINPROGRESS) err_sys("nonblocking connect error"); fptr->f_flags = F_CONNECTING; FD_SET(fd, &rset); FD_SET(fd, &wset); maxfd = max(maxfd, fd); } else write_get_cmd(fptr); }
int main(int argc, char **argv) { int c, sockfd, len = 0; u_char *ptr = NULL; struct addrinfo *ai; if (argc < 2) err_quit("usage: tcpcli01 [ -[gG] <hostname> ... ] <hostname>"); opterr = 0; /* don't want getopt() writing to stderr */ while ( (c = getopt(argc, argv, "gG")) != -1) { switch (c) { case 'g': /* loose source route */ if (ptr) err_quit("can't use both -g and -G"); ptr = inet_srcrt_init(0); break; case 'G': /* strict source route */ if (ptr) err_quit("can't use both -g and -G"); ptr = inet_srcrt_init(1); break; case '?': err_quit("unrecognized option: %c", c); } } if (ptr) while (optind < argc-1) len = inet_srcrt_add(argv[optind++]); else if (optind < argc-1) err_quit("need -g or -G to specify route"); if (optind != argc-1) err_quit("missing <hostname>"); ai = Host_serv(argv[optind], SERV_PORT_STR, AF_INET, SOCK_STREAM); sockfd = Socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (ptr) { len = inet_srcrt_add(argv[optind]); /* dest at end */ Setsockopt(sockfd, IPPROTO_IP, IP_OPTIONS, ptr, len); free(ptr); } Connect(sockfd, ai->ai_addr, ai->ai_addrlen); str_cli(stdin, sockfd); /* do it all */ exit(0); }
int main(int argc, char** argv) { int c; struct addrinfo* ai; char* h; opterr = 0; /* don't want getopt() writing to stderr */ while ((c = getopt(argc, argv, "v")) != -1) { switch (c) { case 'v': verbose++; break; case '?': err_quit("unrecognized option: %c", c); } } if (optind != argc - 1) err_quit("usage: ping [ -v ] <hostname>"); host = argv[optind]; pid = getpid() & 0xffff; /* ICMP ID field is 16 bits */ Signal(SIGALRM, sig_alrm); ai = Host_serv(host, NULL, 0, 0); h = Sock_ntop_host(ai->ai_addr, ai->ai_addrlen); printf("PING %s (%s): %d data bytes\n", ai->ai_canonname ? ai->ai_canonname : h, h, datalen); /* 4initialize according to protocol */ if (ai->ai_family == AF_INET) { pr = &proto_v4; #ifdef IPV6 } else if (ai->ai_family == AF_INET6) { pr = &proto_v6; if (IN6_IS_ADDR_V4MAPPED( &(((struct sockaddr_in6*)ai->ai_addr)->sin6_addr))) err_quit("cannot ping IPv4-mapped IPv6 address"); #endif } else err_quit("unknown address family %d", ai->ai_family); pr->sasend = ai->ai_addr; pr->sarecv = Calloc(1, ai->ai_addrlen); pr->salen = ai->ai_addrlen; readloop(); exit(0); }
int main (int argc, char **argv) { int c; struct addrinfo *ai; char *host; int loop_ttl (); opterr = 0; while ( (c = getopt (argc, argv, "m:")) != -1) { switch (c) { case 'm': if ( (max_ttl = atoi(optarg)) <= 1) { err_quit ("invalid -m value"); } break; default: err_quit ("unrecognizd option: %c", c); } } if (optind != argc - 1) { err_quit ("usage: trout [ -m <maxttl>] <hostname>"); } host = argv[optind]; ai = Host_serv (host, NULL, 0, 0); printf ("trout to %s (%s): %d hops max, %d data bytes\n", ai->ai_canonname, Sock_ntop_host (ai->ai_addr, ai->ai_addrlen), max_ttl, datalen); if (ai->ai_family != AF_INET) { err_quit ("unknown address family %d", ai->ai_family); } sasend = ai->ai_addr; salen = ai->ai_addrlen; sarecv = Calloc (1, salen); salast = Calloc (1, salen); sabind = Calloc (1, salen); exit (0); }
void start_connect(struct file *fptr) { int fd, flags, n; struct addrinfo *ai; ai = Host_serv(fptr->f_host, SERV, 0, SOCK_STREAM); fd = Socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); fptr->f_fd = fd; printf("start_connect for %s, fd %d\n", fptr->f_name, fd); /* 4Set socket nonblocking */ flags = Fcntl(fd, F_GETFL, 0); Fcntl(fd, F_SETFL, flags | O_NONBLOCK); /* 4Initiate nonblocking connect to the server. */ if ( (n = connect(fd, ai->ai_addr, ai->ai_addrlen)) < 0) { if (errno != EINPROGRESS) err_sys("nonblocking connect error"); fptr->f_flags = F_CONNECTING; //register int ret = 0; struct kevent changes[1]; EV_SET(&changes[0], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); ret = kevent(kq, changes, 1, NULL, 0, NULL); if (ret < 0) { err_sys("kevent()"); } EV_SET(&changes[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL); ret = kevent(kq, changes, 1, NULL, 0, NULL); if (fd > maxfd) maxfd = fd; } else if (n >= 0) /* connect is already done */ { write_get_cmd(fptr); /* write() the GET command */ } }
int main(int argc, char **argv) { int sockfd; socklen_t salen; struct addrinfo *res; struct sockaddr *cli, *serv; if (argc != 2) err_quit("usage: test2 <IPaddress>"); #ifdef notdef res = Host_serv(argv[1], "daytime", AF_UNSPEC, SOCK_DGRAM); printf("res->ai_addrlen = %d\n", res->ai_addrlen); printf("res->ai_addr = %p\n", res->ai_addr); printf("res->ai_next = %p\n", res->ai_next); printf("res->ai_addr->sa_family = %p\n", res->ai_addr->sa_family); #endif sockfd = Udp_client(argv[1], "13", (void **) &serv, &salen); printf("sockfd = %d\n", sockfd); exit(0); }
int main(int argc, char *argv[]) { int c, lopt=0; char *ptr, localname[1024], *localport; struct addrinfo *aip; /* end main1 */ /* include main2 */ opterr = 0; /* don't want getopt() writing to stderr */ while ( (c = getopt(argc, argv, "0i:l:v")) != -1) { switch (c) { case '0': zerosum = 1; break; case 'i': device = optarg; /* pcap device */ break; case 'l': /* local IP address and port #: a.b.c.d.p */ if ( (ptr = strrchr(optarg, '.')) == NULL) usage("invalid -l option"); *ptr++ = 0; /* null replaces final period */ localport = ptr; /* service name or port number */ strncpy(localname, optarg, sizeof(localname)); lopt = 1; break; case 'v': verbose = 1; break; case '?': usage("unrecognized option"); } } /* end main2 */ /* include main3 */ if (optind != argc-2) usage("missing <host> and/or <serv>"); /* 4convert destination name and service */ aip = Host_serv(argv[optind], argv[optind+1], AF_INET, SOCK_DGRAM); dest = aip->ai_addr; /* don't freeaddrinfo() */ destlen = aip->ai_addrlen; /* * Need local IP address for source IP address for UDP datagrams. * Can't specify 0 and let IP choose, as we need to know it for * the pseudoheader to calculate the UDP checksum. * If -l option supplied, then use those values; otherwise, * connect a UDP socket to the destination to determine the right * source address. */ if (lopt) { /* 4convert local name and service */ aip = Host_serv(localname, localport, AF_INET, SOCK_DGRAM); local = aip->ai_addr; /* don't freeaddrinfo() */ locallen = aip->ai_addrlen; } else { int s; s = Socket(AF_INET, SOCK_DGRAM, 0); Connect(s, dest, destlen); /* kernel chooses correct local address for dest */ locallen = sizeof(locallookup); local = (struct sockaddr *)&locallookup; Getsockname(s, local, &locallen); if (locallookup.sin_addr.s_addr == htonl(INADDR_ANY)) err_quit("Can't determine local address - use -l\n"); close(s); } open_output(); /* open output, either raw socket or libnet */ open_pcap(); /* open packet capture device */ setuid(getuid()); /* don't need superuser privileges anymore */ Signal(SIGTERM, cleanup); Signal(SIGINT, cleanup); Signal(SIGHUP, cleanup); test_udp(); cleanup(0); }