Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <netinet/ip.h>
- #include <netinet/tcp.h>
- #include <netinet/udp.h>
- #include <netinet/ip_icmp.h>
- #include <netinet/if_ether.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <pcap/pcap.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <ifaddrs.h>
- #include <netdb.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <net/if.h>
- #include <ctype.h>
- #include <errno.h>
- #include <signal.h>
- //the packet length
- #define PCKT_LEN 8192
- #define HOSTNAME_LEN 253 //domain name/IP addr can be max 253 characters long
- #define PORTS_LEN 128 //size for array with ports to scan
- pcap_t *pcap;
- /* ethernet headers are always exactly 14 bytes [1] */
- #define SIZE_ETHERNET 14
- /* Ethernet header */
- struct sniff_ethernet {
- u_char ether_dhost[ETHER_ADDR_LEN]; /* destination host address */
- u_char ether_shost[ETHER_ADDR_LEN]; /* source host address */
- u_short ether_type; /* IP? ARP? RARP? etc */
- };
- #define IP_HL(ip) (((ip)->ip_hl) & 0x0f)
- /*******************************************************************
- * Title: rawudp.c
- * Date: 20.4.2019
- * Code version:
- * Availability: https://www.tenouk.com/Module43a.html
- ********************************************************************/
- // UDP header's structure
- struct udpheader {
- unsigned short int udph_srcport;
- unsigned short int udph_destport;
- unsigned short int udph_len;
- unsigned short int udph_chksum;
- };
- /* Structure of a TCP header */
- struct tcpheader {
- unsigned short int tcph_srcport;
- unsigned short int tcph_destport;
- unsigned int tcph_seqnum;
- unsigned int tcph_acknum;
- unsigned char tcph_reserved:4;//, tcph_offset:4;
- unsigned char th_offx2; /* data offset, rsvd */
- #define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)
- unsigned char tcph_flags;
- unsigned int
- tcp_res1:4, /*little-endian*/
- tcph_hlen:4, /*length of tcp header in 32-bit words*/
- tcph_fin:1, /*Finish flag "fin"*/
- tcph_syn:1, /*Synchronize sequence numbers to start a connection*/
- tcph_rst:1, /*Reset flag */
- tcph_psh:1, /*Push, sends data to the application*/
- tcph_ack:1, /*acknowledge*/
- tcph_urg:1, /*urgent pointer*/
- tcph_res2:2;
- unsigned short int tcph_win;
- unsigned short int tcph_chksum;
- unsigned short int tcph_urgptr;
- };
- // Function for checksum calculation. From the RFC,
- // the checksum algorithm is:
- // "The checksum field is the 16 bit one's complement of the one's
- // complement sum of all 16 bit words in the header. For purposes of
- // computing the checksum, the value of the checksum field is zero."
- unsigned short csum(unsigned short *buf, int nwords)
- {
- unsigned long sum;
- for (sum = 0; nwords > 0; nwords--)
- {
- sum += *buf++;
- }
- sum = (sum >> 16) + (sum &0xffff);
- sum += (sum >> 16);
- return (unsigned short)(~sum);
- }
- /*
- * END OF REUSED CODE
- */
- //from linux man page: a pcap_handler routine to be called with three arguments: a u_char pointer
- //which is passed in the user argument to pcap_loop() or pcap_dispatch(), a const struct
- //pcap_pkthdr pointer pointing to the packet time stamp and lengths, and a const u_char pointer
- //to the first caplen (as given in the struct pcap_pkthdr a pointer to which is passed to the
- //callback routine) bytes of data from the packet. The struct pcap_pkthdr and the packet data
- //are not to be freed by the callback routine, and are not guaranteed to be valid after the call‐
- //back routine returns; if the code needs them to be valid after the callback, it must make a
- //copy of them.
- //args = arguments to the packet handler
- //pkthdr = the pcap format packet header
- //ptr_caplen = the packet libcap has extracted from the network interface
- void udp_pcap_handler(u_char *args, const struct pcap_pkthdr *pkthdr, const u_char *ptr_caplen)
- {
- }
- void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
- {
- printf(" From: Janinka with love");
- static int count = 1; /* packet counter */
- /* declare pointers to packet headers */
- const struct sniff_ethernet *ethernet; /* The ethernet header [1] */
- const struct ip *ip; /* The IP header */
- const struct tcpheader *tcp; /* The TCP header */
- const char *payload; /* Packet payload */
- int size_ip;
- int size_tcp;
- int size_payload;
- printf("\nPacket number %d:\n", count);
- count++;
- /* define ethernet header */
- ethernet = (struct sniff_ethernet*)(packet);
- /* define/compute ip header offset */
- ip = (struct ip*)(packet + SIZE_ETHERNET);
- //size_ip =
- //size_ip = IP_HL(ip)*4;
- if (size_ip < 20) {
- printf(" * Invalid IP header length: %u bytes\n", size_ip);
- // return;
- }
- /* print source and destination IP addresses */
- printf(" From: %s\n", inet_ntoa(ip->ip_src));
- printf(" To: %s\n", inet_ntoa(ip->ip_dst));
- /* determine protocol */
- switch(ip->ip_p) {
- case IPPROTO_TCP:
- printf(" Protocol: TCP\n");
- tcp = (struct tcpheader *)(packet + SIZE_ETHERNET + size_ip);
- if( tcp->tcph_flags == 0x14 )
- printf(" closed");
- else
- printf(" open");
- break;
- case IPPROTO_UDP:
- printf(" Protocol: UDP\n");
- return;
- case IPPROTO_ICMP:
- printf(" Protocol: ICMP\n");
- return;
- case IPPROTO_IP:
- printf(" Protocol: IP\n");
- return;
- default:
- printf(" Protocol: unknown\n");
- return;
- }
- /*
- * OK, this packet is TCP.
- */
- /* define/compute tcp header offset */
- tcp = (struct tcpheader*)(packet + SIZE_ETHERNET + size_ip);
- size_tcp = TH_OFF(tcp)*4;
- if (size_tcp < 20) {
- printf(" * Invalid TCP header length: %u bytes\n", size_tcp);
- //return;
- }
- printf(" Src port: %d\n", ntohs(tcp->tcph_srcport));
- printf(" Dst port: %d\n", ntohs(tcp->tcph_destport));
- /* define/compute tcp payload (segment) offset */
- payload = (u_char *)(packet + SIZE_ETHERNET + size_ip + size_tcp);
- /* compute tcp payload (segment) size */
- size_payload = ntohs(ip->ip_len) - (size_ip + size_tcp);
- /*
- * Print payload data; it might be binary, so don't just
- * treat it as a string.
- */
- if (size_payload > 0) {
- printf(" Payload (%d bytes):\n", size_payload);
- //print_payload(payload, size_payload);
- }
- return;
- }
- void alarm_handler(int sig)
- {
- pcap_breakloop(pcap);
- }
- int main(int argc, char *argv[])
- {
- char interface[PORTS_LEN] = "";
- char tcp_ports[PORTS_LEN] = "";
- char udp_ports[PORTS_LEN] = "";
- char scanned_computer[HOSTNAME_LEN];//will contain domain name or IP adderss of the scanned machine
- struct addrinfo hints, *info, *res;
- struct ifreq *my_ifreq;
- char errbuf[PCAP_ERRBUF_SIZE];
- int sd, sendfd, recvfd;
- bool udp_range = false, tcp_range = false;
- char *udp_parse, *tcp_parse;
- struct servent *srvport;
- struct ifaddrs *ifaddr, *ifa;
- int family;
- struct sockaddr_in *sa;
- char *addr;
- char buffer[PCKT_LEN];
- struct ip *iph = (struct ip *) buffer;
- struct udpheader *udp = (struct udpheader *) (buffer + sizeof(struct ip));
- struct tcpheader *tcp = (struct tcpheader *)(buffer + sizeof(struct ip));
- struct sockaddr_in sin, din;
- int one = 1;
- const int *val = &one;
- memset(buffer, 0, PCKT_LEN);
- //parse arguments
- if (argc < 3 || argc > 8)
- {
- fprintf(stderr, "Wrong combination of arguments\n");
- }
- for (int i = 1; i <= argc - 1 ; i += 2)
- {
- if (strcmp(argv[i], "-i") == 0)
- {
- strcpy(interface, argv[i + 1]);
- }
- else if ((strcmp(argv[i], "-pu") == 0) && (argv[i + 1] != NULL))
- {
- strcpy(udp_ports, argv[i + 1]);
- printf("UDP: %s\n", udp_ports);
- }
- else if (strcmp(argv[i], "-pt") == 0)
- {
- strcpy(tcp_ports, argv[i + 1]);
- printf("%s\n", tcp_ports);
- }
- else
- {
- strcpy(scanned_computer, argv[i]);
- i -= 1;
- }
- }
- if (scanned_computer == NULL)
- {
- fprintf(stderr, "IP address or domain name not specified\n");
- return -1;
- }
- else
- {
- //getting IP address of the scanned machine:
- struct hostent *he;
- he = gethostbyname(scanned_computer);
- if(he == NULL)
- {
- fprintf(stderr, "gethostbyname() error\n");
- return -1;
- }
- char *tmp_scanned_computer = inet_ntoa(*((struct in_addr*) he->h_addr_list[0]));
- strcpy(scanned_computer, tmp_scanned_computer);
- }
- int my_beautiful_port = 32769; //I do realise the user can meet the port, so setting a fix number
- //like this is not ideal, but I'm running out of time
- //getting target IP address
- /*******************************************************************
- * Title: "Server program"
- * Date: 20.4.2019
- * Code version:
- * Availability: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html
- ********************************************************************/
- memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_family = AF_UNSPEC; // allow IPv4 or IPv6
- hints.ai_socktype = SOCK_DGRAM; /* Datagram socket .. (or SOCK_STREAM?) */
- hints.ai_flags = AI_PASSIVE; /* For wildcard IP address (or AI_CANONNAME?)*/
- hints.ai_protocol = 0; /* Any protocol */
- hints.ai_canonname = NULL;
- hints.ai_addr = NULL;
- hints.ai_next = NULL;
- /*
- * END OF REUSED CODE
- */
- //get dst IP/domain name from args
- if (isdigit(scanned_computer[1]) == 0)
- {
- struct hostent *dst_host = gethostbyname(scanned_computer);
- if (dst_host == NULL)
- {
- fprintf(stderr, "Wrong domain name\n");
- return -1;
- strcpy(scanned_computer, dst_host->h_addr_list[0]);
- }
- }
- else
- {
- ;
- }
- //get source IP address
- if (getifaddrs(&ifaddr) == -1)
- {
- perror;
- return -1;
- }
- if (interface[0] == 0)
- {
- for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
- {
- if (ifa->ifa_addr == NULL)
- {
- continue;
- }
- if (0 == (ifa->ifa_flags & (IFF_LOOPBACK)) && (ifa->ifa_flags & (IFF_RUNNING))
- && (ifa->ifa_addr->sa_family == AF_INET))
- {
- family = ifa->ifa_addr->sa_family;//IPv4 or IPv6 asi AF_INET
- sa = (struct sockaddr_in *) ifa->ifa_addr;
- strcpy(interface, ifa->ifa_name);
- addr = inet_ntoa(sa->sin_addr);//source address
- break;
- }
- if (ifa->ifa_next == NULL)
- {
- fprintf(stderr, "couldn't resolve IP address\n");
- return -1;
- }
- }
- }
- else
- {
- for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
- {
- if ((strcmp(interface, ifa->ifa_name) == 0) && (ifa->ifa_addr->sa_family == AF_INET))
- {
- sa = (struct sockaddr_in *) ifa->ifa_addr;
- strcpy(interface, ifa->ifa_name);
- addr = inet_ntoa(sa->sin_addr);//source address
- break;
- }
- else
- {
- if (ifa->ifa_next == NULL)
- {
- fprintf(stderr, "couldn't resolve IP address\n");
- return -1;
- }
- continue;
- }
- }
- }
- //parse udp ports
- int scanned_udp_ports[PORTS_LEN];
- if(strstr(udp_ports, "-") != NULL)
- {
- //range of udp ports is given
- //int scanned_udp_ports[2];
- udp_range = true;
- int i = 0;
- while ((udp_parse = strtok( i ? NULL : udp_ports, "-")) != NULL)
- {
- if (i == 2)
- {
- fprintf(stderr, "wrong range format given for UDP ports\n");
- return 1;
- }
- scanned_udp_ports[i++] = atoi(udp_parse);
- }
- if (scanned_udp_ports[0] > scanned_udp_ports[1])
- {
- //if the user inserted range parameter in descending order, swap
- int tmp = scanned_udp_ports[0];
- scanned_udp_ports[0] = scanned_udp_ports[1];
- scanned_udp_ports[1] = tmp;
- }
- //check port numbers given: ????
- if ((scanned_udp_ports[2] - scanned_udp_ports[1]) > 65536)
- {
- fprintf(stderr, "Wrong port number given\n");
- return 1;
- }
- }
- else
- {
- int i = 0;
- while ((udp_parse = strtok( i ? NULL : udp_ports, ",")) != NULL)
- {
- //strcpy(scanned_udp_ports[i++], (udp_parse));
- scanned_udp_ports[i++] = atoi(udp_parse);
- }
- scanned_udp_ports[i] = 0;
- }
- //parse tcp ports
- int scanned_tcp_ports[PORTS_LEN];
- if (tcp_ports[0] != 0)
- {
- if (strstr(tcp_ports, "-") != NULL)
- {
- //range of tcp ports is given
- tcp_range = true;
- int i = 0;
- while ((tcp_parse = strtok( i ? NULL : tcp_ports, "-")) != NULL)
- {
- if (i == 2)
- {
- fprintf(stderr, "wrong range format given for TCP ports\n");
- return 1;
- }
- scanned_tcp_ports[i++] = atoi(tcp_parse);
- }
- if (scanned_tcp_ports[0] > scanned_tcp_ports[1])
- {
- //if the user inserted range parameter in descending order, swap
- int tmp = scanned_tcp_ports[0];
- scanned_tcp_ports[0] = scanned_tcp_ports[1];
- scanned_tcp_ports[1] = tmp;
- }
- //check port numbers given: ????
- if ((scanned_tcp_ports[1] - scanned_tcp_ports[0]) > 65536)
- {
- fprintf(stderr, "Wrong port number given\n");
- return 1;
- }
- }
- else
- {
- int i = 0;
- while ((tcp_parse = strtok( i ? NULL : tcp_ports, ",")) != NULL)
- {
- scanned_tcp_ports[i++] = atoi(tcp_parse);
- }
- scanned_tcp_ports[i] = '\0';
- }
- }
- //create socket for sending packets
- // sd = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
- // if (sd < 0)
- // {
- // perror("Socket() error");
- // return -1;
- // }
- //start scan
- printf("PORT\tSTATE\n");
- //UDP scan
- if (udp_ports != NULL)
- {
- /*******************************************************************
- * Title: rawudp.c
- * Date: 20.4.2019
- * Code version:
- * Availability: https://www.tenouk.com/Module43a.html
- ********************************************************************/
- sin.sin_family = AF_INET;
- din.sin_family = AF_INET;
- // Port numbers
- sin.sin_port = htons(my_beautiful_port);
- // IP addresses
- sin.sin_addr.s_addr = inet_addr(addr);//my hostname/ip
- din.sin_addr.s_addr = inet_addr(scanned_computer);
- // Fabricate the IP header or we can use the
- // standard header structures but assign our own values.
- iph->ip_hl = 5;
- iph->ip_v = 4;
- iph->ip_tos = 16; // Low delay
- iph->ip_len = sizeof(struct ip) + sizeof(struct udpheader);
- iph->ip_id = htonl(54321);
- iph->ip_ttl = 255; //64; // hops
- iph->ip_p = 17; // UDP
- iph->ip_off = 0;
- // Source IP address, can use spoofed address here!!!
- iph->ip_src.s_addr = inet_addr(addr);
- //h The destination IP address
- iph->ip_dst.s_addr = inet_addr(scanned_computer);
- // Fabricate the UDP header. Source port number, redundant
- udp->udph_srcport = htons(my_beautiful_port);
- // Destination port number
- udp->udph_len = htons(sizeof(struct udpheader));
- // Calculate the checksum for integrity
- iph->ip_sum = csum((unsigned short *)buffer, sizeof(struct ip) +
- sizeof(struct udpheader));
- iph->ip_len = sizeof(struct ip) + sizeof(struct udpheader);
- //create a raw socket with UDP protocol, IPv4
- //to receive ICMP response packets
- recvfd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
- if (recvfd < 0)
- {
- perror("UDP socket() error\n");
- return 1;
- }
- if (setsockopt(recvfd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
- {
- fprintf(stderr, "setsockopt() error\n");
- return -1;
- }
- /*
- * END OF REUSED CODE
- */
- if (udp_range)
- {
- for (int i = scanned_udp_ports[0]; i <= scanned_udp_ports[1]; i++)
- {
- udp->udph_destport = htons(i);
- din.sin_port = htons(i);
- iph->ip_sum = csum((unsigned short *)buffer,
- (sizeof(struct ip) + sizeof(struct udpheader)));
- //packet capture
- if ((pcap = pcap_open_live(interface, PCKT_LEN, 1, 500, errbuf)) == NULL)
- {
- fprintf(stderr, "pcap_open_live() error: %s\n", strerror(errno));
- pcap_freealldevs((pcap_if_t *)pcap);
- return -1;
- }
- struct bpf_program fp;
- if (pcap_compile(pcap, &fp, "icmp and icmp[icmptype] == icmp-unreach"
- , 0, PCAP_NETMASK_UNKNOWN) == -1)
- {
- fprintf(stderr, "pcap_compile() error\n");
- return -1;
- }
- if (pcap_setfilter(pcap, &fp) == -1)
- {
- fprintf(stderr, "pcap_setfilter() error\n");
- return -1;
- }
- //a UDP/ICMP packet could get lost on the way, therefore trying to repeat it a few times
- //unless I get a response
- int pcap_dispatch_ret_val;
- for(int j = 0; j <= 2; j++)
- {
- if (sendto(recvfd, buffer, iph->ip_len, 0, (struct sockaddr *) &sin, sizeof(sin)) < 0)
- {
- fprintf(stderr, "sending UDP packet failed %s\n", strerror(errno));
- return -1;
- }
- alarm(2);
- signal(SIGALRM, alarm_handler);
- pcap_dispatch_ret_val = pcap_dispatch(pcap, 1, udp_pcap_handler, NULL);
- if (pcap_dispatch_ret_val == 0)
- {
- if (j < 2)
- {
- continue;
- }
- else
- {
- printf("%d/udp\topen\n", i);
- }
- }
- else if (pcap_dispatch_ret_val == -1)
- {
- fprintf(stderr, "pcap_dispatch() error: %s\n", strerror(errno));
- return -1;
- }
- else if (pcap_dispatch_ret_val == -2)
- {
- if (j < 2)
- {
- continue;
- }
- else
- {
- printf("%d/udp\topen\n", i);
- }
- }
- else
- {
- printf("%d/udp\tclosed\n", i);
- }
- }
- pcap_close(pcap);
- }
- }
- else
- {
- for (int i = 0; scanned_udp_ports[i] != 0; i++)
- {
- udp->udph_destport = htons(i);
- din.sin_port = htons(i);
- iph->ip_sum = csum((unsigned short *)buffer, (sizeof(struct ip) + sizeof(struct udpheader)));
- //packet capture
- if ((pcap = pcap_open_live(interface, PCKT_LEN, 1, 500, errbuf)) == NULL)
- {
- fprintf(stderr, "pcap_open_live() error: %s\n", strerror(errno));
- pcap_freealldevs((pcap_if_t *)pcap);
- return -1;
- }
- struct bpf_program fp;
- if (pcap_compile(pcap, &fp, "icmp and icmp[icmptype] == icmp-unreach"
- , 0, PCAP_NETMASK_UNKNOWN) == -1)
- {
- fprintf(stderr, "pcap_compile() error\n");
- return -1;
- }
- if (pcap_setfilter(pcap, &fp) == -1)
- {
- fprintf(stderr, "pcap_setfilter() error\n");
- return -1;
- }
- //a UDP/ICMP packet could get lost on the way, therefore trying to test it a few times
- int pcap_dispatch_ret_val;
- for(int j = 0; j <= 2; j++)
- {
- if (sendto(recvfd, buffer, iph->ip_len, 0, (struct sockaddr *) &sin, sizeof(sin)) < 0)
- {
- fprintf(stderr, "sending UDP packet failed %s\n", strerror(errno));
- return -1;
- }
- alarm(2);
- signal(SIGALRM, alarm_handler);
- pcap_dispatch_ret_val = pcap_dispatch(pcap, 1, udp_pcap_handler, NULL);
- if (pcap_dispatch_ret_val == 0)
- {
- if (j < 2)
- {
- continue;
- }
- else
- {
- printf("%d/udp\topen\n", scanned_udp_ports[i]);
- }
- }
- else if (pcap_dispatch_ret_val == -1)
- {
- //not using pcap_breakloop, no need to check explicitly for -2
- //TODO: NOW I'm using breakloop
- fprintf(stderr, "pcap_dispatch() error: %s\n", strerror(errno));
- return -1;
- }
- else if (pcap_dispatch_ret_val == -2)
- {
- if (j < 2)
- {
- continue;
- }
- else
- {
- printf("%d/udp\topen\n", scanned_udp_ports[i]);
- }
- }
- else
- {
- printf("%d/udp\tclosed\n", scanned_udp_ports[i]);
- break;
- }
- }
- pcap_close(pcap);
- }
- }
- }
- //TCP scan
- if (tcp_ports != NULL)
- {
- // IP structure
- iph->ip_hl = 5;
- iph->ip_v = 4;
- iph->ip_tos = 16;
- iph->ip_len = sizeof(struct ip) + sizeof(struct tcpheader);
- iph->ip_id = htonl(54321);
- iph->ip_off = 0;
- iph->ip_ttl = 64;
- iph->ip_p = 6; // TCP
- iph->ip_sum = 0; // Done by kernel
- // Source IP, modify as needed, spoofed, we accept through command line argument
- iph->ip_src.s_addr = inet_addr(addr);
- // Destination IP, modify as needed, but here we accept through command line argument
- iph->ip_dst.s_addr = inet_addr(scanned_computer);
- sin.sin_addr.s_addr = inet_addr(addr);
- din.sin_addr.s_addr = inet_addr(scanned_computer);
- sin.sin_family = AF_INET;
- din.sin_family = AF_INET;
- // Source port, can be any, modify as needed
- sin.sin_port = htons(my_beautiful_port);
- ///
- sendfd = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
- if (sendfd < 0)
- {
- perror("socket() error");
- return -1;
- }
- if (tcp_range)
- {
- for (int i = scanned_tcp_ports[0]; i <= scanned_tcp_ports[1]; i++)
- {
- /*******************************************************************
- * Title: rawtcp.c
- * Date: 20.4.2019
- * Code version:
- * Availability: https://www.tenouk.com/Module43a.html
- ********************************************************************/
- tcp->tcph_srcport = htons(my_beautiful_port);
- tcp->tcph_destport = htons(i);
- tcp->tcph_seqnum = htonl(1);
- tcp->tcph_acknum = 0;
- tcp->th_offx2 = 5;
- tcp->tcph_syn = 1;
- tcp->tcph_ack = 0;
- tcp->tcph_win = htons(32767);
- tcp->tcph_chksum = 0; // Done by kernel
- tcp->tcph_urgptr = 0;
- din.sin_port = htons(i);
- iph->ip_sum = csum((unsigned short *)buffer,
- (sizeof(struct ip) + sizeof(struct tcpheader)));
- iph->ip_len = sizeof(struct ip) + sizeof(struct tcpheader);
- // Inform the kernel do not fill up the headers' structure, we fabricated our own
- if(setsockopt(sendfd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
- {
- perror("setsockopt() error");
- exit(-1);
- }
- /*
- * END OF REUSED CODE
- */
- char filter_exp[] = "port 32769 && port 23"; /* filter expression [3] */
- struct bpf_program fp; /* compiled filter program (expression) */
- bpf_u_int32 mask; /* subnet mask */
- bpf_u_int32 net; /* ip */
- int num_packets = 10;
- /* open capture device */
- pcap = pcap_open_live(interface, PCKT_LEN, 1, 1000, errbuf);
- if (pcap == NULL) {
- fprintf(stderr, "Couldn't open device %s: %s\n", interface, errbuf);
- exit(EXIT_FAILURE);
- }
- /* make sure we're capturing on an Ethernet device [2] */
- // if (pcap_datalink(pcap) != DLT_EN10MB) {
- // fprintf(stderr, "%s is not an Ethernet\n", interface);
- // exit(EXIT_FAILURE);
- // }
- /* compile the filter expression */
- if (pcap_compile(pcap, &fp, filter_exp, 0, net) == -1) {
- fprintf(stderr, "Couldn't parse filter %s: %s\n",
- filter_exp, pcap_geterr(pcap));
- exit(EXIT_FAILURE);
- }
- /* apply the compiled filter */
- if (pcap_setfilter(pcap, &fp) == -1) {
- fprintf(stderr, "Couldn't install filter %s: %s\n",
- filter_exp, pcap_geterr(pcap));
- exit(EXIT_FAILURE);
- }
- if(sendto(sendfd, buffer, iph->ip_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
- // Verify
- {
- perror("sendto() error");
- exit(-1);
- }
- printf("%d\n", i);
- /* now we can set our callback function */
- // pcap_loop(pcap, num_packets, got_packet, NULL);
- alarm(2);
- signal(SIGALRM, alarm_handler);
- int pcap_dispatch_ret_val;
- pcap_dispatch_ret_val = pcap_dispatch(pcap, 1, got_packet, NULL);
- if (pcap_dispatch_ret_val == -1)
- {
- fprintf(stderr, "pcap_dispatch() error\n");
- return -1;
- }
- else if (pcap_dispatch_ret_val <= 0)
- {
- printf("%d/tcp\topen\n", i);
- }
- else
- {
- printf("%d/tcp\tclosed\n",i);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement