static void test_decode(const char *hexstr, const char *base58_str) { size_t hs_len = strlen(hexstr) / 2; unsigned char *raw = calloc(1, hs_len); size_t out_len; bool rc = decode_hex(raw, hs_len, hexstr, &out_len); if (!rc) { fprintf(stderr, "raw %p, sizeof(raw) %lu, hexstr %p %s\n", raw, hs_len, hexstr, hexstr); assert(rc); } cstring *s = base58_decode(base58_str); if (memcmp(s->str, raw, out_len < s->len ? out_len : s->len)) { dumphex("decode have", s->str, s->len); dumphex("decode want", raw, out_len); assert(memcmp(s->str, raw, out_len) == 0); } if (s->len != out_len) { fprintf(stderr, "decode len: have %u, want %u\n", (unsigned int) s->len, (unsigned int) out_len); dumphex("decode have", s->str, s->len); dumphex("decode want", raw, out_len); assert(s->len == out_len); } free(raw); cstr_free(s, true); }
int read_command(char** pkt){ if(DEBUG>10) std::cout<<"DEBUG[commands.cpp] read_command(*{NULL})"<<std::endl; *pkt=0; int n=eth_read(0); // testing >>> //std::cout<<"TESTING: read_command, copy wdat to rdat"<<std::endl; //n=24; //memcpy(rdat,wdat,4*n); //n=RAMPAGE_SIZE+4; //memcpy(rdat,(const void*)&wdat[nn],n); //rdat[3] = (rdat[3]&0xff) + 0xd0; //if(time(0)%10==0) rdat[10]++; //if(time(0)%15==0) rdat[2]++; //if(time(0)%2==0 && (rdat[0]&0xff) == 0xf3) rdat[2]++; // <<< testing if(n<=6){ if(DEBUG>10) std::cout<<"DEBUG[commands.cpp] read_command return -2 <== n="<<n<<" <= 6"<<std::endl; return -2; } *pkt = rdat; // currently just setting the pointer, but I may want to make a copy (otherwise I can just use rbuf) if(DEBUG>20){ std::cout<<"DEBUG[commands.cpp] read_command read in packet with "<<n<<" bytes:"<<std::endl; dumphex(n,*pkt); std::cout<<std::endl; } int num=0; if ((rdat[0]&0xff) == 0xf0 && (rdat[1]&0xff) == 0xf0) num=0; else if((rdat[0]&0xff) == 0xf1 && (rdat[1]&0xff) == 0xf1) num=1; else if((rdat[0]&0xff) == 0xf2 && (rdat[1]&0xff) == 0xf2) num=2; else if((rdat[0]&0xff) == 0xf3 && (rdat[1]&0xff) == 0xf3) num=3; // return packet for F3 will now start with f3f3 else if((rdat[0]&0xff) == 0xf5 && (rdat[1]&0xff) == 0xf5) num=5; else if((rdat[0]&0xff) == 0xf7 && (rdat[1]&0xff) == 0xf7) num=7; else if((rdat[0]&0xff) == 0xfd && (rdat[1]&0xff) == 0xfd) num=0xd; else if((rdat[0]&0xff) == 0xfe && (rdat[1]&0xff) == 0xfe) num=0xe; else num=-1; if(num==3){ // compare the address word from the header to the first word (-0xa000+0xd000) in the data if( (rdat[2]&0xff) != (rdat[4]&0xff) || (rdat[3]&0xff) != ((rdat[5]&0xff) - 0xa0 + 0xd0) ){ if(DEBUG>10){ std::cout<<"DEBUG[commands.cpp] Address words do not agree: "; dumphex(6,rdat); std::cout<<std::endl; } num = -num; } } if(DEBUG>10) std::cout<<"DEBUG[commands.cpp] read_command return "<<num<<std::endl; return num; }
int main(int argc, char *argv[]) { dumphex(_start, &_etext); exit(0); }
int eth_read(int suppression) { if(DEBUG>10) std::cout<<"DEBUG[eth_lib.cpp] eth_read("<<suppression<<")"<<std::endl; /* * suppression=0 => pass all packets * suppression=1 => skip packets with 1-6 bytes and start will 0x03 * suppression=2 => same as 1, plus ignore packets that begin with 0x33 or 0xff (multicast packets) */ int lp; nrdat=0; do { //printf(" Receiving..."); nrdat=read(fd_schar,rpkt, MAX_DAT_SIZE); //printf(" nrdat %d \n",nrdat); for(lp=0; suppression>0 && nrdat>=0 && nrdat <7 && rpkt[0]==0x03 && lp<100; lp++) { // Will ignore packets with 1-6 bytes and start will 0x03 (???) printf("...\n"); usleep(100); nrdat=read(fd_schar,rpkt, MAX_DAT_SIZE); } } while(suppression>1 && nrdat>6 && (rpkt[0]&1)==1); //ignore multicast packets (ignores packet with first byte that is odd) //if(DEBUG>20){ std::cout<<"DEBUG[eth_lib.cpp] eth_read read packet of "<<nrdat<<" bytes:"<<std::endl; dumphex(nrdat,rdat); std::cout<<std::endl; //} return nrdat; }
static void respond(Req *r, int err) { int n, flags, flags2; if(err && !(r->flags2 & SMB_FLAGS2_NT_STATUS)) err = doserror(err); flags = (r->flags & (r->namecmp != strcmp ? NOCASEMASK : 0)) | SMB_FLAGS_REPLY; flags2 = (r->flags2 & (SMB_FLAGS2_NT_STATUS | SMB_FLAGS2_LONG_NAMES | SMB_FLAGS2_UNICODE)) | SMB_FLAGS2_IS_LONG_NAME; if(r->cmd != 0x73) /* SMB_COM_SESSION_SETUP_ANDX */ memset(r->sig, 0, sizeof(r->sig)); n = pack(r->rh, r->rh, r->rh+32, "lblbww[]__wwww", MAGIC, r->cmd, err, flags, flags2, r->pid>>16, r->sig, r->sig+sizeof(r->sig), r->tid, r->pid & 0xFFFF, r->uid, r->mid); if(err){ r->rp = r->rh+n; r->rp += pack(r->rh, r->rp, r->re, "#0b{*2}#1w{}"); } if(debug > 1) dumphex("respond", r->rh, r->rp); if(debug) fprint(2, "respond: err=%x\n\n", err); n = r->rp - r->rh; r->lh[0] = 0; r->lh[1] = 0; r->lh[2] = n>>8 & 0xFF; r->lh[3] = n & 0xFF; write(1, r->lh, LENHDR+n); }
static long sendAPDU(SCARDHANDLE hCard, const char *apdu, unsigned char *recvBuf, DWORD *recvBufLen, const char *chipNr, int idx, bool doDump) { unsigned char sendBuf[280]; size_t sendBufLen = sizeof(sendBuf); // Hex string -> byte array if (0 == hex2bin(apdu, sendBuf, &sendBufLen)) { // Check the APDU if (sendBufLen < 4) printf("ERR: APDU should be at least 4 bytes\n"); else if (sendBufLen > 5 && ((size_t) (5 + sendBuf[4]) != sendBufLen)) printf("ERR: wrong P3 byte in case 3 APDU\n"); else { if (doDump) dumphex(" - sending ", sendBuf, sendBufLen); delayMS(50); long ret = SCardTransmit(hCard, &g_rgSCardT0Pci, sendBuf, (DWORD) sendBufLen, NULL, recvBuf, recvBufLen); CHECK_PCSC_RET("SCardTransmit", ret); if (SCARD_S_SUCCESS == ret) { if (doDump) { dumphex(" received ", recvBuf, *recvBufLen); printf("\n"); } if (NULL != chipNr) StoreAPDUs(chipNr, idx, sendBuf, sendBufLen, recvBuf, *recvBufLen); return 0; // success } } } return -1; // failed }
static long doirb(psd_file_t f){ static struct dictentry resource = {0, NULL, "RESOURCE", "dummy", NULL}; char type[4], name[0x100]; int id, namelen; long size; size_t padded_size; struct dictentry *d; fread(type, 1, 4, f); id = get2B(f); namelen = fgetc(f); fread(name, 1, PAD2(1+namelen)-1, f); name[namelen] = 0; size = get4B(f); padded_size = PAD2(size); d = findbyid(id); if((verbose || print_rsrc || resdump) && !xmlout){ printf(" resource '%c%c%c%c' (%5d,\"%s\"):%5ld bytes", type[0],type[1],type[2],type[3], id, name, size); if(d) printf(" [%s]", d->desc); putchar('\n'); } if(d && d->tag){ if(xml){ fprintf(xml, "\t<RESOURCE TYPE='%c%c%c%c' ID='%d'", type[0],type[1],type[2],type[3], id); if(namelen) fprintf(xml, " NAME='%s'", name); } if(d->func){ if(xml) fputs(">\n", xml); entertag(f, 2, size, &resource, d, 1); if(xml) fputs("\t</RESOURCE>\n\n", xml); } else if(xml){ fputs(" /> <!-- not parsed -->\n", xml); } } if(resdump){ char *temp_buf = checkmalloc(padded_size); if(fread(temp_buf, 1, padded_size, f) < padded_size) fatal("did not read expected bytes in image resource\n"); dumphex((unsigned char*)temp_buf, size); putchar('\n'); free(temp_buf); } else fseeko(f, padded_size, SEEK_CUR); // skip resource block data return 4+2+PAD2(1+namelen)+4+padded_size; /* returns total bytes in block */ }
/*ARGSUSED*/ static void chall_ext(uint8_t type, uint8_t this_ext_len, uchar_t *p) { char auth_prn_str[BUFLEN]; /* payload points to the challenge */ dumphex(p, /* don't write past our string buffer ... */ (this_ext_len*3 > BUFLEN ? BUFLEN / 3 : this_ext_len), auth_prn_str, "Challenge = %s"); }
/*ARGSUSED*/ static void unk_ext(uint8_t type, uint8_t this_ext_len, uchar_t *p) { char auth_prn_str[BUFLEN]; /* Unknown extension; just dump the rest of the payload */ dumphex(p, /* don't write past our string buffer ... */ (this_ext_len*3 > BUFLEN ? BUFLEN : this_ext_len), auth_prn_str, "Payload = %s"); }
void cache_dump(handle hd) { cache * c = (cache *)hd; ASSERT(c); fprintf(stderr, "<%s> cache=%p, buf=%p, buflen=%d, datalen=%d.\n", __FUNCTION__, c, c->buf, c->buflen, c->datalen); dumphex("cache data:", c->buf, c->buflen); }
static void key_ext(uint8_t type, uint8_t this_ext_len, uchar_t *p) { uint16_t alg, spi_hi, spi_low; char *alg_string; char *hafa = (type == MN_HA_KEY ? "HA" : "FA"); char sec_msg[32]; char auth_prn_str[BUFLEN]; /* Algorithm Type */ (void) memcpy(&alg, p, sizeof (alg)); alg = ntohs(alg); switch (alg) { case KEY_ALG_NONE: alg_string = "None"; break; case SA_MD5_MODE_PREF_SUF: alg_string = "MD5/prefix+suffix"; break; case SA_HMAC_MD5: alg_string = "HMAC MD5"; break; default: alg_string = "Unknown"; break; } (void) sprintf(get_line((char *)p-dlc_header, 1), "Algorithm = 0x%x: %s", alg, alg_string); p += sizeof (alg); this_ext_len -= sizeof (alg); /* AAA SPI */ GETSPI(p, spi_hi, spi_low); (void) sprintf(get_line((char *)p - dlc_header, 1), "AAA Security Parameter Index = 0x%x%x", ntohs(spi_hi), ntohs(spi_low)); p += sizeof (spi_hi) + sizeof (spi_low); this_ext_len -= sizeof (spi_hi) + sizeof (spi_low); /* HA / FA SPI */ GETSPI(p, spi_hi, spi_low); (void) sprintf(get_line((char *)p - dlc_header, 1), "%s Security Parameter Index = 0x%x%x", hafa, ntohs(spi_hi), ntohs(spi_low)); p += sizeof (spi_hi) + sizeof (spi_low); this_ext_len -= sizeof (spi_hi) + sizeof (spi_low); /* The rest is the security info; dump it in hex */ sprintf(sec_msg, "%s Security Info = %%s", hafa); dumphex(p, /* don't write past our string buffer ... */ (this_ext_len*3 > BUFLEN ? BUFLEN : this_ext_len), auth_prn_str, sec_msg); }
static void dumpvar(EFI_SYSTEM_TABLE *systab, EFI_GUID *guid, CHAR16 *name) { char *data = NULL; UINTN data_size = 0; Print(L"Dumping "); Print(name); Print(L"\n"); data = LibGetVariableAndSize(name, guid, &data_size); dumphex(data, data_size); FreePool(data); }
static void decode(unsigned char *buf, int count) { struct pl_cmd *data; int hc, uc, fc; char scratch[80]; if (!buf) return; dsyslog(LOG_INFO,"received data - decoding: %s\n", dumphex(scratch, buf, count)); hc = uc = fc = -1; if (buf[0] == X10_PL_ACK) { updateack(1); } else if (buf[0] == X10_PL_NAK) { updateack(-1); } else if (buf[0] == X10_PL_EXTDATASTART){ // 0x45='E' dsyslog(LOG_INFO,"Error: Extended data not supported\n"); } else if (buf[0] == X10_PL_XMITRCV || buf[0] == X10_PL_XMITRCVSELF) { data = (struct pl_cmd *) buf; dsyslog(LOG_INFO,"packet: hc=0x%x, uc=0x%x, fc=0x%x\n",data->hc, data->uc, data->fc); hc = decode_housecode(data->hc & ~0x40); if (hc < 0 || hc >= MAX_HOUSECODES) { dsyslog(LOG_INFO,"invalid housecode 0x%x\n", data->hc); return; } uc = decode_unitcode(data->uc & ~0x40); if (uc < 0) fc = decode_functioncode(data->uc & ~0x40); dsyslog(LOG_INFO,"calling receiver...\n"); if (io->received(hc, uc, fc,NULL,0)) dsyslog(LOG_INFO,"unknown cmd %s\n", dumphex(scratch,buf, count)); } else { dsyslog(LOG_INFO,"unable to handle data %s\n",dumphex(scratch,buf,count)); } }
void ir_dump(psd_file_t f, int level, int len, struct dictentry *parent){ unsigned char row[BYTESPERLINE]; int n; if(verbose) for(; len; len -= n){ n = len < BYTESPERLINE ? len : BYTESPERLINE; fread(row, 1, n, f); dumphex(row, n); } else fseeko(f, len, SEEK_CUR); }
int eth_write() { if(DEBUG>10) std::cout<<"DEBUG[eth_lib.cpp] eth_write()"<<std::endl; if(nwdat>MAX_DAT_SIZE) { printf("ERROR: nwdat=%d is too large! MAX_DAT_SIZE=%d",nwdat,MAX_DAT_SIZE); return 0; } int n_written; //printf(" Creating the packet... nwdat %d ...",nwdat); //if(DEBUG>20){ std::cout<<"DEBUG[eth_lib.cpp] eth_write sending packet of "<<nwdat<<" bytes:"<<std::endl; dumphex(nwdat,wdat); std::cout<<std::endl; //} n_written = write(fd_schar, (const void *)wpkt, nwdat); //printf(" n_written %d \n",n_written); return n_written; }
/*ARGSUSED*/ static void spi_ext(uint8_t type, uint8_t this_ext_len, uchar_t *p) { uint16_t spi_hi, spi_low; char auth_prn_str[BUFLEN]; /* SPI */ GETSPI(p, spi_hi, spi_low); (void) sprintf(get_line((char *)p - dlc_header, 1), "Security Parameter Index = 0x%x%x", ntohs(spi_hi), ntohs(spi_low)); p += sizeof (spi_hi) + sizeof (spi_low); this_ext_len -= sizeof (spi_hi) + sizeof (spi_low); /* The rest is the authenticator; dump it in hex */ dumphex(p, /* don't write past our string buffer ... */ (this_ext_len*3 > BUFLEN ? BUFLEN : this_ext_len), auth_prn_str, "Authenticator = %s"); }
int main(int argc,char *argv[]) { int n; char *tmp; char devname[12]; // check arguments if(argc != 2) return usage(argc, argv); // set and check device name tmp=argv[1]; if(strcmp(tmp,"1") && strcmp(tmp,"2")) return usage(argc, argv); sprintf(devname, "/dev/schar%s",tmp); printf("devname %s\n",devname); // open device eth_open(devname); eth_register_mac(); while(1){ // never ending loop, Ctrl-C to stop // receive data n=eth_read(0); if (n<=6) continue; printf(" nread %d \n",n); printf(" nrdat %d \n",nrdat); printf("Dump: \""); dumphex(n,rpkt); printf("\"\n"); printf("Use Ctrl-C to stop.\n"); } // close device eth_close(); return 0; }
int transmit(int hc, int uc, int cmd) { int ret = 0, i = 0; unsigned char data[5]; char scratch[40]; dsyslog(LOG_INFO,"transmit: hc=0x%x, uc=0x%x, fc=0x%x\n",hc,uc,cmd); if (cmd >= X10_CMD_END){ dsyslog(LOG_INFO,"transmit: invalid fc\n"); return 1; } hc &= 0x0f; memset(data,0,sizeof(data)); i = 0; data[i++] = X10_PL_SENDCMD; data[i++] = housecode[hc] | 0x40; if (uc >= 0) data[i++] = unitcode[uc] | 0x40; if (cmd >= 0) data[i++] = functioncode[cmd] | 0x40; data[4] = X10_PL_REPEATONCE; if (sem_wait_timeout(&cts,timeout)) { syslog(LOG_INFO,"Timeout while waiting for transmitter\n"); syslog(LOG_INFO,"If this persists, restart daemon\n"); return -1; } if (startup()) { syslog(LOG_INFO,"unable to communicate with PowerLinc Transceiver\n"); ret = -1; goto done; } dsyslog(LOG_INFO,"sending %s\n",dumphex(scratch,(void *)data,sizeof(data))); ret = write(serial,data,sizeof(data)); dsyslog(LOG_INFO,"sleeping for %d microseconds\n",delay); usleep(delay*1000); done: sem_post(&cts); // enable transmitter return ret; }
int main(int argc, char* argv[]) { struct tuntap_dev tuntap; int i; int mtu = 1400; printf("Welcome to n2n\n"); initWin32(); open_wintap(&tuntap, "static", "192.168.0.2", "255.255.255.0", "00 ff 65 21 b3 dd",mtu); //for (i = 0; i < 10; i++) while(1) { u_char buf[MTU]; int rc; rc = tuntap_read(&tuntap, buf, sizeof(buf)); dumphex(NULL, (char*)buf, rc); buf[0] = 2; buf[1] = 3; buf[2] = 4; printf("tun_read returned %d\n", rc); rc = tuntap_write(&tuntap, buf, rc); printf("tun_write returned %d\n", rc); if (_kbhit()) { int ch = getchar(); if (ch == 'q') break; } } // rc = tun_open (device->device_name, IF_MODE_TUN); tuntap_close(&tuntap); WSACleanup(); return(0); }
void vg_output_match_console(vg_context_t *vcp, EC_KEY *pkey, const char *pattern) { unsigned char key_buf[512], *pend; char addr_buf[64], addr2_buf[64]; char privkey_buf[VG_PROTKEY_MAX_B58]; const char *keytype = "Privkey"; int len; int isscript = (vcp->vc_format == VCF_SCRIPT); EC_POINT *ppnt; int free_ppnt = 0; if (vcp->vc_pubkey_base) { ppnt = EC_POINT_new(EC_KEY_get0_group(pkey)); EC_POINT_copy(ppnt, EC_KEY_get0_public_key(pkey)); EC_POINT_add(EC_KEY_get0_group(pkey), ppnt, ppnt, vcp->vc_pubkey_base, NULL); free_ppnt = 1; keytype = "PrivkeyPart"; } else { ppnt = (EC_POINT *) EC_KEY_get0_public_key(pkey); } assert(EC_KEY_check_key(pkey)); vg_encode_address(ppnt, EC_KEY_get0_group(pkey), vcp->vc_pubkeytype, addr_buf); if (isscript) vg_encode_script_address(ppnt, EC_KEY_get0_group(pkey), vcp->vc_addrtype, addr2_buf); if (vcp->vc_key_protect_pass) { len = vg_protect_encode_privkey(privkey_buf, pkey, vcp->vc_privtype, VG_PROTKEY_DEFAULT, vcp->vc_key_protect_pass); if (len) { keytype = "Protkey"; } else { fprintf(stderr, "ERROR: could not password-protect key\n"); vcp->vc_key_protect_pass = NULL; } } if (!vcp->vc_key_protect_pass) { vg_encode_privkey(pkey, vcp->vc_privtype, privkey_buf); } if (!vcp->vc_result_file || (vcp->vc_verbose > 0)) { printf("\r%79s\r\nPattern: %s\n", "", pattern); } if (vcp->vc_verbose > 0) { if (vcp->vc_verbose > 1) { pend = key_buf; len = i2o_ECPublicKey(pkey, &pend); printf("Pubkey (hex): "); dumphex(key_buf, len); printf("Privkey (hex): "); dumpbn(EC_KEY_get0_private_key(pkey)); pend = key_buf; len = i2d_ECPrivateKey(pkey, &pend); printf("Privkey (ASN1): "); dumphex(key_buf, len); } } if (!vcp->vc_result_file || (vcp->vc_verbose > 0)) { if (isscript) printf("P2SHAddress: %s\n", addr2_buf); printf("Address: %s\n" "%s: %s\n", addr_buf, keytype, privkey_buf); } if (vcp->vc_result_file) { FILE *fp = fopen(vcp->vc_result_file, "a"); if (!fp) { fprintf(stderr, "ERROR: could not open result file: %s\n", strerror(errno)); } else { fprintf(fp, "Pattern: %s\n" , pattern); if (isscript) fprintf(fp, "P2SHAddress: %s\n", addr2_buf); fprintf(fp, "Address: %s\n" "%s: %s\n", addr_buf, keytype, privkey_buf); fclose(fp); } } if (free_ppnt) EC_POINT_free(ppnt); }
int main(int argc, char *argv[]) { int data_size; uint8_t *img_buf, *img_buf_in; BITMAPFILEHEADER file_head; BITMAPINFOHEADER info_head; BITMAPFILEHEADER file_head_in; BITMAPINFOHEADER info_head_in; if (argc != 3) { printf("usage %s <in> <out>\n", argv[0]); return -1; } /*open files*/ int in = open(argv[1], O_RDONLY); int out = open(argv[2], O_WRONLY | O_CREAT, 0644); if ((in == -1) || (out == -1)) { perror("open image"); return -1; } read(in, &file_head_in, sizeof(file_head_in)); read(in, &info_head_in, sizeof(info_head_in)); dumphex((uint8_t *)&info_head_in, sizeof(info_head_in)); printf("in %d x %d x %d\n", info_head_in.biWidth, info_head_in.biHeight, info_head_in.biBitCount); data_size = 4 * info_head_in.biHeight * info_head_in.biWidth; /*initialize bmp structs*/ file_head.bfType = 0x4d42; file_head.bfSize = sizeof(file_head) + sizeof(info_head) + data_size; file_head.bfReserved1 = file_head.bfReserved2 = 0; file_head.bfOffBits = sizeof(file_head) + sizeof(info_head); info_head.biSize = sizeof(info_head); info_head.biWidth = info_head_in.biWidth; info_head.biHeight = info_head_in.biHeight; info_head.biPlanes = 1; info_head.biBitCount = 32; info_head.biCompression = 0; info_head.biSizeImage = data_size; info_head.biXPelsPerMeter = 0; info_head.biYPelsPerMeter = 0; info_head.biClrUsed = 0; info_head.biClrImportant = 0; int bpl_in = info_head_in.biWidth * 3; if (bpl_in % 4) { bpl_in += 4 - bpl_in % 4; } img_buf_in = (unsigned char *)malloc(info_head_in.biHeight * bpl_in); img_buf = (unsigned char *)malloc(data_size); if (img_buf_in == NULL || img_buf == NULL) { printf("malloc failed!\n"); return -1; } /*read img data and */ int len = read(in, img_buf_in, info_head_in.biHeight * bpl_in); printf("read %d / %d bytes\n", len, info_head_in.biHeight * bpl_in); /* convert 24 to 32 bpp */ for (int i = 0; i < info_head.biHeight; i++) { for (int j = 0; j < info_head.biWidth; j++) { int32_t *pout = (int32_t *)img_buf + i * info_head.biWidth + j; unsigned char *pin = img_buf_in + i * bpl_in + j * 3; *pout = *(pin + 2) << 16 | *(pin + 1) << 8 | *(pin + 0) << 0 | 0xff000000; } } /* save file */ write(out, &file_head, sizeof(file_head)); write(out, &info_head, sizeof(info_head)); for (int i = 0; i < info_head.biHeight; i++) { /* revese img and write to file */ write(out, img_buf + info_head.biWidth * i *4, info_head.biWidth * 4); // write(out, img_buf + info_head.biWidth * (info_head.biHeight-i-1)*4, info_head.biWidth * 4); } close(in); close(out); return 0; }
int main(int argc, char **argv) { struct ir_command command; struct ir_command timeoutCommand; int c; int led_brightness = 1; while ((c = getopt (argc, argv, "mBi:b:s:H:hd")) != -1) switch (c) { case 'm': multi_mode = 1; break; case 'i': idle_mode = atol(optarg); break; case 'b': button_mode = atol(optarg); break; case 's': special_mode = atol(optarg); break; case 'H': hold_mode = atol(optarg); break; case 'd': debug = 1; break; case 'h': usage(argc,argv); exit(0); break; case 'B': led_brightness = 0; break; case '?': switch(optopt) { case 'i': case 'b': case 's': case 'H': fprintf (stderr, "Option -%c requires an argument.\n", optopt); break; default: if (isprint (optopt)) fprintf (stderr, "Unknown option `-%c'.\n", optopt); else fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); } return 1; default: abort(); } set_led_brightness(led_brightness); memset(&timeoutCommand, 0, sizeof(timeoutCommand)); if(debug) printf("Creating socket...\n"); sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { printf("Error creating socket\n"); return -1; } if(debug) printf("Preparing button map...\n"); button_map[EVENT_UP] = new CPacketBUTTON(EVENT_UP, "JS0:AppleRemote", BTN_DOWN); button_map[EVENT_DOWN] = new CPacketBUTTON(EVENT_DOWN, "JS0:AppleRemote", BTN_DOWN); button_map[EVENT_LEFT] = new CPacketBUTTON(EVENT_LEFT, "JS0:AppleRemote", BTN_DOWN); button_map[EVENT_RIGHT] = new CPacketBUTTON(EVENT_RIGHT, "JS0:AppleRemote", BTN_DOWN); button_map[EVENT_PLAY] = new CPacketBUTTON(EVENT_PLAY, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); button_map[EVENT_MENU] = new CPacketBUTTON(EVENT_MENU, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); button_map[EVENT_HOLD_PLAY] = new CPacketBUTTON(EVENT_HOLD_PLAY, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); button_map[EVENT_HOLD_MENU] = new CPacketBUTTON(EVENT_HOLD_MENU, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); button_map[EVENT_UP | EVENT_RELEASE ] = new CPacketBUTTON(EVENT_UP, "JS0:AppleRemote", BTN_UP); button_map[EVENT_DOWN | EVENT_RELEASE ] = new CPacketBUTTON(EVENT_DOWN, "JS0:AppleRemote", BTN_UP); button_map[EVENT_LEFT | EVENT_RELEASE ] = new CPacketBUTTON(EVENT_LEFT, "JS0:AppleRemote", BTN_UP); button_map[EVENT_RIGHT | EVENT_RELEASE ] = new CPacketBUTTON(EVENT_RIGHT, "JS0:AppleRemote", BTN_UP); button_map[EVENT_EXTRA_PLAY] = new CPacketBUTTON(EVENT_EXTRA_PLAY, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); button_map[EVENT_EXTRA_PAUSE] = new CPacketBUTTON(EVENT_EXTRA_PAUSE, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); button_map[EVENT_EXTRA_STOP] = new CPacketBUTTON(EVENT_EXTRA_STOP, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); button_map[EVENT_EXTRA_REPLAY] = new CPacketBUTTON(EVENT_EXTRA_REPLAY, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); button_map[EVENT_EXTRA_SKIP] = new CPacketBUTTON(EVENT_EXTRA_SKIP, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); button_map[EVENT_EXTRA_REWIND] = new CPacketBUTTON(EVENT_EXTRA_REWIND, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); button_map[EVENT_EXTRA_FORWARD] = new CPacketBUTTON(EVENT_EXTRA_FORWARD, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); button_map[EVENT_EXTRA_PAGEUP] = new CPacketBUTTON(EVENT_EXTRA_PAGEUP, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); button_map[EVENT_EXTRA_PAGEDOWN] = new CPacketBUTTON(EVENT_EXTRA_PAGEDOWN, "JS0:AppleRemote", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_UP] = new CPacketBUTTON(EVENT_HARMONY_UP, "JS0:Harmony", BTN_DOWN); multi_map[EVENT_HARMONY_UP | EVENT_RELEASE] = new CPacketBUTTON(EVENT_HARMONY_UP, "JS0:Harmony", BTN_UP); multi_map[EVENT_HARMONY_DOWN] = new CPacketBUTTON(EVENT_HARMONY_DOWN, "JS0:Harmony", BTN_DOWN); multi_map[EVENT_HARMONY_DOWN | EVENT_RELEASE] = new CPacketBUTTON(EVENT_HARMONY_DOWN, "JS0:Harmony", BTN_UP); multi_map[EVENT_HARMONY_LEFT] = new CPacketBUTTON(EVENT_HARMONY_LEFT, "JS0:Harmony", BTN_DOWN); multi_map[EVENT_HARMONY_LEFT | EVENT_RELEASE] = new CPacketBUTTON(EVENT_HARMONY_LEFT, "JS0:Harmony", BTN_UP); multi_map[EVENT_HARMONY_RIGHT] = new CPacketBUTTON(EVENT_HARMONY_RIGHT, "JS0:Harmony", BTN_DOWN); multi_map[EVENT_HARMONY_RIGHT | EVENT_RELEASE] = new CPacketBUTTON(EVENT_HARMONY_RIGHT, "JS0:Harmony", BTN_UP); multi_map[EVENT_HARMONY_OK] = new CPacketBUTTON(EVENT_HARMONY_OK, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_MENU] = new CPacketBUTTON(EVENT_HARMONY_MENU, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_HOLD_OK] = new CPacketBUTTON(EVENT_HARMONY_HOLD_OK, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_HOLD_MENU] = new CPacketBUTTON(EVENT_HARMONY_HOLD_MENU, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_PLAY] = new CPacketBUTTON(EVENT_HARMONY_PLAY, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_STOP] = new CPacketBUTTON(EVENT_HARMONY_STOP, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_PAUSE] = new CPacketBUTTON(EVENT_HARMONY_PAUSE, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_REPLAY] = new CPacketBUTTON(EVENT_HARMONY_REPLAY, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_SKIP] = new CPacketBUTTON(EVENT_HARMONY_SKIP, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_REWIND] = new CPacketBUTTON(EVENT_HARMONY_REWIND, "JS0:Harmony", BTN_DOWN); multi_map[EVENT_HARMONY_REWIND | EVENT_RELEASE] = new CPacketBUTTON(EVENT_HARMONY_REWIND, "JS0:Harmony", BTN_UP); multi_map[EVENT_HARMONY_FORWARD] = new CPacketBUTTON(EVENT_HARMONY_FORWARD, "JS0:Harmony", BTN_DOWN); multi_map[EVENT_HARMONY_FORWARD | EVENT_RELEASE] = new CPacketBUTTON(EVENT_HARMONY_FORWARD, "JS0:Harmony", BTN_UP); multi_map[EVENT_HARMONY_RECORD] = new CPacketBUTTON(EVENT_HARMONY_RECORD, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_PREV] = new CPacketBUTTON(EVENT_HARMONY_PREV, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_GUIDE] = new CPacketBUTTON(EVENT_HARMONY_GUIDE, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_INFO] = new CPacketBUTTON(EVENT_HARMONY_INFO, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_EXIT] = new CPacketBUTTON(EVENT_HARMONY_EXIT, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_VOLUP] = new CPacketBUTTON(EVENT_HARMONY_VOLUP, "JS0:Harmony", BTN_DOWN); multi_map[EVENT_HARMONY_VOLUP | EVENT_RELEASE] = new CPacketBUTTON(EVENT_HARMONY_VOLUP, "JS0:Harmony", BTN_UP); multi_map[EVENT_HARMONY_VOLDOWN] = new CPacketBUTTON(EVENT_HARMONY_VOLDOWN, "JS0:Harmony", BTN_DOWN); multi_map[EVENT_HARMONY_VOLDOWN | EVENT_RELEASE] = new CPacketBUTTON(EVENT_HARMONY_VOLDOWN, "JS0:Harmony", BTN_UP); multi_map[EVENT_HARMONY_1] = new CPacketBUTTON(EVENT_HARMONY_1, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_2] = new CPacketBUTTON(EVENT_HARMONY_2, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_3] = new CPacketBUTTON(EVENT_HARMONY_3, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_4] = new CPacketBUTTON(EVENT_HARMONY_4, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_5] = new CPacketBUTTON(EVENT_HARMONY_5, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_6] = new CPacketBUTTON(EVENT_HARMONY_6, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_7] = new CPacketBUTTON(EVENT_HARMONY_7, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_8] = new CPacketBUTTON(EVENT_HARMONY_8, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_9] = new CPacketBUTTON(EVENT_HARMONY_9, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_0] = new CPacketBUTTON(EVENT_HARMONY_0, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_CLEAR] = new CPacketBUTTON(EVENT_HARMONY_CLEAR, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_ENTER] = new CPacketBUTTON(EVENT_HARMONY_ENTER, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_MUTE] = new CPacketBUTTON(EVENT_HARMONY_MUTE, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_ASPECT] = new CPacketBUTTON(EVENT_HARMONY_ASPECT, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F1] = new CPacketBUTTON(EVENT_HARMONY_F1, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F2] = new CPacketBUTTON(EVENT_HARMONY_F2, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F3] = new CPacketBUTTON(EVENT_HARMONY_F3, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F4] = new CPacketBUTTON(EVENT_HARMONY_F4, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F5] = new CPacketBUTTON(EVENT_HARMONY_F5, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F6] = new CPacketBUTTON(EVENT_HARMONY_F6, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F7] = new CPacketBUTTON(EVENT_HARMONY_F7, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F8] = new CPacketBUTTON(EVENT_HARMONY_F8, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F9] = new CPacketBUTTON(EVENT_HARMONY_F9, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F10] = new CPacketBUTTON(EVENT_HARMONY_F10, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F11] = new CPacketBUTTON(EVENT_HARMONY_F11, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F12] = new CPacketBUTTON(EVENT_HARMONY_F12, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F13] = new CPacketBUTTON(EVENT_HARMONY_F13, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_F14] = new CPacketBUTTON(EVENT_HARMONY_F14, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_CHANUP] = new CPacketBUTTON(EVENT_HARMONY_CHANUP, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_CHANDOWN] = new CPacketBUTTON(EVENT_HARMONY_CHANDOWN, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_LRGDOWN] = new CPacketBUTTON(EVENT_HARMONY_LRGDOWN, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_LRGUP] = new CPacketBUTTON(EVENT_HARMONY_LRGUP, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_PWRTOGGLE] = new CPacketBUTTON(EVENT_HARMONY_PWRTOGGLE, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_QUEUE] = new CPacketBUTTON(EVENT_HARMONY_QUEUE, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_SLEEP] = new CPacketBUTTON(EVENT_HARMONY_SLEEP, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_RED] = new CPacketBUTTON(EVENT_HARMONY_RED, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_GREEN] = new CPacketBUTTON(EVENT_HARMONY_GREEN, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_YELLOW] = new CPacketBUTTON(EVENT_HARMONY_YELLOW, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); multi_map[EVENT_HARMONY_BLUE] = new CPacketBUTTON(EVENT_HARMONY_BLUE, "JS0:Harmony", BTN_DOWN | BTN_NO_REPEAT | BTN_QUEUE); pairedRemoteId = readPairedAddressId(); if(debug) printf("Paired to: %x\n", pairedRemoteId); if(debug) printf("Ready!\n"); set_led(LEDMODE_WHITE); int keydown = 0; set_led(idle_mode); while(1){ int result = usb_interrupt_read(get_ir(), 0x82, (char*) &command, sizeof(command), keydown ? BUTTON_TIMEOUT : 0); if(result > 0) { // we have an IR code! unsigned long start = millis(); if(debug) dumphex((unsigned char*) &command, result); if(command.flags == 0x26) { // set command.event = 0xee; } switch(command.event) { case 0xee: case 0xe5: if(pairedRemoteId == 0 || command.address == pairedRemoteId || (is_multi_candidate(command))) { set_led(button_mode); handle_button(command); } break; case 0xe0: set_led(special_mode); handle_special(command); break; default: if(debug) printf("Unknown event %x\n", command.event); } keydown = 1; } else if(result == -110) { // timeout, reset led keydown = 0; set_led(idle_mode); handle_button(timeoutCommand); handle_special(timeoutCommand); } else { // something else keydown = 0; if(debug) printf("Got nuffing: %d\n", result); } } reattach(); }
static int get_language(sc_card_t *card) { sc_apdu_t apdu; u8 prefs[240], *lg_value; u8 path[] = { 0x3F, 0x00, 0xDF, 0x01, 0x40, 0x39 }; int r, i, len; /* Get the language from the card's preferences file */ assert(card != NULL); sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xA4, 0x08, 0x0C); apdu.lc = sizeof(path); apdu.data = path; apdu.datalen = sizeof(path); apdu.resplen = 0; apdu.le = 0; r = sc_lock(card); if (r < 0) goto prefs_error; r = sc_transmit_apdu(card, &apdu); if (r < 0) { sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Select_File[prefs_file] command failed: %d\n", r); sc_unlock(card); goto prefs_error; } r = sc_check_sw(card, apdu.sw1, apdu.sw2); if (r < 0) { sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Select_File[prefs_file]: card returned %d\n", r); sc_unlock(card); goto prefs_error; } r = iso_ops->read_binary(card, 0, prefs, sizeof(prefs), 0); sc_unlock(card); if (r <= 0) { sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Read_Binary[prefs_file] returned %d\n", r); goto prefs_error; } #if 0 dumphex("Prefs: ", prefs, r); #endif i = get_pref(prefs, r, "[gen]", "lg", &len); if (i <= 0 || len < 2) { sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Couldn't find language in prefs file: %d\n", i); goto prefs_error; } lg_value = prefs + i; /* language code(s) found, starts here */ i = 0; while (1) { while (i <= len - 2 && (lg_value[i] == ' ' || lg_value[i] == '|')) i++; if (i > len - 2) goto prefs_error; r = str2lang(card->ctx, lg_value + i); if (r >= 0) return r; i += 2; } prefs_error: /* If troubles with the card's prefs file, get the language from the OS */ #ifdef _WIN32 switch (GetUserDefaultLangID() & 0x00FF) { case 0x13: return LNG_DUTCH; case 0x0C: return LNG_FRENCH; case 0x07: return LNG_GERMAN; default: return LNG_ENG; } #endif return LNG_ENG; /* default */ }
static int filter_packet(lua_State *L, int cb_ref, char *buf, int len) { int offset = 0; int z; printf("Type: 0x%x - 0x%04x - 0x%x\n Len: %d\n", *((uint8_t *) (buf + 14)), *((uint16_t *) (buf + 12)), *((uint8_t *) (buf + 23)), len); // Allow ARP if ( *((uint16_t *) (buf + 12)) == 0x0608) { return 1; } dump(buf, len); dumphex(buf, len); lua_rawgeti(L, LUA_REGISTRYINDEX, cb_ref); lua_pushlstring(L, buf, len); lua_pushinteger(L, len); if ( lua_pcall(L, 2, 1, 0) != 0 ) { fprintf(stderr, "%s\n", lua_tostring(L, -1)); exit(1); } // Retrieve result if ( !lua_isnumber(L, -1) ) { fprintf(stderr, "Lua function must return a number!\n"); exit(1); } z = lua_tonumber(L, -1); lua_pop(L, 1); // pop returned value printf("Return value from LUA script: %d\n", z); if (z) { return 0; } // Allow STP if ( *((uint16_t *) (buf + 12)) == 0x2700) { return 1; } // Allow IPv6 neighbor solicitation if (*((uint16_t *) (buf + 12)) == 0xdd86 && *((uint8_t *) (buf + 23)) == 0x80) { return 1; } // GRE encapsulated if (*((uint16_t *) (buf + 12)) == 0x0008 && *((uint8_t *) (buf + 23)) == 0x2f) { offset = 24; printf("Decoded GRE\n-----------\nType: 0x%x - 0x%04x - 0x%x\n Len: %d\n", *((uint8_t *) (buf + offset + 14)), *((uint16_t *) (buf + offset + 12)), *((uint8_t *) (buf + offset + 23)), len); } // Allow ICMP if (*((uint16_t *) (buf + offset + 12)) == 0x0008 && *((uint8_t *) (buf + offset + 23)) == 0x1) { return 1; } // Allow SSH if (*((uint16_t *) (buf + offset + 12)) == 0x0008 && *((uint8_t *) (buf + offset + 23)) == 0x6) { return 1; } // Drop anything else return 0; }
void read_write( int fd ) { fd_set fds; char buf[BUFSIZE]; int cc, infd = 0, outfd = 1; M_INT64 total_bytes; time_t start_time, last_time, time_now; int total_1s; int total_5s; int each_sec[5]; int each_ind; FD_ZERO(&fds); if ( ISSET(OPT_FLOOD) ) { /* if you want to test a link that compresses data, like PPP with built-in libz support, send random data, since it doesn't compress well. */ for ( cc = 0; cc < BUFSIZE; cc++ ) buf[cc] = random() & 0xff; outfd = fd; } last_time = time_now = time( &start_time ); total_bytes = 0; total_1s = 0; total_5s = 0; each_sec[0] = 0; each_sec[1] = 0; each_sec[2] = 0; each_sec[3] = 0; each_sec[4] = 0; each_ind = 0; while (1) { if ( !ISSET(OPT_FLOOD) ) { #define setup_fds() do { \ FD_ZERO( &fds ); \ if ( !ISSET(OPT_NOSTDIN)) \ FD_SET( 0, &fds ); \ FD_SET( fd, &fds ); } while(0) setup_fds(); cc = select( fd+1, &fds, NULL, NULL, NULL ); if (FD_ISSET(0, &fds)) { infd = 0; outfd = fd; } else { infd = fd; outfd = 1; } if (infd == fd && ISSET(OPT_PINGACK)) { int in_size, temp_cc; cc = read(infd, &in_size, 4); if (cc == 4) { cc = 0; in_size = ntohl(in_size); while (cc < in_size) { temp_cc = read(infd, buf+cc, in_size-cc); cc += temp_cc; } write(fd,buf,1); } else { cc = 0; } } else { cc = read(infd, buf, BUFSIZE); } } else { cc = BUFSIZE; } if (ISSET(OPT_RAWQ) && (buf[0] == 3)) { printf( "\n" ); break; } total_bytes += cc; total_1s += cc; if (ISSET(OPT_VERBOSE) && time(&time_now) != last_time) { char rate[30]; total_5s -= each_sec[each_ind]; each_sec[each_ind] = total_1s; total_1s = 0; total_5s += each_sec[each_ind]; each_ind = (each_ind+1)%5; last_time = time_now; strcpy( rate, m_dump_number( total_bytes / (time_now-start_time), 10 )); if ( (time_now-start_time) < 5 ) fprintf( stderr, " %s bytes in %d seconds (%s bps) \r", m_dump_number( total_bytes, 10 ), time_now-start_time, rate ); else fprintf( stderr, " %s bytes in %d seconds (%s bps) " "(5s avg %d bps) \r", m_dump_number( total_bytes, 10 ), time_now-start_time, rate, total_5s / 5 ); } if (cc > 0) { if (ISSET(OPT_RAWQ) && (infd == 0)) { int x; for (x = 0; x < cc; x++) if (buf[x] == 10) buf[x] = 13; } if (outfd == fd && ISSET(OPT_PINGACK)) { int outsize; outsize = htonl(cc); write(fd, &outsize, 4); } if ( ISSET(OPT_DELNULS)) { int a,b; char outbuf[ BUFSIZE ]; for ( a = b = 0; a < cc; a++ ) if ( buf[a] != 0 && buf[a] != 13 ) outbuf[b++] = buf[a]; write( outfd, outbuf, b ); } else { write(outfd, buf, cc); } if (ISSET(OPT_ECHO)) dumphex(infd, buf, cc); if (outfd == fd && ISSET(OPT_PINGACK)) { read(fd, buf, 1); } } else { break; } } if (ISSET(OPT_VERBOSE)) fprintf( stderr, "\n" ); if (ISSET(OPT_VERBOSE) || ISSET(OPT_STAT)) { char rate[30]; /* avoid a divide-by-zero if the transfer takes less than a second. */ if (time(&time_now) == start_time) time_now++; strcpy( rate, m_dump_number( total_bytes / (time_now-start_time), 10 )); fprintf( stderr, " %s bytes in %d seconds (%s bps) \n", m_dump_number( total_bytes, 10 ), time_now-start_time, rate ); } close(fd); }