/** * converts a string representation of TWO MAC addresses, which * are comma deliminated into two hex values. Either *first or *second * can be NULL if there is nothing before or after the comma. * returns: * 1 = first mac * 2 = second mac * 3 = both mac's * 0 = none */ int dualmac2hex(const char *dualmac, u_char *first, u_char *second, int len) { char *tok, *temp, *string; int ret = 0; string = safe_strdup(dualmac); /* if we've only got a comma, then return NULL's */ if (len <= 1) { second = first = NULL; return 0; } temp = strtok_r(string, ",", &tok); if (strlen(temp)) { mac2hex(temp, first, len); ret = 1; } temp = strtok_r(NULL, ",", &tok); /* temp is null if no comma */ if (temp != NULL) { if (strlen(temp)) { mac2hex(temp, second, len); ret += 2; } } return ret; }
/** * Figures out if a MAC is listed in a comma delimited * string of MAC addresses. * returns TCPR_DIR_C2S if listed * returns TCPR_DIR_S2C if not listed */ tcpr_dir_t macinstring(const char *macstring, const u_char *mac) { char *tok, *tempstr, *ourstring; u_char tempmac[6]; int len = 6, ret = TCPR_DIR_S2C; ourstring = safe_strdup(macstring); tempstr = strtok_r(ourstring, ",", &tok); if (strlen(tempstr)) { mac2hex(tempstr, tempmac, len); if (memcmp(mac, tempmac, len) == 0) { dbgx(3, "Packet matches: " MAC_FORMAT " sending out primary.\n", MAC_STR(tempmac)); ret = TCPR_DIR_C2S; goto EXIT_MACINSTRING; } } else { goto EXIT_MACINSTRING; } while ((tempstr = strtok_r(NULL, ",", &tok)) != NULL) { mac2hex(tempstr, tempmac, len); if (memcmp(mac, tempmac, len) == 0) { ret = TCPR_DIR_C2S; dbgx(3, "Packet matches: " MAC_FORMAT " sending out primary.\n", MAC_STR(tempmac)); goto EXIT_MACINSTRING; } } EXIT_MACINSTRING: safe_free(ourstring); #ifdef DEBUG if (ret == TCPR_DIR_S2C) dbg(3, "Packet doesn't match any MAC addresses sending out secondary.\n"); #endif return ret; }
void post_args(_U_ int argc, _U_ char *argv[]) { char ebuf[SENDPACKET_ERRBUF_SIZE]; struct tcpr_ether_addr *eth_buff; char *intname; sendpacket_t *sp; #ifdef ENABLE_PCAP_FINDALLDEVS interface_list_t *intlist = get_interface_list(); #else interface_list_t *intlist = NULL; #endif #ifdef DEBUG if (HAVE_OPT(DBUG)) debug = OPT_VALUE_DBUG; #else if (HAVE_OPT(DBUG)) warn("not configured with --enable-debug. Debugging disabled."); #endif #ifdef ENABLE_VERBOSE if (HAVE_OPT(VERBOSE)) options.verbose = 1; if (HAVE_OPT(DECODE)) options.tcpdump->args = safe_strdup(OPT_ARG(DECODE)); #endif if (HAVE_OPT(UNIDIR)) options.unidir = 1; if (HAVE_OPT(LIMIT)) options.limit_send = OPT_VALUE_LIMIT; /* default is -1 */ if ((intname = get_interface(intlist, OPT_ARG(INTF1))) == NULL) errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF1)); options.intf1 = safe_strdup(intname); if (HAVE_OPT(INTF2)) { if ((intname = get_interface(intlist, OPT_ARG(INTF2))) == NULL) errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF2)); options.intf2 = safe_strdup(intname); } if (HAVE_OPT(MAC)) { int ct = STACKCT_OPT(MAC); char **list = STACKLST_OPT(MAC); int first = 1; do { char *p = *list++; if (first) mac2hex(p, (u_char *)options.intf1_mac, ETHER_ADDR_LEN); else mac2hex(p, (u_char *)options.intf2_mac, ETHER_ADDR_LEN); first = 0; } while (--ct > 0); } /* * Figure out MAC addresses of sending interface(s) * if user doesn't specify MAC address on CLI, query for it */ if (memcmp(options.intf1_mac, "\00\00\00\00\00\00", ETHER_ADDR_LEN) == 0) { if ((sp = sendpacket_open(NULL, options.intf1, ebuf, TCPR_DIR_C2S)) == NULL) errx(-1, "Unable to open interface %s: %s", options.intf1, ebuf); if ((eth_buff = sendpacket_get_hwaddr(sp)) == NULL) { warnx("Unable to get MAC address: %s", sendpacket_geterr(sp)); err(-1, "Please consult the man page for using the -M option."); } sendpacket_close(sp); memcpy(options.intf1_mac, eth_buff, ETHER_ADDR_LEN); } if (memcmp(options.intf2_mac, "\00\00\00\00\00\00", ETHER_ADDR_LEN) == 0) { if ((sp = sendpacket_open(NULL, options.intf2, ebuf, TCPR_DIR_S2C)) == NULL) errx(-1, "Unable to open interface %s: %s", options.intf2, ebuf); if ((eth_buff = sendpacket_get_hwaddr(sp)) == NULL) { warnx("Unable to get MAC address: %s", sendpacket_geterr(sp)); err(-1, "Please consult the man page for using the -M option."); } sendpacket_close(sp); memcpy(options.intf2_mac, eth_buff, ETHER_ADDR_LEN); } /* * Open interfaces for sending & receiving */ if ((options.pcap1 = pcap_open_live(options.intf1, options.snaplen, options.promisc, options.to_ms, ebuf)) == NULL) errx(-1, "Unable to open interface %s: %s", options.intf1, ebuf); if (strcmp(options.intf1, options.intf2) == 0) errx(-1, "Whoa tiger! You don't want to use %s twice!", options.intf1); /* we always have to open the other pcap handle to send, but we may not listen */ if ((options.pcap2 = pcap_open_live(options.intf2, options.snaplen, options.promisc, options.to_ms, ebuf)) == NULL) errx(-1, "Unable to open interface %s: %s", options.intf2, ebuf); /* poll should be -1 to wait indefinitely */ options.poll_timeout = -1; }
int main(int argc, char **argv) { int a; int flag_smac=0, flag_cmac=0, flag_sip=0, flag_cip=0; unsigned int checksum=0; unsigned int rest=0; FILE* source; FILE* target; char * filename="patched.bin"; unsigned char kernal[0x2000]; unsigned char c; char * command; char * param; int rv; if(argc==1) { fprintf(stderr, " Saves an optimized kernal-image with patched ip and mac-address.\n"); fprintf(stderr, " mac-address is of format: 12:34:56:78:9a:bc\n"); fprintf(stderr, " ip-address is of format: 192.168.2.13\n\n"); // fprintf(stderr, "Usage: makerom\n"); fprintf(stderr, "The following iptions are available:\n\n"); fprintf(stderr, " --smac=<mac-address>\nthe 64net/2-server's mac-address (required)\n\n"); fprintf(stderr, " --sip=<ip-address>\nthe 64net/2-server's ip-address (required)\n\n"); fprintf(stderr, " --cmac=<mac-address>\nthe c64's mac-address (required)\n\n"); fprintf(stderr, " --cip=<ip-address>\nthe c64's ip-address (required)\n\n"); fprintf(stderr, " --name=<output filename>\noptional output filename (default=output.bin)\n"); exit(2); } for(a=1; a<argc; a++) { // printf("arg: %s\n",argv[a]); command=strtok(argv[a],"="); param=strtok(NULL,"="); // printf("command: %s param: %s \n",command,param); if(strcmp(command,"--smac")==0) { smac=(unsigned char*)malloc(6); if(!param || !mac2hex(param,smac)) { fprintf(stderr, "ERROR: --smac: mac-address not given or incomplete.\n"); exit(2); } } else if(strcmp(command,"--cmac")==0) { cmac=(unsigned char*)malloc(6); if(!param || !mac2hex(param,cmac)) { fprintf(stderr, "ERROR: --cmac: mac-address not given or incomplete.\n"); exit(2); } } else if(strcmp(command,"--cip")==0) { cip=(unsigned char*)malloc(4); if(!param || !ip2hex(param,cip)) { fprintf(stderr, "ERROR: --cip: ip-address not given or incomplete.\n"); exit(2); } } else if(strcmp(command,"--sip")==0) { sip=(unsigned char*)malloc(4); if(!param || !ip2hex(param,sip)) { fprintf(stderr, "ERROR: --sip: ip-address not given or incomplete.\n"); exit(2); } } else if(strcmp(command,"--name")==0) { if(!param) { fprintf(stderr, "ERROR: --name: no filename given.\n"); exit(2); } filename=param; } else { fprintf(stderr, "ERROR: Unknown option: %s\n",command); exit(2); } } printf("server mac is: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",smac[0],smac[1],smac[2],smac[3],smac[4],smac[5]); printf("client mac is: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",cmac[0],cmac[1],cmac[2],cmac[3],cmac[4],cmac[5]); printf("server ip is: %.2x.%.2x.%.2x.%.2x\n",sip[0],sip[1],sip[2],sip[3]); printf("client ip is: %.2x.%.2x.%.2x.%.2x\n",cip[0],cip[1],cip[2],cip[3]); memcpy(&ipheader[0x00],smac,6); memcpy(&ipheader[0x06],cmac,6); memcpy(&ipheader[0x1a],cip,4); memcpy(&ipheader[0x1e],sip,4); checksum=0x1e; /* add minlen of packet already, so we only need to substract payload off checksum later on */ /* only calc checksum on ip header -> from offset 0x0e on */ for(a=0x0e; a<0x22; a+=2) { checksum+=(ipheader[a]*256+ipheader[a+1]); /* add overflowing bits to simulate a 16 bit addition without clc */ rest=checksum>>16; checksum=(checksum&0xffff)+rest; } checksum &= 0xffff; checksum ^= 0xffff; printf("precalculated checksum is: $%.4x\n",checksum); ipheader[0x2a]=(unsigned char)((checksum>>8)&0xff); ipheader[0x2b]=(unsigned char)(checksum&0xff); // for(a=0;a<0x2c;a++) { // printf("$%.2x ",ipheader[a]); // } // printf("\n"); /* now open kernal file */ source=fopen("kernal","r"); if(!source) { fprintf(stderr, "ERROR: could not open file (%s).\n","kernal"); exit(2); } a=0; while(fread(&c,1,1,source)) { if(a<0x2000) kernal[a]=c; a++; } fclose(source); printf("successfully read $%.4x bytes of kernal rom\n",a); if(a!=0x2000) { fprintf(stderr, "ERROR: kernal rom has wrong size (!=0x2000).\n"); exit(2); } printf("patching ip struct...\n",a); memcpy(kernal+IP_HEADER_OFFSET,ipheader,0x2c); target=fopen(filename,"w"); if(target) { fwrite(kernal,1,0x2000,target); } else { fprintf(stderr, "ERROR: could not open file for writing (%s).\n",filename); exit(2); } printf("patched kernal written to '%s'\n",filename); fclose(target); /* patch ip, mac and chksum */ /* write out new kernal */ // fdw = fopen (write_name, write_mode); // if(fdw==NULL) { fprintf(stderr, "ERROR: can't open output file\n"); exit(0); } // c=address&255; // fwrite(&c,1,1,fdw); // c=address>>8; // fwrite(&c,1,1,fdw); // fclose(fdw); free(sip); free(cip); free(smac); free(cmac); exit (0); // c=0; fread(&c,1,1,fdr); bpp=(int)c; }