/** * Main loop of HD driver. * *****************************************************************************/ PUBLIC void task_hd() { MESSAGE msg; init_hd(); while (1) { send_recv(RECEIVE, ANY, &msg); int src = msg.source; switch (msg.type) { case DEV_OPEN: hd_open(msg.DEVICE); break; case DEV_CLOSE: hd_close(msg.DEVICE); break; case DEV_READ: case DEV_WRITE: hd_rdwt(&msg); break; case DEV_IOCTL: hd_ioctl(&msg); break; default: dump_msg("HD driver::unknown msg", &msg); spin("FS::main_loop (invalid msg.type)"); break; } send_recv(SEND, src, &msg); } }
/** * * CCApp Provider main routine. * * @param arg - CCApp msg queue * * @return void * * @pre None */ void CCApp_task(void * arg) { static const char fname[] = "CCApp_task"; phn_syshdr_t *syshdr = NULL; appListener *listener = NULL; void * msg; //initialize the listener list sll_lite_init(&sll_list); CCAppInit(); while (1) { msg = cprGetMessage(ccapp_msgq, TRUE, (void **) &syshdr); if ( msg) { #if DEBUG dump_msg("CCAPPTASKDUMPMSG", (unsigned int *)msg, syshdr->Len, syshdr->Cmd); #else CCAPP_DEBUG(DEB_F_PREFIX"Received Cmd[%d] for app[%d]\n", DEB_F_PREFIX_ARGS(SIP_CC_PROV, fname), syshdr->Cmd, syshdr->Usr.UsrInfo); #endif listener = getCcappListener(syshdr->Usr.UsrInfo); if (listener != NULL) { (* ((appListener)(listener)))(msg, syshdr->Cmd); } else { CCAPP_DEBUG(DEB_F_PREFIX"Event[%d] doesn't have a dedicated listener.\n", DEB_F_PREFIX_ARGS(SIP_CC_PROV, fname), syshdr->Usr.UsrInfo); } cprReleaseSysHeader(syshdr); cprReleaseBuffer(msg); } } }
/** dump lruhash msg cache */ static int dump_msg_lruhash(SSL* ssl, struct worker* worker, struct lruhash* h) { struct lruhash_entry* e; struct query_info* k; struct reply_info* d; /* lruhash already locked by caller */ /* walk in order of lru; best first */ for(e=h->lru_start; e; e = e->lru_next) { regional_free_all(worker->scratchpad); lock_rw_rdlock(&e->lock); /* make copy of rrset in worker buffer */ if(!copy_msg(worker->scratchpad, e, &k, &d)) { lock_rw_unlock(&e->lock); return 0; } lock_rw_unlock(&e->lock); /* release lock so we can lookup the rrset references * in the rrset cache */ if(!dump_msg(ssl, k, d, *worker->env.now)) { return 0; } } return 1; }
static void cmd_top(const str* arg) { long num; long lines; char* end; if ((num = strtol(arg->s, &end, 10)) < 0) return respond(err_syntax); if (!msgnum_check(num)) return; while (*end == SPACE) ++end; if (*end == 0) { msgs[num-1].read = 1; return dump_msg(num, LONG_MAX); } if ((lines = strtol(end, &end, 10)) < 0 || *end != 0) return respond(err_syntax); dump_msg(num, lines); }
static void dump_msg_queue(void) { unsigned long flags; RpcMsgQueue *msg; spin_lock_irqsave(&rpc_msg_queue_lock, flags); printk("MSG QUEUE count: %d\n", rpc_msg_queue_cnt); printk("head: "); dump_msg(rpc_msg_queue_head); printk("tail: "); dump_msg(rpc_msg_queue_tail); msg = rpc_msg_queue_head; while (msg) { dump_msg(msg); msg = msg->next; } spin_unlock_irqrestore(&rpc_msg_queue_lock, flags); }
/** * <Ring 1> Main loop of task TTY. *****************************************************************************/ PUBLIC void task_tty() { TTY * tty; MESSAGE msg; init_keyboard(); for (tty = TTY_FIRST; tty < TTY_END; tty++) init_tty(tty); select_console(0); while (1) { for (tty = TTY_FIRST; tty < TTY_END; tty++) { do { tty_dev_read(tty); tty_dev_write(tty); } while (tty->ibuf_cnt); } send_recv(RECEIVE, ANY, &msg); int src = msg.source; assert(src != TASK_TTY); TTY* ptty = &tty_table[msg.DEVICE]; switch (msg.type) { case DEV_OPEN: reset_msg(&msg); msg.type = SYSCALL_RET; send_recv(SEND, src, &msg); break; case DEV_READ: tty_do_read(ptty, &msg); break; case DEV_WRITE: tty_do_write(ptty, &msg); break; case HARD_INT: /** * waked up by clock_handler -- a key was just pressed * @see clock_handler() inform_int() */ key_pressed = 0; continue; default: dump_msg("TTY::unknown msg", &msg); break; } } }
void lsp_msg_ack(LSPMessage* data_msg, uint32_t socket, const struct sockaddr_in* addr) { LSPMessage ack_msg = LSPMESSAGE__INIT; int ack_len; uint8_t* ack_buf; ack_msg.connid = data_msg->connid; ack_msg.seqnum = data_msg->seqnum; ack_msg.payload.len = 0; ack_len = lspmessage__get_packed_size(&ack_msg); ack_buf = malloc(ack_len); lspmessage__pack(&ack_msg, ack_buf); DEBUG("lsp_msg_ack: data_msg info"); dump_msg(data_msg); DEBUG("lsp_msg_ack: ack_msg info"); dump_msg(&ack_msg); sendto_maydrop(socket, ack_buf, ack_len, 0, (struct sockaddr*)addr, sizeof(struct sockaddr), false); free(ack_buf); // already free ack_buf //lspmessage__free_unpacked(&ack_msg, NULL); }
/*---------------------------------------------------------------- * do_ioctl * * TODO: describe * * Arguments: * argc number of command line arguments * argv array of argument strings * * Returns: * 0 - success * ~0 - failure ----------------------------------------------------------------*/ int do_ioctl( UINT8 *msg, UINT32 msgcode ) { int result = -1; int fd; p80211ioctl_req_t req; /* set the magic */ req.magic = P80211_IOCTL_MAGIC; /* get a socket */ fd = socket(AF_INET, SOCK_STREAM, 0); if ( fd == -1 ) { perror("wlanctl-ng"); return result; } req.len = MSG_BUFF_LEN; /* TODO: need to fix the length */ req.data = msg; strcpy( req.name, devname); req.result = 0; if ( debug ) { fprintf(stderr, "raw msg before ioctl:\n"); dump_msg(msg); } result = ioctl( fd, P80211_IFREQ, &req); if ( debug ) { fprintf(stderr, "raw msg after ioctl:\n"); dump_msg(msg); } if ( result == -1 ) { perror("wlanctl-ng"); } close(fd); return result; }
void task_fs() { printk("Task FS begins.\n"); init_buffer( 0x300000 ); init_fs(); MESSAGE msg; while(1) { send_recv( RECEIVE, ANY, &msg ); int src = msg.source; switch(msg.type) { case OPEN: msg.FD = do_open( &msg ); break; case CLOSE: msg.RETVAL = do_close( &msg ); break; case READ: case WRITE: msg.CNT = do_rdwr( &msg ); break; case RESUME_PROC: src = msg.PROC_NR; break; case FORK: msg.RETVAL = fs_fork( &msg ); break; case EXIT: msg.RETVAL = fs_exit( &msg ); break; default: dump_msg("FS:unknow message:", &msg ); assert(0); break; } if( msg.type != SUSPEND_PROC ) { msg.type = SYSCALL_RET; send_recv( SEND, src, &msg ); } } spin("FS"); }
static int send_rule(int action, int swport) { znl2msg msg; int i, len, rc, type; /* Handle reset */ if (action == RESET) { /* First send a FIN to reset everything, then SYN */ fill_header(&msg.n, ZNL2_FIN); /* don't test != 0 -- error is OK as long as we get a reply */ if (send_msg(&msg, sock) < 0) return -1; sequence = seq_init = random(); fill_header(&msg.n, ZNL2_SYN); if (send_msg(&msg, sock) != 0) return -1; return 0; } /* Handling for add and delete is identical */ if (action == DELETE) get_nl2msg(&msg, ZNL2_DELTFILTER, swport); else if (action == ADD) get_nl2msg(&msg, ZNL2_NEWTFILTER, swport); else return -1; if (verbose >= 2) dump_msg(&msg); if (sock < 0) { /* Only pretending: debug */ if (msg.n.znl2msg_type == ZNL2_DELTFILTER && swport == UNSPEC) if (++pretend_count > num_pretend_rules) return ZNL2_NOFIL; return 0; } return send_msg(&msg, sock); }
ssize_t recvfrom_maydrop(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen, bool drop) { LSPMessage* msg; uint32_t connid; uint32_t seqnum; uint32_t msg_len; ssize_t ret; bool init_packet; recv_again: DEBUG("recvfrom_maydrop: begin"); ret = recvfrom(sockfd, buf, len, flags, src_addr, addrlen); if (ret <= 0) { DEBUG("recvfrom_maydrop: recvfrom error, ret %d", ret); exit(-1); goto recv_again; } msg = lspmessage__unpack(NULL, ret, buf); msg_len = msg->payload.len; connid = msg->connid; seqnum = msg->seqnum; DEBUG("recvfrom_maydrop: recv data"); dump_msg(msg); if (connid == 0 && seqnum == 0 && msg_len == 0) { // server read may drop init packet. DEBUG("recvfrom_maydrop: init packet, not drop"); init_packet = true; } if (drop_mode && drop) { if (lsp_should_drop() && !init_packet) { DEBUG("recvfrom_maydrop: recv drop connid %d seqnum %d", connid, seqnum); memset(buf, 0, MAXDATASIZE); goto recv_again; } } return ret; }
/************************************************************************************************** * task_fs ************************************************************************************************** * <Ring 1> Main loop of Task FS. *************************************************************************************************/ PUBLIC void task_fs(){ init_fs(); while(TRUE){ send_recv(RECEIVE, ANY, &fs_msg); int src = fs_msg.source; pcaller = &proc_table[src]; switch(fs_msg.type){ case OPEN: fs_msg.FD = do_open(); break; case CLOSE: fs_msg.RETVAL = do_close(); break; case READ: case WRITE: fs_msg.CNT = do_rdwt(); break; case UNLINK: fs_msg.RETVAL = do_unlink(); break; case RESUME_PROC: src = fs_msg.PROC_NR; break; case FORK: fs_msg.RETVAL = fs_fork(); break; default: dump_msg("FS::unkown message:", &fs_msg); assert(0); break; } /* reply */ if(fs_msg.type != SUSPEND_PROC){ fs_msg.type = SYSCALL_RET; send_recv(SEND, src, &fs_msg); } } spin("FS"); }
static void cmd_dump_dbox(int argc ATTR_UNUSED, char *argv[]) { struct istream *input; int fd; unsigned int hdr_size; bool ret; fd = open(argv[1], O_RDONLY); if (fd < 0) i_fatal("open(%s) failed: %m", argv[1]); input = i_stream_create_fd_autoclose(&fd, (size_t)-1); i_stream_set_name(input, argv[1]); hdr_size = dump_file_hdr(input); do { printf("\n"); T_BEGIN { ret = dump_msg(input, hdr_size); } T_END; } while (ret); i_stream_destroy(&input); }
/** * <Ring 1> The main loop of TASK MM. * *****************************************************************************/ PUBLIC void task_mm() { init_mm(); while (1) { send_recv(RECEIVE, ANY, &mm_msg); int src = mm_msg.source; int reply = 1; int msgtype = mm_msg.type; switch (msgtype) { case FORK: mm_msg.RETVAL = do_fork(); break; case EXIT: do_exit(mm_msg.STATUS); reply = 0; break; case EXEC: mm_msg.RETVAL = do_exec(); break; case WAIT: do_wait(); reply = 0; break; default: dump_msg("MM::unknown msg", &mm_msg); assert(0); break; } if (reply) { mm_msg.type = SYSCALL_RET; send_recv(SEND, src, &mm_msg); } } }
/* * 从目标handler的消息池中获取到一个MSG * 参数: * handler - 当前handler * what - 消息代码 * data - 该消息携带的数据 * data_size - 数据大小 * 返回值: * 得到信息,返回获取到的 msg 指针 * 失败则返回 NULL */ MSG* obtain_message(HANDLER* handler, int what, void* data, int data_size) { MSG* msg = NULL; ENTER_LOG(); if (handler==NULL) { LEAVE_LOG(); return NULL; } msg = obtain(handler); if (msg) { msg->what = what; if (data_size>0) { msg->data = malloc(data_size+2); if (msg->data) { memset(msg->data, 0, data_size+2); memcpy(msg->data, data, data_size); msg->data_size = data_size; } else { upil_error("malloc error: %d", errno); remove_message(msg); msg = NULL; } } if (msg) dump_msg("obtain new message:", msg); } else { upil_dbg("no msg cant be obtain."); } upil_printf(MSG_MSGDUMP, "now msg count: %d", handler->msg_count); LEAVE_LOG(); return msg; }
PUBLIC void task_fs() { printl("Task FS begins.\n"); init_fs(); while (1) { send_recv(RECEIVE, ANY, &fs_msg); int src = fs_msg.source; pcaller = &proc_table[src]; switch (fs_msg.type) { case OPEN: fs_msg.FD = do_open(); break; case CLOSE: fs_msg.RETVAL = do_close(); break; case READ: case WRITE: fs_msg.CNT = do_rdwt(); break; case UNLINK: fs_msg.RETVAL = do_unlink(); break; case RESUME_PROC: src = fs_msg.PROC_NR; // 恢复最初请求的进程,如 TestB break; case FORK: fs_msg.RETVAL = fs_fork(); break; case EXIT: fs_msg.RETVAL = fs_exit(); break; case STAT: fs_msg.RETVAL = do_stat(); break; default: dump_msg("FS::unknown message:", &fs_msg); assert(0); break; } /* 如果发送者要求挂起,则不回送消息 */ if (fs_msg.type != SUSPEND_PROC) { fs_msg.type = SYSCALL_RET; send_recv(SEND, src, &fs_msg); } } spin("FS"); }
/** * <Ring 1> The main loop of TASK FS. * *****************************************************************************/ PUBLIC void task_fs() { printl("{FS} Task FS begins.\n"); init_fs(); while (1) { send_recv(RECEIVE, ANY, &fs_msg); int msgtype = fs_msg.type; int src = fs_msg.source; pcaller = &proc_table[src]; switch (msgtype) { case OPEN: fs_msg.FD = do_open(); break; case CLOSE: fs_msg.RETVAL = do_close(); break; case READ: case WRITE: fs_msg.CNT = do_rdwt(); break; case UNLINK: fs_msg.RETVAL = do_unlink(); break; case RESUME_PROC: src = fs_msg.PROC_NR; break; case FORK: fs_msg.RETVAL = fs_fork(); break; case EXIT: fs_msg.RETVAL = fs_exit(); break; /* case LSEEK: */ /* fs_msg.OFFSET = do_lseek(); */ /* break; */ case STAT: fs_msg.RETVAL = do_stat(); break; default: dump_msg("FS::unknown message:", &fs_msg); assert(0); break; } #ifdef ENABLE_DISK_LOG char * msg_name[128]; msg_name[OPEN] = "OPEN"; msg_name[CLOSE] = "CLOSE"; msg_name[READ] = "READ"; msg_name[WRITE] = "WRITE"; msg_name[LSEEK] = "LSEEK"; msg_name[UNLINK] = "UNLINK"; /* msg_name[FORK] = "FORK"; */ /* msg_name[EXIT] = "EXIT"; */ /* msg_name[STAT] = "STAT"; */ switch (msgtype) { case UNLINK: dump_fd_graph("%s just finished. (pid:%d)", msg_name[msgtype], src); //panic(""); case OPEN: case CLOSE: case READ: case WRITE: case FORK: case EXIT: /* case LSEEK: */ case STAT: break; case RESUME_PROC: break; default: assert(0); } #endif /* reply */ if (fs_msg.type != SUSPEND_PROC) { fs_msg.type = SYSCALL_RET; send_recv(SEND, src, &fs_msg); } } }
/** * This function is used to submit an I/O Request to an EP. * * - When the request completes the request's completion callback * is called to return the request to the driver. * - An EP, except control EPs, may have multiple requests * pending. * - Once submitted the request cannot be examined or modified. * - Each request is turned into one or more packets. * - A BULK EP can queue any amount of data; the transfer is * packetized. * - Zero length Packets are specified with the request 'zero' * flag. */ static int dwc_otg_pcd_ep_queue(struct usb_ep *_ep, struct usb_request *_req, int _gfp_flags) { int prevented = 0; dwc_otg_pcd_request_t *req; dwc_otg_pcd_ep_t *ep; dwc_otg_pcd_t *pcd; unsigned long flags = 0; DWC_DEBUGPL(DBG_PCDV,"%s(%p,%p,%d)\n", __func__, _ep, _req, _gfp_flags); req = container_of(_req, dwc_otg_pcd_request_t, req); if (!_req || !_req->complete || !_req->buf || !list_empty(&req->queue)) { if( !_req ) printk("bad _req\n"); if( !_req->complete ) printk("bad _req->complete\n"); if( !_req->buf ) printk("bad _req->buf\n"); if( !list_empty(&req->queue) ) printk("bad list_empty\n"); DWC_WARN("%s, bad params\n", __func__); return -EINVAL; } ep = container_of(_ep, dwc_otg_pcd_ep_t, ep); if (!_ep || (!ep->desc && ep->dwc_ep.num != 0)) { DWC_WARN("%s, bad ep\n", __func__); return -EINVAL; } pcd = ep->pcd; //cathy, if suspended, drop request if ( (GET_CORE_IF(pcd)->dev_if->suspended == 1) && (ep->dwc_ep.num != 0) ) { DWC_DEBUGPL(DBG_PCDV,"%s, epnum = %d, drop request\n", __func__, ep->dwc_ep.num); return -ESHUTDOWN; } if (!pcd->driver || pcd->gadget.speed == USB_SPEED_UNKNOWN) { DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n", pcd->gadget.speed); DWC_WARN("%s, bogus device state\n", __func__); return -ESHUTDOWN; } DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n", _ep->name, _req, _req->length, _req->buf); if (!GET_CORE_IF(pcd)->core_params->opt) { if (ep->dwc_ep.num != 0) { DWC_ERROR("%s queue req %p, len %d buf %p\n", _ep->name, _req, _req->length, _req->buf); } } SPIN_LOCK_IRQSAVE(&ep->pcd->lock, flags); #if defined(DEBUG) & defined(VERBOSE) dump_msg(_req->buf, _req->length); #endif _req->status = -EINPROGRESS; _req->actual = 0; /* * For EP0 IN without premature status, zlp is required? */ if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) { DWC_DEBUGPL(DBG_PCDV, "%s-OUT ZLP\n", _ep->name); //_req->zero = 1; } /* Start the transfer */ if (list_empty(&ep->queue) && !ep->stopped) { /* EP0 Transfer? */ if (ep->dwc_ep.num == 0) { switch (pcd->ep0state) { case EP0_IN_DATA_PHASE: DWC_DEBUGPL(DBG_PCD, "%s ep0: EP0_IN_DATA_PHASE\n", __func__); break; case EP0_OUT_DATA_PHASE: DWC_DEBUGPL(DBG_PCD, "%s ep0: EP0_OUT_DATA_PHASE\n", __func__); if (pcd->request_config) { /* Complete STATUS PHASE */ ep->dwc_ep.is_in = 1; pcd->ep0state = EP0_STATUS; } break; default: DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n", pcd->ep0state); SPIN_UNLOCK_IRQRESTORE(&pcd->lock, flags); return -EL2HLT; } ep->dwc_ep.dma_addr = (u32)_req->buf & ~Uncache_Mask; //_req->dma; //cathy ep->dwc_ep.start_xfer_buff = _req->buf; ep->dwc_ep.xfer_buff = _req->buf; ep->dwc_ep.xfer_len = _req->length; ep->dwc_ep.xfer_count = 0; ep->dwc_ep.sent_zlp = 0; ep->dwc_ep.total_len = ep->dwc_ep.xfer_len; dwc_otg_ep0_start_transfer( GET_CORE_IF(pcd), &ep->dwc_ep ); } else { /* Setup and start the Transfer */ ep->dwc_ep.dma_addr = (u32)_req->buf & ~Uncache_Mask; //_req->dma; //cathy //ep->dwc_ep.dma_addr = _req->dma; ep->dwc_ep.start_xfer_buff = _req->buf; ep->dwc_ep.xfer_buff = _req->buf; ep->dwc_ep.xfer_len = _req->length; ep->dwc_ep.xfer_count = 0; ep->dwc_ep.sent_zlp = 0; ep->dwc_ep.total_len = ep->dwc_ep.xfer_len; dwc_otg_ep_start_transfer( GET_CORE_IF(pcd), &ep->dwc_ep ); } } if ((req != 0) || prevented) { ++pcd->request_pending; list_add_tail(&req->queue, &ep->queue); //cathy #if 0 if (ep->dwc_ep.is_in && ep->stopped && !(GET_CORE_IF(pcd)->dma_enable)) { /** @todo NGS Create a function for this. */ diepmsk_data_t diepmsk = { .d32 = 0}; diepmsk.b.intktxfemp = 1; dwc_modify_reg32( &GET_CORE_IF(pcd)->dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32 ); } #endif }
/** * <Ring 1> The main loop of TASK MM. * *****************************************************************************/ PUBLIC void task_mm() { init_mm(); while (1) { send_recv(RECEIVE, ANY, &mm_msg); int src = mm_msg.source; int reply = 1; int msgtype = mm_msg.type; switch (msgtype) { case FORK: mm_msg.RETVAL = do_fork(); break; case EXIT: do_exit(mm_msg.STATUS); reply = 0; break; case EXEC: mm_msg.RETVAL = do_exec(); break; case WAIT: do_wait(); reply = 0; break; case KILL: mm_msg.RETVAL = do_kill(); break; case RAISE: mm_msg.RETVAL = do_raise(); break; case BRK: mm_msg.RETVAL = do_brk(); break; case ACCT: mm_msg.RETVAL = do_acct(); break; case GETUID: mm_msg.RETVAL = do_getuid(); break; case SETUID: mm_msg.RETVAL = do_setuid(); break; case GETGID: mm_msg.RETVAL = do_getgid(); break; case SETGID: mm_msg.RETVAL = do_setgid(); break; case GETEUID: mm_msg.RETVAL = do_geteuid(); break; case GETEGID: mm_msg.RETVAL = do_getegid(); break; case SIGACTION: mm_msg.RETVAL = do_sigaction(); break; case ALARM: mm_msg.RETVAL = do_alarm(); break; default: dump_msg("MM::unknown msg", &mm_msg); assert(0); break; } if (reply) { mm_msg.type = SYSCALL_RET; send_recv(SEND, src, &mm_msg); } } }
PUBLIC void task_fs() { init_buffer(); printl("VFS: VFS is running.\n"); MESSAGE msg; struct file_system * fs; while (1){ send_recv(RECEIVE, ANY, &msg); int src = msg.source; pcaller = &proc_table[src]; int msgtype = msg.type; fs = file_systems; switch (msgtype) { case FS_REGISTER: register_filesystem(&msg); break; case OPEN: printl("Doing open, %d\n", fs->open); msg.FD = fs->open(&msg); printl("Done open, %d\n", fs->open); break; case CLOSE: msg.RETVAL = fs->close(&msg); break; case READ: case WRITE: msg.CNT = fs->rdwt(&msg); break; case UNLINK: msg.RETVAL = fs->unlink(&msg); break; case MOUNT: msg.RETVAL = fs->mount(&msg); break; case UMOUNT: msg.RETVAL = fs->umount(&msg); break; case MKDIR: msg.RETVAL = fs->mkdir(&msg); break; case RESUME_PROC: src = msg.PROC_NR; break; case FORK: msg.RETVAL = fs->fork(&msg); break; case EXIT: msg.RETVAL = fs->exit(&msg); break; case LSEEK: msg.OFFSET = fs->lseek(&msg); break; case STAT: msg.RETVAL = fs->stat(&msg); break; case CHROOT: msg.RETVAL = fs->chroot(&msg); break; case CHDIR: msg.RETVAL = fs->chdir(&msg); break; default: dump_msg("VFS: Unknown message:", &msg); assert(0); break; } /* reply */ if (msg.type != SUSPEND_PROC) { msg.type = SYSCALL_RET; send_recv(SEND, src, &msg); } } }
static void message_release_proxy(void) { int server_socket; struct sockaddr_in server_addr; char *header, *xml_request, *default_ns; char response_buf[1024]; int read_size; struct xml_node *request_root, *auth_node, *id_node, *secrete_node, *proxy_type_node, *proxy_key_node; if((proxy_key == NULL) || (server_instance == NULL)) return; server_socket = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(server_instance); server_addr.sin_port = ntohs(CLOUD_PORT); if(connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { close(server_socket); return; } //Build XML Request Document Tree default_ns = (char *) malloc(strlen("http://") + strlen(server_instance) + strlen("/GenericMessage") + 1); sprintf(default_ns, "http://%s/GenericMessage", server_instance); request_root = xml_new_element(NULL, "ReleaseProxy", default_ns); auth_node = xml_new_element(NULL, "Authentication", NULL); id_node = xml_new_element(NULL, "Id", NULL); secrete_node = xml_new_element(NULL, "Secrete", NULL); proxy_type_node = xml_new_element(NULL, "ProxyType", NULL); proxy_key_node = xml_new_element(NULL, "ProxyKey", NULL); xml_add_child(request_root, auth_node); xml_add_child(auth_node, id_node); xml_add_child(auth_node, secrete_node); xml_add_child(id_node, xml_new_text(device_id)); xml_add_child(secrete_node, xml_new_text(device_secrete)); xml_add_child(request_root, proxy_type_node); xml_add_child(proxy_type_node, xml_new_text("ControllerProxy")); //ProxyType for controller should be "ControllerProxy" xml_add_child(request_root, proxy_key_node); xml_add_child(proxy_key_node, xml_new_text(proxy_key)); xml_request = xml_dump_tree(request_root); xml_delete_tree(request_root); //Write HTTP header = http_post_header(server_instance, "/cgi-bin/generic_message", "text/xml", (int) strlen(xml_request)); write(server_socket, header, strlen(header)); dump_msg("\nsend http header:", header, strlen(header)); http_free(header); write(server_socket, xml_request, strlen(xml_request)); dump_msg("send xml request:", xml_request, strlen(xml_request)); xml_free(xml_request); //Read HTTP if((read_size = read(server_socket, response_buf, sizeof(response_buf))) != 0) { struct xml_node *response_root, *instance_text; struct xml_node_set *node_set; char *http_header, *http_body; if((http_header = http_response_header(response_buf, read_size)) != NULL) { dump_msg("\nrecv http header:", http_header, strlen(http_header)); http_free(http_header); } if((http_body = http_response_body(response_buf, read_size)) != NULL) { dump_msg("recv http body:", http_body, strlen(http_body)); http_free(http_body); } } free(default_ns); close(server_socket); }
static void message_forward_message(char *application_content, char *timeout) { int server_socket; struct sockaddr_in server_addr; char *header, *xml_request, *default_ns, *doc_prefix = NULL, *doc_name = NULL, *doc_uri = NULL; char response_buf[1024]; int read_size; struct xml_node *request_root, *auth_node, *id_node, *secrete_node, *message_node, *type_node, *content_node, *application_root, *timeout_node; if(xml_doc_name(application_content, strlen(application_content), &doc_prefix, &doc_name, &doc_uri) == -1) return; if((application_root = xml_parse_doc(application_content, strlen(application_content), doc_prefix, doc_name, doc_uri)) == NULL) return; server_socket = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(server_instance); server_addr.sin_port = ntohs(CLOUD_PORT); if(connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { close(server_socket); return; } //Build XML Request Document Tree default_ns = (char *) malloc(strlen("http://") + strlen(server_instance) + strlen("/GenericMessage") + 1); sprintf(default_ns, "http://%s/GenericMessage", server_instance); request_root = xml_new_element(NULL, "ForwardMessage", default_ns); auth_node = xml_new_element(NULL, "Authentication", NULL); id_node = xml_new_element(NULL, "Id", NULL); secrete_node = xml_new_element(NULL, "Secrete", NULL); message_node = xml_new_element(NULL, "Message", NULL); type_node = xml_new_element(NULL, "Type", NULL); content_node = xml_new_element(NULL, "Content", NULL); xml_add_child(request_root, auth_node); xml_add_child(auth_node, id_node); xml_add_child(auth_node, secrete_node); xml_add_child(id_node, xml_new_text(device_id)); xml_add_child(secrete_node, xml_new_text(device_secrete)); xml_add_child(request_root, message_node); xml_add_child(message_node, type_node); xml_add_child(message_node, content_node); xml_add_child(type_node, xml_new_text("ToDevice")); //For controller, Message/Type should be "ToDevice" xml_add_child(content_node, application_root); //Add application document root to request content node as child if(timeout != NULL) { timeout_node = xml_new_element(NULL, "Timeout", NULL); xml_add_child(timeout_node, xml_new_text(timeout)); xml_add_child(request_root, timeout_node); } xml_request = xml_dump_tree(request_root); xml_delete_tree(request_root); //Write HTTP header = http_post_header(server_instance, "/cgi-bin/generic_message", "text/xml", (int) strlen(xml_request)); write(server_socket, header, strlen(header)); dump_msg("\nsend http header:", header, strlen(header)); http_free(header); write(server_socket, xml_request, strlen(xml_request)); dump_msg("send xml request:", xml_request, strlen(xml_request)); xml_free(xml_request); //Read HTTP if((read_size = read(server_socket, response_buf, sizeof(response_buf))) != 0) { struct xml_node *response_root, *instance_text; struct xml_node_set *node_set; char *http_header, *http_body; if((http_header = http_response_header(response_buf, read_size)) != NULL) { dump_msg("\nrecv http header:", http_header, strlen(http_header)); http_free(http_header); } if((http_body = http_response_body(response_buf, read_size)) != NULL) { dump_msg("recv http body:", http_body, strlen(http_body)); http_free(http_body); } } if(doc_prefix) xml_free(doc_prefix); if(doc_name) xml_free(doc_name); if(doc_uri) xml_free(doc_uri); free(default_ns); close(server_socket); }
static char *loadbalance_lookup_address(void) { int server_socket; struct sockaddr_in server_addr; char *header, *xml_request, *default_ns, *public_address = NULL; char response_buf[1024]; int read_size, ret = -1; struct xml_node *request_root, *auth_node, *id_node, *secrete_node; server_socket = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(server_instance); server_addr.sin_port = ntohs(CLOUD_PORT); if(connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) return NULL; //Build XML Request Document Tree default_ns = (char *) malloc(strlen("http://") + strlen(server_instance) + strlen("/Location") + 1); sprintf(default_ns, "http://%s/Location", server_instance); request_root = xml_new_element(NULL, "LookupAddress", default_ns); auth_node = xml_new_element(NULL, "Authentication", NULL); id_node = xml_new_element(NULL, "Id", NULL); secrete_node = xml_new_element(NULL, "Secrete", NULL); xml_add_child(request_root, auth_node); xml_add_child(auth_node, id_node); xml_add_child(auth_node, secrete_node); xml_add_child(id_node, xml_new_text(device_id)); xml_add_child(secrete_node, xml_new_text(device_secrete)); xml_request = xml_dump_tree(request_root); xml_delete_tree(request_root); //Write HTTP header = http_post_header(server_instance, "/cgi-bin/location", "text/xml", (int) strlen(xml_request)); write(server_socket, header, strlen(header)); dump_msg("\nsend http header:", header, strlen(header)); http_free(header); write(server_socket, xml_request, strlen(xml_request)); dump_msg("send xml request:", xml_request, strlen(xml_request)); xml_free(xml_request); //Read HTTP if((read_size = read(server_socket, response_buf, sizeof(response_buf))) != 0) { struct xml_node *response_root, *address_text; struct xml_node_set *node_set; char *http_header, *http_body; if((http_header = http_response_header(response_buf, read_size)) != NULL) { dump_msg("\nrecv http header:", http_header, strlen(http_header)); http_free(http_header); } if((http_body = http_response_body(response_buf, read_size)) != NULL) { dump_msg("recv http body:", http_body, strlen(http_body)); http_free(http_body); } if((response_root = xml_parse_doc(response_buf, read_size, NULL, "LookupAddressResponse", default_ns)) != NULL) { node_set = xml_find_path(response_root, "/LookupAddressResponse/PublicAddress"); if(node_set->count) { address_text = xml_text_child(node_set->node[0]); if(address_text) { public_address = (char *) malloc(strlen(address_text->text) + 1); strcpy(public_address, address_text->text); } } xml_delete_set(node_set); xml_delete_tree(response_root); } } free(default_ns); close(server_socket); return public_address; }
static void message_query_status(char *op, char *time1, char *time2, char *limit) { int server_socket; struct sockaddr_in server_addr; char *header, *xml_request, *default_ns; char response_buf[4096]; int read_size; struct xml_node *request_root, *auth_node, *id_node, *secrete_node, *op_node, *time_node, *time1_node, *time2_node, *limit_node; server_socket = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(server_instance); server_addr.sin_port = ntohs(CLOUD_PORT); if(connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { close(server_socket); return; } //Build XML Request Document Tree default_ns = (char *) malloc(strlen("http://") + strlen(server_instance) + strlen("/GenericMessage") + 1); sprintf(default_ns, "http://%s/GenericMessage", server_instance); request_root = xml_new_element(NULL, "QueryStatus", default_ns); auth_node = xml_new_element(NULL, "Authentication", NULL); id_node = xml_new_element(NULL, "Id", NULL); secrete_node = xml_new_element(NULL, "Secrete", NULL); op_node = xml_new_element(NULL, "TimeOperation", NULL); xml_add_child(request_root, auth_node); xml_add_child(auth_node, id_node); xml_add_child(auth_node, secrete_node); xml_add_child(id_node, xml_new_text(device_id)); xml_add_child(secrete_node, xml_new_text(device_secrete)); xml_add_child(request_root, op_node); xml_add_child(op_node, xml_new_text(op)); if(strcmp(op, "TimeRangeBetween") == 0) { time_node = xml_new_element(NULL, "TimeRange", NULL); xml_add_child(request_root, time_node); time1_node = xml_new_element(NULL, "StartTime", NULL); time2_node = xml_new_element(NULL, "EndTime", NULL); xml_add_child(time_node, time1_node); xml_add_child(time_node, time2_node); xml_add_child(time1_node, xml_new_text(time1)); xml_add_child(time2_node, xml_new_text(time2)); } else { time_node = xml_new_element(NULL, "Time", NULL); xml_add_child(request_root, time_node); xml_add_child(time_node, xml_new_text(time1)); if((strcmp(op, "TimeGreaterThan") == 0) || (strcmp(op, "TimeLessThan") == 0)) { limit_node = xml_new_element(NULL, "QueryLimit", NULL); xml_add_child(request_root, limit_node); xml_add_child(limit_node, xml_new_text(limit)); } } xml_request = xml_dump_tree(request_root); xml_delete_tree(request_root); //Write HTTP header = http_post_header(server_instance, "/cgi-bin/generic_message", "text/xml", (int) strlen(xml_request)); write(server_socket, header, strlen(header)); dump_msg("\nsend http header:", header, strlen(header)); http_free(header); write(server_socket, xml_request, strlen(xml_request)); dump_msg("send xml request:", xml_request, strlen(xml_request)); xml_free(xml_request); //Read HTTP if((read_size = read(server_socket, response_buf, sizeof(response_buf))) != 0) { struct xml_node *response_root; struct xml_node_set *node_set; char *http_header, *http_body; if((http_header = http_response_header(response_buf, read_size)) != NULL) { dump_msg("\nrecv http header:", http_header, strlen(http_header)); http_free(http_header); } if((http_body = http_response_body(response_buf, read_size)) != NULL) { dump_msg("recv http body:", http_body, strlen(http_body)); http_free(http_body); } if((response_root = xml_parse_doc(response_buf, read_size, NULL, "QueryStatusResponse", default_ns)) != NULL) { node_set = xml_find_path(response_root, "/QueryStatusResponse/Status"); if(node_set->count) { int i; for(i = 0; i < node_set->count; i ++) { char *status_buf = xml_dump_tree(node_set->node[i]); printf("Receive Application-Dependent Statu[%d]: %s\n", i, status_buf); xml_free(status_buf); } } xml_delete_set(node_set); xml_delete_tree(response_root); } } free(default_ns); close(server_socket); }
static void message_retrieve_message(void) { int server_socket; struct sockaddr_in server_addr; char *header, *xml_request, *default_ns; char response_buf[1024]; int read_size; struct xml_node *request_root, *auth_node, *id_node, *secrete_node, *proxy_type_node, *proxy_key_node, *period_node; char vpn_server_ip[16], cmd_buf[64]; char *pserver_ip_start=NULL; char *pserver_ip_end=NULL; int get_server_ip_flag=0; server_socket = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(server_instance); server_addr.sin_port = ntohs(CLOUD_PORT); if(connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { close(server_socket); return; } //Build XML Request Document Tree default_ns = (char *) malloc(strlen("http://") + strlen(server_instance) + strlen("/GenericMessage") + 1); sprintf(default_ns, "http://%s/GenericMessage", server_instance); request_root = xml_new_element(NULL, "RetrieveMessage", default_ns); auth_node = xml_new_element(NULL, "Authentication", NULL); id_node = xml_new_element(NULL, "Id", NULL); secrete_node = xml_new_element(NULL, "Secrete", NULL); proxy_type_node = xml_new_element(NULL, "ProxyType", NULL); proxy_key_node = xml_new_element(NULL, "ProxyKey", NULL); period_node = xml_new_element(NULL, "AckPeriod", NULL); xml_add_child(request_root, auth_node); xml_add_child(auth_node, id_node); xml_add_child(auth_node, secrete_node); xml_add_child(id_node, xml_new_text(device_id)); xml_add_child(secrete_node, xml_new_text(device_secrete)); xml_add_child(request_root, proxy_type_node); xml_add_child(request_root, proxy_key_node); xml_add_child(request_root, period_node); xml_add_child(proxy_type_node, xml_new_text("ControllerProxy")); //ProxyType for controller should be "ControllerProxy" xml_add_child(proxy_key_node, xml_new_text(proxy_key)); xml_add_child(period_node, xml_new_text("30")); //Set ack period in seconds xml_request = xml_dump_tree(request_root); xml_delete_tree(request_root); //Write HTTP header = http_post_header(server_instance, "/cgi-bin/generic_message", "text/xml", (int) strlen(xml_request)); write(server_socket, header, strlen(header)); dump_msg("\nsend http header:", header, strlen(header)); http_free(header); write(server_socket, xml_request, strlen(xml_request)); dump_msg("send xml request:", xml_request, strlen(xml_request)); xml_free(xml_request); //Read HTTP while(get_server_ip_flag==0 && (read_size = read(server_socket, response_buf, sizeof(response_buf))) != 0) { struct xml_node *response_root; struct xml_node_set *node_set; char *http_header, *http_body; if((http_header = http_response_header(response_buf, read_size)) != NULL) { dump_msg("\nrecv http header:", http_header, strlen(http_header)); http_free(http_header); } if((http_body = http_response_body(response_buf, read_size)) != NULL) { dump_msg("recv http body:", http_body, strlen(http_body)); http_free(http_body); } if((response_root = xml_parse_doc(response_buf, read_size, NULL, "RetrieveMessageResponse", default_ns)) != NULL) { int has_message = 0; node_set = xml_find_path(response_root, "/RetrieveMessageResponse/Message/Id"); if(node_set->count) { struct xml_node *id_text = xml_text_child(node_set->node[0]); if(id_text) { if(strcmp(id_text->text, device_id) == 0) has_message = 1; } } xml_delete_set(node_set); if(has_message) { node_set = xml_find_path(response_root, "/RetrieveMessageResponse/Message/Content"); if(node_set->count) { struct xml_node *content_node = node_set->node[0]; if(content_node->child) { char *content_buf = xml_dump_tree(content_node->child); printf("Receive Application-Dependent Content: %s\n", content_buf); if(strncmp(content_buf, "<OpenvpnApplication><ServerIpElement>", strlen("<OpenvpnApplication><ServerIpElement>")) == 0) { pserver_ip_start=content_buf+strlen("<OpenvpnApplication><ServerIpElement>"); for(pserver_ip_end=pserver_ip_start; pserver_ip_end!=NULL && *pserver_ip_end!='<'; pserver_ip_end++); strncpy(vpn_server_ip, pserver_ip_start, pserver_ip_end-pserver_ip_start); vpn_server_ip[pserver_ip_end-pserver_ip_start]=0; printf("Recv VPN Server IP Address: %s\n", vpn_server_ip); sprintf(cmd_buf, "echo VpnServerIp:%s > /etc/openvpn/vpn_server_ip", vpn_server_ip); system(cmd_buf); get_server_ip_flag=1; system("/usr/sbin/openvpn_client_start.sh &"); } xml_free(content_buf); } } xml_delete_set(node_set); } else { node_set = xml_find_path(response_root, "/RetrieveMessageResponse/Result"); if(node_set->count) { xml_delete_set(node_set); xml_delete_tree(response_root); break; } xml_delete_set(node_set); } xml_delete_tree(response_root); } } free(default_ns); close(server_socket); }