示例#1
0
/**
 * @brief discovery_ipnd_add_service_udp_cl
 * @param buffer the beacon message buffer
 * @param buf_len the max length of beacon message buffer
 * @param poffset pointer to the offset inside the message buffer
 * @return The count of added service blocks
 */
static int discovery_ipnd_add_service_udp_cl(uint8_t* const buffer, const size_t buf_len, int* const poffset)
{
	/*
	 * Overwrite offset at the end of the function,
	 * if the processing not failed
	 */
	uint8_t offset = *poffset;

	/* a service block for IPND
	 * consist of the following fields
	 * (see IPND darft 01 for more details)
	 *
	 * 1 byte	Service name length
	 * x byte	Service name contains "udpcl"
	 * 1 byte	Service parameters length
	 * y byte	Service parameters contains "port=65535;"
	 */
	const uint8_t service_name_len = STATIC_STRLEN(DISCOVERY_IPND_SERVICE_UDP);
	const uint8_t service_param_type_len = STATIC_STRLEN(DISCOVERY_IPND_SERVICE_PORT);
	const uint8_t UINT16_AS_STRING_LEN = 5;
	const uint8_t MAX_SERVICE_PARAM_LEN = service_param_type_len + UINT16_AS_STRING_LEN + 1;

	if ( buf_len < (offset + 1 + service_name_len + 1 + MAX_SERVICE_PARAM_LEN) ) {
		LOG(LOGD_DTN, LOG_DISCOVERY, LOGL_ERR, "Discovery message buffer is too small for UDP-CL service parameters.");
		return 0;
	}

	/* add the service name length */
	buffer[offset++] = service_name_len;

	/* add the service name */
	uint8_t* const service_name_buf = &buffer[offset];
	if (memcpy(service_name_buf, DISCOVERY_IPND_SERVICE_UDP, service_name_len) != service_name_buf) {
		LOG(LOGD_DTN, LOG_DISCOVERY, LOGL_ERR, "memcpy failed.");
		return 0;
	}
	offset += service_name_len;

	/* remember the position for the service parameter length byte */
	uint8_t* const service_param_len = &buffer[offset++];

	/* build and add the service parameter value */
	const int len = snprintf((char*)&buffer[offset], MAX_SERVICE_PARAM_LEN, DISCOVERY_IPND_SERVICE_PORT"%u;", CL_UDP_BUNDLE_PORT);
	if (len < 0) {
		LOG(LOGD_DTN, LOG_DISCOVERY, LOGL_ERR, "snprintf failed.");
		return 0;
	}
	(*service_param_len) = len;
	offset += len;

	/*
	 * Everything is done successfully.
	 * So return the new offset and
	 * the count of added service blocks.
	 */
	(*poffset) = offset;
	/* One service block was added */
	return 1;
}
示例#2
0
文件: http.c 项目: gtk-gnutella/gwc
/**
 * Marks the end of the message headers.
 */
int
http_terminate_headers(http_t *ctx)
{
  const char crlf[] = "\r\n";
  int ret;
  
  ret = http_send(ctx, crlf, STATIC_STRLEN(crlf));
  ACCLOG_COMMIT(ctx->acclog);
  return 0 != ret ? -1 : 0;
}
示例#3
0
/**
 * \brief Function that could parse the IPND service block
 * \param buffer Pointer to the block
 * \param length Length of the block
 * \return the number of decoded bytes
 */
