// write the file (or pipe) to another file static void _near ListSaveFile( void ) { int i, nFH, nMode; long lTemp; POPWINDOWPTR wn; TCHAR szBuffer[ MAXLISTLINE+1 ]; // disable ^C / ^BREAK handling HoldSignals(); wn = wOpen( 2, 1, 4, GetScrCols() - 2, nInverse, LIST_SAVE_TITLE, NULL ); wn->nAttrib = nNormal; wClear(); wWriteListStr( 0, 1, wn, LIST_QUERY_SAVE ); egets( szBuffer, MAXFILENAME, EDIT_DATA | EDIT_BIOS_KEY ); if ( szBuffer[0] ) { // save start position lTemp = LFile.lViewPtr; nMode = _O_BINARY; if (( nFH = _sopen( szBuffer, ( nMode | _O_WRONLY | _O_CREAT | _O_TRUNC ), _SH_DENYNO, _S_IWRITE | _S_IREAD )) >= 0 ) { // reset to beginning of file ListSetCurrent( 0L ); do { for ( i = 0; ( i < MAXLISTLINE ); i++ ) { // don't call GetNextChar unless absolutely necessary if ( LFile.lpCurrent == LFile.lpEOF ) break; szBuffer[i] = (TCHAR)GetNextChar(); } szBuffer[i] = _TEXT('\0'); } while (( i > 0 ) && ( wwrite( nFH, szBuffer, i ) > 0 )); _close( nFH ); // restore start position LFile.lViewPtr = lTemp; ListSetCurrent( LFile.lViewPtr ); } else honk(); } wRemove( wn ); // enable ^C / ^BREAK handling EnableSignals(); }
static int compress_fd (int from, int to) { ssize_t us, cs, len; u8 buf1[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16]; u8 buf2[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16]; u8 *header; nr_read = nr_written = 0; while ((us = rread (from, &buf1[MAX_HDR_SIZE], blocksize)) > 0) { cs = lzf_compress (&buf1[MAX_HDR_SIZE], us, &buf2[MAX_HDR_SIZE], us > 4 ? us - 4 : us); if (cs) { header = &buf2[MAX_HDR_SIZE - TYPE1_HDR_SIZE]; header[0] = 'Z'; header[1] = 'V'; header[2] = 1; header[3] = cs >> 8; header[4] = cs & 0xff; header[5] = us >> 8; header[6] = us & 0xff; len = cs + TYPE1_HDR_SIZE; } else { // write uncompressed header = &buf1[MAX_HDR_SIZE - TYPE0_HDR_SIZE]; header[0] = 'Z'; header[1] = 'V'; header[2] = 0; header[3] = us >> 8; header[4] = us & 0xff; len = us + TYPE0_HDR_SIZE; } if (raw) { header = &buf2[MAX_HDR_SIZE]; len = cs; } if (wwrite (to, header, len) == -1) return -1; }
/* void testbitmask(void) { setmaskbit(31); setmaskbit(95); setmaskbit(159);setmaskbit(223); setmaskbit(287); setmaskbit(351); setmaskbit(415);setmaskbit(479); setmaskbit(90); setmaskbit(154);setmaskbit(218); setmaskbit(282); setmaskbit(346); setmaskbit(347); setmaskbit(348); setmaskbit(349); setmaskbit(350); setmaskbit(100); setmaskbit(164);setmaskbit(228); setmaskbit(292); setmaskbit(356); setmaskbit(355); setmaskbit(354); setmaskbit(353); setmaskbit(352); printfreemask(); clearmaskbit(31); clearmaskbit(95); clearmaskbit(159);clearmaskbit(223); clearmaskbit(287); clearmaskbit(351); clearmaskbit(415);clearmaskbit(479); clearmaskbit(90); clearmaskbit(154);clearmaskbit(218); clearmaskbit(282); clearmaskbit(346); clearmaskbit(347); clearmaskbit(348); clearmaskbit(349); clearmaskbit(350); clearmaskbit(100); clearmaskbit(164);clearmaskbit(228); clearmaskbit(292); clearmaskbit(356); clearmaskbit(355); clearmaskbit(354); clearmaskbit(353); clearmaskbit(352); printfreemask(); } */ void test_fs(void){ int fd1, fd2; //File descriptors fd1 = fcreat("Test File", 0); //Creating a new file printf("Helo %d\n", fd1); //fd2 = fcreat("Sample", 0); //Creating a new file fd1 = fopen("Test File", 1);//Opening a file in write-only mode printf("file opened in write only mode..%d\n",fd1); wwrite(fd1); fclose(fd1); //fwrite(fd1, ) fd1 = fopen("Test File", 0); //Opening the same file in Read only mode rread(fd1); //fclose(fd1); //fd2 = fopen("Sample", 1); //Opening a file in write only mode //fwrite(fd2, ) //fseek(fd2, 2); //fd2 = fopen("Sample", 0); //Opening a file in read only mode //rread(fd2); //fclose(fd2); }
int main(int argc, char** argv) { u_char *packet; int i; uint32_t ck; struct sockaddr *sin_src; struct sockaddr *sin_net; struct sockaddr *sin_dst; struct sockaddr *sin_mask; struct ip6_pseudo_hdr ps_hdr; int sendsize; struct options opts; FILE* fp_domain; char * domain; struct writer_info wi; int pkt_len; double speed_limit; double pkt_sent; int q_class; int q_type; pthread_t thread; pthread_create(&thread,NULL,readliner,&speed_limit); sleeper = 50; pkt_sent = 0; speed_limit = 50000; /** * * headers */ struct ether_header *eth; struct iphdr *ip; struct ip6_hdr *ip6; struct udphdr *udp; u_char *dns; u_char dns_opt[] = {00, 00, 0x29, 0x08, 00, 00, 00, 00, 00, 00, 00}; setoptions(argc, argv, &opts); packet = calloc(sizeof (u_char) * 4096, 1); sin_src = calloc(sizeof (struct sockaddr_storage), 1); sin_net = calloc(sizeof (struct sockaddr_storage), 1); sin_dst = calloc(sizeof (struct sockaddr_storage), 1); sin_mask = calloc(sizeof (struct sockaddr_storage), 1); domain = calloc(sizeof (char) * 1024, 1); //domain = NULL; if (packet == NULL || sin_src == NULL || sin_net == NULL || sin_dst == NULL || sin_mask == NULL || domain == NULL) { return -1; } /** * * ETHERNET static */ eth = (struct ether_header *) packet; if (ether_setaddr(opts.smac, &(eth->ether_shost)) < 0) { fprintf(stderr, "smac error:%s\n"); return 1; } if (ether_setaddr(opts.dmac, &(eth->ether_dhost)) < 0) { fprintf(stderr, "dmac error\n"); return 1; } /** * * IP static */ if (getipaddr(opts.dip, sin_dst, opts.family) < 0) { fprintf(stderr, "dip error\n"); return 2; } if (getipaddr(opts.snet, sin_net, opts.family) < 0) { fprintf(stderr, "sip error\n"); return 2; } if (sin_net->sa_family != sin_dst->sa_family) { fprintf(stderr, "IP family doesn't match\n"); return 2; } set_mask(sin_mask, sin_net->sa_family, opts.smask); if (sin_net->sa_family == AF_INET) { eth->ether_type = htons(ETHERTYPE_IP); ip = (struct iphdr *) (packet + ETHER_HDR_LEN); ip->daddr = ((struct sockaddr_in *) sin_dst)->sin_addr.s_addr; ip->version = 4; ip->ihl = 5; // no opts ip->frag_off = 0; ip->id = 0; ip->protocol = IPPROTO_UDP; ip->tos = 0; ip->ttl = IPDEFTTL; udp = (struct udphdr *) (((void *) ip) + IP_HDR_LEN); } else if (sin_net->sa_family == AF_INET6) { eth->ether_type = htons(ETHERTYPE_IPV6); ip6 = (struct ip6_hdr *) (packet + ETHER_HDR_LEN); ip6->ip6_vfc = 6 << 4; ip6->ip6_hlim = IPDEFTTL; ip6->ip6_nxt = IPPROTO_UDP; memcpy(&(ip6->ip6_dst), &(((struct sockaddr_in6 *) sin_dst)->sin6_addr), 16); udp = (struct udphdr *) (((void *) ip6) + IP6_HDR_LEN); } else { fprintf(stderr, "Family unknown\n"); return 2; } /** * * UDP static */ //udp->check = 0; udp->dest = htons(53); /** * * DNS static */ dns = (u_char *) (((void *) udp) + UDP_HDR_LEN); wopen(&wi,&opts); init_domain_file(&fp_domain, opts.in_file_name); if (fp_domain == NULL) { fprintf(stderr, "Can't open queries file\n"); wclose(&wi); return 4; } for (i = 0; i < opts.count; i++) { if(pkt_sent >= sleeper) { speed_calc(pkt_sent,speed_limit, &sleeper); pkt_sent = 0; usleep(1); } /** * * DNS dynamic */ if(nextdomain(fp_domain, &domain, &q_type, &q_class)) { printf("Can't read next domain\n"); exit(1); } sendsize = res_mkquery(QUERY, domain, q_class, q_type, NULL, 0, NULL, dns, PACKETSZ); dns[11] = 1; memcpy(dns + sendsize, dns_opt, 11); sendsize += 11; /** * * UDP dynamic */ udp->source = htons(rand()); udp->len = htons(sendsize + UDP_HDR_LEN); udp->check = 0; /** * * IP dynamic */ get_rand_addr(sin_net, sin_mask, sin_src); if (sin_net->sa_family == AF_INET) { ip->saddr = ((struct sockaddr_in *) sin_src)->sin_addr.s_addr; ip->tot_len = htons(sendsize + UDP_HDR_LEN + (ip->ihl * 4)); pkt_len = sendsize + UDP_HDR_LEN + (ip->ihl * 4) + ETHER_HDR_LEN; ip->check = 0; ip->check = inet_cksum(ip, (ip->ihl * 4), 0); } else if (sin_net->sa_family == AF_INET6) { memcpy(&(ip6->ip6_src), &(((struct sockaddr_in6 *) sin_src)->sin6_addr), 16); ip6->ip6_plen = htons(sendsize + UDP_HDR_LEN); pkt_len = sendsize + UDP_HDR_LEN + sizeof (struct ip6_hdr) +ETHER_HDR_LEN; ps_hdr.src = ip6->ip6_src; ps_hdr.dst = ip6->ip6_dst; ps_hdr.len = udp->len; ps_hdr.nh = ntohs(17); ck = inet_cksum(&ps_hdr, sizeof (ps_hdr), 0); udp->check = inet_cksum(udp, sendsize + UDP_HDR_LEN, (~ck)&0xffff); } if (pkt_len > opts.mtu) { fprintf(stderr, "too long: %s needs %d MTU is %d\n", domain, pkt_len, opts.mtu); } else { wwrite(&wi,packet,pkt_len); } pkt_sent++; } wclose(&wi); fclose(fp_domain); return (EXIT_SUCCESS); }