⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendip.c.txt

📁 经过修改的sendip结构
💻 TXT
📖 第 1 页 / 共 2 页
字号:
		free(newmod);		return FALSE;	}	if(NULL==(get_optchar=dlsym(newmod->handle,"get_optchar"))) {		fprintf(stderr,"%s\n",dlerror());		dlclose(newmod->handle);		free(newmod);		return FALSE;	}	newmod->num_opts = n_opts();	newmod->optchar=get_optchar();	/* TODO: check uniqueness */	newmod->opts = get_opts();	num_opts+=newmod->num_opts;out:	newmod->pack=NULL;	newmod->prev=last;	newmod->next=NULL;	last = newmod;	if(last->prev) last->prev->next = last;	if(!first) first=last;	return TRUE;}static void print_usage(void) {	sendip_module *mod;	int i;	printf("Usage: %s [-v] [-d data] [-h] [-f datafile] [-p module] [module options] hostname\n",progname);	printf(" -d data\tadd this data as a string to the end of the packet\n");	printf(" -f datafile\tread packet data from file\n");	printf(" -h\t\tprint this message\n");	printf(" -p module\tload the specified module (see below)\n");	printf(" -v\t\tbe verbose\n");	printf("\n\nModules are loaded in the order the -p option appears.  The headers from\n");	printf("each module are put immediately inside the headers from the previos model in\n");	printf("the final packet.  For example, to embed bgp inside tcp inside ipv4, do\n");	printf("sendip -p ipv4 -p tcp -p bgp ....\n");	printf("\n\nModules available at compile time:\n");	printf("\tipv4 ipv6 icmp tcp udp bgp rip ntp\n\n");	for(mod=first;mod!=NULL;mod=mod->next) {		printf("\n\nArguments for module %s:\n",mod->name);		for(i=0;i<mod->num_opts;i++) {			printf("   -%c%s %c\t%s\n",mod->optchar,					  mod->opts[i].optname,mod->opts[i].arg?'x':' ',					  mod->opts[i].description);			if(mod->opts[i].def) printf("   \t\t  Default: %s\n", 												 mod->opts[i].def);		}	}}static void  add_module(){	num_opts = 0;		first=last=NULL;	sendip_module *mod;		/* magic random seed that gives 4 really random octets */	srandom(time(NULL) ^ (getpid()+(42<<15)));	load_module("ipv4");	load_module("tcp");	num_modules=2;//	/* Initialize all *///	for(mod=first;mod!=NULL;mod=mod->next) {//		mod->pack=mod->initialize();//	}}	static char* add_option(int argc,char*const argv[],int* datafile,int* datalen){       int i, longindex=0;        char* data=NULL;        char* rbuff[33];	bool usage=FALSE;	int optc;	sendip_module *mod;               if(!strcmp(argv[0],"-f")){                *datafile=open(argv[1],O_RDONLY);		if(*datafile == -1) {			perror("Couldn't open data file");			fprintf(stderr,"No data will be included\n");		} else {			*datalen = lseek(*datafile,0,SEEK_END);			if(*datalen == -1) {				perror("Error reading data file: lseek()"); 					                    fprintf(stderr,"No data will be included\n");				*datalen=0;			} else if(*datalen == 0) {				fprintf(stderr,"Data file is empty\nNo data will be included\n");			} else {				data = mmap(NULL,*datalen,PROT_READ,MAP_SHARED,*datafile,0);  					    if(data == MAP_FAILED) {					perror("Couldn't read data file: mmap()");					fprintf(stderr,"No data will be included\n");					data = NULL;					*datalen=0;				}			}		}         }       	/* Initialize all */	for(mod=first;mod!=NULL;mod=mod->next) {		mod->pack=mod->initialize();        }       /* Build the getopt listings */	struct option *opts;	opts = malloc((2+num_opts)*sizeof(struct option));	memset(opts,'\0',(2+num_opts)*sizeof(struct option));	i=0;	for(mod=first;mod!=NULL;mod=mod->next) {		int j;		char *s;   // nasty kludge because option.name is const		for(j=0;j<mod->num_opts;j++) {			opts[i].name = s = malloc(strlen(mod->opts[j].optname)+1);			sprintf(s,"%c%s",mod->optchar,mod->opts[j].optname);			opts[i].has_arg = mod->opts[j].arg;			opts[i].flag = NULL;			opts[i].val = mod->optchar;			i++;		}	}      /* Do the get opt */	gnuopterr=1;	gnuoptind=0;	while(EOF != (optc=getopt_long_only(argc,argv,"p:vd:hf:",opts,&longindex))) {				switch(optc) {		case 'p':		case 'v':		case 'd':		case 'h':			break;		case ':':			usage=TRUE;			fprintf(stderr,"Option %s requires an argument\n",					  opts[longindex].name);			break;		case '?':			usage=TRUE;			fprintf(stderr,"Option starting %c not recognized\n",gnuoptopt);			break;		default:			for(mod=first;mod!=NULL;mod=mod->next) {				if(mod->optchar==optc) {										/* Random option arguments */					if(gnuoptarg != NULL && !strcmp(gnuoptarg,"r")) {						/* need a 32 bit number, but random() is signed and							nonnegative so only 31bits - we simply repeat one */						unsigned long r = (unsigned long)random()<<1;						r+=(r&0x00000040)>>6;						sprintf(rbuff,"%lu",r);						gnuoptarg = rbuff;					}					if(!mod->do_opt(opts[longindex].name,gnuoptarg,mod->pack)) {						usage=TRUE;					}				}			}		}	}       /* gnuoptind is the first thing that is not an option - should have exactly		one hostname...	*/	/*if(argc != gnuoptind+1) { 		usage=TRUE;		if(argc-gnuoptind < 1) fprintf(stderr,"No hostname specified\n");		else fprintf(stderr,"More than one hostname specified\n");	} else {*/		if(first && first->set_addr) {			first->set_addr("192.168.1.107",first->pack);		}	//}       free(opts);       return data;}	void  packet_data(char* data,int datalen,int datafile,sendip_data* packet){      int i;      sendip_module *mod;      /* EVIL EVIL EVIL! */	/* Stick all the bits together.  This means that finalize better not		change the size or location of any packet's data... */	packet->data = NULL;	packet->alloc_len = 0;	packet->modified = 0;	for(mod=first;mod!=NULL;mod=mod->next) {		packet->alloc_len+=mod->pack->alloc_len;	}	if(data != NULL) packet->alloc_len+=datalen;	packet->data = malloc(packet->alloc_len);	for(i=0, mod=first;mod!=NULL;mod=mod->next) {		memcpy(packet->data+i,mod->pack->data,mod->pack->alloc_len);		free(mod->pack->data);		mod->pack->data = packet->data+i;		i+=mod->pack->alloc_len;	}	/* Add any data */	if(data != NULL) memcpy(packet->data+i,data,datalen);	if(datafile != -1) {		munmap(data,datalen);		close(datafile);		datafile=-1;	}	       /* Finalize from inside out */	{		char hdrs[num_modules];		sendip_data *headers[num_modules];		sendip_data d;		d.alloc_len = datalen;		d.data = packet->data+packet->alloc_len-datalen;		for(i=0,mod=first;mod!=NULL;mod=mod->next,i++) {			hdrs[i]=mod->optchar;			headers[i]=mod->pack;		}		for(i=num_modules-1,mod=last;mod!=NULL;mod=mod->prev,i--) {						/* Remove this header from enclosing list */			hdrs[i]='\0';			headers[i] = NULL;			mod->finalize(hdrs, headers, &d, mod->pack);			/* Get everything ready for the next call */			d.data-=mod->pack->alloc_len;			d.alloc_len+=mod->pack->alloc_len;		}	   }      }int mysend(  sendip_data* packet,int i){       char data[10];	 strcpy(data,"ok");	       	/* And send the packet */	{		int af_type;		if(first==NULL) {			if(data == NULL) {				fprintf(stderr,"Nothing specified to send!\n");				print_usage();				unload_modules(FALSE,verbosity);				return 1;			} else {				af_type = AF_INET;			}		}		else if(first->optchar=='i') af_type = AF_INET;		else if(first->optchar=='6') af_type = AF_INET6;		else {			fprintf(stderr,"Either IPv4 or IPv6 must be the outermost packet\n");			unload_modules(FALSE,verbosity);			return 1;		}		i = sendpacket(packet,mylist[i].list[mylist[i].para_num],af_type,verbosity);	}	return 0;}void main(void){          int datafile=-1,datalen=0,i;     char* data=NULL;     sendip_data packet[10];     add_module();    for(i=0;i<10;i++){     datafile=-1;datalen=0;data=NULL;     data=add_option(mylist[i].para_num,  mylist[i].list, &datafile, &datalen);     packet_data( data,datalen, datafile, &packet[i]);     munmap(data,datalen);    }     for(i=0;i<10;i++)  mysend(&packet[i],i);     unload_modules(FALSE,verbosity);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -