int connect_dest(char *host_name, char *port) { struct hostent *host; struct servent *service; struct sockaddr_in addr; int sock; addr.sin_family = AF_INET; if ((addr.sin_addr.s_addr = inet_addr(host_name)) == (in_addr_t) -1) { if ((host = gethostbyname(host_name)) != NULL) { memcpy((char *) &addr.sin_addr, host->h_addr, host->h_length); } else { fprintf(stderr, "unknown host %s\n", host_name); return -1; } } if ((addr.sin_port = htons(atoi(port))) == 0) { if ((service = getservbyname(port, "tcp")) != NULL) { addr.sin_port = service->s_port; } else { fprintf(stderr, "unrecognized port %s\n", port); return -1; } } check((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)), "socket", -1); show_addr("Connecting to", &addr); check(connect(sock, (struct sockaddr *)&addr, sizeof(addr)), "connect", -1); return sock; }
void got_packet (u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { static int count = 1; int etype=0, protocol=0; int size_ip, size_tcp, size_total; const struct sniff_ethernet * ethernet; const struct sniff_ip * ip; const struct sniff_tcp *tcp; ethernet = (struct sniff_ethernet*)(packet); ip = (struct sniff_ip*)(packet + 14); size_ip = IP_HL(ip); tcp = (struct sniff_tcp*)(packet + 14 + size_ip); size_tcp = TH_OFF(tcp)*4; size_total = ntohs(ip->ip_len) + 14; printf("\ncount : %d\n", count); count++; printf("------------------------------\n"); etype = show_addr(packet); if (etype == IPV4) { protocol = show_ipv4_ip(packet); if (protocol == TCP) { show_port(packet); if (size_total != (size_ip + size_tcp + 14)) { printf("------------------------------\n"); show_data(args, header, packet, *(packet+size_total), size_total); printf("------------------------------\n"); } } else if (protocol == UDP) { show_port(packet); printf("------------------------------\n"); show_data(args, header, packet, 42, size_total); printf("------------------------------\n"); } printf("------------------------------\n"); } else if (etype == ARP) { show_ark_ip(packet); } }