示例#1
0
// ######################################################################
void SerialPort::connect() {
  // Check to see if we have a hardcoded device name. If so, then let's just
  // go ahead and enable that port. If
  printf("INFO: Looking Device Name [%s]\n", itsDevName.c_str());
  if (itsDevName != "") {
    printf("INFO: Opening %s\n", itsDevName.c_str());
    enablePort(itsDevName);
  } else if (itsDevName == "search") {
    printf("INFO: Searching for devices\n");
    itsCmdDevName = "";

    DIR *directory_p;
    struct dirent *entry_p;

    // Open the device directory to search for devices whose names match the
    // search prefix
    directory_p = ::opendir("/dev");
    if (directory_p == NULL)
      printf("FATAL ERROR: Could Not Open /dev Directory!\n");

    // Iterate through the directory entries
    while ((entry_p = ::readdir(directory_p))) {
      std::string entryName(entry_p->d_name);
      if (entryName.find(itsSearchPrefix.c_str()) !=
          std::string::npos) {  // If the directory entry name matches our
                                // search prefix, then let's try configuring a
                                // serial
        // port on that device, sending it an identity request command (0x00),
        // and comparing the result
        // with our required device description

        enablePort("/dev/" + entryName);
        unsigned char cmd[1] = {0};

        write(cmd, 1);
        std::vector<unsigned char> deviceStringVec = readFrame(cmd[0], 255);

        std::string deviceString(deviceStringVec.begin(),
                                 deviceStringVec.end());
        printf("INFO: %s : %s", entryName.c_str(), deviceString.c_str());

        if (deviceString == itsDeviceDescription) {
          itsCmdDevName = "/dev/" + entryName;
          break;
        }
      }
    }
    (void)::closedir(directory_p);
    if (itsCmdDevName == "") {
      printf(
          "FATAL ERROR: Could Not Find Serial Device Matching Descriptor "
          "(%s)\n",
          itsDeviceDescription.c_str());
    }
  } else {
    printf("INFO: Opening from cmd line %s\n", itsCmdDevName.c_str());
    enablePort(itsCmdDevName);
  }
}
示例#2
0
OMX_ERRORTYPE Component::allocOutputBuffers()
{
	
    if(!handle) 
    {
        ofLogError(__func__) << getName() << " NO HANDLE";
        return OMX_ErrorNone;
    }
    OMX_ERRORTYPE error = OMX_ErrorNone;
	

	OMX_PARAM_PORTDEFINITIONTYPE portFormat;
	OMX_INIT_STRUCTURE(portFormat);
	portFormat.nPortIndex = outputPort;

	error = OMX_GetParameter(handle, OMX_IndexParamPortDefinition, &portFormat);
    OMX_TRACE(error);
	if(error != OMX_ErrorNone)
	{
		return error;
	}


	if(getState() != OMX_StateIdle)
	{
		if(getState() != OMX_StateLoaded)
		{
			setState(OMX_StateLoaded);
		}
		setState(OMX_StateIdle);
	}

	error = enablePort(outputPort);
    OMX_TRACE(error);
	if(error != OMX_ErrorNone)
	{
		return error;
	}

	for (size_t i = 0; i < portFormat.nBufferCountActual; i++)
	{
		OMX_BUFFERHEADERTYPE *buffer = NULL;
        error = OMX_AllocateBuffer(handle, &buffer, outputPort, NULL, portFormat.nBufferSize);
        OMX_TRACE(error);
        if(error != OMX_ErrorNone)
        {
            return error;
        }
		buffer->nOutputPortIndex = outputPort;
		buffer->nFilledLen       = 0;
		buffer->nOffset          = 0;
		buffer->pAppPrivate      = (void*)i;
        outputBuffers.push_back(buffer);
        outputBuffersAvailable.push(buffer);
	}


	return error;
}
示例#3
0
bool MidiInputController::enableAllPorts() {
    bool enabledPort = false;
    vector<pair<int, string> > ports = availableMidiDevices();
    vector<pair<int, string> >::iterator it = ports.begin();

    while(it != ports.end()) {
        // Don't enable MIDI input from our own virtual output
        if(it->second != kMidiVirtualOutputName)
            enabledPort |= enablePort((it++)->first);
        else
            it++;
    }

    return enabledPort;
}
示例#4
0
/*
 * Function: IMAP_ParseArgs(char *)
 *
 * Purpose: Process the preprocessor arguments from the rules file and
 *          initialize the preprocessor's data struct.  This function doesn't
 *          have to exist if it makes sense to parse the args in the init
 *          function.
 *
 * Arguments: args => argument list
 *
 * Returns: void function
 *
 */
