void be_dns_read_cb(struct ev_loop *loop, struct ev_io *w, int revents) { struct client *cli = ((struct client*) (((char*)w) - offsetof(struct client, ev_read))); ev_timer_stop(EV_A_ &cli->ev_tout); mark_time(cli, READ_BE); char dns_buffer[4096]; int ret = recv(cli->fd_be, dns_buffer, sizeof(dns_buffer), 0); if (ret <= 0) { ev_io_stop(EV_A_ w); delete_client(cli); return; } dns_buffer[ret] = 0; printf("recv: %s\n", dns_buffer); if (ret > 0) { cli->status = 200; } else { cli->status = 404; } ev_io_stop(EV_A_ w); ev_io_init(&cli->ev_write, cli->write_cb, cli->fd, EV_WRITE); ev_io_start(loop, &cli->ev_write); }
void cmd_user(t_server *serv, int fd, t_message *msg) { if (serv->client[fd].registered) reply_response(serv, fd, ERR_ALREADYREGISTRED, 0); else if (!(serv->client[fd].keyed) || !(serv->client[fd].nick[0])) delete_client(serv, fd); else if (msg->param_count < 4) reply_response(serv, fd, ERR_NEEDMOREPARAMS, 0); else { serv->client[fd].username = strdup(msg->param[0]); serv->client[fd].realname = strdup(msg->param[3]); serv->client[fd].registered = 1; reply_response(serv, fd, RPL_WELCOME, 0); reply_response(serv, fd, RPL_YOURHOST, 0); reply_response(serv, fd, RPL_CREATED, 0); reply_response(serv, fd, RPL_MYINFO, 0); reply_response(serv, fd, RPL_LUSERCLIENT, 0); reply_response(serv, fd, RPL_LUSEROP, 0); reply_response(serv, fd, RPL_LUSERUNKNOWN, 0); reply_response(serv, fd, RPL_LUSERCHANNELS, 0); reply_response(serv, fd, RPL_LUSERME, 0); printf("Client registred %s on socket %d\n", serv->client[fd].nick, fd); } }
/* clear SIGCHLD flag and adjust context if needed */ static void wait_children( struct server_ctx* ctx, int options ) { int status, n = 0; pid_t pid; assert( ctx ); if (0 == g_childexit) { TRACE( (void)tmfputs ("No children exited since last check\n", g_flog) ); return; } g_childexit = 0; TRACE( (void)tmfputs ("Waiting on exited children\n", g_flog) ); while( 0 < (pid = waitpid( -1, &status, options )) ) { TRACE( (void)tmfprintf( g_flog, "Client [%d] has exited.\n", pid) ); delete_client( ctx, pid ); ++n; } if( (-1 == pid) && ( ECHILD != errno ) ) { mperror(g_flog, errno, "%s: waitpid", __func__); } if (n > 0) { TRACE( (void)tmfprintf (g_flog, "Cleaned up %d children, " "%ld still running\n", n, (long)(ctx->clmax - ctx->clfree)) ); } return; }
static void xmms_visualization_client_shutdown (xmms_visualization_t *vis, int32_t id, xmms_error_t *err) { g_mutex_lock (&vis->clientlock); delete_client (id); g_mutex_unlock (&vis->clientlock); }
/** * Allows to create a new client */ inline void create_client() { //Delete the previous client if any delete_client(); //Create a new client m_client = new generic_client(m_uri, bind(&client_manager::notify_new_msg, this, _1), bind(&client_manager::notify_conn_closed, this), NULL); }
/** * The basic destructor class */ virtual ~client_manager() { //Destroy the translation jobs sending thread if (m_sending_thread_ptr != NULL) { delete m_sending_thread_ptr; } //Destroy the client delete_client(); }
int virtual_stream_disconnect(Channel * c, char * token, char * id) { int err = 0; StreamClient * client = find_client(id, c); if (client == NULL) err = errno; if (!err) delete_client(client); return err == 0 ? 0 : -1; }
static thread_func_t tcp_register_client_thread(void* s) { char buffer[NL_BUFFER_SIZE+1]; server_game* g; client* c = (client*)s; void* remember = NULL; /* Then, we add the client to the array, such that he will * be informed whenever a new server arrives or leaves. */ lock_w(&client_array); rAdd(&client_array, (void*)c); lock_uw(&client_array); /* We now give him the preliminary information, list all the * servers already present. */ lock_r(&game_server_array); nl_tcp_sndInt(game_server_array.length, c->socket); if((g = (server_game*)rGet(&game_server_array, FIRST, &remember)) != NULL) { do { nl_tcp_snd(g->name, c->socket); /* Send the server's name */ nl_snd_addr(g->addr, c->socket); /* And now it's address. */ } while((g = (server_game*)rGet(&game_server_array, NEXT, &remember)) != NULL); } lock_ur(&game_server_array); /* * Done. We can now communicate. * Actually we do not have much to say to each other. * This is just a little trick to just wait, and then the * client leaves, this will exit. */ nl_tcp_rcv(buffer, c->socket); /* Close the client socket * If it has been closed from extern, don't try to close it again! */ close(c->socket); lock_w(&client_array); rDelete(&client_array, (void*)c, NULL); delete_client((void*)c); lock_uw(&client_array); LOG("Client thread terminated."); thread_exit(); }
int queue_get_udp_data(t_conf *conf, t_simple_list *client) { char buffer[MAX_REQ_LEN + 1]; struct dns_hdr *hdr; int len; t_list *queue; buffer[MAX_REQ_LEN] = 0; len = read(conf->sd_udp, buffer, MAX_REQ_LEN); hdr = (struct dns_hdr *) buffer; for (; client; client = client->next) { for (queue = client->queue; queue; queue = queue->next) { if ((queue->status == SENT) && (queue->peer.id == hdr->id)) { if (hdr->rcode) { if (hdr->rcode == RCODE_NAME_ERR) { /* Reply already sent and acked by server Bug ? */ if (client->control.cumul_errors++ > MAX_CLIENT_ERROR) { DPRINTF(1, "Too many packet lost by server. Reseting connection ...\n"); return (delete_client(conf, client)); } } DPRINTF(2, "Connection reject code %d id = 0x%x (%s)\n", hdr->rcode, hdr->id, (hdr->rcode == RCODE_REFUSED) ? "Connection Lost" : /* state not found */ (hdr->rcode == RCODE_NAME_ERR) ? "Query not found or already done" : "" /* already replied */ ); return (0); } DPRINTF(2, "Received [%d] id=0x%x\n", queue->info.num_seq, hdr->id); client->control.cumul_errors = 0; memcpy(queue->data, buffer, len); queue->data[len] = 0; queue->status = RECEIVED; queue->len = len; queue_flush(conf, client); return (0); } } } DPRINTF(2, "received reply for unknow request 0x%x \n", hdr->id); return (0); }
void Store::release_client() { // If this thread already has a client delete it and remove it from // thread-local storage. TRC_DEBUG("Looking to release thread-local client"); Client* client = (Client*)pthread_getspecific(_thread_local); if (client != NULL) { TRC_DEBUG("Found thread-local client - destroying"); delete_client(client); client = NULL; pthread_setspecific(_thread_local, NULL); } }
int extract_data(t_conf *conf, t_simple_list *client, t_list *queue) { char buffer[MAX_REQ_LEN - DNS_HDR_SIZE - REQ_HDR_SIZE]; char name[MAX_REQ_LEN - DNS_HDR_SIZE - REQ_HDR_SIZE]; struct rr_hdr *reply; t_packet *packet; int len; uint16_t seq_tmp; if (!(reply = get_reply(queue->data, queue->len))) return (-1); dns_simple_decode(JUMP_RR_HDR(reply), buffer, DECODED_LEN((queue->len - (int) (JUMP_RR_HDR(reply) - queue->data)) + 1)); DPRINTF(3, "dns_decode [%d] = %s\n",queue->info.num_seq, buffer); if ((len = base64_decode((unsigned char *)name, buffer))) { packet = (t_packet *)name; seq_tmp = ntohs(packet->seq); packet->seq = seq_tmp; queue->info.num_seq = packet->seq; if (packet->type == DESAUTH) { DPRINTF(1, "Received desauth\n"); delete_client(conf, client); return (-1); } if (queue->peer.type == DATA) client->control.data_pending--; else client->control.nop_pending--; if ((packet->type & DATA) != DATA) return (0); if ((len - PACKET_LEN) < 0) { MYERROR("bug ! reply len = %d\n", len); printf("encode = %s -> %s\n ... dumping reply and calling while(1); \n", (char *)(JUMP_RR_HDR(reply)), (char *)buffer); memdump(queue->data, queue->len); while(1); } DPRINTF(2, "Write [%d] %d\n", packet->seq, len - PACKET_LEN); write(client->sd_tcp, &name[PACKET_LEN], len - PACKET_LEN); } return (0); }
static void command_disconnect(char * token, Channel * c) { char id[256]; StreamClient * client = NULL; int err = 0; json_read_string(&c->inp, id, sizeof(id)); if (read_stream(&c->inp) != 0) exception(ERR_JSON_SYNTAX); if (read_stream(&c->inp) != MARKER_EOM) exception(ERR_JSON_SYNTAX); client = find_client(id, c); if (client == NULL) err = errno; if (!err) delete_client(client); write_stringz(&c->out, "R"); write_stringz(&c->out, token); write_errno(&c->out, err); write_stream(&c->out, MARKER_EOM); }
// Warning Zombie ahead void delete_zombie(t_conf *conf) { t_simple_list *client; t_simple_list *tmp; struct timeval tv; struct timezone tz; if (!(gettimeofday(&tv, &tz))) for (client = conf->client; client; client = tmp) { tmp = client->next; if (tv.tv_sec > client->control.tv.tv_sec) { if (client->sd_tcp != -1) close(client->sd_tcp); delete_client(conf, client); } } }
static void channel_close_listener(Channel * c) { LINK * l = NULL; for (l = clients.next; l != &clients;) { StreamClient * client = all2client(l); l = l->next; if (client->channel == c) { trace(LOG_ALWAYS, "Stream is left connected by client: VS%d", client->stream->id); delete_client(client); } } for (l = subscriptions.next; l != &subscriptions;) { Subscription * h = all2subscription(l); l = l->next; if (h->channel == c) { delete_subscription(h); } } }
gboolean write_udp (xmmsc_vis_udp_t *t, xmms_vis_client_t *c, int32_t id, struct timeval *time, int channels, int size, short *buf, int socket) { xmmsc_vis_udp_data_t packet_d; xmmsc_vischunk_t *__unaligned_dest; short res; int offset; char* packet; /* first check if the client is still there */ if (t->grace == 0) { delete_client (id); return FALSE; } if (t->socket == 0) { return FALSE; } packet = packet_init_data (&packet_d); t->grace--; XMMSC_VIS_UNALIGNED_WRITE (packet_d.__unaligned_grace, htons (t->grace), uint16_t); __unaligned_dest = packet_d.__unaligned_data; XMMSC_VIS_UNALIGNED_WRITE (&__unaligned_dest->timestamp[0], (int32_t)htonl (time->tv_sec), int32_t); XMMSC_VIS_UNALIGNED_WRITE (&__unaligned_dest->timestamp[1], (int32_t)htonl (time->tv_usec), int32_t); XMMSC_VIS_UNALIGNED_WRITE (&__unaligned_dest->format, (uint16_t)htons (c->format), uint16_t); res = fill_buffer (__unaligned_dest->data, &c->prop, channels, size, buf); XMMSC_VIS_UNALIGNED_WRITE (&__unaligned_dest->size, (uint16_t)htons (res), uint16_t); offset = ((char*)&__unaligned_dest->data - (char*)__unaligned_dest); sendto (socket, packet, XMMS_VISPACKET_UDP_OFFSET + offset + res * sizeof (int16_t), 0, (struct sockaddr *)&t->addr, sizeof (t->addr)); free (packet); return TRUE; }
static void xmms_visualization_destroy (xmms_object_t *object) { XMMS_DBG ("Deactivating visualization object."); xmms_object_unref (vis->output); /* TODO: assure that the xform is already dead! */ g_mutex_clear (&vis->clientlock); xmms_log_debug ("starting cleanup of %d vis clients", vis->clientc); for (; vis->clientc > 0; --vis->clientc) { delete_client (vis->clientc - 1); } if (xmms_socket_valid (vis->socket)) { /* it seems there is no way to remove the watch */ g_io_channel_shutdown (vis->socketio, FALSE, NULL); xmms_socket_close (vis->socket); } xmms_visualization_unregister_ipc_commands (); }
static void __exit alsa_seq_dummy_exit(void) { delete_client(); }
static void free_clients(t_server *infos) { while (infos->network.client != NULL) infos->network.client = delete_client(infos, infos->network.client->data); }
/*main function*/ int main(int argc, char **argv) { int opt; enum OPER_TYPE type; char path[FILE_NAME_LEN] = {0}; char output_path[FILE_NAME_LEN] = {0}; int delete_job; Client *c= NULL; if(argc<2) return -1; struct option long_options[] = { {"Simulate", 1, NULL, 'S'}, {"quit", 0, NULL, 'q'}, {"backup", 1, NULL, 'b'}, {"delete", 1, NULL, 'd'}, {"restore", 1, NULL, 'r'}, {"list", 0, NULL, 'l'}, {"pipeline", 0, NULL, 'p'}, {"output", 1, NULL, 'o'}, {"cloud", 0, NULL, 'c'}, {"rewrite", 1, NULL, 'R'}, /*CFL*/ {"CFL", 1, NULL, 'C'}, {"seg_reuse", 1, NULL, 's'}, /*capping*/ {"cap_size", 1, NULL, 'a'}, {"cap_count", 1, NULL, 'e'}, /*perferect rewrite*/ {"ref_threshold", 1, NULL, 'u'}, {"overhead", 1, NULL, 'O'}, {"test", 1, NULL, 't'}, {"server", 1, NULL, 'H'}, {"help", 0, NULL, 'h'}, {NULL, 0, NULL, 0} }; while((opt=getopt_long(argc, argv, "S:qb:d:r:lpo:cR:C:s:a:e:u:O:t:H:h", long_options, NULL))!=-1){ switch(opt){ case 'S': type=type_simulate; SIMULATE = true; strncpy(path, optarg, strlen(optarg)); break; case 'q': type=type_quit; break; case 'b': type=type_backup; strncpy(path,optarg,strlen(optarg)); break; case 'd': type=type_delete; sscanf(optarg, "%d", &delete_job); break; case 'r': type=type_restore; strncpy(path,optarg,strlen(optarg)); break; case 'R': if (strcmp(optarg, "CFL") == 0) REWRITE = CFL_REWRITE; else if (strcmp(optarg, "CAP") == 0) REWRITE = CAPPING_REWRITE; else if (strcmp(optarg, "PER") == 0) REWRITE = PERFECT_REWRITE; break; case 'C': sscanf(optarg, "%f", &LWM); break; case 's': sscanf(optarg, "%f", &default_container_reuse); break; case 'a': sscanf(optarg, "%lld", &buffer_capacity); break; case 'e': sscanf(optarg, "%d", &max_refer_count); break; case 'u': sscanf(optarg, "%f", &segment_usage_threshold); break; case 'O': OVERHEAD = true; sscanf(optarg, "%f", &bandwidth); break; case 't': type=type_test; strncpy(path,optarg,strlen(optarg)); break; case 'h': type=type_help; break; break; case 'H': memset(SERVER_IP,0,30); strncpy(SERVER_IP,optarg,strlen(optarg)); break; case 'l': type=type_list; break; case 'p': G_PIPELINE = true; break; case 'o': OUTPUT_RESULT = true; strncpy(output_path, optarg, strlen(optarg)); break; case 'c': CLOUD = true; break; default: printf("Your command is wrong \n"); type=type_help; break; } } if(type==type_help){ usage(argv[0]); return 0; } c = create_client(); if (type == type_simulate) { simulata_backup(c, path, output_path); free(c); return 0; }else if(OVERHEAD == true) { backup_overhead(c, path, output_path); free(c); return 0; } if ((c->fd = bnet_connect(SERVER_IP, SERVER_PORT)) == -1){ err_msg1("Connection rejected!"); return 0; } if (G_PIPELINE) { c->recipe_fd = bnet_connect(SERVER_IP, SERVER_OTH_PORT); if (c->recipe_fd == -1) { err_msg1("Connection rejected!"); return 0; } } if(type==type_test){ test_data(c->fd, path); return 0; } switch(type){ case type_login: login(c); break; case type_logout: logout(c); break; case type_quit: quit_client(c); break; case type_backup: if (G_PIPELINE) pipeline_backup(c, path, output_path); else backup_client(c, path, output_path); break; case type_restore: restore_client(c, path, output_path); break; case type_delete: delete_client(c, delete_job); break; case type_list: list_client(c); break; default: printf("Your command is wrong \n"); break; } free_client(c); return 0; }
int do_server(t_conf *conf) { fd_set rfds; int retval; int max_fd; t_simple_list *client; t_simple_list *tmp; struct timeval tv; struct timezone tz; #ifdef DEBUG char buffer[MINI_BUFF]; #endif while (1) { max_fd = prepare_select(conf, &rfds, &tv); retval = select(max_fd + 1 , &rfds, NULL, NULL, &tv); queue_flush_expired_data(conf); if (retval == -1) { perror(""); MYERROR("select"); return (-1); } if (!retval) { if (gettimeofday(&tv, &tz)) { MYERROR("Time Error"); return (-1); } delete_zombie(conf); continue; } #ifdef DEBUG if ((conf->foreground) && (FD_ISSET(0, &rfds))) { read(0, buffer, MINI_BUFF); queue_dump(conf->client); continue; } #endif if (FD_ISSET(conf->sd_udp, &rfds)) get_incoming_request(conf); else { for (client = conf->client; client; client = tmp) { tmp = client->next; if ((client->sd_tcp != -1) && (FD_ISSET(client->sd_tcp, &rfds))) { if (queue_read_tcp(conf, client)) { if (client->sd_tcp != -1) close(client->sd_tcp); delete_client(conf, client); } } } } delete_zombie(conf); } }