uint8_t discovery_ipnd_parse_service_block(uint32_t eid, const uint8_t* buffer, const uint8_t length, ipnd_msg_attrs_t* const attrs) {
	uint32_t offset, num_services, i, sdnv_len, tag_len, data_len;
	uint8_t *tag_buf;
	int h;

	// decode the number of services
	sdnv_len = sdnv_decode(buffer, length, &num_services);
	offset = sdnv_len;
	buffer += sdnv_len;

	// iterate through all services
	for (i = 0; num_services > i; ++i) {
		// decode service tag length
		sdnv_len = sdnv_decode(buffer, length - offset, &tag_len);
		offset += sdnv_len;
		buffer += sdnv_len;

		// decode service tag string
		tag_buf = (uint8_t*)buffer;
		offset += tag_len;
		buffer += tag_len;

		// decode service content length
		sdnv_len = sdnv_decode(buffer, length - offset, &data_len);
		offset += sdnv_len;
		buffer += sdnv_len;

		/* parse UDP-CL service data, if available */
		const size_t udpcl_len = STATIC_STRLEN(DISCOVERY_IPND_SERVICE_UDP);
		if (tag_len == udpcl_len && memcmp(tag_buf, DISCOVERY_IPND_SERVICE_UDP, udpcl_len) == 0) {
			// TODO warn if the port will be overwritten
			discovery_ipnd_parse_service_param(buffer, data_len, attrs);
		}

		// Allow all registered DTN APPs to parse the IPND service block
		for(h=0; dtn_apps[h] != NULL; h++) {
			if( dtn_apps[h]->parse_ipnd_service_block == NULL ) {
				continue;
			}

			dtn_apps[h]->parse_ipnd_service_block(eid, tag_buf, tag_len, (uint8_t*)buffer, data_len);
		}

		offset += data_len;
		buffer += data_len;
	}


	return offset;
}
示例#4
0
/**
 * @brief discovery_ipnd_parse_service_param parses the port parameter of
 * a service block of a ipnd beacon message
 * @param attrs will contain the found port after parsing
 */
