int ModbusSlave232::modbus_request(unsigned char *data) { int response_length; unsigned int crc_calc = 0; unsigned int crc_received = 0; unsigned char recv_crc_hi; unsigned char recv_crc_lo; response_length = receive_request(data); if (response_length > 0) { crc_calc = crc(data, 0, response_length - 2); recv_crc_hi = (unsigned) data[response_length - 2]; recv_crc_lo = (unsigned) data[response_length - 1]; crc_received = data[response_length - 2]; crc_received = (unsigned) crc_received << 8; crc_received = crc_received | (unsigned) data[response_length - 1]; /*********** check CRC of response ************/ if (crc_calc != crc_received) { return NO_REPLY; } /* check for slave id */ if (slave != data[SLAVE]) { return NO_REPLY; } } return (response_length); }
virtual bool respond(talas::protocol::tls::connection& connection) { bool success = true; if ((success = receive_request(connection))) { success = send_response(connection); } return success; }
/** * Wait operation without node-ref and tag */ void _xmp_mpi_wait_noargs() { if(remove_request_noargs()){ return; } int tag; int node; receive_request(&tag, &node); }
/** * Wait operation with node-ref * * @param[in] node node number */ void _xmp_mpi_wait_node(const int node) { if(remove_request_node(node)){ return; } while(1){ int recv_tag; int recv_node; receive_request(&recv_node, &recv_tag); if(recv_node == node){ return; } do_post(recv_node, recv_tag); } }
/** * Wait operation with node-ref and tag * * @param[in] node node number * @param[in] tag tag */ void _xmp_mpi_wait(const int node, const int tag) { XACC_DEBUG("wait (%d,%d)", node,tag); if(remove_request(node, tag)){ XACC_DEBUG("wait end (already recved)"); return; } while(1){ int recv_tag; int recv_node; receive_request(&recv_node, &recv_tag); if(recv_node == node && recv_tag == tag){ XACC_DEBUG("wait end (recved)"); return; } do_post(recv_node, recv_tag); } }
static void *consumer_func(void *arg) { char buf[MAX_CONSUMER_READ_SIZE]; /* XXX: Should later be absolute times instead of indices */ int ind_from, size, size_temp; while (1) { receive_request(&ind_from, &size); if (ind_from+size < RX_BUFFER_SIZE) memcpy(buf, rx_buff + ind_from, size); else { size_temp = RX_BUFFER_SIZE - ind_from; memcpy(buf, rx_buff + ind_from, size_temp); memcpy(buf + size_temp, rx_buff, size - size_temp); } printf("Consumer\n"); } }
int main(void) { SOCKET listenfd; /* server socket */ SOCKET connectfd; /* client communication socket */ wsa_init(); /* create server socket */ listenfd = tcp_create_listener(SIMPLE_WEB_LISTEN_PORT, DEFAULT_LISTEN_BACKLOG); DIE(listenfd < 0, "tcp_create_listener"); connectfd = accept_connection(listenfd); receive_request(connectfd); send_reply(connectfd); closesocket(listenfd); wsa_cleanup(); return 0; }
bool CHttpServer::parse_request(String &method, String &uri, String &query, HeaderList &headers, String &body ) { method.clear(); uri.clear(); headers.clear(); body.clear(); size_t s = 0; ByteVector request; bool parsing_headers = true; size_t content_length = 0; for (;;) { if (!receive_request(request)) return false; size_t lim = request.size(); while (parsing_headers) { size_t e; for(e = s; e < lim && request[e] != '\r'; ++e); if (e >= lim - 1) { // Incomplete line, will read further break; } if (request[e + 1] != '\n') { if (verbose) RAWLOG_ERROR("Wrong request syntax, line should ends by '\\r\\n'"); return false; } String line(&request[s], e - s); s = e + 2; if (line.empty()) { parsing_headers = false; break; } if (uri.empty()) { // Parse start line if (!parse_startline(line, method, uri, query) || uri.empty()) return false; } else { Header hdr; if (!parse_header(line, hdr) || hdr.name.empty()) return false; headers.push_back(hdr); String low; std::transform(hdr.name.begin(), hdr.name.end(), std::back_inserter(low), &::tolower); if (low == "content-length") { content_length = ::atoi(hdr.value.c_str()); } } } if (!parsing_headers) { if (content_length == 0) return true; if (lim - s < content_length) continue; body.assign(&request[s], &request[s] + content_length); return true; } } }
bool receive_meta(connection_t *c) { int oldlen, i, result; int lenin, lenout, reqlen; bool decrypted = false; char inbuf[MAXBUFSIZE]; /* Strategy: - Read as much as possible from the TCP socket in one go. - Decrypt it. - Check if a full request is in the input buffer. - If yes, process request and remove it from the buffer, then check again. - If not, keep stuff in buffer and exit. */ lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0); if(lenin <= 0) { if(!lenin || !errno) { ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)", c->name, c->hostname); } else if(sockwouldblock(sockerrno)) return true; else logger(LOG_ERR, "Metadata socket read error for %s (%s): %s", c->name, c->hostname, sockstrerror(sockerrno)); return false; } oldlen = c->buflen; c->buflen += lenin; while(lenin > 0) { /* Decrypt */ if(c->status.decryptin && !decrypted) { result = EVP_DecryptUpdate(c->inctx, (unsigned char *)inbuf, &lenout, (unsigned char *)c->buffer + oldlen, lenin); if(!result || lenout != lenin) { logger(LOG_ERR, "Error while decrypting metadata from %s (%s): %s", c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL)); return false; } memcpy(c->buffer + oldlen, inbuf, lenin); decrypted = true; } /* Are we receiving a TCPpacket? */ if(c->tcplen) { if(c->tcplen <= c->buflen) { if(!c->node) { if(c->outgoing && proxytype == PROXY_SOCKS4 && c->allow_request == ID) { if(c->buffer[0] == 0 && c->buffer[1] == 0x5a) { ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Proxy request granted"); } else { logger(LOG_ERR, "Proxy request rejected"); return false; } } else if(c->outgoing && proxytype == PROXY_SOCKS5 && c->allow_request == ID) { if(c->buffer[0] != 5) { logger(LOG_ERR, "Invalid response from proxy server"); return false; } if(c->buffer[1] == (char)0xff) { logger(LOG_ERR, "Proxy request rejected: unsuitable authentication method"); return false; } if(c->buffer[2] != 5) { logger(LOG_ERR, "Invalid response from proxy server"); return false; } if(c->buffer[3] == 0) { ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Proxy request granted"); } else { logger(LOG_ERR, "Proxy request rejected"); return false; } } else { logger(LOG_ERR, "c->tcplen set but c->node is NULL!"); abort(); } } else { if(c->allow_request == ALL) { receive_tcppacket(c, c->buffer, c->tcplen); } else { logger(LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname); return false; } } c->buflen -= c->tcplen; lenin -= c->tcplen - oldlen; memmove(c->buffer, c->buffer + c->tcplen, c->buflen); oldlen = 0; c->tcplen = 0; continue; } else { break; } } /* Otherwise we are waiting for a request */ reqlen = 0; for(i = oldlen; i < c->buflen; i++) { if(c->buffer[i] == '\n') { c->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */ reqlen = i + 1; break; } } if(reqlen) { c->reqlen = reqlen; if(!receive_request(c)) return false; c->buflen -= reqlen; lenin -= reqlen - oldlen; memmove(c->buffer, c->buffer + reqlen, c->buflen); oldlen = 0; continue; } else { break; } } if(c->buflen >= MAXBUFSIZE) { logger(LOG_ERR, "Metadata read buffer overflow for %s (%s)", c->name, c->hostname); return false; } return true; }
bool receive_meta(connection_t *c) { int oldlen, i, result; int lenin, lenout, reqlen; bool decrypted = false; char inbuf[MAXBUFSIZE]; /* Strategy: - Read as much as possible from the TCP socket in one go. - Decrypt it. - Check if a full request is in the input buffer. - If yes, process request and remove it from the buffer, then check again. - If not, keep stuff in buffer and exit. */ lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0); if(lenin <= 0) { if(!lenin || !errno) { ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)", c->name, c->hostname); } else if(sockwouldblock(sockerrno)) return true; else logger(LOG_ERR, "Metadata socket read error for %s (%s): %s", c->name, c->hostname, sockstrerror(sockerrno)); return false; } oldlen = c->buflen; c->buflen += lenin; while(lenin > 0) { /* Decrypt */ if(c->status.decryptin && !decrypted) { result = EVP_DecryptUpdate(c->inctx, (unsigned char *)inbuf, &lenout, (unsigned char *)c->buffer + oldlen, lenin); if(!result || lenout != lenin) { logger(LOG_ERR, "Error while decrypting metadata from %s (%s): %s", c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL)); return false; } memcpy(c->buffer + oldlen, inbuf, lenin); decrypted = true; } /* Are we receiving a TCPpacket? */ if(c->tcplen) { if(c->tcplen <= c->buflen) { receive_tcppacket(c, c->buffer, c->tcplen); c->buflen -= c->tcplen; lenin -= c->tcplen - oldlen; memmove(c->buffer, c->buffer + c->tcplen, c->buflen); oldlen = 0; c->tcplen = 0; continue; } else { break; } } /* Otherwise we are waiting for a request */ reqlen = 0; for(i = oldlen; i < c->buflen; i++) { if(c->buffer[i] == '\n') { c->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */ reqlen = i + 1; break; } } if(reqlen) { c->reqlen = reqlen; if(!receive_request(c)) return false; c->buflen -= reqlen; lenin -= reqlen - oldlen; memmove(c->buffer, c->buffer + reqlen, c->buflen); oldlen = 0; continue; } else { break; } } if(c->buflen >= MAXBUFSIZE) { logger(LOG_ERR, "Metadata read buffer overflow for %s (%s)", c->name, c->hostname); return false; } return true; }
/* * Handles the request process. It uses the receive_request function * and parses the received data filling a request_t data structure. * * @param thread_id: the thread id handling the request * @param sockfd: socket file descriptor to read data from the client * @param req: request_t data structure to store the parsed data */ int handle_request(int thread_id, int sockfd, request_t *req) { char *buffer = NULL; char *method = NULL; char *query = NULL; char *content_length = NULL; int start, end, pos, tmp, n; int string_length, message_length; int received; uint8_t i; start = 0; end = 0; pos = 0; tmp = 0; n = 0; string_length = 0; message_length = 0; received = 0; /* allocate first 1024 bytes for the request */ buffer = malloc(REQUEST_ALLOC_SIZE); memset(buffer, 0, REQUEST_ALLOC_SIZE); if ((n = receive_request(thread_id, sockfd, &buffer)) < 0) { /* There has been an error receiving the client request :( */ free(buffer); return ERROR; } else if (n == 0) { free(buffer); debug(conf.output_level, "[%d] empty request\n", thread_id); return ERROR; } debug(conf.output_level, "[%d] Request:\n%s\n", thread_id, buffer); while (strncmp(&buffer[start], "\r\n", 2) != 0) { while (strncmp(&buffer[end], "\r\n", 2) != 0) end++; if (start == 0) { pos = 0; tmp = 0; /* allocate request. REMEMBER TO FREE request AFTER sending response */ while (strncmp(&buffer[pos], " ", 1) != 0) pos++; method = malloc(pos + 1); memset(method, 0, pos + 1); strncat(method, buffer, pos); for (i = 0; i < 7; i++) { if(strncmp(methods[i], method, strlen(method)) == 0) { req->method = i; } } paranoid_free_string(method); pos++; tmp = pos; while (strncmp(&buffer[pos], " ", 1) != 0) pos++; req->uri = malloc(pos + 1 - tmp); memset(req->uri, 0, pos + 1 - tmp); strncat(req->uri, &buffer[tmp], pos - tmp); req->_mask |= _REQUEST_URI; pos++; tmp = pos; while (strncmp(&buffer[pos], "\r\n", 2) != 0) pos++; req->version = malloc(pos + 1 - tmp); memset(req->version, 0, pos + 1 - tmp); strncat(req->version, &buffer[tmp], pos - tmp); req->_mask |= _REQUEST_VERSION; } else { pos = 0; tmp = 0; /* reallocate header_t struct of request_t */ if (req->num_headers == 0) { req->headers = malloc(sizeof(header_t *)); } else { req->headers = realloc(req->headers, (req->num_headers + 1)*sizeof(header_t *)); } req->headers[req->num_headers] = malloc(sizeof(header_t *)); /* parse headers */ while (strncmp(&buffer[start + pos], ":", 1) != 0) pos++; req->headers[req->num_headers]->name = malloc(pos++); tmp = pos; while (strncmp(&buffer[start + pos], "\r\n", 2) != 0) pos++; req->headers[req->num_headers]->value = malloc((pos++) - tmp); tmp = pos; sscanf(&buffer[start], "%[^:]: %[^\r\n]", req->headers[req->num_headers]->name, req->headers[req->num_headers]->value); req->num_headers++; } end += 2; start = end; } /* Look for the query part */ if (strchr(req->uri, '?') != NULL) { query = strchr(req->uri, '?'); string_length = strlen(query); req->query = malloc(string_length + 1); memset(req->query, 0, string_length + 1); strncat(req->query, query, string_length); req->_mask |= _REQUEST_QUERY; debug(conf.output_level, "[%d] DEBUG: query %s\n", thread_id, req->query); } /* Get the resource requested */ if (req->_mask & _REQUEST_QUERY) { string_length = strlen(req->uri) - strlen(req->query); // Strip query string from uri } else { string_length = strlen(req->uri); } req->resource = malloc(string_length + 1); memset(req->resource, 0, string_length + 1); strncat(req->resource, req->uri, string_length); req->_mask |= _REQUEST_RESOURCE; string_length = 0; /* free buffer */ free(buffer); if (get_request_header(req, "Content-Length", &content_length) != -1) { message_length = atoi(content_length); if (message_length > REQUEST_MAX_MESSAGE_SIZE) { return ERROR; } buffer = malloc(REQUEST_ALLOC_MESSAGE_SIZE); if ((received = receive_message_body(thread_id, sockfd, &buffer, message_length)) < 0) { /* There has been an error receiving the message body :( */ free(buffer); free_request(req); return ERROR; } req->message_body = malloc(received + 1); memset(req->message_body, 0, received + 1); memcpy(req->message_body, buffer, received); req->_mask |= _REQUEST_MESSAGE; free(buffer); debug(conf.output_level, "[%d] DEBUG: message body: %s\n", thread_id, req->message_body); } return 0; }