void IMAP_ParseArgs(IMAPConfig *config, char *args)
{
    int ret = 0;
    char *arg;
    char errStr[ERRSTRLEN];
    int errStrLen = ERRSTRLEN;

    if ((config == NULL) || (args == NULL))
        return;

    enablePort( config->ports, IMAP_DEFAULT_SERVER_PORT );
    config->memcap = DEFAULT_IMAP_MEMCAP;
    _dpd.fileAPI->set_mime_decode_config_defauts(&(config->decode_conf));
    _dpd.fileAPI->set_mime_log_config_defauts(&(config->log_config));

    *errStr = '\0';

    arg = strtok(args, CONF_SEPARATORS);

    while ( arg != NULL )
    {
        unsigned long value = 0;

        if ( !strcasecmp(CONF_PORTS, arg) )
        {
            ret = ProcessPorts(config, errStr, errStrLen);
        }
        else if ( !strcasecmp(CONF_IMAP_MEMCAP, arg) )
        {
            ret = _dpd.checkValueInRange(strtok(NULL, CONF_SEPARATORS), CONF_IMAP_MEMCAP,
                    MIN_IMAP_MEMCAP, MAX_IMAP_MEMCAP, &value);
            config->memcap = (uint32_t)value;
        }
        else if ( !strcasecmp(CONF_MAX_MIME_MEM, arg) )
        {
            ret = _dpd.checkValueInRange(strtok(NULL, CONF_SEPARATORS), CONF_MAX_MIME_MEM,
                    MIN_MIME_MEM, MAX_MIME_MEM, &value);
            config->decode_conf.max_mime_mem = (int)value;
        }
        else if(!_dpd.fileAPI->parse_mime_decode_args(&(config->decode_conf), arg, "IMAP"))
        {
            ret = 0;
        }
        else if ( !strcasecmp(CONF_DISABLED, arg) )
        {
            config->disabled = 1;
        }
        else
        {
            DynamicPreprocessorFatalMessage("%s(%d) => Unknown IMAP configuration option %s\n",
                                            *(_dpd.config_file), *(_dpd.config_line), arg);
        }

        if (ret == -1)
        {
            /*
            **  Fatal Error, log error and exit.
            */
            if (*errStr)
            {
                DynamicPreprocessorFatalMessage("%s(%d) => %s\n",
                                                *(_dpd.config_file), *(_dpd.config_line), errStr);
            }
            else
            {
                DynamicPreprocessorFatalMessage("%s(%d) => Undefined Error.\n",
                                                *(_dpd.config_file), *(_dpd.config_line));
            }
        }

        /*  Get next token */
        arg = strtok(NULL, CONF_SEPARATORS);
    }

}
示例#5
0
/**
**  Process the port list.
**
**  This configuration is a list of valid ports and is ended by a
**  delimiter.
**
**  @param ErrorString error string buffer
**  @param ErrStrLen   the length of the error string buffer
**
**  @return an error code integer
**          (0 = success, >0 = non-fatal error, <0 = fatal error)
**
**  @retval  0 successs
**  @retval -1 generic fatal error
**  @retval  1 generic non-fatal error
*/
static int ProcessPorts(IMAPConfig *config, char *ErrorString, int ErrStrLen)
{
    char *pcToken;
    char *pcEnd;
    int  iPort;
    int  iEndPorts = 0;
    int num_ports = 0;

    if (config == NULL)
    {
        snprintf(ErrorString, ErrStrLen, "IMAP config is NULL.\n");
        return -1;
    }

    pcToken = strtok(NULL, CONF_SEPARATORS);
    if(!pcToken)
    {
        snprintf(ErrorString, ErrStrLen, "Invalid port list format.");
        return -1;
    }

    if(strcmp(CONF_START_LIST, pcToken))
    {
        snprintf(ErrorString, ErrStrLen,
                "Must start a port list with the '%s' token.", CONF_START_LIST);

        return -1;
    }

    /* Since ports are specified, clear default ports */
    disablePort( config->ports, IMAP_DEFAULT_SERVER_PORT );

    while ((pcToken = strtok(NULL, CONF_SEPARATORS)) != NULL)
    {
        if(!strcmp(CONF_END_LIST, pcToken))
        {
            iEndPorts = 1;
            break;
        }

        iPort = strtol(pcToken, &pcEnd, 10);

        /*
        **  Validity check for port
        */
        if(*pcEnd)
        {
            snprintf(ErrorString, ErrStrLen,
                     "Invalid port number.");

            return -1;
        }

        if(iPort < 0 || iPort > MAXPORTS-1)
        {
            snprintf(ErrorString, ErrStrLen,
                     "Invalid port number.  Must be between 0 and 65535.");

            return -1;
        }

        enablePort( config->ports, iPort );
        num_ports++;
    }

    if(!iEndPorts)
    {
        snprintf(ErrorString, ErrStrLen,
                 "Must end '%s' configuration with '%s'.",
                 CONF_PORTS, CONF_END_LIST);

        return -1;
    }
    else if(!num_ports)
    {
        snprintf(ErrorString, ErrStrLen,
             "IMAP: Empty port list not allowed.");
        return -1;
    }

    return 0;
}
示例#6
0
/*
 * Function: SMTP_ParseArgs(char *)
 *
 * Purpose: Process the preprocessor arguments from the rules file and
 *          initialize the preprocessor's data struct.  This function doesn't
 *          have to exist if it makes sense to parse the args in the init
 *          function.
 *
 * Arguments: args => argument list
 *
 * Returns: void function
 *
 */