static void discovery_ipnd_parse_service_param(const uint8_t* const service_param, const uint32_t param_len, ipnd_msg_attrs_t* const attrs)
{
	const size_t param_min_len = STATIC_STRLEN(DISCOVERY_IPND_SERVICE_PORT);
	if (param_len < param_min_len) {
		/*
		 * Service parameter is to small for
		 * containing a port number.
		 */
		return;
	}

	// TODO the parameters are seperated by simicolons.
	// So check all other parameters if there are more than one
	if (memcmp(service_param, DISCOVERY_IPND_SERVICE_PORT, param_min_len) != 0) {
		/*
		 * Service parameter does not contain the
		 * port number of this service
		 */
		return;
	}

	const char* const service_port_val = (char*)service_param + param_min_len;
	attrs->port = atoi(service_port_val);
}
示例#5
0
static void
handle_qhit(const char *data, size_t size)
{
  const struct gnutella_qhit_header *header;
  const struct gnutella_guid *guid;
  const struct gnutella_qhit_item *item;
  size_t guid_offset = size - sizeof *guid;
  unsigned hits;
  size_t pos;
  
  RUNTIME_ASSERT(size <= GNUTELLA_MAX_PAYLOAD);
  
  if (size < sizeof *header) {
    fprintf(stderr, "handle_qhit(): Too little payload for header.\n");
    return;
  }
  header = cast_to_const_void_ptr(data);
  
  hits = (unsigned char) header->hits;
  printf("Hits: %u\n", hits);
  printf("Address: %s\n",
    net_addr_port_to_string(net_addr_peek_ipv4(cast_to_const_char_ptr(header->addr)),
      peek_le16(header->port)));
  printf("Speed: %lu\n", (unsigned long) peek_le32(header->speed));

  if (size < sizeof *header + sizeof *guid) {
    fprintf(stderr, "handle_qhit(): Insufficient payload for query hit.\n");
    return;
  }
  if (size >= sizeof *header + sizeof *guid) {
    
    guid = cast_to_const_void_ptr(&data[guid_offset]);
    printf("Servent ID: %08lx-%08lx-%08lx-%08lx\n",
        (unsigned long) peek_be32(&guid->data[0]),
        (unsigned long) peek_be32(&guid->data[4]),
        (unsigned long) peek_be32(&guid->data[8]),
        (unsigned long) peek_be32(&guid->data[12]));
  }
  printf("----\n");

  pos = sizeof *header;
  for (/* NOTHING */; hits > 0; hits--) {
    const char *nul_ptr;
   
    if (pos >= guid_offset || guid_offset - pos < sizeof *item + 2)
      break;

    item = cast_to_const_void_ptr(&data[pos]);
    printf("Index: %lu\n", (unsigned long) peek_le32(item->index));
    printf("Size:  %lu\n", (unsigned long) peek_le32(item->size));

    pos += sizeof *item; 

    nul_ptr = memchr(&data[pos], 0, guid_offset - pos);
    if (!nul_ptr) {
      fprintf(stderr, "handle_qhit(): Non-terminated filename.\n");
      return;
    } else {
      size_t len;

      len = (nul_ptr - &data[pos]);
      if (len > (((size_t) -1) / 4 - 1)) {
        fprintf(stderr, "handle_qhit(): Filename is too long.\n");
        /* Ignore */
      } else {
        const char *p;
        size_t avail;
        
        printf("Filename: ");

        avail = len;
        p = &data[pos];
        while (avail > 0) {
          uint32_t cp;
          cp = utf8_decode(p, avail);
          if ((uint32_t) -1 != cp) {
            uint8_t u_len, i;

            u_len = utf8_first_byte_length_hint((unsigned char) *p);
            RUNTIME_ASSERT(u_len > 0);
            RUNTIME_ASSERT(avail >= u_len);
            avail -= u_len;

            if (cp >= 0x20 && cp != 0x7f) {
              for (i = 0; i < u_len; i++) {
                putchar((unsigned char) p[i]);
              }
            } else {
              char ch = cp & 0xff;
              printf("%s", escape_buffer(&ch, 1)); 
            }
            
            p += u_len;
          } else {
            if (verbosity > 0) {
              fprintf(stderr, "handle_qhit(): Invalid UTF-8.\n");
            }
            break;
          }
        }
      }
      printf("\n");

      pos += len;
    }

    RUNTIME_ASSERT(nul_ptr);
    RUNTIME_ASSERT(&data[pos] == nul_ptr);
    RUNTIME_ASSERT('\0' == *nul_ptr);

    pos++;
    RUNTIME_ASSERT(pos <= guid_offset);

    nul_ptr = memchr(&data[pos], 0, guid_offset - pos);
    if (!nul_ptr) {
      fprintf(stderr, "handle_qhit(): Non-terminated extension block.\n");
      return;
    } else if (nul_ptr != &data[pos]) {
      size_t len = nul_ptr - &data[pos];
      
      printf("Extension size:  %lu\n", (unsigned long) len);
     
      handle_extension(&data[pos], len);
      pos += len;
    }

    RUNTIME_ASSERT(nul_ptr);
    RUNTIME_ASSERT(&data[pos] == nul_ptr);
    RUNTIME_ASSERT('\0' == *nul_ptr);

    pos++;
    RUNTIME_ASSERT(pos <= guid_offset);

    printf("------\n");
  }

  if (hits > 0) {
    fprintf(stderr, "handle_qhit(): Expected %u more hits.\n", hits);
  }

  if (pos < guid_offset) {
      static const unsigned vendor_id_len = 4;
      
      printf("Extended QHD size:  %lu\n", (unsigned long) guid_offset - pos);
      if (guid_offset - pos >= vendor_id_len) {

        printf("Vendor ID: %s\n", escape_buffer(&data[pos], vendor_id_len));

        pos += vendor_id_len;
        if (pos < guid_offset) {
          uint8_t open_data_size = data[pos];
          bool has_ggep = false;
          
          printf("Open data size:  %u\n", open_data_size);
          pos++;

          if (open_data_size > guid_offset - pos) {
            printf("Open data size is too large.\n");
            return;
          }

          if (open_data_size >= 2) {
            uint8_t mask = data[pos];
            uint8_t value = data[pos + 1];
          
            printf("mask:  0x%02x\n", mask);
            printf("value: 0x%02x\n", value);
            
            if (0x20 & mask) {
              has_ggep = 0x20 & value;
              printf("Has GGEP: %s\n", has_ggep ? "yes" : "no");
            }
            if (0x10 & mask) {
              printf("Has speed: %s\n", (0x10 & value) ? "yes" : "no");
            }
            if (0x08 & mask) {
              printf("Has uploaded: %s\n", (0x08 & value) ? "yes" : "no");
            }
            if (0x04 & mask) {
              printf("Busy: %s\n", (0x04 & value) ? "yes" : "no");
            }
            /* mask and value are swapped */
            if (0x01 & value) {
              printf("Must push: %s\n", (0x01 & mask) ? "yes" : "no");
            }
          }

          pos += open_data_size;

          if (pos < guid_offset) {
            size_t priv_data_size = guid_offset - pos;
            static const char id_deflate[] = "{deflate}";
            const char *priv_data, *x;
            
            priv_data = &data[pos];
            priv_data_size = guid_offset - pos;
            
            printf("Private data area size:  %lu\n",
              (unsigned long) priv_data_size);

            handle_extension(priv_data, priv_data_size);

            x = compat_memmem(priv_data, priv_data_size,
                    id_deflate, STATIC_STRLEN(id_deflate));
            if (x) {
              char buf[64 * 1024];
              const char *src;
              size_t n, src_len;

              src = &x[STATIC_STRLEN(id_deflate)];
              src_len = priv_data_size - STATIC_STRLEN(id_deflate);
              n = buffer_inflate(buf, sizeof buf, src, src_len);
              if ((size_t) -1 != n) {
                printf("Inflated:  %s\n", escape_buffer(buf, n));
              }
            }

            pos += priv_data_size;
          }

          RUNTIME_ASSERT(pos == guid_offset);
        }
      }
  }
  
  printf("----\n");
}
示例#6
0
int Arguments::parse_one_arg(int argc, char** argv) {
  if (argc <= 0) {
    // we already finished parsing.
    return 0;
  }

  int count = 1; // in most cases we find an argument of 1 word

#if ENABLE_MULTIPLE_PROFILES_SUPPORT
  if ((jvm_strcmp(argv[0], "-profile") == 0) && argc >= 2) {
    JVM_SetProfile(argv[1]);
    count = 2;
  } else
#endif // ENABLE_MULTIPLE_PROFILES_SUPPORT
  if (jvm_strcmp(argv[0], "-version") == 0) {
    Globals::print_version();
    JVMSPI_Exit(0);
    ::jvm_exit(0);
  }
  else if ((jvm_strcmp(argv[0], "-?") == 0) ||
           (jvm_strcmp(argv[0], "-help") == 0)) {
    JVMSPI_DisplayUsage(NULL);
    JVMSPI_Exit(1);
    ::jvm_exit(1);
  }
  else if (((jvm_strcmp(argv[0], "-classpath") == 0) ||
            (jvm_strcmp(argv[0], "-cp") == 0)) && argc >= 2) {
    set_pathname_from_const_ascii(&_classpath, argv[1]);
    count = 2;
  }
  else if (jvm_strcmp(argv[0], "-int") == 0) {
    UseCompiler = false;
  }
  else if (jvm_strcmp(argv[0], "-comp") == 0) {
    MixedMode = false;
    UseCompiler = true;
  }
  else if ((*argv[0] == '-') && (*(argv[0]+1) == 'D')) {
    char *key;
    char *value;

    key = argv[0] + 2;
    for (value = key; *value ; value++) {
        if (*value == '=') {
          *value++ = 0;
          break;
        }
    }
    JVMSPI_SetSystemProperty(key, value);
  }

#if ENABLE_INTERPRETER_GENERATOR || USE_SOURCE_IMAGE_GENERATOR
  else if (jvm_strcmp(argv[0], "-outputdir") == 0) {
    set_pathname_from_const_ascii(&_generator_output_dir, argv[1]);
    count = 2;
  }
  else if (jvm_strcmp(argv[0], "-generate") == 0) {
    GenerateDebugAssembly = true;
    GenerateAssemblyCode = true;
  }
  else if (jvm_strcmp(argv[0], "-generateoptimized") == 0) {
    GenerateDebugAssembly = false;
    GenerateAssemblyCode = true;
  }
#endif

#if ENABLE_JVMPI_PROFILE 
  //  To get the library name to be loaded
  else if(jvm_strncmp(argv[0], "-Xrun", 5) == 0) {
    Arguments::_jvmpi_profiler_lib = (char*)OsMemory_allocate(jvm_strlen(argv[0]) + 2);
    jvm_strcpy(Arguments::_jvmpi_profiler_lib, "lib");
    jvm_strcat(Arguments::_jvmpi_profiler_lib, &(argv[0][5]));
    jvm_strcat(Arguments::_jvmpi_profiler_lib, ".so");
  }
#endif

#if ENABLE_ROM_GENERATOR

#if USE_SOURCE_IMAGE_GENERATOR
  else if (jvm_strcmp(argv[0], "-romconfig") == 0) {
    set_pathname_from_const_ascii(&_rom_config_file, argv[1]);
    count = 2;
  }
  else if (jvm_strcmp(argv[0], "-romincludepath") == 0) {
    ROMIncludePath *ptr =
      (ROMIncludePath*)OsMemory_allocate(sizeof(ROMIncludePath));
    ptr->_next = _rom_include_paths;
    ptr->_file._path = NULL;
    set_pathname_from_const_ascii(&(ptr->_file), argv[1]);
    _rom_include_paths = ptr;
    count = 2;
  }
  else if (jvm_strcmp(argv[0], "-romize") == 0) {
    // Romize the system classes
    GenerateROMImage = true;
#if ENABLE_TTY_TRACE
    TraceMirandaMethods = true;
#endif
    count = 1;
  }
#endif // USE_SOURCE_IMAGE_GENERATOR

#if USE_BINARY_IMAGE_GENERATOR
  else if (jvm_strcmp(argv[0], "-convert") == 0) {
    // Romize an application
    GenerateROMImage = true;
#if ENABLE_TTY_TRACE
    TraceMirandaMethods = true;
#endif
    count = 1;
  }
#if ENABLE_LIB_IMAGES
  else if (jvm_strcmp(argv[0], "-convertshared") == 0) {
    // Romize a library, so it could be used shared 
    GenerateSharedROMImage = true;
    GenerateROMImage = true;
#if ENABLE_TTY_TRACE
    TraceMirandaMethods = true;
#endif
    count = 1;
  }
#endif
#endif

  else if (jvm_strcmp(argv[0], "-romoutputfile") == 0) {
    set_pathname_from_const_ascii(&_rom_output_file, argv[1]);
    count = 2;
  }

#endif // ENABLE_ROM_GENERATOR

#if USE_DEBUG_PRINTING
  else if (jvm_strcmp(argv[0], "-definitions") == 0) {
    Globals::print_definitions();
    JVMSPI_Exit(1);
    ::jvm_exit(1);
  }
  else if (jvm_strcmp(argv[0], "-flags") == 0) {
    Globals::print_flags();
    JVMSPI_Exit(1);
    ::jvm_exit(1);
  }
  else if (jvm_strcmp(argv[0], "-buildopts") == 0) {
    Globals::print_build_options();
    JVMSPI_Exit(1);
    ::jvm_exit(1);
  }
  else if (jvm_strcmp(argv[0], "-errorcodes") == 0) {
    Globals::print_error_codes();
    JVMSPI_Exit(1);
    ::jvm_exit(1);
  }
#endif

#if !defined(PRODUCT) || ENABLE_TTY_TRACE
  else if (jvm_strcmp(argv[0], "-verbose") == 0) {
#if ENABLE_TTY_TRACE
    VerboseGC = true;
    VerboseClassLoading = true;
#endif
  }
  // Parse the CompileOnlyXXX= option, if present, and record the
  // classes and methods that should always be compiled.  This must
  // be done before the following to allow correct handling of
  // classes which start with the letters K or M
  else if (jvm_strncmp(argv[0], COMPILE_ONLY_METHOD,
                       STATIC_STRLEN(COMPILE_ONLY_METHOD)) == 0) {
    _method_CompileOnly = argv[0] + STATIC_STRLEN(COMPILE_ONLY_METHOD);
  }
  else if (jvm_strncmp(argv[0], COMPILE_ONLY_CLASS,
                       STATIC_STRLEN(COMPILE_ONLY_CLASS)) == 0) {
    _class_CompileOnly = argv[0] + STATIC_STRLEN(COMPILE_ONLY_CLASS);
  }
#endif
#if !defined(PRODUCT)
  else if (jvm_strcmp(argv[0], "-compilertestconfig") == 0) {
    if (argc < 2) {
      JVMSPI_DisplayUsage((char*)"Compiler test config file not specified.");
      JVMSPI_Exit(1);
      ::jvm_exit(1);
    }
    RunCompilerTests = true;
    set_pathname_from_const_ascii(&_compiler_test_config_file, argv[1]);
    count = 2;
  }
#endif /* NOT PRODUCT */
#if ENABLE_METHOD_TRAPS
  else if (jvm_strncmp(argv[0], METHOD_TRAP, STATIC_STRLEN(METHOD_TRAP)) == 0) {
    if (!parse_method_trap_param(argv[0] + STATIC_STRLEN(METHOD_TRAP))) {
      JVMSPI_DisplayUsage((char*)"Invalid usage of MethodTrap argument");
      JVMSPI_Exit(1);
      ::jvm_exit(1);
    }
  }
#endif
#if ENABLE_REMOTE_TRACER
  else if (((jvm_strcmp(argv[0], "-tracehost") == 0)) && argc > 2) {
    traceHost = argv[1];
    count = 2;
  }
#endif
#if ENABLE_JAVA_DEBUGGER
  else if (jvm_strcmp(argv[0], "-debugger") == 0) {
    JavaDebugger::set_debugger_option_on(true);
  } else if (jvm_strcmp(argv[0], "-port") == 0) {
    _debugger_port = jvm_atoi(argv[1]);
    count=2;
  } else if (jvm_strcmp(argv[0], "-suspend") == 0) {
    JavaDebugger::set_suspend(true);
  } else if (jvm_strcmp(argv[0], "-nosuspend") == 0) {
    JavaDebugger::set_suspend(false);
  }
#if ENABLE_ISOLATES
  else if (jvm_strcmp(argv[0], "-debug_isolate") == 0) {
    JavaDebugger::set_debug_isolate_option_on(true);
  }
  else if (jvm_strcmp(argv[0], "-debug_main") == 0) {
    JavaDebugger::set_debug_main_option_on(true);
  }
#endif
#endif
#if ENABLE_MEMORY_PROFILER
  else if (jvm_strcmp(argv[0], "-memory_profiler") == 0) {
    JavaDebugger::set_debugger_option_on(true);
  }
#endif
#if ENABLE_MEMORY_MONITOR
  else if (jvm_strcmp(argv[0], "-monitormemory") == 0) {
    Arguments::_monitor_memory = 1;
  }
#endif
  else if ((argv[0][0] == '-')||(argv[0][0] == '+') || (argv[0][0] == '=')) {
    if (!Globals::parse_argument(argv[0])) {
      count = -1; // Indicate parse error
    }
#if ENABLE_ROM_GENERATOR
    if (jvm_strcmp(argv[0], "+EnableAllROMOptimizations") == 0) {
      // Turn on all available ROM optimizations, so you don't have to
      // enable them individually
      //
      // This also means you can selectively turn off some ROM
      // optimizations such as "+EnableAllROMOptimizations
      // -CompactROMFieldTables". Note that the order is important.
      AggressiveROMSymbolRenaming = true;
      CompactROMFieldTables       = true;
      CompactROMMethodTables      = true;
      CompactROMBytecodes         = true;
      RenameNonPublicROMClasses   = true;
      RenameNonPublicROMSymbols   = true;
      SimpleROMInliner            = true;
      RemoveDuplicatedROMStackmaps= true;
      RemoveDeadMethods           = true;
    }
#if ENABLE_ROM_JAVA_DEBUGGER
    if (jvm_strcmp(argv[0], "+MakeROMDebuggable") == 0) {
      // Turn off any optimizations that would change bytecode offsets
      CompactROMFieldTables = false;
      CompactROMMethodTables = false;
      CompactROMBytecodes         = false;
      RenameNonPublicROMSymbols   = false;
      AggressiveROMSymbolRenaming = false;
    }
#endif
    if (jvm_strcmp(argv[0], "-EnableBaseOptimizations") == 0) {
      // Turn off all optimizations
      AggressiveROMSymbolRenaming = false;
      CompactROMFieldTables       = false;
      CompactROMMethodTables      = false;
      CompactROMBytecodes         = false;
      RenameNonPublicROMClasses   = false;
      RenameNonPublicROMSymbols   = false;
      SimpleROMInliner            = false;
      RemoveDuplicatedROMStackmaps= false;
      RemoveDeadMethods           = false;
      RewriteROMConstantPool      = false;
    }
#endif
  }
  else {
    /* We don't recognize argv[0] as an argument */
    count = 0;
  }

  return count;
}