コード例 #1
0
void Connection::connect()
{
in_addr* addr = atoaddr( m_Host.c_str() );
if( !addr )
{
    std::cerr << m_Host.c_str() << std::endl;
   throw Wobbly( "Invalid network address" );

}

sockaddr_in address;
memset( (char*)&address, 0, sizeof(address) );
address.sin_family = AF_INET;
address.sin_port = htons( m_Port );
address.sin_addr.s_addr = addr->s_addr;

m_Sock = socket( AF_INET, SOCK_STREAM, 0 );
if( m_Sock < 0 )
BailOnSocketError( "socket()" );

// printf("Connecting to %s on port %d.\n",inet_ntoa(*addr), port);

if( ::connect( m_Sock, (sockaddr const*)&address, sizeof(address) ) < 0 )
BailOnSocketError( "connect()" );
}
コード例 #2
0
ファイル: socket-ns3.cpp プロジェクト: haddyclipk/ICS
// ----------------------------------------------------------------------
void
SocketNs3::
connect()
throw(SocketException)
{
    in_addr addr;
    if (!atoaddr(host_.c_str(), addr))
        BailOnSocketError("tcpip::SocketNs3::connect() @ Invalid network address");

    sockaddr_in address;
    memset((char*)&address, 0, sizeof(address));
    address.sin_family = AF_INET;
    address.sin_port = htons(port_);
    address.sin_addr.s_addr = addr.s_addr;

    socket_ = static_cast<int>(socket(PF_INET, SOCK_STREAM, 0));
    if (socket_ < 0)
        BailOnSocketError("tcpip::SocketNs3::connect() @ socket");

    if (::connect(socket_, (sockaddr const*)&address, sizeof(address)) < 0)
        BailOnSocketError("tcpip::SocketNs3::connect() @ connect");

    if (socket_ >= 0) {
        int x = 1;
        setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, (const char*)&x, sizeof(x));
    }

}
コード例 #3
0
ファイル: qlclient.c プロジェクト: AndrewCRMartin/qlite
BOOL InitLocks(char *service, char *host, int port)
{
   /* Set up the port address                                           */
   if(port == 0)
   {
      if((sPort = atoport(service, "tcp")) < 0)
      {
         sPort = htons(DEFAULT_QLPORT);
      }
   }
   else
   {
      sPort = htons(port);
   }

   /* Get an address for the remote server                              */
   if((sAddr=atoaddr(host))==NULL)
      return(FALSE);

   /* Fill in an address structure for the remote server                */
   memset((char *) &sAddress, 0, sizeof(sAddress));
   sAddress.sin_family = AF_INET;
   sAddress.sin_port = sPort;
   sAddress.sin_addr.s_addr = sAddr->s_addr;

   
   return(TRUE);
}
コード例 #4
0
ファイル: socket.c プロジェクト: Telecom-Robotics/Battleship
int sock_connect(short port, char *netaddress)
{
  struct in_addr *addr;
  struct sockaddr_in address;
  int sock_fd;

  addr = atoaddr(netaddress);
  if (addr == NULL) {
    fprintf(stderr,"make_connection:  Invalid network address.\n");
    return -1;
  }

  memset((char *) &address, 0, sizeof(address));
  address.sin_family = AF_INET;
  address.sin_port = htons(port);
  address.sin_addr.s_addr = addr->s_addr;

  sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (connect(sock_fd, (struct sockaddr *) &address, sizeof(address)) < 0) {
    perror("connect");
    return -1;
  }

  return sock_fd;
}
コード例 #5
0
ファイル: msock.c プロジェクト: damiensh22/IllustraBot2
/*
** ClientSocket()
** a wrapper around makeConnection() for Stream socket
** returns the socket on success (a positive number), -1 on failure
** NOTE: port is in host byte order
*/
int ClientSocket(char *netaddress,u_short port)
{
    struct in_addr
        *addr;

    struct sockaddr_in
        address;

    int
        connected,
        sockfd;

    int
        nport;

    addr=atoaddr(netaddress);
    if (addr == (struct in_addr *) NULL)
    {
        (void) fprintf (stderr,"ClientSocket(): Invalid network address\n");
        return (-1);
    }

    /*
    ** convert port to network byte order first
    */
    nport=htons(port);
    if (nport == -1)
    {
        (void) fprintf (stderr,"Failed to convert port %d to netword byte order!\n",port);
        return (-1);
    }

    memset((char *) &address,0,sizeof(address));
    address.sin_family=AF_INET;
    address.sin_port=nport;
    address.sin_addr.s_addr=addr->s_addr;

    sockfd=socket(AF_INET,SOCK_STREAM,0);

    /*
    ** connect
    */
    connected=connect(sockfd,(struct sockaddr *) &address,
        sizeof(address));

    if (connected < 0)
    {
        perror("connect");
        (void) fprintf (stderr,"connect() failed!\n");
        return (-1);
    }

    return (sockfd);
}
コード例 #6
0
ファイル: msock.c プロジェクト: damiensh22/IllustraBot2
int makeConnection(char *service,int type,char *netaddress)
{
    int
        port=(-1);

    struct in_addr
        *addr;

    int
        sockfd,
        connected;

    struct sockaddr_in
        address;

    if (type == SOCK_STREAM)
        port=atoport(service,"tcp");

    if (type == SOCK_DGRAM)
        port=atoport(service,"udp");

    if (port == -1)
    {
        (void) fprintf (stderr,"makeConnection: Invalid socket type\n");
        return (-1);
    }

    addr=atoaddr(netaddress);
    if (addr == (struct in_addr *) NULL)
    {
        (void) fprintf (stderr,"makeConnection: Invalid network address\n");
        return (-1);
    }

    memset((char *) &address,0,sizeof(address));
    address.sin_family=AF_INET;
    address.sin_port=port;
    address.sin_addr.s_addr=addr->s_addr;

    sockfd=socket(AF_INET,type,0);

    if (type == SOCK_STREAM)
    {
        connected=connect(sockfd,(struct sockaddr *) &address,
            sizeof(address));

        if (connected < 0)
        {
            perror("connect");
            return (-1);
        }

        return (sockfd);
    }

    /*
    ** must be udp
    */

    if (bind(sockfd,(struct sockaddr *) &address,sizeof(address)) < 0)
    {
        perror ("bind");
        return (-1);
    }
    return (sockfd);
}
コード例 #7
0
ファイル: diso.c プロジェクト: bfeng/mipc
/*
 * m a i n
 *
 * Main routine of dis_o386.
 */