void SMTP_ParseArgs(SMTPConfig *config, char *args)
{
    int ret = 0;
    char *arg;
    char *value;
    char errStr[ERRSTRLEN];
    int errStrLen = ERRSTRLEN;
    int deprecated_options = 0;

    if ((config == NULL) || (args == NULL))
        return;

    enablePort( config->ports, SMTP_DEFAULT_SERVER_PORT );
    enablePort( config->ports, XLINK2STATE_DEFAULT_PORT );
    enablePort( config->ports, SMTP_DEFAULT_SUBMISSION_PORT );
    config->inspection_type = SMTP_STATELESS;
    config->max_command_line_len = DEFAULT_MAX_COMMAND_LINE_LEN;
    config->max_header_line_len = DEFAULT_MAX_HEADER_LINE_LEN;
    config->max_response_line_len = DEFAULT_MAX_RESPONSE_LINE_LEN;
    config->max_mime_depth = DEFAULT_MAX_MIME_DEPTH;
    config->memcap = DEFAULT_SMTP_MEMCAP;
    config->alert_xlink2state = 1;
    config->print_cmds = 1;
    config->enable_mime_decoding = 0;
    _dpd.fileAPI->set_mime_decode_config_defauts(&(config->decode_conf));
    _dpd.fileAPI->set_mime_log_config_defauts(&(config->log_config));
    config->log_config.email_hdrs_log_depth = DEFAULT_LOG_DEPTH;

    config->cmd_config = (SMTPCmdConfig *)calloc(CMD_LAST, sizeof(SMTPCmdConfig));
    if (config->cmd_config == NULL)
    {
        DynamicPreprocessorFatalMessage("%s(%d) => Failed to allocate memory for SMTP "
                                        "command structure\n",
                                        *(_dpd.config_file), *(_dpd.config_line));
    }

    *errStr = '\0';

    arg = strtok(args, CONF_SEPARATORS);

    while ( arg != NULL )
    {
        unsigned long val = 0;

        if ( !strcasecmp(CONF_PORTS, arg) )
        {
            ret = ProcessPorts(config, errStr, errStrLen);
        }
        else if ( !strcasecmp(CONF_INSPECTION_TYPE, arg) )
        {
            value = strtok(NULL, CONF_SEPARATORS);
            if ( value == NULL )
            {
                return;
            }
            if ( !strcasecmp(CONF_STATEFUL, value) )
            {
                config->inspection_type = SMTP_STATEFUL;
            }
            else
            {
                config->inspection_type = SMTP_STATELESS;
            }
        }
        else if ( !strcasecmp(CONF_NORMALIZE, arg) )
        {
            value = strtok(NULL, CONF_SEPARATORS);
            if ( value == NULL )
            {
                return;
            }
            if ( !strcasecmp(CONF_NONE, value) )
            {
                config->normalize = NORMALIZE_NONE;
            }
            else if ( !strcasecmp(CONF_ALL, value) )
            {
                config->normalize = NORMALIZE_ALL;
            }
            else
            {
                config->normalize = NORMALIZE_CMDS;
            }
        }
        else if ( !strcasecmp(CONF_IGNORE_DATA, arg) )
        {
            config->decode_conf.ignore_data = 1;
        }
        else if ( !strcasecmp(CONF_IGNORE_TLS_DATA, arg) )
        {
            config->ignore_tls_data = 1;
        }
        else if ( !strcasecmp(CONF_MAX_COMMAND_LINE_LEN, arg) )
        {
            char *endptr;

            value = strtok(NULL, CONF_SEPARATORS);
            if ( value == NULL )
                return;

            config->max_command_line_len = strtol(value, &endptr, 10);
        }
        else if ( !strcasecmp(CONF_MAX_HEADER_LINE_LEN, arg) )
        {
            char *endptr;

            value = strtok(NULL, CONF_SEPARATORS);
            if ( value == NULL )
                return;

            config->max_header_line_len = strtol(value, &endptr, 10);
        }
        else if ( !strcasecmp(CONF_MAX_RESPONSE_LINE_LEN, arg) )
        {
            char *endptr;

            value = strtok(NULL, CONF_SEPARATORS);
            if ( value == NULL )
                return;

            config->max_response_line_len = strtol(value, &endptr, 10);
        }
        else if ( !strcasecmp(CONF_NO_ALERTS, arg) )
        {
            config->no_alerts = 1;
        }
        else if ( !strcasecmp(CONF_ALERT_UNKNOWN_CMDS, arg) )
        {
            config->alert_unknown_cmds = 1;
        }
        else if ( !strcasecmp(CONF_INVALID_CMDS, arg) )
        {
            /* Parse disallowed commands */
            ret = ProcessCmds(config, errStr, errStrLen, ACTION_ALERT, SMTP_CMD_TYPE_NORMAL);
        }
        else if ( !strcasecmp(CONF_VALID_CMDS, arg) )
        {
            /* Parse allowed commands */
            ret = ProcessCmds(config, errStr, errStrLen, ACTION_NO_ALERT, SMTP_CMD_TYPE_NORMAL);
        }
        else if ( !strcasecmp(CONF_AUTH_CMDS, arg) )
        {
            ret = ProcessCmds(config, errStr, errStrLen, ACTION_NO_ALERT, SMTP_CMD_TYPE_AUTH);
        }
        else if ( !strcasecmp(CONF_DATA_CMDS, arg) )
        {
            ret = ProcessCmds(config, errStr, errStrLen, ACTION_NO_ALERT, SMTP_CMD_TYPE_DATA);
        }
        else if ( !strcasecmp(CONF_BDATA_CMDS, arg) )
        {
            ret = ProcessCmds(config, errStr, errStrLen, ACTION_NO_ALERT, SMTP_CMD_TYPE_BDATA);
        }
        else if ( !strcasecmp(CONF_NORMALIZE_CMDS, arg) )
        {
            /* Parse normalized commands */
            ret = ProcessCmds(config, errStr, errStrLen, ACTION_NORMALIZE, SMTP_CMD_TYPE_NORMAL);
        }
        else if ( !strcasecmp(CONF_ALT_MAX_COMMAND_LINE_LEN, arg) )
        {
            /* Parse max line len for commands */
            ret = ProcessAltMaxCmdLen(config, errStr, errStrLen);
        }
        else if ( !strcasecmp(CONF_SMTP_MEMCAP, arg) )
        {
            ret = _dpd.checkValueInRange(strtok(NULL, CONF_SEPARATORS), CONF_SMTP_MEMCAP,
                    MIN_SMTP_MEMCAP, MAX_SMTP_MEMCAP, &val);
            config->memcap = (uint32_t)val;
        }
        else if ( !strcasecmp(CONF_MAX_MIME_MEM, arg) )
        {
            ret = _dpd.checkValueInRange(strtok(NULL, CONF_SEPARATORS), CONF_MAX_MIME_MEM,
                    MIN_MIME_MEM, MAX_MIME_MEM, &val);
            config->decode_conf.max_mime_mem = (int)val;
        }
        else if ( !strcasecmp(CONF_MAX_MIME_DEPTH, arg) )
        {
            deprecated_options = 1;
            _dpd.logMsg("WARNING: %s(%d) => The SMTP config option 'max_mime_depth' is deprecated.\n",
                            *(_dpd.config_file), *(_dpd.config_line));
            ret = ProcessMaxMimeDepth(config, errStr, errStrLen);
        }
        else if ( !strcasecmp(CONF_ENABLE_MIME_DECODING, arg) )
        {
            deprecated_options = 1;
            _dpd.logMsg("WARNING: %s(%d) => The SMTP config option 'enable_mime_decoding' is deprecated.\n",
                                        *(_dpd.config_file), *(_dpd.config_line));
            config->enable_mime_decoding = 1;
        }
        else if ( !strcasecmp(CONF_DISABLED, arg) )
        {
            config->disabled = 1;
        }
        else if ( !strcasecmp(CONF_XLINK2STATE, arg) )
        {
            ret = ProcessXlink2State(config, errStr, errStrLen);
        }
        else if ( !strcasecmp(CONF_LOG_FILENAME, arg) )
        {
            config->log_config.log_filename = 1;
        }
        else if ( !strcasecmp(CONF_LOG_MAIL_FROM, arg) )
        {
            config->log_config.log_mailfrom = 1;
        }
        else if ( !strcasecmp(CONF_LOG_RCPT_TO, arg) )
        {
            config->log_config.log_rcptto = 1;
        }
        else if ( !strcasecmp(CONF_LOG_EMAIL_HDRS, arg) )
        {
            config->log_config.log_email_hdrs = 1;
        }
        else if ( !strcasecmp(CONF_EMAIL_HDRS_LOG_DEPTH, arg) )
        {
            ret = ProcessLogDepth(config, errStr, errStrLen);
        }

        else if ( !strcasecmp(CONF_PRINT_CMDS, arg) )
        {
            config->print_cmds = 1;
        }

        else if(!_dpd.fileAPI->parse_mime_decode_args(&(config->decode_conf), arg, "SMTP"))
        {
            ret = 0;
        }
        else
        {
            DynamicPreprocessorFatalMessage("%s(%d) => Unknown SMTP configuration option %s\n",
                                            *(_dpd.config_file), *(_dpd.config_line), arg);
        }

        if (ret == -1)
        {
            /*
            **  Fatal Error, log error and exit.
            */
            if (*errStr)
            {
                DynamicPreprocessorFatalMessage("%s(%d) => %s\n",
                                                *(_dpd.config_file), *(_dpd.config_line), errStr);
            }
            else
            {
                DynamicPreprocessorFatalMessage("%s(%d) => Undefined Error.\n",
                                                *(_dpd.config_file), *(_dpd.config_line));
            }
        }

        /*  Get next token */
        arg = strtok(NULL, CONF_SEPARATORS);
    }

    // NOTE: the default b64_depth is not defined in this file 
    //       but is equal to DEFAULT_MAX_MIME_DEPTH
    if(config->decode_conf.b64_depth == DEFAULT_MAX_MIME_DEPTH)
    {
        if(config->enable_mime_decoding)
            config->decode_conf.b64_depth = config->max_mime_depth;
    }
    else if(deprecated_options)
    {
        DynamicPreprocessorFatalMessage("%s(%d) => Cannot specify 'enable_mime_decoding' or 'max_mime_depth' with "
                                       "'b64_decode_depth'\n",
                                     *(_dpd.config_file), *(_dpd.config_line), arg);
    }

    if(!config->log_config.email_hdrs_log_depth)
    {
        if(config->log_config.log_email_hdrs)
        {
            _dpd.logMsg("WARNING: %s(%d) => 'log_email_hdrs' enabled with 'email_hdrs_log_depth' = 0."
                    "Email headers won't be logged. Please set 'email_hdrs_log_depth' > 0 to enable logging.\n",
                    *(_dpd.config_file), *(_dpd.config_line));
        }
        config->log_config.log_email_hdrs = 0;
    }

}