/* Sends a ServerGreeting message to the Control-Client after * the TCP connection has been established. */ static int send_greeting(uint8_t mode_mask, struct client_info *client) { int socket = client->socket; int i; ServerGreeting greet; memset(&greet, 0, sizeof(greet)); greet.Modes = authmode & mode_mask; for (i = 0; i < 16; i++) greet.Challenge[i] = rand() % 16; for (i = 0; i < 16; i++) greet.Salt[i] = rand() % 16; greet.Count = (1 << 12); int rv = send(socket, &greet, sizeof(greet), 0); if (rv < 0) { fprintf(stderr, "[%s] ", inet_ntoa(client->addr.sin_addr)); perror("Failed to send ServerGreeting message"); cleanup_client(client); } else { printf("Sent ServerGreeting message to %s. Result %d\n", inet_ntoa(client->addr.sin_addr), rv); } return rv; }
void services_die(const char *msg, int rboot) { ilog(L_NOTICE, "Dying: %s", msg); cleanup_channel_modes(); cleanup_conf(); #ifdef HAVE_RUBY cleanup_ruby(); #endif cleanup_db(); cleanup_modules(); EVP_cleanup(); send_queued_all(); exit_client(&me, &me, "Services shutting down"); send_queued_all(); if(me.uplink != NULL) MyFree(me.uplink->server); cleanup_client(); cleanup_channel(); cleanup_interface(); cleanup_mqueue(); cleanup_tor(); unregister_callback(iorecv_cb); unregister_callback(connected_cb); unregister_callback(iosend_cb); libio_cleanup(); exit(rboot); }
static void __exit dk_module_exit(void) { #ifdef DK_DEBUG printk("DK::Module exit \n"); #endif // DK_DEBUG #if defined(OWL_PB42) || defined(PYTHON_EMU) bus_module_exit(); #endif cleanup_client(); dk_dev_exit(); return; }
/* After a ServerGreeting the Control-Client should respond with a * SetUpResponse. This function treats this message */ static int receive_greet_response(struct client_info *client) { int socket = client->socket; SetUpResponse resp; memset(&resp, 0, sizeof(resp)); int rv = recv(socket, &resp, sizeof(resp), 0); if (rv <= 0) { fprintf(stderr, "[%s] ", inet_ntoa(client->addr.sin_addr)); perror("Failed to receive SetUpResponse"); cleanup_client(client); } else { printf("Received SetUpResponse message from %s with mode %d. Result %d\n", inet_ntoa(client->addr.sin_addr), resp.Mode, rv); } return rv; }
/* Sent a ServerStart message to the Control-Client to end * the TWAMP-Control session establishment phase */ static int send_start_serv(struct client_info *client, TWAMPTimestamp StartTime) { int socket = client->socket; ServerStart msg; memset(&msg, 0, sizeof(msg)); msg.Accept = kOK; msg.StartTime = StartTime; int rv = send(socket, &msg, sizeof(msg), 0); if (rv <= 0) { fprintf(stderr, "[%s] ", inet_ntoa(client->addr.sin_addr)); perror("Failed to send ServerStart message"); cleanup_client(client); } else { client->status = kConfigured; printf("ServerStart message sent to %s\n", inet_ntoa(client->addr.sin_addr)); } return rv; }
static INT32 __init dk_module_init(void) { INT32 error; #if defined(PYTHON_EMU) UINT32 *addr; #endif #ifndef OCTEON INT32 ap83_81=0; #endif #ifdef DK_DEBUG printk("DK::Module init \n"); #endif // DK_DEBUG #ifndef OWL_PB42 get_chip_id(0,CHIP_ID_LOCATION,4,&ap83_81); // for getting the chip rev_id; to differentiate between PB and AP printk("CHIP REV ID: %x\n",ap83_81); #endif #if defined(PYTHON_EMU) addr = (UINT32 *)(MERLIN_PCI_COMMAND_REG_ADDRESS); printk("Writing value 0x6 to Merlin PCI command register\n"); writel(0x6,addr); // enabling ddr and dma of Merlin if((ap83_81==0x100)||(ap83_81==0x1100)||(ap83_81==0x101)||(ap83_81==0x1101) ||(ap83_81==0x2120)||(ap83_81==0x1120)||(ap83_81==0x0120) // Wasp 1.0 package C, B and A ||(ap83_81==0x2121)||(ap83_81==0x1121)||(ap83_81==0x0121) // Wasp 1.1 package C, B and A ||(ap83_81==0x2122)||(ap83_81==0x1122)||(ap83_81==0x0122) // Wasp 1.2 package C, B and A ||(ap83_81==0x2123)||(ap83_81==0x1123)||(ap83_81==0x0123)){ // Wasp 1.3 package C, B and A addr = (UINT32 *)(VIRIAN_BASE_ADDRESS); writel(readl(addr)& 0xfffeffff,addr); printk("Resetting bit 16 of VIRIAN register 0xb80f0000\n"); }else{ addr = (UINT32 *)(0xb80f001c ); writel(readl(addr)& 0xfffeffff,addr); printk("Resetting bit 16 of Python register 0xb80f001c \n"); } #endif error = dk_dev_init(); if (error < 0) { printk("DK::Cannot register device \n"); return error; } init_client(); #ifdef AP83 if (init_wmac_device()){ // enabling the wmac ; setting the handle for applications printk("Error in initializing wmac \n"); return error; } printk("IRSHAD!!!!!! \n"); #ifndef WASP_OSPREY printk("IRSHAD2 returning!!!!!! \n"); return 0; #endif #endif #if defined(OWL_PB42) || defined(PYTHON_EMU) printk("IRSHAD2!!!!!! \n"); error = bus_module_init(); #endif #if !defined(OWL_PB42) && !defined(PYTHON_EMU) if (error < 0) { cleanup_client(); dk_dev_exit(); printk("DK::Cannot locate device. Reset the machine \n"); return error; } #endif return 0; }
int main(int argc, char *argv[]) { char *progname = NULL; srand(time(NULL)); /* Obtain the program name without the full path */ progname = (progname = strrchr(argv[0], '/')) ? progname + 1 : *argv; /* Sanity check */ if (getuid() == 0) { fprintf(stderr, "%s should not be run as root\n", progname); exit(EXIT_FAILURE); } /* Parse options */ if (parse_options(progname, argc, argv)) { usage(progname); exit(EXIT_FAILURE); } /* Obtain start server time in TWAMP format */ TWAMPTimestamp StartTime = get_timestamp(); int listenfd; listenfd = socket(AF_INET, SOCK_STREAM, 0); if (listenfd < 0) { perror("Error opening socket"); exit(EXIT_FAILURE); } /* Set Server address and bind on the TWAMP port */ struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(SERVER_PORT); used_sockets++; if (bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) < 0) { perror("Error on binding"); exit(EXIT_FAILURE); } /* Start listening on the TWAMP port for new TWAMP-Control connections */ if (listen(listenfd, MAX_CLIENTS)) { perror("Error on listen"); exit(EXIT_FAILURE); } FD_ZERO(&read_fds); FD_SET(listenfd, &read_fds); fd_max = listenfd; struct client_info clients[MAX_CLIENTS]; memset(clients, 0, MAX_CLIENTS * sizeof(struct client_info)); int newsockfd; struct sockaddr_in client_addr; fd_set tmp_fds; FD_ZERO(&tmp_fds); int rv; while (1) { tmp_fds = read_fds; if (select(fd_max + 1, &tmp_fds, NULL, NULL, NULL) < 0) { perror("Error in select"); close(listenfd); exit(EXIT_FAILURE); } /* If an event happened on the listenfd, then a new TWAMP-Control * connection is received */ if (FD_ISSET(listenfd, &tmp_fds)) { uint32_t client_len = sizeof(client_addr); if ((newsockfd = accept(listenfd, (struct sockaddr *)&client_addr, &client_len)) < 0) { perror("Error in accept"); } else { /* Add a new client if there are any slots available */ int pos = find_empty_client(clients, MAX_CLIENTS); uint8_t mode_mask = 0; if (pos != -1) { clients[pos].status = kConnected; clients[pos].socket = newsockfd; clients[pos].addr = client_addr; clients[pos].sess_no = 0; used_sockets++; FD_SET(newsockfd, &read_fds); if (newsockfd > fd_max) fd_max = newsockfd; mode_mask = 0xFF; } rv = send_greeting(mode_mask, &clients[pos]); } } /* Receives other packets from the established TWAMP-Control sessions */ uint8_t buffer[4096]; int i, j; for (i = 0; i < MAX_CLIENTS; i++) /* It can only receive TWAMP-Control messages from Online clients */ if (clients[i].status != kOffline) if (FD_ISSET(clients[i].socket, &tmp_fds)) { switch (clients[i].status) { case kConnected: /* If a TCP session has been established and a * ServerGreeting has been sent, wait for the * SetUpResponse and finish the TWAMP-Control setup */ rv = receive_greet_response(&clients[i]); if (rv > 0) { rv = send_start_serv(&clients[i], StartTime); } break; case kConfigured: /* Reset the buffer to receive a new message */ memset(buffer, 0, 4096); rv = recv(clients[i].socket, buffer, 4096, 0); if (rv <= 0) { cleanup_client(&clients[i]); break; } /* Check the message received: It can only be * StartSessions or RequestTWSession */ switch (buffer[0]) { case kStartSessions: rv = receive_start_sessions(&clients[i], (StartSessions *)buffer); break; case kRequestTWSession: rv = receive_request_session(&clients[i], (RequestSession *)buffer); break; default: break; } if (rv <= 0) cleanup_client(&clients[i]); break; case kTesting: /* In this state can only receive a StopSessions msg */ memset(buffer, 0, 4096); rv = recv(clients[i].socket, buffer, 4096, 0); if (rv <= 0) { cleanup_client(&clients[i]); break; } if (buffer[0] == kStopSessions) { rv = receive_stop_sessions(&clients[i], (StopSessions *) buffer); } break; default: break; } } /* Check for TWAMP-Test packets */ for (i = 0; i < MAX_CLIENTS; i++) { struct timeval current; gettimeofday(¤t, NULL); if (clients[i].status == kTesting) { uint8_t has_active_test_sessions = 0; for (j = 0; j < clients[i].sess_no; j++) { rv = get_actual_shutdown(¤t, &clients[i].shutdown_time, &clients[i].sessions[j].req.Timeout); if (rv > 0) { has_active_test_sessions = 1; if (FD_ISSET(clients[i].sessions[j].socket, &tmp_fds)) { rv = receive_test_message(&clients[i], j); } } else { FD_CLR(clients[i].sessions[j].socket, &read_fds); close(clients[i].sessions[j].socket); used_sockets--; clients[i].sessions[j].socket = -1; } } if (!has_active_test_sessions) { memset(&clients[i].shutdown_time, 0, sizeof(clients[i].shutdown_time)); clients[i].sess_no = 0; clients[i].status = kConfigured; } } } } return 0; }
/* * Authors: Zhi Li * Purpose: It terminates the program with the error message * at client side * * Expected input: It takes the error string as input * * Implementation details: * It prints error message, frees the memory, close * the socket at client and exit the program */ void client_exit_with_error(char *msg) { perror(msg); cleanup_client(); exit(1); }