int main(int argc, char *argv[])
{
  char *cp, objfile[BUFF_LEN], symbfile[BUFF_LEN];
  char table[MAXSECT*(SZ_NAME+2)];
  int j, errors;
  unsigned long int addrfirst, addrlast, addrcount;
  struct stat statbuff;

  /* initial set up */
  if ((cp = strrchr(argv[0], PSEP)) == (char *)NULL)
	cp = argv[0];
  else
	cp++;
  strncpy(progname, cp, BUFF_MAX);
  strncpy(objfile, OBJF, BUFF_MAX);
  addrfirst = addrlast = addrcount = 0;

  /* clear the in-core name tables */
  o_strtab = (char *)NULL;
  for (j = 0 ; j < MAXSECT ; j++)
	o_secnam[j] = table + j * (SZ_NAME + 2);	/* nb. leading '_' */
  for (j = 0 ; j < sizeof(table) ; j++) table[j] = '\0';

  /* check for an MSDOS-style option */
  if (argc == 2 && argv[1][0] == '/') {
	usage();
	exit(0);
  }

  /* parse arguments */
  errors = opterr = 0;
  while ((j = getopt(argc, argv, "O:S:abdf:hl:mnrstx:")) != EOF) {
	switch (j & 0177) {
#if 0
	case 'C':			/* core file name */
		opt_C = TRUE;
		if (optarg != (char *)NULL)
			strncpy(binfile, optarg, BUFF_MAX);
		else
			errors++;
		break;
	case 'E':			/* executable file name */
		opt_E = TRUE;
		if (optarg != (char *)NULL)
			strncpy(binfile, optarg, BUFF_MAX);
		else
			errors++;
		break;
#endif
	case 'O':			/* object file name */
		opt_O = TRUE;
		if (optarg != (char *)NULL)
			strncpy(objfile, optarg, BUFF_MAX);
		else
			errors++;
		break;
	case 'S':			/* symbol table name */
		opt_S = TRUE;
		if (optarg != (char *)NULL)
			strncpy(symbfile, optarg, BUFF_MAX);
		else
			errors++;
		break;
	case 'a':			/* dump tables and disassemble segments */
		opt_a = TRUE;
		break;
	case 'b':			/* dump straight binary */
		opt_b = TRUE;
		break;
	case 'd':			/* dump the data segment */
		opt_d = TRUE;
		break;
	case 'f':			/* first address of dump */
		opt_f = TRUE;
		if (optarg != (char *)NULL) {
			addrfirst = atoaddr(optarg);
		}
		else
			errors++;
		break;
	case 'h':			/* dump the header */
		opt_h = TRUE;
		break;
	case 'l':			/* last address of dump */
		opt_l = TRUE;
		if (optarg != (char *)NULL) {
			addrlast = atoaddr(optarg);
		}
		else
			errors++;
		break;
	case 'm':			/* dump the rom segment */
		opt_m = TRUE;
		break;
	case 'n':			/* dump the symbol names */
		opt_n = TRUE;
		break;
	case 'r':			/* dump the relocation structures */
		opt_r = TRUE;
		break;
	case 's':			/* dump the symbol table */
		opt_s = TRUE;
		break;
	case 't':			/* dump the text segment */
		opt_t = TRUE;
		break;
#if 0
	case 'u':			/* dump the bss segment */
		opt_u = TRUE;
		break;
#endif
	case 'x':			/* debugging flag */
		opt_x = TRUE;
		if (optarg != (char *)NULL)
			dbglvl = atoi(optarg);
		break;
	case '?':
	default:
		usage();
		exit(1);
		break;
	}
  }

  /* check the flags */
  if (errors > 0) {
	usage();
	exit(1);
  }
  if (opt_a && (opt_d || opt_h || opt_m || opt_n ||
		opt_r || opt_s || opt_t)) {
	usage();
	exit(1);
  }
  if ((opt_f || opt_l) && (addrlast > 0  && addrfirst > addrlast)) {
	usage();
	exit(1);
  }

  /* check for a specific input file */
  if (optind < argc)
	strncpy(objfile, argv[optind], BUFF_MAX);

  /* we must have a binary file of some sort */
  if ((objfp = fopen(objfile, "rb")) == (FILE *)NULL ||
		stat(objfile, &statbuff) == -1) {
	perror(objfile);
	exit(1);
  }

  /* initialise the object file data structures */
  if (init_objf(objfp) == FAILED) {
	perror(objfile);
	exit(1);
  }

  /* show the output file name and date */
  fprintf(stdout, "File name: %s\nFile date: %s",
		objfile, ctime(&statbuff.st_ctime));

  /* show the header and section data - default behaviour */
  if (opt_a || opt_h || (!opt_d && !opt_m && !opt_n &&
		!opt_r && !opt_s && !opt_t)) {
	fprintf(stdout, "\nHeader data:\n");
	(void) dump_ohdr(&o_hdrbuf);
	fprintf(stdout, "\nSection data:\n");
	(void) fseek(objfp, OFF_SECT(hdrbuf), SEEK_SET);
	(void) dump_oshdr(objfp, 0, o_hdrbuf.oh_nsect);
  }

  /* The core start address is zero for every section.  What allowances
   * should be made for the differences between file and core images?
   */

  /* dump or disassemble the rom section */
  if (opt_a || opt_m) {
	if (opt_b)
		(void) dump_osec(addrfirst, addrlast, ROM, FALSE);
	else
		(void) dump_osec(addrfirst, addrlast, ROM, TRUE);
  }

  /* dump or disassemble the data section */
  if (opt_a || opt_d) {
	if (opt_b)
		(void) dump_osec(addrfirst, addrlast, DATA, FALSE);
	else
		(void) dump_osec(addrfirst, addrlast, DATA, TRUE);
  }

  /* dump or disassemble the text section */
  if (opt_a || opt_t) {
	/* check that all offsets are valid */
	if (addrfirst > o_sectab[TEXT].os_flen || addrlast > o_sectab[TEXT].os_flen) {
		fprintf(stderr, "Invalid %s address range 0x%08.8lu to 0x%08.8lu\n",
						"text", addrfirst, addrlast);
	}
	else {
		if (opt_b)
			(void) dump_osec(addrfirst, addrlast, TEXT, FALSE);
		else {
			addrcount = (addrlast == 0) ? o_sectab[TEXT].os_flen : addrlast;
			addrcount -= addrfirst;
			disfp = objfp;			/* file to be disassembled */
			(void) fseek(disfp, o_sectab[TEXT].os_foff + addrfirst, SEEK_SET);
			fprintf(stdout, "\nDisassembled text:\n");
			(void) dasm(addrfirst, addrcount);
		}
	}
  }

  /* show the relocation data */
  if (opt_a || opt_r) {
	if (opt_b)
		addrcount = o_hdrbuf.oh_nrelo * sizeof(struct outrelo);
	else
		addrcount = o_hdrbuf.oh_nrelo;
	/* check that all offsets are valid */
	if (addrfirst >= addrcount || addrlast >= addrcount) {
		fprintf(stderr, "Invalid %s address range 0x%08.8lu to 0x%08.8lu\n",
				"relocation", addrfirst, addrlast);
	}
	else {
		if (opt_l)
			addrcount = addrlast + 1;
		addrcount = addrcount - addrfirst;
		if (opt_b) {
			fprintf(stdout, "\nRelocation data dump:\n");
			(void) fseek(objfp, OFF_RELO(o_hdrbuf) + addrfirst, SEEK_SET);
			(void) dump_hex(objfp, addrfirst, addrcount);
		}
		else {
			fprintf(stdout, "\nRelocation data:\n");
			(void) fseek(objfp, OFF_RELO(o_hdrbuf) + addrfirst *
					sizeof(struct outrelo), SEEK_SET);
			(void) dump_orel(objfp, addrfirst, addrcount);
		}
	}
  }

  /* show the symbol data */
  if (opt_a || opt_s) {
	if (opt_b)
		addrcount = o_hdrbuf.oh_nname * sizeof(struct outname);
	else
		addrcount = o_hdrbuf.oh_nname;
	/* check that all offsets are valid */
	if (addrfirst >= addrcount || addrlast >= addrcount) {
		fprintf(stderr, "Invalid %s address range 0x%08.8lu to 0x%08.8lu\n",
				"symbol", addrfirst, addrlast);
	}
	else {
		if (opt_l)
			addrcount = addrlast + 1;
		addrcount = addrcount - addrfirst;
		if (opt_b) {
			fprintf(stdout, "\nSymbol data dump:\n");
			(void) fseek(objfp, OFF_NAME(o_hdrbuf) + addrfirst, SEEK_SET);
			(void) dump_hex(objfp, addrfirst, addrcount);
		}
		else {
			fprintf(stdout, "\nSymbol data:\n");
			(void) fseek(objfp, OFF_NAME(o_hdrbuf) + addrfirst *
					sizeof(struct outname), SEEK_SET);
			(void) dump_osym(objfp, addrfirst, addrcount);
		}
	}
  }

  /* show the string data */
  if (opt_a || opt_n) {
	if (opt_b)
		addrcount = o_hdrbuf.oh_nchar;
	else
		addrcount = o_hdrbuf.oh_nname;	/* assumes one name per symbol */
	/* check that all offsets are valid */
	if (addrfirst >= addrcount || addrlast >= addrcount) {
		fprintf(stderr, "Invalid %s address range 0x%08.8lu to 0x%08.8lu\n",
				"symbol", addrfirst, addrlast);
	}
	else {
		if (opt_l)
			addrcount = addrlast + 1;
		addrcount = addrcount - addrfirst;
		if (opt_b) {
			fprintf(stdout, "\nName data dump:\n");
			(void) fseek(objfp, OFF_CHAR(o_hdrbuf) + addrfirst, SEEK_SET);
			(void) dump_hex(objfp, addrfirst, addrcount);
		}
		else {
			fprintf(stdout, "\nName data:\n");
			(void) fseek(objfp, o_symtab[addrfirst].on_foff, SEEK_SET);
			(void) dump_ostr(objfp, addrfirst, addrcount);
		}
	}
  }

  /* wrap up */
  fclose(objfp);

  exit(0);
}
コード例 #8
0
ファイル: InternetGo.c プロジェクト: dkrivets/gtkgo
void InternetGo (void)
{
  if ((ifilestuff == 0) && (inet == 0))
  {
    shost = gtk_entry_get_text (GTK_ENTRY (connectentry1));
    theaddress = atoaddr (shost);
    iport = atoi (gtk_entry_get_text (GTK_ENTRY (connectentry2)));

    gtk_widget_destroy (connectwindow);
    iyesno = 0;

    bzero ((char *) &host_addr, sizeof (host_addr));
    host_addr.sin_family = AF_INET;
    host_addr.sin_addr.s_addr = inet_addr (inet_ntoa (*theaddress));
    host_addr.sin_port = htons (iport);

    if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
    {
      Message ("Unable to open socket.", 3, 0);
    }
    itheerror = connect (sockfd, (struct sockaddr *) &host_addr, sizeof
      (host_addr));
    if (itheerror < 0)
    {
      sprintf (serrortemp, "Unable to connect to host (%s).", strerror
        (errno));
      Message (serrortemp, 3, 0);
    }
    else
    {
      fcntl (sockfd, F_SETFL, O_NONBLOCK);
      biscomputer = 0;
      sprintf (bplayername, "%s", "unknown");
      wiscomputer = 0;
      sprintf (wplayername, "%s", "unknown");
      StatusBar ();

      if (ichangestuff == 0)
      {
        Play ("open.mp3", 0);
      }

      internetwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_title (GTK_WINDOW (internetwindow), "Internet window");
      gtk_signal_connect (GTK_OBJECT (internetwindow), "delete_event",
        GTK_SIGNAL_FUNC (CloseSocket), NULL);
      gtk_window_set_policy (GTK_WINDOW (internetwindow), 0, 0, 1);
      gtk_container_border_width (GTK_CONTAINER (internetwindow), 0);
      gtk_widget_realize (internetwindow);

      internetbox1 = gtk_vbox_new (FALSE, 0);
      gtk_container_border_width (GTK_CONTAINER (internetbox1), 0);
      gtk_container_add (GTK_CONTAINER (internetwindow), internetbox1);
      gtk_widget_show (internetbox1);
      internetbox2 = gtk_hbox_new (FALSE, 0);
      gtk_container_border_width (GTK_CONTAINER (internetbox2), 0);
      gtk_box_pack_start (GTK_BOX (internetbox1), internetbox2, TRUE, TRUE, 5);
      gtk_widget_show (internetbox2);
      internetbox3 = gtk_vbox_new (FALSE, 0);
      gtk_container_border_width (GTK_CONTAINER (internetbox3), 0);
      gtk_box_pack_start (GTK_BOX (internetbox2), internetbox3, TRUE, TRUE, 0);
      gtk_widget_show (internetbox3);
      internettable1 = gtk_table_new (2, 1, FALSE);
      gtk_widget_show (internettable1);
      gtk_box_pack_start (GTK_BOX (internetbox3), internettable1, TRUE, TRUE,
        0);
      internettable2 = gtk_table_new (2, 3, FALSE);
      gtk_widget_show (internettable2);
      gtk_box_pack_start (GTK_BOX (internetbox3), internettable2, TRUE, TRUE,
        0);

      text = gtk_text_new (NULL, NULL);
      gtk_widget_set_usize (text, 535, 266);
      gtk_text_set_editable (GTK_TEXT (text), FALSE);
      gtk_table_attach (GTK_TABLE (internettable1), text, 0, 1, 0, 1,
        GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 0);
      gtk_widget_show (text);

      vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
      adj = (GTK_TEXT (text))->vadj;
      gtk_table_attach (GTK_TABLE (internettable1), vscrollbar, 1, 2, 0, 1,
        GTK_FILL, GTK_EXPAND  | GTK_FILL, 5, 0);
      gtk_widget_show (vscrollbar);

      cmap = gdk_colormap_get_system ();
      color.red = 0xffff;
      color.green = 0;
      color.blue = 0;
      if (!gdk_color_alloc (cmap, &color))
      {
        Message ("Could not allocate color.", 3, 0);
      }

      gtk_widget_realize (text);

      wrapcheck = gtk_check_button_new_with_label ("wrap");
      gtk_table_attach (GTK_TABLE (internettable2), wrapcheck, 0, 1, 0, 1,
        GTK_FILL, GTK_FILL, 5, 10);
      gtk_signal_connect (GTK_OBJECT (wrapcheck), "toggled",
        GTK_SIGNAL_FUNC (ToggleWrap), text);
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wrapcheck), FALSE);
      gtk_widget_show (wrapcheck);

      internetentry = gtk_entry_new_with_max_length (50);
      gtk_signal_connect (GTK_OBJECT (internetentry), "activate",
        GTK_SIGNAL_FUNC (SendData), NULL);
      gtk_table_attach (GTK_TABLE (internettable2), internetentry, 1, 2, 0, 1,
        GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 10);
      gtk_widget_grab_focus (internetentry);
      gtk_widget_show (internetentry);

      inetsep = gtk_hseparator_new ();
      gtk_table_attach (GTK_TABLE (internettable2), inetsep, 0, 2, 1, 2,
        GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
      gtk_widget_show (inetsep);

      inetbutton = gtk_button_new_with_label ("Disconnect");
      gtk_signal_connect (GTK_OBJECT (inetbutton), "clicked",
        GTK_SIGNAL_FUNC (CloseSocket), NULL);
      GTK_WIDGET_SET_FLAGS (inetbutton, GTK_CAN_DEFAULT);
      gtk_window_set_default (GTK_WINDOW (internetwindow), inetbutton);
      gtk_table_attach (GTK_TABLE (internettable2), inetbutton, 0, 2, 2, 3,
        GTK_EXPAND | GTK_FILL, GTK_FILL, 2, 2);
      gtk_widget_show (inetbutton);

      gtk_widget_show (internetwindow);
      inet = 1;
      ReceiveData ();
    }
  }
}
コード例 #9
0
ファイル: wireconf.c プロジェクト: ntrtrung/eBATMAN
// add an ipfw rule containing a dummynet pipe to the firewall
// return SUCCESS on succes, ERROR on error
int add_rule(int s, uint16_t rulenum, int pipe_nr, char *src, char *dst, 
	     int direction)
{
  int clen;
  in4_addr addr;
  uint16_t port;
  ipfw_insn cmd[10];
  struct ip_fw *p;
  
  // additional rule length counter
  clen = 0;

  // process source address

  if(strncmp(src, "any", strlen(src))==0)
    {
      // source address was "any"
      cmd[clen].opcode = O_IP_SRC;
      cmd[clen].len = 0;
      cmd[clen].arg1 = 0;
    }
  else
    {
      if(atoaddr(src, &addr, &port) < 0)
	{
	  WARNING("Invalid argument to add_rule: %s\n", src);
	  return ERROR;
	}
      cmd[clen].opcode = O_IP_SRC;
      cmd[clen].len = 2;
      cmd[clen].arg1 = 0;
      ((uint32_t *)cmd)[clen + 1] = addr.word;
      clen += 2;
      if(port > 0)
	{
	  cmd[clen].opcode = O_IP_SRCPORT;
	  cmd[clen].len = 2;
	  cmd[clen].arg1 = 0;
	  ((uint32_t *)cmd)[clen + 1]
	    = port | port << 16;
	  clen += 2;
	}
    }

  // process destination address
  if(strncmp(dst, "any", strlen(dst))==0)
    {
      // destination address was "any"
      cmd[clen].opcode = O_IP_DST;
      cmd[clen].len = 0;
      cmd[clen].arg1 = 0;
    }
  else
    {
      if(atoaddr(dst, &addr, &port) < 0)
	{
	  WARNING("Invalid argument to add_rule: %s", dst);
	  return ERROR;
	}

      cmd[clen].opcode = O_IP_DST;
      cmd[clen].len = 2;
      cmd[clen].arg1 = 0;
      ((uint32_t *)cmd)[clen + 1] = addr.word;
      clen += 2;
      if(port > 0)
	{
	  cmd[clen].opcode = O_IP_DSTPORT;
	  cmd[clen].len = 2;
	  cmd[clen].arg1 = 0;
	  ((uint32_t *)cmd)[clen + 1]
	    = port | port << 16;
	  clen += 2;
	}
    }

  // use in/out direction indicators
  if(direction != DIRECTION_BOTH)
    {
      // basic command code for in/out operation
      cmd[clen].opcode = O_IN; 
      cmd[clen].len = 1;

      // a negation mask is used for meaning "out"
      if(direction == DIRECTION_OUT)
	cmd[clen].len |= F_NOT;
      //printf("len=0x%x len&F_NOT=0x%x flen=%d\n", cmd[clen].len, cmd[clen].len & F_NOT, F_LEN(cmd+clen));
      clen += 1;
    }

  // configure pipe
  cmd[clen].opcode = O_PIPE;
  cmd[clen].len = 2;
  cmd[clen].arg1 = pipe_nr;
  ((uint32_t *)cmd)[clen + 1] = 0;
  clen += 1;	/* trick! */
  if((p = (struct ip_fw *)malloc(sizeof(struct ip_fw) + clen * 4)) == NULL) 
    {
      WARNING("Could not allocate memory for a new rule");
      return ERROR;
    }
  bzero(p, sizeof(struct ip_fw));
  p->act_ofs = clen - 1;
  p->cmd_len = clen + 1;
  p->rulenum = rulenum;
  bcopy(cmd, &p->cmd, clen * 4);

  // apply appropriate socket options
  if(apply_socket_options(s, IP_FW_ADD, p, sizeof(struct ip_fw) + clen*4) < 0)
    {
      WARNING("Adding rule operation failed");
      return ERROR;
    }

  return 0;
}