int portEqualsRange(char* str, ifids_port* portRange) { int result; result = portIfidsAreEqual(portRange,parsePort(str)); return result; }
Port::Port (TiXmlElement* element): findBy(NULL), ifComponentInstantiationRef(false), ifDeviceThatLoadedThisComponentRef(false), ifDeviceUsedByThisComponentRef(false), ifFindBy(false) { parsePort(element); }
void NetworkDialog::readConfig() { FILE *file = fopen("Comote.config", "r"); if (file) { char buf[256 + 1 /*tz*/] = { 0 }; while (fscanf(file, "%256s", buf) != EOF) { if (strncmp(buf, "ip", 2) == 0) { parseHostname(file, ui.comboBoxHostname); } else if (strncmp(buf, "tcp", 3) == 0) { parsePort(file, ui.comboBoxTCPPort); } #if USE_TUIO else if (strncmp(buf, "udp", 3) == 0) { parsePort(file, ui.comboBoxUDPPort); } #endif } fclose(file); } // Defaults //if (ui.comboBoxHostname->count() == 0) //{ // addToComboBox(ui.comboBoxHostname, "127.0.0.1"); //} if (ui.comboBoxTCPPort->count() == 0) { addToComboBox(ui.comboBoxTCPPort, "31043"); } #if USE_TUIO if (ui.comboBoxUDPPort->count() == 0) { addToComboBox(ui.comboBoxUDPPort, "50096"); } #endif }
mocca::net::TCPNetworkAddress::TCPNetworkAddress(const std::string& address) { auto parts = mocca::splitString<std::string>(address, ':'); if (parts.size() != 2) { throw Error("TCP connection string is malformed, must have the format 'ip:port'", __FILE__, __LINE__); } auto ip = parts.front(); auto port = parsePort(parts.back()); checkIp(ip); checkPort(port); ip_ = ip; port_ = port; }
void Uri::Private::parseAuthority() { m_parserAux.clear(); const size_t parserOldPos = m_parserPos; parseUserinfo(); if (!expectChar('@')) { m_parserPos = parserOldPos; } parseHost(); if (expectChar(':')) { parsePort(); } }
static ParseResult<Authority> parseAuthority(char const* str) { ParseResult<Authority> result{}; if (str[0] == '\0' || str[0] != '/' || str[1] != '/') { result.ch = str; return result; } auto user = parseUser(str+2); // For "//" auto host = parseHost(user.ch); auto port = parsePort(host.ch); result.value.userIs(user.value); result.value.hostIs(host.value); result.value.portIs(port.value); result.ch = port.ch; return result; }
//------------------------------------------------------------------------------ // Parse the cmd line parameters to start testing // Return:error code signaling if operation succeded or sytax error //------------------------------------------------------------------------------ int parseCmdParameters() { if (svParameters.size() > 2) { return ERROR_CMD_SYNTAX; } else { sFilePath = svParameters.at(0); //is 2 parameters, then second is a port if( 2 == svParameters.size() ) { string sTemp; sTemp = svParameters.at(1); if(sTemp.at(0) == '/') { if (ERROR_SUCCESS == parsePort() ) { sPort = sTemp.substr(1, sTemp.npos); return ERROR_SUCCESS; } else return ERROR_CMD_SYNTAX; } else return ERROR_CMD_SYNTAX; } else sPort = ""; } return ERROR_SUCCESS; }
// source = scheme ":" // / ( [ scheme "://" ] host [ port ] ) // / "'self'" // bool CSPSourceList::parseSource(const UChar* begin, const UChar* end, String& scheme, String& host, int& port, bool& hostHasWildcard, bool& portHasWildcard) { if (begin == end) return false; if (equalIgnoringCase("'self'", begin, end - begin)) { addSourceSelf(); return false; } if (equalIgnoringCase("'unsafe-inline'", begin, end - begin)) { addSourceUnsafeInline(); return false; } if (equalIgnoringCase("'unsafe-eval'", begin, end - begin)) { addSourceUnsafeEval(); return false; } const UChar* position = begin; const UChar* beginHost = begin; skipUtil(position, end, ':'); if (position == end) { // This must be a host-only source. if (!parseHost(beginHost, position, host, hostHasWildcard)) return false; return true; } if (end - position == 1) { ASSERT(*position == ':'); // This must be a scheme-only source. if (!parseScheme(begin, position, scheme)) return false; return true; } ASSERT(end - position >= 2); if (position[1] == '/') { if (!parseScheme(begin, position, scheme) || !skipExactly(position, end, ':') || !skipExactly(position, end, '/') || !skipExactly(position, end, '/')) return false; beginHost = position; skipUtil(position, end, ':'); } if (position == beginHost) return false; if (!parseHost(beginHost, position, host, hostHasWildcard)) return false; if (position == end) { port = 0; return true; } if (!skipExactly(position, end, ':')) ASSERT_NOT_REACHED(); if (!parsePort(position, end, port, portHasWildcard)) return false; return true; }
// source = scheme ":" // / ( [ scheme "://" ] host [ port ] [ path ] ) // / "'self'" bool CSPSourceList::parseSource(const UChar* begin, const UChar* end, String& scheme, String& host, int& port, String& path, CSPSource::WildcardDisposition& hostWildcard, CSPSource::WildcardDisposition& portWildcard) { if (begin == end) return false; if (equalIgnoringCase("'none'", begin, end - begin)) return false; if (end - begin == 1 && *begin == '*') { addSourceStar(); return true; } if (equalIgnoringCase("'self'", begin, end - begin)) { addSourceSelf(); return true; } if (equalIgnoringCase("'unsafe-inline'", begin, end - begin)) { addSourceUnsafeInline(); return true; } if (equalIgnoringCase("'unsafe-eval'", begin, end - begin)) { addSourceUnsafeEval(); return true; } String nonce; if (!parseNonce(begin, end, nonce)) return false; if (!nonce.isNull()) { addSourceNonce(nonce); return true; } DigestValue hash; ContentSecurityPolicyHashAlgorithm algorithm = ContentSecurityPolicyHashAlgorithmNone; if (!parseHash(begin, end, hash, algorithm)) return false; if (hash.size() > 0) { addSourceHash(algorithm, hash); return true; } const UChar* position = begin; const UChar* beginHost = begin; const UChar* beginPath = end; const UChar* beginPort = 0; skipWhile<UChar, isNotColonOrSlash>(position, end); if (position == end) { // host // ^ return parseHost(beginHost, position, host, hostWildcard); } if (position < end && *position == '/') { // host/path || host/ || / // ^ ^ ^ return parseHost(beginHost, position, host, hostWildcard) && parsePath(position, end, path); } if (position < end && *position == ':') { if (end - position == 1) { // scheme: // ^ return parseScheme(begin, position, scheme); } if (position[1] == '/') { // scheme://host || scheme:// // ^ ^ if (!parseScheme(begin, position, scheme) || !skipExactly<UChar>(position, end, ':') || !skipExactly<UChar>(position, end, '/') || !skipExactly<UChar>(position, end, '/')) return false; if (position == end) return false; beginHost = position; skipWhile<UChar, isNotColonOrSlash>(position, end); } if (position < end && *position == ':') { // host:port || scheme://host:port // ^ ^ beginPort = position; skipUntil<UChar>(position, end, '/'); } } if (position < end && *position == '/') { // scheme://host/path || scheme://host:port/path // ^ ^ if (position == beginHost) return false; beginPath = position; } if (!parseHost(beginHost, beginPort ? beginPort : beginPath, host, hostWildcard)) return false; if (beginPort) { if (!parsePort(beginPort, beginPath, port, portWildcard)) return false; } else { port = 0; } if (beginPath != end) { if (!parsePath(beginPath, end, path)) return false; } return true; }
// source = scheme ":" // / ( [ scheme "://" ] host [ port ] [ path ] ) // / "'self'" // bool ContentSecurityPolicySourceList::parseSource(const UChar* begin, const UChar* end, String& scheme, String& host, int& port, String& path, bool& hostHasWildcard, bool& portHasWildcard) { if (begin == end) return false; if (equalLettersIgnoringASCIICase(begin, end - begin, "'none'")) return false; if (end - begin == 1 && *begin == '*') { m_allowStar = true; return true; } if (equalLettersIgnoringASCIICase(begin, end - begin, "'self'")) { m_allowSelf = true; return true; } if (equalLettersIgnoringASCIICase(begin, end - begin, "'unsafe-inline'")) { m_allowInline = true; return true; } if (equalLettersIgnoringASCIICase(begin, end - begin, "'unsafe-eval'")) { m_allowEval = true; return true; } const UChar* position = begin; const UChar* beginHost = begin; const UChar* beginPath = end; const UChar* beginPort = nullptr; skipWhile<UChar, isNotColonOrSlash>(position, end); if (position == end) { // host // ^ return parseHost(beginHost, position, host, hostHasWildcard); } if (position < end && *position == '/') { // host/path || host/ || / // ^ ^ ^ return parseHost(beginHost, position, host, hostHasWildcard) && parsePath(position, end, path); } if (position < end && *position == ':') { if (end - position == 1) { // scheme: // ^ return parseScheme(begin, position, scheme); } if (position[1] == '/') { // scheme://host || scheme:// // ^ ^ if (!parseScheme(begin, position, scheme) || !skipExactly<UChar>(position, end, ':') || !skipExactly<UChar>(position, end, '/') || !skipExactly<UChar>(position, end, '/')) return false; if (position == end) return false; beginHost = position; skipWhile<UChar, isNotColonOrSlash>(position, end); } if (position < end && *position == ':') { // host:port || scheme://host:port // ^ ^ beginPort = position; skipUntil<UChar>(position, end, '/'); } } if (position < end && *position == '/') { // scheme://host/path || scheme://host:port/path // ^ ^ if (position == beginHost) return false; beginPath = position; } if (!parseHost(beginHost, beginPort ? beginPort : beginPath, host, hostHasWildcard)) return false; if (!beginPort) port = 0; else { if (!parsePort(beginPort, beginPath, port, portHasWildcard)) return false; } if (beginPath != end) { if (!parsePath(beginPath, end, path)) return false; } return true; }
void client(char *next_ss_info, char *url) { char *mutable_info = calloc(strlen(next_ss_info), sizeof(char)); memcpy(mutable_info, next_ss_info, strlen(next_ss_info)); int sockfd, status, connect_status; struct sockaddr_in serv_addr; struct addrinfo hints; struct addrinfo *servinfo; char *ip = parseIP(mutable_info); if (DEBUG) printf("Parsed IP address: %s\n", ip); memset(mutable_info, 0, strlen(next_ss_info)); memcpy(mutable_info, next_ss_info, strlen(next_ss_info)); char *port = parsePort(mutable_info); if (DEBUG) printf("Parsed port: %s\n", port); memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if ((status = getaddrinfo(ip, port, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); exit(1); } sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol); connect_status = connect(sockfd, servinfo->ai_addr, servinfo->ai_addrlen); if (connect_status < 0) { perror("Error trying to connect\n"); return; } char *url_packet = calloc(strlen(url) + 6, sizeof(char)); uint16_t url_length = strlen(url); url_length = htons(url_length); memcpy(url_packet + 4, &url_length, 2); memcpy(url_packet + 6, url, strlen(url)); if (DEBUG) printf("Connected to socket, trying to send url packet\n"); if (send(sockfd, url_packet, strlen(url) + 6, 0) < 0) { printf("Send failed\n"); // figure out how to handle failed send } if (DEBUG) printf("Sent url packet\n"); char *rec_buffer = calloc(500, sizeof(char)); int recv_status = recv(sockfd, rec_buffer, 500, 0); char *tmp_buffer = calloc(100, sizeof(char)); char *filename = "chainlist.txt"; FILE *file; file = fopen(filename, "rb"); uint32_t fileLength = getFileLength(filename); ///////////////// check size ////////////// if (fileLength < 401) { //printf("File length under 400bytes\n"); char *content = (char *) calloc(406, sizeof(char)); uint16_t packetLength = fileLength; uint32_t fileLengthSend = htonl(fileLength); uint16_t packetLengthSend = htons(packetLength); memcpy(content + 0, &fileLengthSend, 4); memcpy(content + 4, &packetLengthSend, 2); char ch; int counter = 0; while ((ch = fgetc(file)) != EOF) { content[counter + 6] = ch; counter++; } if (send(sockfd, content, 6 + fileLength, 0) < 0) { printf("Send failed\n"); abort(); } recv_status = recv(sockfd, tmp_buffer, 500, 0); free(content); } else { /*File over 400 bytes*/ //printf("File length over 400bytes\n"); /*All parts of file will be 400 bytes*/ /*Find out how many times file needs to be split*/ int numberOfParts = fileLength / 400; /*Figure out last part of files size*/ int sizeOfLastPart = fileLength % 400; /*Loop through the file the number of times it is split*/ int i = 0; for (i; i <= numberOfParts; i++) { //if(DEBUG) printf("In iteration %d of the loop for breaking up packets\n", i); char *content; /*Last part of the file. Could be smaller than 400 bytes*/ if (numberOfParts == i) { //if(DEBUG) printf("Sending last part of file\n"); content = (char *) calloc(6 + sizeOfLastPart, sizeof(char)); /*Go to right position in file*/ fseek(file, i * 400, SEEK_SET); /*Set variables for packet header*/ uint16_t packetLength = 6 + sizeOfLastPart; uint32_t fileLengthSend = htonl(fileLength); uint16_t packetLengthSend = htons(packetLength); /*Fill buffer with correct information*/ memcpy(content + 0, &fileLengthSend, 4); memcpy(content + 4, &packetLengthSend, 2); /*Write chars into buffer*/ int j = 0; for (j; j < sizeOfLastPart; j++) { char ch; ch = fgetc(file); content[6 + j] = ch; } if (send(sockfd, content, 6 + sizeOfLastPart, 0) < 0) { printf("Send failed\n"); abort(); } recv_status = recv(sockfd, tmp_buffer, 500, 0); //printf("send in loop iteration %d is:\n", i); char c; int k = 0; for (k = 0; k < sizeOfLastPart + 6; k++) { printf("%c", content[k]); } free(content); } /*Middle of file where the parts are still 400 bytes*/ else { //if(DEBUG) printf("Sending middle parts of file\n"); char *content_buffer = calloc(406, sizeof(char)); /*Go to right position in file*/ fseek(file, i * 400, SEEK_SET); /*Set variables for packet header*/ uint16_t packetLength = 400; uint32_t fileLengthSend = htonl(fileLength); uint16_t packetLengthSend = htons(packetLength); /*Fill buffer with correct information*/ memcpy(content_buffer + 0, &fileLengthSend, 4); memcpy(content_buffer + 4, &packetLengthSend, 2); /*Write chars into buffer*/ int j = 0; for (j; j < 400; j++) { char ch; ch = fgetc(file); content_buffer[6 + j] = ch; /******************************************/ if (i == 0) { //printf("Char at %d is %c\n", i, ch); } /******************************************/ } if (send(sockfd, content_buffer, 406, 0) < 0) { printf("Send failed\n"); abort(); } recv_status = recv(sockfd, tmp_buffer, 500, 0); //printf("send in loop iteration %d is:\n", i); free(content_buffer); } } } printf("waiting for file...\n"); uint32_t total_bytes_received = 0; uint32_t file_length = 1; uint32_t data_received = 0; unsigned int msg_length; char *ack = "Ack"; outBuff = NULL; outBuff_size = 0; int counter = 0; while (data_received < file_length) { char *receiverBuffer = (char *) calloc(500, sizeof(char)); msg_length = 0; recv_status = recv(sockfd, receiverBuffer, 500, 0); if (recv_status < 0) { perror("Error: receive failed\n"); } memcpy(&file_length, receiverBuffer + 0, 4); file_length = ntohl(file_length); memcpy(&msg_length, receiverBuffer + 4, 2); msg_length = ntohs(msg_length); if (outBuff == NULL) { int total_packets = file_length / 400; if (file_length % 400 > 0) total_packets++; //memset(&outBuff_size, 0, sizeof(uint32_t)); //memset(&outBuff, 0, sizeof(uint32_t)); outBuff_size = file_length + (total_packets * 6); outBuff = (char *) calloc(outBuff_size, sizeof(char)); file_length += file_length + (total_packets * 6); } char *data = calloc(msg_length + 6, sizeof(char)); memcpy(data, receiverBuffer, msg_length + 6); memcpy(outBuff + total_bytes_received, data, msg_length + 6); data_received += msg_length; total_bytes_received += msg_length + 6; free(data); memset(rec_buffer, 0, 500); /* send ack to add temporary to delay */ if (send(sockfd, ack, strlen(ack), 0) < 0) { int send_status = send(sockfd, ack, strlen(ack), 0); while (send_status < 0) { send_status = send(sockfd, ack, strlen(ack), 0); } } counter++; } printf("Relaying file...\n"); //printf("Goodbye!\n"); return; }
static CommandLineParseResult parseCommandLine(QCommandLineParser *parser, ArgsData *args, QString *errorMessage) { parser->setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); const QCommandLineOption configFileOption("config", "Config file.", "file"); parser->addOption(configFileOption); const QCommandLineOption logFileOption("logfile", "File to log to.", "file"); parser->addOption(logFileOption); const QCommandLineOption logLevelOption("loglevel", "Log level (default: 2).", "x"); parser->addOption(logLevelOption); const QCommandLineOption verboseOption("verbose", "Verbose output. Same as --loglevel=3."); parser->addOption(verboseOption); const QCommandLineOption mergeOutputOption(QStringList() << "m" << "merge-output", "Combine output of subprocesses."); parser->addOption(mergeOutputOption); const QCommandLineOption portOption("port", "Run a single HTTP server instance.", "[addr:]port"); parser->addOption(portOption); const QCommandLineOption idOption("id", "Set instance ID (needed to run multiple instances).", "x"); parser->addOption(idOption); const QCommandLineOption routeOption("route", "Add route (overrides routes file).", "line"); parser->addOption(routeOption); const QCommandLineOption helpOption = parser->addHelpOption(); const QCommandLineOption versionOption = parser->addVersionOption(); if(!parser->parse(QCoreApplication::arguments())) { *errorMessage = parser->errorText(); return CommandLineError; } if(parser->isSet(versionOption)) return CommandLineVersionRequested; if(parser->isSet(helpOption)) return CommandLineHelpRequested; if(parser->isSet(configFileOption)) args->configFile = parser->value(configFileOption); if(parser->isSet(logFileOption)) args->logFile = parser->value(logFileOption); if(parser->isSet(logLevelOption)) { bool ok; int x = parser->value(logLevelOption).toInt(&ok); if(!ok || x < 0) { *errorMessage = "error: loglevel must be greater than or equal to 0"; return CommandLineError; } args->logLevel = x; } if(parser->isSet(verboseOption)) args->logLevel = 3; if(parser->isSet(mergeOutputOption)) args->mergeOutput = true; if(parser->isSet(portOption)) { args->port = parsePort(parser->value(portOption)); if(args->port.second < 1) { *errorMessage = "error: port must be greater than or equal to 1"; return CommandLineError; } } if(parser->isSet(idOption)) { bool ok; int x = parser->value(idOption).toInt(&ok); if(!ok || x < 0) { *errorMessage = "error: id must be greater than or equal to 0"; return CommandLineError; } args->id = x; } if(parser->isSet(routeOption)) { foreach(const QString &r, parser->values(routeOption)) args->routeLines += r; }