int connected_client(void *data) { thread_data *input_data = (thread_data *) data; TCPsocket socket = *input_data->socket; int my_slot = input_data->slot; int i = 0, connection_state = -1; char handshake[10]; char *client_data[10] = { NULL }; char msg[10]; int x = 0, y = 0, id = 0; double angle = 0; sprintf(handshake, "#|%d|#", *input_data->id); send_data(handshake, socket, "#|%d|#"); SDL_Delay(10); node * tmp = input_data->root; for (i = 0; i < 11; i++) { if (tmp != NULL) { sprintf(msg, "*%d|%d|%d|%d*", tmp->astroid.id, tmp->astroid.x, tmp->astroid.y, tmp->astroid.velocity); send_data(msg, socket, "*%d|%d|%d|%d*"); printf("Asteroid #%d: %s\n", tmp->astroid.id, msg); tmp = tmp->next; SDL_Delay(10); } } printf("Server: Start receiving data\n"); ready = 1; while (1) { //SDL_Delay(10); connection_state = read_data(msg, socket, &id, &x, &y, &angle, "#%d|%d|%d|%d|%d|%d#"); if (connection_state == -1) { if (forward_data(msg, id, "#%d|%d|%d|%lf#") == -1) { //return 0; } } else if (read_bullet_data(msg, "?%d|%d|%d|%d|%d?") == -1) { if (forward_data(msg, id, "#%d|%d|%d|%lf#") == -1) { //return 0; } } else if (read_astroid_data(msg, "*%d|%d|%d*", input_data->root) == -1) { printf("YO!\n"); if (forward_data(msg, id, "#%d|%d|%d|%lf#") == -1) { //return 0; } } else if (connection_state == 0) { // Connection is lost! Kill thread! count_thread[my_slot] = 0; SDLNet_TCP_Close(socket); *input_data->id -= 1; printf("Server: %d", *input_data->id); return 0; } } return 0; }
extern Success send_command (Stream s, Connection_Id id) { SEND_HEADER (A_DATA); if (read_reply (s) == NONE) return ERROR; if (forward_data (0, s, TRUE) == OK && forward_data (s, 1, FALSE) == OK) return OK; else return ERROR; }
int write_data_sch(struct object_descriptor *descr, int offset, int size, const void *buffer) { struct write_packet p; int res, wsize; res = io_write(descr, offset, size, buffer); if (res >= 0) { if (offset != -1) descr->meta.size = descr->meta.size > (unsigned int)(offset + size) ? descr->meta.size : offset + size; else descr->meta.size += size; write_metadata(descr); } if (res < 0) { p.obj = descr->id; p.offset = offset; p.size = size; for (unsigned int i = 0; i < peers.size(); i++) { pthread_mutex_lock(&peer_locks[i]); forward_data(peer_srv_socks[i], OP_WRITE, OS_MASTER, &p, sizeof(p), buffer, size); res = receive_reply(peer_srv_socks[i], &wsize, sizeof(wsize)); pthread_mutex_unlock(&peer_locks[i]); if (res >= 0) return wsize; } } return res; }
static int rtp_data_bts(struct osmo_fd *fd, unsigned int what) { char buf[4096]; struct sockaddr_in addr; struct mgcp_endpoint *endp; int rc, proto; endp = (struct mgcp_endpoint *) fd->data; rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf)); if (rc <= 0) return -1; proto = fd == &endp->bts_end.rtp ? PROTO_RTP : PROTO_RTCP; /* We have no idea who called us, maybe it is the BTS. */ /* it was the BTS... */ discover_bts(endp, proto, &addr); if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) { LOGP(DMGCP, LOGL_ERROR, "Data from wrong bts %s on 0x%x\n", inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); return -1; } if (endp->bts_end.rtp_port != addr.sin_port && endp->bts_end.rtcp_port != addr.sin_port) { LOGP(DMGCP, LOGL_ERROR, "Data from wrong bts source port %d on 0x%x\n", ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); return -1; } /* throw away the dummy message */ if (rc == 1 && buf[0] == DUMMY_LOAD) { LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n", ENDPOINT_NUMBER(endp)); return 0; } /* do this before the loop handling */ endp->bts_end.packets += 1; endp->bts_end.octets += rc; forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc); switch (endp->type) { case MGCP_RTP_DEFAULT: return send_to(endp, DEST_NETWORK, proto == PROTO_RTP, &addr, buf, rc); case MGCP_RTP_TRANSCODED: return send_transcoder(&endp->trans_bts, endp->cfg, proto == PROTO_RTP, buf, rc); } LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n", endp->type, ENDPOINT_NUMBER(endp)); return 0; }
void create_replica(int node, oid_t id) { struct create_packet pc; struct write_packet pw; struct object_descriptor obj; void *buffer; read_metadata(id, &obj); buffer = malloc(obj.meta.size); read_data(&obj, 0, obj.meta.size, buffer); pc.obj = id; pc.type = obj.meta.type; strncpy((char*)pc.name, (const char*)obj.meta.name, 256); pw.obj = id; pw.offset = 0; pw.size = obj.meta.size; lock_object(id); pthread_mutex_lock(&peer_locks[node]); forward_packet(peer_srv_socks[node], OP_CREATE, OS_SLAVE, &pc, sizeof(pc)); receive_reply(peer_srv_socks[node], NULL, 0); forward_data(peer_srv_socks[node], OP_WRITE, OS_SLAVE, &pw, sizeof(pw), buffer, pw.size); receive_reply(peer_srv_socks[node], NULL, 0); pthread_mutex_unlock(&peer_locks[node]); unlock_object(id); free(buffer); }
void reqc_write(int sock, struct write_packet *p, const void *buffer) { struct object_descriptor obj; int wsize; lock_object(p->obj); if (read_metadata(p->obj, &obj)) { reqm_write(sock, p, buffer, 1); return; } for (unsigned int i = 0; i < peers.size(); i++) { pthread_mutex_lock(&peer_locks[i]); forward_data(peer_srv_socks[i], OP_WRITE, OS_MASTER, p, sizeof(*p), buffer, p->size); wsize = receive_reply(peer_srv_socks[i], &wsize, sizeof(wsize)); pthread_mutex_unlock(&peer_locks[i]); if (wsize >= 0) { unlock_object(p->obj); send_reply(sock, &wsize, sizeof(wsize)); return; } } unlock_object(p->obj); send_error(sock, ENOENT); }
int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp, struct sockaddr_in *addr, char *buf, int rc) { struct mgcp_trunk_config *tcfg = endp->tcfg; struct mgcp_rtp_end *rtp_end; struct mgcp_rtp_state *rtp_state; int tap_idx; /* For loop toggle the destination and then dispatch. */ if (tcfg->audio_loop) dest = !dest; /* Loop based on the conn_mode, maybe undoing the above */ if (endp->conn_mode == MGCP_CONN_LOOPBACK) dest = !dest; if (dest == MGCP_DEST_NET) { rtp_end = &endp->net_end; rtp_state = &endp->bts_state; tap_idx = MGCP_TAP_NET_OUT; } else { rtp_end = &endp->bts_end; rtp_state = &endp->net_state; tap_idx = MGCP_TAP_BTS_OUT; } if (!rtp_end->output_enabled) rtp_end->dropped_packets += 1; else if (is_rtp) { int cont; int nbytes = 0; int len = rc; do { cont = endp->cfg->rtp_processing_cb(endp, rtp_end, buf, &len, RTP_BUF_SIZE); if (cont < 0) break; mgcp_patch_and_count(endp, rtp_state, rtp_end, addr, buf, len); forward_data(rtp_end->rtp.fd, &endp->taps[tap_idx], buf, len); rc = mgcp_udp_send(rtp_end->rtp.fd, &rtp_end->addr, rtp_end->rtp_port, buf, len); if (rc <= 0) return rc; nbytes += rc; len = cont; } while (len > 0); return nbytes; } else if (!tcfg->omit_rtcp) { return mgcp_udp_send(rtp_end->rtcp.fd, &rtp_end->addr, rtp_end->rtcp_port, buf, rc); } return 0; }
static int rtp_data_net(struct osmo_fd *fd, unsigned int what) { char buf[4096]; struct sockaddr_in addr; struct mgcp_endpoint *endp; int rc, proto; endp = (struct mgcp_endpoint *) fd->data; rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf)); if (rc <= 0) return -1; if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) { LOGP(DMGCP, LOGL_ERROR, "Endpoint 0x%x data from wrong address %s vs. ", ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr)); LOGPC(DMGCP, LOGL_ERROR, "%s\n", inet_ntoa(endp->net_end.addr)); return -1; } if (endp->net_end.rtp_port != addr.sin_port && endp->net_end.rtcp_port != addr.sin_port) { LOGP(DMGCP, LOGL_ERROR, "Data from wrong source port %d on 0x%x\n", ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); return -1; } /* throw away the dummy message */ if (rc == 1 && buf[0] == DUMMY_LOAD) { LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n", ENDPOINT_NUMBER(endp)); return 0; } proto = fd == &endp->net_end.rtp ? PROTO_RTP : PROTO_RTCP; endp->net_end.packets += 1; endp->net_end.octets += rc; forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc); switch (endp->type) { case MGCP_RTP_DEFAULT: return send_to(endp, DEST_BTS, proto == PROTO_RTP, &addr, buf, rc); case MGCP_RTP_TRANSCODED: return send_transcoder(&endp->trans_net, endp->cfg, proto == PROTO_RTP, buf, rc); } LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n", endp->type, ENDPOINT_NUMBER(endp)); return 0; }
static int send_to(struct mgcp_endpoint *endp, int dest, int is_rtp, struct sockaddr_in *addr, char *buf, int rc) { struct mgcp_trunk_config *tcfg = endp->tcfg; /* For loop toggle the destination and then dispatch. */ if (tcfg->audio_loop) dest = !dest; /* Loop based on the conn_mode, maybe undoing the above */ if (endp->conn_mode == MGCP_CONN_LOOPBACK) dest = !dest; if (dest == DEST_NETWORK) { if (is_rtp) { patch_and_count(endp, &endp->bts_state, endp->net_end.payload_type, addr, buf, rc); forward_data(endp->net_end.rtp.fd, &endp->taps[MGCP_TAP_NET_OUT], buf, rc); return udp_send(endp->net_end.rtp.fd, &endp->net_end.addr, endp->net_end.rtp_port, buf, rc); } else if (!tcfg->omit_rtcp) { return udp_send(endp->net_end.rtcp.fd, &endp->net_end.addr, endp->net_end.rtcp_port, buf, rc); } } else { if (is_rtp) { patch_and_count(endp, &endp->net_state, endp->bts_end.payload_type, addr, buf, rc); forward_data(endp->bts_end.rtp.fd, &endp->taps[MGCP_TAP_BTS_OUT], buf, rc); return udp_send(endp->bts_end.rtp.fd, &endp->bts_end.addr, endp->bts_end.rtp_port, buf, rc); } else if (!tcfg->omit_rtcp) { return udp_send(endp->bts_end.rtcp.fd, &endp->bts_end.addr, endp->bts_end.rtcp_port, buf, rc); } } return 0; }
int main(int argc, const char * argv[]) { /* Check arguments */ if(argc != 3) { printf("Usage: %s <FROM_PORT> <TO_PORT>\n", argv[0]); exit(-1); } forward_data(atoi(argv[1]), atoi(argv[2])); }
void ForwardSocket::connection_handle() { conn_receive(); /* Forward local socket data over the channel to the client */ forward_data(); if (_fd == -1) { struct CMD_ClosePort cmd; cmd.type = CMD_CLOSE_PORT; cmd.id = _id; printf("Send closeport %d\n", cmd.id); _parent->txfifo_in( (unsigned char*)&cmd, sizeof(cmd) ); } }
/* * Setup networking * Setup pty * * Forward */ int main(int argc, char** argv) { if (argc < 2) { printf("Usage: ./pty_forward <host> <port>\n"); return EXIT_FAILURE; } int socket_fd = socket(PF_INET, SOCK_STREAM, 0); if (socket_fd < 0) { perror("Failed to create network socket"); return EXIT_FAILURE; } struct hostent* host = gethostbyname(argv[1]); if (!host) { printf("Failed to get the hostent\n"); return EXIT_FAILURE; } struct sockaddr_in addr = { 0, }; addr.sin_family = PF_INET; addr.sin_port = htons(atoi(argv[2])); addr.sin_addr = *(struct in_addr*)host->h_addr; int result = connect(socket_fd, (struct sockaddr*)&addr, sizeof(addr)); if (result < 0) { perror("Connection failed"); return EXIT_FAILURE; } /* * PTY code */ int master_fd; int slave_fd; char slave_name[PATH_MAX]; result = openpty(&master_fd, &slave_fd, slave_name, NULL, NULL); if (result < 0) { perror("openpty()"); return EXIT_FAILURE; } printf("You can now use '%s' %d %d %d\n", slave_name, socket_fd, master_fd, slave_fd); forward_data(socket_fd, master_fd); return EXIT_SUCCESS; }
static gboolean client_handler(GIOChannel *channel, GIOCondition cond, gpointer user_data) { if (forward_data(cond, client_fd, device_fd) == FALSE) { g_printerr("Closing client connection\n"); if (device_watch > 0) { g_source_remove(device_watch); device_watch = 0; } client_watch = 0; return FALSE; } return TRUE; }
void reqm_write(int sock, struct write_packet *p, const void *buffer, int unlock) { struct object_descriptor obj; int wsize, node; if (!read_metadata(p->obj, &obj)) { if (unlock) unlock_object(p->obj); send_error(sock, ENOENT); return; } wsize = write_data(&obj, p->offset, p->size, buffer); if (p->offset != -1) obj.meta.size = obj.meta.size > p->offset + p->size ? obj.meta.size : p->offset + p->size; else obj.meta.size += p->size; write_metadata(&obj); pthread_mutex_lock(&replicas_lock); for (unsigned int i = 0; i < replicas.size(); i++) { if (!memcmp(&replicas[i].id, &p->obj, sizeof(oid_t))) { node = replicas[i].node; pthread_mutex_lock(&peer_locks[node]); forward_data(peer_srv_socks[node], OP_WRITE, OS_SLAVE, p, sizeof(*p), buffer, p->size); receive_reply(peer_srv_socks[node], NULL, 0); pthread_mutex_unlock(&peer_locks[node]); } } pthread_mutex_unlock(&replicas_lock); if (unlock) unlock_object(p->obj); send_reply(sock, &wsize, sizeof(wsize)); }
int main(int argc, char **argv) { opterr = 0; int option_index; int chr; char serial_node[30] = "\0"; int network_port; int show_help = 0; int modem_fd = 0, socket_fd = 0; struct option opts[] = { { "node", required_argument, 0, 'n' }, { "port", required_argument, 0, 'p' }, { "help", no_argument, 0, 'h' }, }; while (1) { option_index = 0; chr = getopt_long(argc, argv, "n:p:h", opts, &option_index); if (chr == -1) break; switch (chr) { case 'h': show_help = 1; break; case 'n': snprintf(serial_node, 30, "%s", optarg); break; case 'p': network_port = atoi(optarg); break; default: break; } } if (show_help || network_port == 0 || serial_node[0] == 0) { printf("usage: samsung_ipc_forward -n <device node> -p <network port>\n"); exit(1); } socket_fd = init_network_connection(network_port); if (socket_fd < 0) return EXIT_FAILURE; modem_fd = open(serial_node, O_RDWR | O_NOCTTY | O_NONBLOCK); int connection_fd = -1; struct sockaddr_in addr = { 0, }; socklen_t length = sizeof(addr); while ((connection_fd = accept(socket_fd, (struct sockaddr*) &addr, &length)) >= 0) { printf("New connection from: '%s'\n", inet_ntoa(addr.sin_addr)); forward_data(modem_fd, connection_fd); close(connection_fd); } close(modem_fd); close(socket_fd); return 0; socket_init_failed: close(socket_fd); return EXIT_FAILURE; }
/* 处理客户端的连接 */ void handle_client(int client_sock, struct sockaddr_in client_addr) { int is_http_tunnel = 0; if(strlen(remote_host) == 0) /* 未指定远端主机名称从http 请求 HOST 字段中获取 */ { #ifdef DEBUG LOG(" ============ handle new client ============\n"); LOG(">>>Header:%s\n",header_buffer); #endif if(read_header(client_sock,header_buffer) < 0) { LOG("Read Http header failed\n"); return; } else { char * p = strstr(header_buffer,"CONNECT"); /* 判断是否是http 隧道请求 */ if(p) { LOG("receive CONNECT request\n"); is_http_tunnel = 1; } if(strstr(header_buffer,"GET /mproxy") >0 ) { LOG("====== hand mproxy info request ===="); //返回mproxy的运行基本信息 hand_mproxy_info_req(client_sock,header_buffer); return; } if(extract_host(header_buffer) < 0) { LOG("Cannot extract host field,bad http protrotol"); return; } LOG("Host:%s port: %d io_flag:%d\n",remote_host,remote_port,io_flag); } } if ((remote_sock = create_connection()) < 0) { LOG("Cannot connect to host [%s:%d]\n",remote_host,remote_port); return; } if (fork() == 0) { // 创建子进程用于从客户端转发数据到远端socket接口 if(strlen(header_buffer) > 0 && !is_http_tunnel) { forward_header(remote_sock); //普通的http请求先转发header } forward_data(client_sock, remote_sock); exit(0); } if (fork() == 0) { // 创建子进程用于转发从远端socket接口过来的数据到客户端 if(io_flag == W_S_ENC) { io_flag = R_C_DEC; //发送请求给服务端进行编码,读取服务端的响应则进行解码 } else if (io_flag == R_C_DEC) { io_flag = W_S_ENC; //接收客户端请求进行解码,那么响应客户端请求需要编码 } if(is_http_tunnel) { send_tunnel_ok(client_sock); } forward_data(remote_sock, client_sock); exit(0); } close(remote_sock); close(client_sock); }