[insert project logo here (125x200px max)]

Navigator

Mailinglists

Please report any errors or ommissions you find to our `Help' mailinglist, or post a message in the Forums.

Copyright and Licensing Information

Snap is (c) Jonathan T. Moore, 1999-2002 and licensed under the GNU General Public License (GPL).

All other parts of Splash are (c) Willem de Bruijn, 2002-2003 and licensed under the BSD Open Source License.

All sourcecode is made publicly available.

Acknowledgement

Splash and the Splash website are hosted by SourceForge.net

SourceForge.net Logo

osi-open source certified logo

Splash - Documentation

SNMP Plus a Lightweight API for SNAP Handling

Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

snap-1.1-wjdb/utils/snapas.c

Go to the documentation of this file.
00001 /* snap-1.0. Copyright (C) 2000 by Jonathan T. Moore and Michael Hicks.
00002  *
00003  * snapas.c : SNAP assembler. Takes a SNAP program in (ascii) SNAP
00004  *   assembly language, and produces a wire-format binary packet.
00005  *   default filename extensions:
00006  *     .sas = SNAP assembly language
00007  *     .sbc = SNAP wire format (SNAP bytecode)
00008  *
00009  * $Id: snapas.c,v 1.2 2003/04/16 10:58:59 wdebruij Exp $
00010  */
00011 
00012 #include <assert.h>
00013 #include <fcntl.h>
00014 #include <stdio.h>
00015 #include <string.h>
00016 #include <linux/ip.h> /* for struct iphdr only*/
00017 #include <sys/types.h>
00018 #include <sys/stat.h>
00019 #include <unistd.h>
00020 #include "../lib/config.h"
00021 #include "../lib/d_printf.h"
00022 #include "../lib/bytecode.h"
00023 #include "../lib/io.h"
00024 #include "../lib/memalloc.h"
00025 #include "../lib/packet.h"
00026 #include "../lib/d_printf.h"
00027 #include "snapparse.h"
00028 #include "labels.h"
00029 #include "../lib/consts.h"
00030 
00031 extern char *basename(const char *);
00032 
00033 void parse_cmdline(int argc, char **argv);
00034 
00035 extern int yyparse(void);
00036 extern int yydebug;
00037 extern FILE *yyin;
00038 
00039 int outfd;          /* output file descriptor */
00040 char *outfilename = NULL;   /* output file name */
00041 char *infilename;       /* input file name */
00042 
00043 packet_t *p;            /* the packet we're building */
00044 instr_t *cbuf;          /* code buffer */
00045 value_t *sbuf;          /* stack buffer */
00046 void *hbuf;         /* heap buffer */
00047 char *pbuf;         /* packet buffer */
00048 
00049 int noop;
00050 
00051 #ifdef YYDEBUG
00052 extern int yydebug;
00053 #endif
00054 
00055 int main(int argc, char **argv) {
00056   int err;
00057   buffer_t bufstr;
00058 
00059   d_printf(10,"%s:%d: starting up\n",__FILE__,__LINE__);
00060   d_printf(10,"%s:%d: sizeof(header_t) = %d bytes\n",__FILE__,__LINE__,
00061        sizeof(header_t));
00062   
00063   parse_cmdline(argc,argv);
00064 
00065   memalloc(p,packet_t *,sizeof(packet_t));
00066   memalloc(cbuf,instr_t *,code_sizeb);
00067   memalloc(sbuf,value_t *,stack_sizeb);
00068   memalloc(hbuf,void *,heap_sizeb);
00069 
00070   memalloc(p->hdr,header_t *,sizeof(header_t));
00071   p->code_min = cbuf;
00072   p->pc = cbuf;
00073   p->code_max = (instr_t *)((void *)cbuf + code_sizeb);
00074   p->stack_min = sbuf;
00075   p->sp = sbuf;
00076   p->stack_max = (value_t *)((void *)sbuf + stack_sizeb);
00077   p->heap_min = hbuf;
00078   p->h_alloc_ptr = hbuf;
00079   p->heap_max = hbuf + heap_sizeb;
00080   p->h_alloc_heap_max = p->stack_max;
00081 
00082   p->is_contiguous = 0;
00083 
00084 #ifdef YYDEBUG
00085   yydebug=1;
00086 #endif
00087   noop = 0;
00088   err = yyparse();
00089   if (err != 0) {
00090     fprintf(stderr,"%s:%d: parse failure (code=%d)\n",__FILE__,__LINE__,err);
00091     fflush(stderr);
00092     exit(1);
00093   }
00094   patch_jumps(p);
00095   p->code_max = p->pc;
00096   p->heap_max = p->h_alloc_ptr;
00097 
00098   d_printf(25,"read %d/%d (%d%%) instructions with no arguments\n",
00099        noop, (p->code_max - p->code_min),
00100        (noop * 100) / (p->code_max - p->code_min));
00101 
00102   marshal_packet(p,p->sp - p->stack_min,&bufstr);
00103 
00104   /* 
00105     we're using the marshal function from ../lib
00106     it prepends an IP header, strip this
00107   */
00108   write(outfd,&bufstr.s[sizeof(struct iphdr) + 4],bufstr.lenb - (sizeof(struct iphdr) + 4));
00109 
00110   return(0);
00111 }
00112 
00113 void usage(int argc, char **argv) {
00114   printf("usage: %s [-?] [-o outfile] infile\n",basename(argv[0]));
00115   printf("\t-? : print this help\n");
00116   printf("\t-o : set the output file name\n");
00117 }
00118 
00119 void parse_cmdline(int argc, char **argv) {
00120   char *opts = "?o:";
00121   char c;
00122   int args_expected = 1;
00123   int args_processed = 0;
00124   int argidx;
00125 
00126   while((c = getopt(argc,argv,opts)) != EOF) {
00127     switch(c) {
00128     case '?':
00129       usage(argc,argv);
00130       exit(1);
00131     case 'o':
00132       outfilename = optarg;
00133       break;
00134     }
00135   }
00136   argidx = optind;
00137   while(args_processed < args_expected) {
00138     if (argidx >= argc) {
00139       /* missing arguments */
00140       printf("%s: missing argument(s)\n",basename(argv[0]));
00141       usage(argc,argv);
00142       exit(1);
00143     }
00144     switch(args_processed) {
00145     case 0:
00146       infilename = argv[argidx]; break;
00147     }
00148     argidx++;
00149     args_processed++;
00150   }
00151   if (argidx < argc) {
00152     /* extra arguments */
00153     printf("%s: extra argument(s)\n",basename(argv[0]));
00154     usage(argc,argv);
00155     exit(1);
00156   }
00157 
00158   assert(infilename != NULL);
00159   if (outfilename == NULL) {
00160     /* set up default output filename */
00161     if (strcmp(infilename,"-") == 0) {
00162       outfilename = "out.sbc";
00163     } else {
00164       int inlen = strlen(infilename);
00165       if (strcmp(&(infilename[inlen-4]),".sas") == 0) {
00166     memalloc(outfilename,char *,inlen + 1);
00167     strncpy(outfilename,infilename,inlen-4);
00168     strcpy(&(outfilename[inlen-4]),".sbc");
00169       } else {
00170     memalloc(outfilename,char *,inlen + 4 + 1);
00171     strncpy(outfilename,infilename,inlen);
00172     strcpy(&(outfilename[inlen]),".sbc");
00173       }
00174     }
00175   }
00176 
00177   /* set up the input and output files correctly */
00178   if (strcmp(infilename,"-") != 0) {
00179     yyin = fopen(infilename,"r");
00180     if (yyin == NULL) {
00181       fprintf(stderr,"%s: unable to open file \"%s\" for input\n",
00182           basename(argv[0]), infilename);
00183       fflush(stderr);
00184       exit(1);
00185     }
00186   }
00187   if (strcmp(outfilename,"-") == 0) {
00188     outfd = 1;
00189   } else {
00190     outfd = open(outfilename,O_WRONLY|O_CREAT|O_TRUNC,
00191          S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
00192     if (outfd == -1) {
00193       fprintf(stderr,"%s: unable to open file \"%s\" for output\n",
00194           basename(argv[0]), outfilename);
00195       fflush(stderr);
00196       exit(1);
00197     }
00198   }
00199 
00200   return;
00201 }