int handle_http_post(http_req * req, http_rsp * rsp) { HashElement * e = NULL; int httpbodylen = 0; int readlen = 0; WEBLOG_API(); #ifdef WEBCFG_SUPPORT_AUTH /* Authentication has a very high priority. */ if (OK != CheckAuthorization(req, rsp)) { return handle_http_error(401, req, rsp); } #endif e = HashLookup(req->headOptions, "Content-Length"); if(!e) { /* RFC2616, 14.23. */ WEBLOG_ERROR("<%s> Missing \"Content-Length\" in POST/PUT.\n", __FUNCTION__); return handle_http_error(400, req, rsp); } httpbodylen = atoi(e->value.data.string); WEBLOG_INFO("<%s> Content-Length:%d.\n", __FUNCTION__, httpbodylen); /* Even if we don't care about the data, we should either : 1. close the socket. 2. read all data out of the socket. This is to enure a keep-alive socket won't fail in next loop. */ if(req->datalen > 0) // data left in head buffer. readlen += req->datalen; /* if there's more data in socket.... */ while(readlen < httpbodylen) { req->datalen = recv(req->sock, req->sockbuf, sizeof(req->sockbuf), 0); if(req->datalen < 0) { print_socket_error(); break; } else if(req->datalen == 0) { WEBLOG_INFO("<%s> client closed. data progress: %d/%d.\n", __FUNCTION__, readlen, httpbodylen); break; } readlen += req->datalen; WEBLOG_VERBOSE("<%s> progress: %10d/%d. \n", __FUNCTION__, readlen, httpbodylen); } rsp->code = 200; return http_send_response(rsp); }
void SCBus::setup_socket() { if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0) { print_socket_error("SCBus: WinSock Initialization failed: "); WSACleanup(); exit(WSAGetLastError()); } udp_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(udp_socket == INVALID_SOCKET) { print_socket_error("SCBus: Server socket creation failed: "); WSACleanup(); exit(WSAGetLastError()); } u_long iMode = 1; if (ioctlsocket(udp_socket, FIONBIO, &iMode)) { print_socket_error("SCBus: Cannot create non-blocking socket: "); WSACleanup(); exit(WSAGetLastError()); } SOCKADDR_IN server_inf; memset((void *)&server_inf, 0, sizeof(server_inf)); server_inf.sin_family = AF_INET; server_inf.sin_addr.s_addr = INADDR_ANY; server_inf.sin_port = htons(SCBUS_PORT); if(bind(udp_socket, (SOCKADDR*)(&server_inf), sizeof(server_inf)) == SOCKET_ERROR) { print_socket_error("SCBus: Unable to bind server socket: "); WSACleanup(); exit(WSAGetLastError()); } cout << "SCBus listening on port " << SCBUS_PORT << endl; }
int handle_http_put(http_req * req, http_rsp * rsp) { HashElement * e = NULL; char * vpath = NULL; int httpbodylen = 0; int readlen = 0; FILE * fp = NULL; int i = 0; unsigned char * buffer = NULL; int buflen = 1024*1024; char strbuf[128] = {0}; WEBLOG_API(); #ifdef WEBCFG_SUPPORT_AUTH /* Authentication has a very high priority. */ if (OK != CheckAuthorization(req, rsp)) { return handle_http_error(401, req, rsp); } #endif vpath = req->uri; ASSERT(vpath && vpath[0]); if(0 == strlen(vpath)) { strcat(vpath, "/"); WEBLOG_VERBOSE("<%s> redirect to root path. [%s].\n", __FUNCTION__, vpath); } e = HashLookup(req->headOptions, "Content-Length"); if(!e) { /* RFC2616, 14.23. */ WEBLOG_ERROR("<%s> Missing \"Content-Length\" in POST/PUT.\n", __FUNCTION__); return handle_http_error(400, req, rsp); } httpbodylen = atoi(e->value.data.string); WEBLOG_INFO("<%s> Content-Length:%d.\n", __FUNCTION__, httpbodylen); if(OK != Webdav_CheckPath(vpath)) return handle_http_error(501, req, rsp); if(WEBDAVR_OK != Webdav_CreateFile(vpath)) return handle_http_error(501, req, rsp); /* read data from socket and save it into target file. */ fp = fopen(vpath, "wb"); if(!fp) return handle_http_error(501, req, rsp); if(req->datalen > 0) // data left in head buffer. { i = fwrite(req->sockbuf, 1, req->datalen, fp); ASSERT(i == req->datalen); WEBLOG_VERBOSE("<%s> write previous buffered %d bytes into %s.\n", __FUNCTION__, req->datalen, vpath); readlen += req->datalen; } /* if there's more data in socket.... req->sockbuf is too small for large files. */ do { buflen /= 2; buffer = (char *)webmalloc(buflen); } while(!buffer); while(readlen < httpbodylen) { req->datalen = recv(req->sock, buffer, buflen, 0); if(req->datalen < 0) { print_socket_error(); break; } else if(req->datalen == 0) { WEBLOG_INFO("<%s> client closed. data progress: %d/%d.\n", __FUNCTION__, readlen, httpbodylen); break; } readlen += req->datalen; WEBLOG_VERBOSE("<%s> progress: %10d/%d, ", __FUNCTION__, readlen, httpbodylen); i = fwrite(buffer, 1, req->datalen, fp); WEBLOG_VERBOSE("R %d <- socket, W %d -> %s.\n", req->datalen, i, vpath); ASSERT(i == req->datalen); } rsp->code = 201; e = HashLookup(req->headOptions, "Host"); if(!e) { /* RFC2616, 14.23. */ WEBLOG_ERROR("<%s> Missing \"Host\" in POST/PUT.\n", __FUNCTION__); sprintf(strbuf, "/webdav%s", vpath); } else { sprintf(strbuf, "http://%s/webdav%s", e->value.data.string, vpath); } http_add_rspoption(rsp, "Location", strbuf); http_send_response(rsp); /****************************** Double Confirm File Size! *******************************/ fseek(fp, 0, SEEK_END); i = ftell(fp); WEBLOG_INFO("<%s> Double Confirm. file-size: %d. content-length: %d.\n", __FUNCTION__, i, httpbodylen); ASSERT(i == httpbodylen); webfree(buffer); fclose(fp); return OK; }
static int test_socket_connect(void) { int listening_sock = socket(AF_INET, SOCK_STREAM, 0); int sock; struct sockaddr_in sin; socklen_t sockaddr_len = sizeof(sin); static const char kTestMessage[] = "test"; char hostname[80], buf[5]; BIO *bio; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) { print_socket_error("inet_pton"); return 0; } if (bind(listening_sock, (struct sockaddr *)&sin, sizeof(sin)) != 0) { print_socket_error("bind"); return 0; } if (listen(listening_sock, 1)) { print_socket_error("listen"); return 0; } if (getsockname(listening_sock, (struct sockaddr *)&sin, &sockaddr_len) || sockaddr_len != sizeof(sin)) { print_socket_error("getsockname"); return 0; } BIO_snprintf(hostname, sizeof(hostname), "%s:%d", "127.0.0.1", ntohs(sin.sin_port)); bio = BIO_new_connect(hostname); if (!bio) { fprintf(stderr, "BIO_new_connect failed.\n"); return 0; } if (BIO_write(bio, kTestMessage, sizeof(kTestMessage)) != sizeof(kTestMessage)) { fprintf(stderr, "BIO_write failed.\n"); BIO_print_errors_fp(stderr); return 0; } sock = accept(listening_sock, (struct sockaddr *) &sin, &sockaddr_len); if (sock < 0) { print_socket_error("accept"); return 0; } if (recv(sock, buf, sizeof(buf), 0) != sizeof(kTestMessage)) { print_socket_error("read"); return 0; } if (memcmp(buf, kTestMessage, sizeof(kTestMessage))) { return 0; } closesocket(sock); closesocket(listening_sock); BIO_free(bio); return 1; }
/* test cli */ int main(int argc, char ** argv) { int ret = 0; log_init(); Cli::cli_append(_my_cli_table_, sizeof(_my_cli_table_)/sizeof(CliItem)); Cli * cli = new Cli(); if(cli); #if 0 pthread_t tid; pthread_create(&tid, NULL, cli_server, NULL); sleep(10000); #endif #ifdef WINDOWS WSADATA wsaData; ret = WSAStartup(MAKEWORD(2, 2), &wsaData); if( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { WSACleanup( ); } #endif SOCKET sockfd = socket(AF_INET, SOCK_DGRAM/*SOCK_STREAM*/, 0); if(sockfd <= 0) { print_socket_error(); } ASSERT(sockfd >= 0); sockaddr_in serverAddr; memset(&serverAddr,0,sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); serverAddr.sin_port = htons(6789); do { printf("[connecting to %s:%d ......]\n", "127.0.0.1", 6789); ret = connect(sockfd,(sockaddr*)&serverAddr,sizeof(sockaddr)); if(ret < 0) { printf("[connection NG, will retry in 5 seconds!]\n"); print_socket_error(); sleep(5000); } }while(ret !=0); printf("[connection OK , server at [%s:%d]]\n", "127.0.0.1", 6789); char buffer[1024]; int counter = 0; while(test_cli[counter]) { sleep(1000); sprintf(buffer, "%s", test_cli[counter]); printf("<sender> [%s].\n", buffer); if(send(sockfd, buffer, sizeof(buffer), 0)<0) { print_socket_error(); } counter++; } ASSERT(0 == close(sockfd)); #ifdef WINDOWS ASSERT(0 == WSACleanup()); #endif return 0; }
int tcp_task(int argc, char ** argv) { int ret = 0; SOCKET sockfd; #ifdef WINDOWS WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD( 2, 0 ); ret = WSAStartup( wVersionRequested, &wsaData ); if( 0 != ret ) { print_socket_error(); return -1; } if (LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 ) { WSACleanup( ); return -1; } #endif sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd <= 0) { print_socket_error(); return -1; } const char * ip = NULL; if(argc == 2) { ip = argv[1]; } else { ip = "127.0.0.1"; } sockaddr_in serverAddr; memset(&serverAddr,0,sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = inet_addr(ip); serverAddr.sin_port = htons(6789); printf("[connecting to %s:%d ......]\n", ip, 6789); ret = connect(sockfd,(sockaddr*)&serverAddr,sizeof(sockaddr)); if(ret < 0) { print_socket_error(); printf("[connection abort!]\n"); close(sockfd); return -1; } printf("[connection OK , server at [%s:%d]]\n", ip, 6789); pthread_t tid; pthread_create(&tid, NULL, cli_rsp, (void*)sockfd); char buffer[1024]; while(1) { memset(buffer, 0, sizeof(buffer)); #ifdef WINDOWS if(gets(buffer)) #else if(fgets(buffer, sizeof(buffer), stdin)) #endif { if(send(sockfd, buffer, sizeof(buffer), 0) < 0) { print_socket_error(); /* lost connection to the server? */ printf("[connection abort!]\n"); break; } } printf(">>"); } close(sockfd); #ifdef WINDOWS WSACleanup(); #endif return 0; }
void SCBus::comm_thread() { setup_socket(); set_wall_time_us(0); // Until we hear anything from Android sc_time_us = 0; double target_time_us = SCBUS_QUANTUM_US; // How far we go in the next quantum struct sockaddr from; int fromlen; SCBusPacket packet; while (true) { // Quantum step for SC double delta_sc_time_us = target_time_us - sc_time_us; if (delta_sc_time_us > 0) { // Let the simulator proceed for the next quantum wait(delta_sc_time_us, SC_US); sc_time_us = target_time_us; } // Catch-up with wall clock double slack = target_time_us - get_wall_time_us(); //cout << "slack=" << slack << "us" << endl; if (slack < 0) { cerr << "SCBus: SystemC is too slow [slack=" << slack << "us]" << endl; } else { // TODO: we might want to move this after processing the packets (?) // Spinning to wait for wall-time to catch up while (get_wall_time_us() < target_time_us) {} } // Process incoming messages int res; double latest = -1; do { fromlen = sizeof(from); res = recvfrom(udp_socket, (char*)&(packet), sizeof(SCBusPacket), 0, &from, &fromlen); if (res > 0) { // Sanity check if (memcmp(MAGIC_A2SC, (const void*)&(packet.header.magic), sizeof(((SCBusPacketHeader *)0)->magic))) { cerr << "SCBus: invalid header, dropping packet" << endl; } else { // search for max timestamp latest = max(double(packet.header.timestamp), latest); // get data for (int i = 0; i < packet.header.length; i++) { rx_fifo.push(packet.data[i]); } // update rx_avail will be taken care of during the next clock cycle (by receive_data()) } } else if (WSAGetLastError() != WSAEWOULDBLOCK) { print_socket_error("SCBus: packet receive error: "); } } while (res > 0); // time adjustments // if we received any packet // if this is the first ever, set wall_time_us and sc_time_us to this timestamp // otherwise, set wall_time_us only to the new timestamp (if diff is beyond the sync resolution) // else (no packets received): do not touch xxx_time_us // always set the new target to the new wall_time_us+SCBUS_QUANTUM_US // TODO: we might change the last step to adapt better // (e.g. use the min(wall_time_us, sc_time_us) + SCBUS_QUANTUM_US if (latest > 0) { if (!ext_synced) { cout << "SCBus: Received external timesync, set local time to: " << latest << " us" << endl; set_wall_time_us(latest); sc_time_us = latest; ext_synced = true; } else { double local = get_wall_time_us(); if (fabs(local-latest) > SCBUS_QUANTUM_US) { cout << "SCBus: Adjusting timesync to: " << latest << " us (dT=" << (local-latest) << " us)" << endl; set_wall_time_us(latest); } } } target_time_us = get_wall_time_us() + SCBUS_QUANTUM_US; // Send outgoing messages if (!tx_fifo.empty()) { int i = 0; while (!tx_fifo.empty()) { packet.data[i++] = tx_fifo.front(); tx_fifo.pop(); if (i > SCBUS_PACKET_MAX_DATA_LENGTH) { // We will continue from here next time break; } } cout << endl; packet.header.length = i; packet.header.cid = 0; packet.header.timestamp = get_wall_time_us(); memcpy(&(packet.header.magic), MAGIC_SC2A, sizeof(((SCBusPacketHeader *)0)->magic)); if (sendto(udp_socket, (char*)&packet, sizeof(SCBusPacketHeader)+packet.header.length, 0, &from, fromlen) == SOCKET_ERROR) { print_socket_error("SCBus: packet send error: "); } } } }