📄 snapas.c
字号:
/* snap-1.0. Copyright (C) 2000 by Jonathan T. Moore and Michael Hicks. * * snapas.c : SNAP assembler. Takes a SNAP program in (ascii) SNAP * assembly language, and produces a wire-format binary packet. * default filename extensions: * .sas = SNAP assembly language * .sbc = SNAP wire format (SNAP bytecode) * * $Id: snapas.c,v 1.2 2003/09/17 11:26:10 tmoerlan Exp $ */#include <assert.h>#include <fcntl.h>#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include "../lib/config.h"#include "../lib/d_printf.h"#include "../lib/bytecode.h"#include "../lib/io.h"#include "../lib/memalloc.h"#include "../lib/packet.h"#include "../lib/d_printf.h"#include "snapparse.h"#include "labels.h"#include "../lib/consts.h"struct iphdr {#if __BYTE_ORDER == __LITTLE_ENDIAN unsigned int ihl:4; unsigned int version:4;#elif __BYTE_ORDER == __BIG_ENDIAN unsigned int version:4; unsigned int ihl:4;#else# error "Please fix <bits/endian.h>"#endif u_int8_t tos; u_int16_t tot_len; u_int16_t id; u_int16_t frag_off; u_int8_t ttl; u_int8_t protocol; u_int16_t check; u_int32_t saddr; u_int32_t daddr; /*The options start here. */ };extern char *basename(const char *);void parse_cmdline(int argc, char **argv);extern int yyparse(void);extern int yydebug;extern FILE *yyin;int outfd; /* output file descriptor */char *outfilename = NULL; /* output file name */char *infilename; /* input file name */packet_t *p; /* the packet we're building */instr_t *cbuf; /* code buffer */value_t *sbuf; /* stack buffer */void *hbuf; /* heap buffer */char *pbuf; /* packet buffer */int noop;#ifdef YYDEBUGextern int yydebug;#endifint main(int argc, char **argv) { int err; buffer_t bufstr; d_printf(10,"%s:%d: starting up\n",__FILE__,__LINE__); d_printf(10,"%s:%d: sizeof(header_t) = %d bytes\n",__FILE__,__LINE__, sizeof(header_t)); parse_cmdline(argc,argv); memalloc(p,packet_t *,sizeof(packet_t)); memalloc(cbuf,instr_t *,code_sizeb); memalloc(sbuf,value_t *,stack_sizeb); memalloc(hbuf,void *,heap_sizeb); memalloc(p->hdr,header_t *,sizeof(header_t)); p->code_min = cbuf; p->pc = cbuf; p->code_max = (instr_t *)((void *)cbuf + code_sizeb); p->stack_min = sbuf; p->sp = sbuf; p->stack_max = (value_t *)((void *)sbuf + stack_sizeb); p->heap_min = hbuf; p->h_alloc_ptr = hbuf; p->heap_max = hbuf + heap_sizeb; p->h_alloc_heap_max = p->stack_max; p->is_contiguous = 0;#ifdef YYDEBUG yydebug=1;#endif noop = 0; err = yyparse(); if (err != 0) { fprintf(stderr,"%s:%d: parse failure (code=%d)\n",__FILE__,__LINE__,err); fflush(stderr); exit(1); } patch_jumps(p); p->code_max = p->pc; p->heap_max = p->h_alloc_ptr; d_printf(25,"read %d/%d (%d%%) instructions with no arguments\n", noop, (p->code_max - p->code_min), (noop * 100) / (p->code_max - p->code_min)); marshal_packet(p,p->sp - p->stack_min,&bufstr); /* we're using the marshal function from ../lib it prepends an IP header, strip this */ write(outfd,&bufstr.s[sizeof(struct iphdr) + 4],bufstr.lenb - (sizeof(struct iphdr) + 4)); return(0);}void usage(int argc, char **argv) { printf("usage: %s [-?] [-o outfile] infile\n",basename(argv[0])); printf("\t-? : print this help\n"); printf("\t-o : set the output file name\n");}void parse_cmdline(int argc, char **argv) { char *opts = "?o:"; char c; int args_expected = 1; int args_processed = 0; int argidx; while((c = getopt(argc,argv,opts)) != EOF) { switch(c) { case '?': usage(argc,argv); exit(1); case 'o': outfilename = optarg; break; } } argidx = optind; while(args_processed < args_expected) { if (argidx >= argc) { /* missing arguments */ printf("%s: missing argument(s)\n",basename(argv[0])); usage(argc,argv); exit(1); } switch(args_processed) { case 0: infilename = argv[argidx]; break; } argidx++; args_processed++; } if (argidx < argc) { /* extra arguments */ printf("%s: extra argument(s)\n",basename(argv[0])); usage(argc,argv); exit(1); } assert(infilename != NULL); if (outfilename == NULL) { /* set up default output filename */ if (strcmp(infilename,"-") == 0) { outfilename = "out.sbc"; } else { int inlen = strlen(infilename); if (strcmp(&(infilename[inlen-4]),".sas") == 0) { memalloc(outfilename,char *,inlen + 1); strncpy(outfilename,infilename,inlen-4); strcpy(&(outfilename[inlen-4]),".sbc"); } else { memalloc(outfilename,char *,inlen + 4 + 1); strncpy(outfilename,infilename,inlen); strcpy(&(outfilename[inlen]),".sbc"); } } } /* set up the input and output files correctly */ if (strcmp(infilename,"-") != 0) { yyin = fopen(infilename,"r"); if (yyin == NULL) { fprintf(stderr,"%s: unable to open file \"%s\" for input\n", basename(argv[0]), infilename); fflush(stderr); exit(1); } } if (strcmp(outfilename,"-") == 0) { outfd = 1; } else { outfd = open(outfilename,O_WRONLY|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH); if (outfd == -1) { fprintf(stderr,"%s: unable to open file \"%s\" for output\n", basename(argv[0]), outfilename); fflush(stderr); exit(1); } } return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -