/****** generator.c - This program generates simulated IP datagrams and ******/ /****** then sends them as byte (unsigned char) data in a UDP datagram to a ******/ /****** receiving program on a simulated router developed as a class project by ******/ /****** a student. The destination IP and port number of the target receiver is ******/ /****** defined below and can be changed to suit the environment. This program ******/ /****** has been developed for use with a network-layer programming lab, and ******/ /****** is provided for student use in testing their program per the assignment ******/ /****** guidelines. Only very limited error checking and/or protection is provided. ******/ /****** Data in the IPpacket and is a randomly generator sequence of char values. ******/ /****** This code should not be modified in any way to make your program work ******/ /****** properly. Your program will be demonstrated with the instructor's original ******/ /****** version. ******/ /****** Developed by R. Mohan and M. O'Dell for CSE 5344, November 2006. ******/ /****** Usage: generator /*Preprocessor*/ #define WIN //BSD for BSD sockets and WIN for Winsock #include #include #include #include #include #ifdef WIN #include #endif #ifdef BSD #include #include #include #include #include #include #endif /* Fixed IP Address and Port Number */ #define IP_ADDR "127.0.0.1" #define PORT_NUM 1050 /*Definition of structures*/ typedef struct ip_packet ippacket; typedef ippacket* ippacket1; struct ip_packet{ unsigned ihl: 4; /* These two fields reversed to account for Little-Endian */ unsigned version: 4; /* byte ordering. Must change if Big-Endian */ unsigned tos: 8; unsigned total_len: 16; unsigned id: 16; unsigned frag: 16; unsigned ttl: 8; unsigned protocol: 8; unsigned checksum: 16; unsigned sourceaddr: 32; unsigned destinationaddr: 32; // No IP options // IP data field follows unsigned sourceport: 16; unsigned destinationport: 16; char data[]; }; /* Prototype Declarations */ ippacket1* out_pkt_generator(); void packetinfo(ippacket1 packet); void sendIP(ippacket1 packet); /* Definition of functions */ ippacket1* out_pkt_generator(){ static int pkt_id; static int count=0; int nopackets; unsigned sourceport,destinationport; unsigned len, sourceip,destinationip,protocol; int s1,s2,s3,s4,d1,d2,d3,d4; unsigned short no1,no2,no3,no4,no5,no6,no7,no8,no9,no10,no11,no12,no13; unsigned short checksum; unsigned long temp; char addr_change; int i,m,temp1,temp2; char* info; char dot; char end; // Get source IP address and destination network prefix printf("Set up or change IP addresses (y/n):"); if(count==0) { addr_change=getchar(); count++; } else { addr_change=getchar(); addr_change=getchar(); } if (addr_change=='y' | addr_change=='Y') { printf("Enter source ip address: "); scanf("%d.%d.%d.%d%c",&s1,&s2,&s3,&s4,&end); if(end=='.') { printf("Invalid IP Address\n"); exit(-1); } if(s1>255 | s2>255 | s3>255 | s4>255) { printf("Invalid IP Address\n"); exit(-1); } sourceip=(s1<<24)|(s2<<16)|(s3<<8)|s4; printf("Enter destination ip network address (e.g., 123.255): "); scanf("%d.%d%c",&d1,&d2,&dot); /* Error Checking */ if(dot=='.') { printf("Invalid IP Address\n"); exit(-1); } if(d1>255 | d2>255) { printf("Invalid destination network address.\n"); exit(-1); } destinationip=(d1<<24)|(d2<<16)|0<<8|0; } else { //use pre-defined addresses printf("Using predefined addresses.\n"); //Source IP s1=222; s2=222; s3=111; s4=111; //Network prefix //Change it according to the network topology defined d1=223; d2=16; } srand(time(NULL)); d3=rand()%256; d4=rand()%256; sourceip=(s1<<24)|(s2<<16)|(s3<<8)|s4; destinationip=(d1<<24)|(d2<<16)|d3<<8|d4; /* Random Generation of source and destination port numbers */ temp1=rand()%2; if(temp1==0) destinationport=25; else destinationport=80; temp2=rand()%10000; while(temp2<2000) temp2=rand()%10000; sourceport=temp2; printf("Enter the size of data: "); scanf("%u",&len); info=(char*) malloc (len); printf("Enter the next protocol: "); scanf("%u",&protocol); //Random number generation of protocol type /* protocol = rand()%256; */ printf("Enter Number of packets in a stream/burst: "); scanf("%d",&nopackets); printf("\nNo of packets %d\n\n",nopackets); if(nopackets>0) { ippacket1* packet; packet = (ippacket1*)malloc(nopackets*(sizeof(ippacket))); pkt_id=rand()%(65536); for(i=0;iihl= 5; packet[i]->version = 4; packet[i]->tos=0; packet[i]->total_len=(packet[i]->ihl*4)+4+len; packet[i]->id=pkt_id++; packet[i]->frag=0; packet[i]->ttl=rand()%8 + 1; packet[i]->protocol=protocol; printf("The packet id is %d\n",packet[i]->id); printf("The ttl for this packet is %d\n",packet[i]->ttl); /* Checksum Computation */ no1 = packet[i]->version; no1=no1<<12; no2=packet[i]->ihl; no2=no2<<8; no3=no1 | no2 | packet[i]->tos; no4=packet[i]->ttl; no4=no4<<8; no5=no4 | packet[i]->protocol; no6=s1; no6=no6<<8; no7=no6 | s2; no8=s3; no8=no8<<8; no9=no8 | s4; no10=d1; no10=no10<<8; no11=no10 | d2; no12=d3; no12=no12<<8; no13=no12 | d4; temp = no3 + packet[i]->total_len + packet[i]->id + packet[i]->frag + no5 + no7 + no9 + no11 + no13; while(temp>>16) temp = (temp&0xFFFF) + (temp>>16); checksum = ~(temp); printf("Checksum is: %d\n", checksum); packet[i]->checksum=checksum; packet[i]->sourceaddr = htonl(sourceip); packet[i]->destinationaddr= htonl(destinationip); packet[i]->sourceport=sourceport; packet[i]->destinationport=destinationport; printf("Source port number is %u\n",packet[i]->sourceport); printf("Destination port number is %u\n",packet[i]->destinationport); if (len>0){ for(m=0;mdata[j]=info[j]; printf("The data contained in packet %d is: %s\n",i+1,packet[i]->data); } sendIP(packet[i]); // prepare and send the packet packetinfo(packet[i]); //print source and destination IP addresses in generated packet } return packet; } else exit(0); } void packetinfo(ippacket1 packet) { unsigned short a1,a2,a3,a4,a5,a6,a7,a8; a1=(packet->destinationaddr) & 0x000000ff; a2=((packet->destinationaddr)>>8) & 0x000000ff; a3=((packet->destinationaddr)>>16) & 0x000000ff; a4=((packet->destinationaddr)>>24) & 0x000000ff; a5=(packet->sourceaddr) & 0x000000ff; a6=((packet->sourceaddr)>>8) & 0x000000ff; a7=((packet->sourceaddr)>>16) & 0x000000ff; a8=((packet->sourceaddr)>>24) & 0x000000ff; printf("Source IP Add : %d.%d.%d.%d\n",a5,a6,a7,a8); printf("Destination IP Add: %d.%d.%d.%d\n\n",a1,a2,a3,a4); } void sendIP (ippacket1 packet) { #ifdef WIN WORD wVersionRequested = MAKEWORD(1,1); // Stuff for WSA functions WSADATA wsaData; // Stuff for WSA functions #endif unsigned int client_s; // Client socket descriptor struct sockaddr_in server_addr; // Server Internet address int addr_len; // Internet address length int i; unsigned int pkt_len; unsigned char* out_buf; union pkt { ippacket1 pkt_buf; unsigned char* send_buf; } packet_view; pkt_len = packet->total_len; /* Convert to Little-Endian byte ordering where necessary*/ packet->total_len = htons(packet->total_len); packet->id = htons(packet->id); packet->frag = htons(packet->frag); packet->checksum = htons(packet->checksum); /* Set up the union to convert packet structure to byte array*/ packet_view.pkt_buf = (ippacket1) malloc (pkt_len); packet_view.pkt_buf = packet; out_buf = (unsigned char*) malloc (pkt_len); /* Move packet contents to out_buf for transmission in udp datagram */ for (i=0; i < pkt_len; i++) out_buf[i] = packet_view.send_buf[i]; #ifdef WIN /* This stuff initializes winsock */ WSAStartup(wVersionRequested, &wsaData); #endif /* Create a datagram socket */ /* AF_INET is Address Family Internet and SOCK_DGRAM is datagram */ client_s = socket(AF_INET, SOCK_DGRAM, 0); /* Fill-in "router" socket address information */ server_addr.sin_family = AF_INET; // Address family to use server_addr.sin_port = htons(PORT_NUM); // Port num to use server_addr.sin_addr.s_addr = inet_addr(IP_ADDR); // IP address to use /* Print what will be sent */ printf("\nSending packet.........\nIP Header is: "); for (i = 0; i<20; i++) printf("%x ",out_buf[i]); printf("\nSource port # is %d", (out_buf[21])<<8|(out_buf[20])); printf("\nDestination port # is %d", (out_buf[23])<<8|(out_buf[22])); printf("\nIP Packet data is: "); for (i = 24; i