int register_dcc_events(struct pollfd **pfd, int *n_fd) { char *message = NULL; int loop = 0, redraw_rc = 0; for(loop=0; loop<n_dcc; loop++) { dcc_list[loop].ifd = -1; switch(get_dcc_state(loop)) { case DSTATE_TCP_CONNECT: if ((dcc_list[loop].fd_conn = connect_to(&dcc_list[loop].ri, &message)) == -1) { set_dcc_state(loop, DSTATE_ERROR); update_statusline(dcc_list[loop].server_nr, dcc_list[loop].channel_nr, "DCC: cannot connect (file %s), reason: %s (%d) / %s", dcc_list[loop].filename, strerror(errno), errno, message); free(message); } else { set_dcc_state(loop, DSTATE_DCC_CONNECTING); update_statusline(dcc_list[loop].server_nr, dcc_list[loop].channel_nr, "DCC: connecting (file %s)", dcc_list[loop].filename); } redraw_rc = 1; break; case DSTATE_DCC_CONNECTING: assert(dcc_list[loop].fd_conn != -1); if (dcc_list[loop].mode == DCC_RECEIVE_FILE) dcc_list[loop].ifd = add_poll(pfd, n_fd, dcc_list[loop].fd_conn, POLLOUT | POLLHUP); else if (dcc_list[loop].mode == DCC_SEND_FILE) dcc_list[loop].ifd = add_poll(pfd, n_fd, dcc_list[loop].fd_conn, POLLIN | POLLHUP); else LOG("state_connecting: invalid internal DCC mode: %d\n", dcc_list[loop].mode); break; case DSTATE_CONNECTED1: assert(dcc_list[loop].fd_conn != -1); if (dcc_list[loop].mode == DCC_RECEIVE_FILE) dcc_list[loop].ifd = add_poll(pfd, n_fd, dcc_list[loop].fd_conn, POLLIN | POLLHUP); else if (dcc_list[loop].mode == DCC_SEND_FILE) dcc_list[loop].ifd = add_poll(pfd, n_fd, dcc_list[loop].fd_conn, POLLOUT | POLLHUP); else LOG("state_connected: invalid internal DCC mode: %d\n", dcc_list[loop].mode); break; case DSTATE_NO_CONNECTION: case DSTATE_ERROR: case DSTATE_RUNNING: case DSTATE_DISCONNECTED: break; } } return redraw_rc; }
static int process_cmd_sock(int h) { sock_cmd_t cmd = {-1, 0, 0, 0, 0}; int fd = ts[h].cmd_fdr; if(recv(fd, &cmd, sizeof(cmd), MSG_WAITALL) != sizeof(cmd)) { APPL_TRACE_ERROR1("recv cmd errno:%d", errno); return FALSE; } APPL_TRACE_DEBUG1("cmd.id:%d", cmd.id); switch(cmd.id) { case CMD_ADD_FD: add_poll(h, cmd.fd, cmd.type, cmd.flags, cmd.user_id); break; case CMD_WAKEUP: break; case CMD_USER_PRIVATE: asrt(ts[h].cmd_callback); if(ts[h].cmd_callback) ts[h].cmd_callback(fd, cmd.type, cmd.flags, cmd.user_id); break; case CMD_EXIT: return FALSE; default: APPL_TRACE_DEBUG1("unknown cmd: %d", cmd.id); break; } return TRUE; }
int btsock_thread_add_fd(int h, int fd, int type, int flags, uint32_t user_id) { if(h < 0 || h >= MAX_THREAD) { APPL_TRACE_ERROR1("invalid bt thread handle:%d", h); return FALSE; } if(ts[h].cmd_fdw == -1) { APPL_TRACE_ERROR0("cmd socket is not created. socket thread may not initialized"); return FALSE; } if(flags & SOCK_THREAD_ADD_FD_SYNC) { //must executed in socket poll thread if(ts[h].thread_id == pthread_self()) { //cleanup one-time flags flags &= ~SOCK_THREAD_ADD_FD_SYNC; add_poll(h, fd, type, flags, user_id); return TRUE; } APPL_TRACE_DEBUG0("THREAD_ADD_FD_SYNC is not called in poll thread, fallback to async"); } sock_cmd_t cmd = {CMD_ADD_FD, fd, type, flags, user_id}; APPL_TRACE_DEBUG2("adding fd:%d, flags:0x%x", fd, flags); return send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0) == sizeof(cmd); }
int zebra_process_one (zebra_processor_t *proc, int timeout) { if(proc_lock(proc) < 0) return(-1); int rc = 0; if(proc->video) { zebra_image_scanner_enable_cache(proc->scanner, 1); rc = zebra_video_enable(proc->video, 1); /* FIXME failure recovery? */ int vid_fd = zebra_video_get_fd(proc->video); if(vid_fd >= 0) add_poll(proc, vid_fd, proc_video_handler); proc->active = 1; #ifdef HAVE_LIBPTHREAD pthread_cond_broadcast(&proc->event); #endif proc_event_wait(proc, EVENT_OUTPUT, timeout); rc = zebra_video_enable(proc->video, 0); if(vid_fd >= 0) remove_poll(proc, vid_fd); proc->active = 0; proc->events &= ~EVENT_INPUT; zebra_image_scanner_enable_cache(proc->scanner, 0); } else rc = -1; proc_unlock(proc); return(rc); }
int zebra_processor_set_active (zebra_processor_t *proc, int active) { if(proc_lock(proc) < 0) return(-1); if(!proc->video) { proc_unlock(proc); return(-1); } zebra_image_scanner_enable_cache(proc->scanner, active); int rc = zebra_video_enable(proc->video, active); int vid_fd = zebra_video_get_fd(proc->video); if(vid_fd >= 0) { if(active) add_poll(proc, vid_fd, proc_video_handler); else remove_poll(proc, vid_fd); } /* FIXME failure recovery? */ proc->active = active; proc->events &= ~EVENT_INPUT; #ifdef HAVE_LIBPTHREAD if(proc->threaded) { assert(!proc->sem); assert(pthread_equal(proc->sem_owner, pthread_self())); pthread_mutex_lock(&proc->mutex); proc->sem++; pthread_cond_broadcast(&proc->event); pthread_cond_signal(&proc->cond); pthread_mutex_unlock(&proc->mutex); } #endif return(rc); }
zebra_processor_t *zebra_processor_create (int threaded) { zebra_processor_t *proc = calloc(1, sizeof(zebra_processor_t)); if(!proc) return(NULL); err_init(&proc->err, ZEBRA_MOD_PROCESSOR); proc->kick_fds[0] = proc->kick_fds[1] = -1; proc->scanner = zebra_image_scanner_create(); if(!proc->scanner) { free(proc); return(NULL); } if(threaded) { #ifdef HAVE_LIBPTHREAD proc->threaded = 1; proc->sem = 1; /* FIXME check errors */ pthread_mutex_init(&proc->mutex, NULL); pthread_cond_init(&proc->cond, NULL); pthread_cond_init(&proc->event, NULL); pipe(proc->kick_fds); add_poll(proc, proc->kick_fds[0], NULL); #else /* FIXME record warning */ #endif } return(proc); }
/* create dummy socket pair used to wake up select loop */ static inline void init_cmd_fd(int h) { asrt(ts[h].cmd_fdr == -1 && ts[h].cmd_fdw == -1); if(socketpair(AF_UNIX, SOCK_STREAM, 0, &ts[h].cmd_fdr) < 0) { APPL_TRACE_ERROR1("socketpair failed: %s", strerror(errno)); return; } APPL_TRACE_DEBUG3("h:%d, cmd_fdr:%d, cmd_fdw:%d", h, ts[h].cmd_fdr, ts[h].cmd_fdw); //add the cmd fd for read & write add_poll(h, ts[h].cmd_fdr, 0, SOCK_THREAD_FD_RD, 0); }
CompEventSource::CompEventSource (Display *dpy, int fd) : Glib::Source (), mDpy (dpy), mConnectionFD (fd) { mPollFD.set_fd (mConnectionFD); mPollFD.set_events (Glib::IO_IN); set_priority (G_PRIORITY_DEFAULT); add_poll (mPollFD); set_can_recurse (true); connect (sigc::mem_fun <bool, CompEventSource> (this, &CompEventSource::callback)); }
static gboolean gsk_packet_queue_fd_open (GskIO *io, GError **error) { GskPacketQueue *queue = GSK_PACKET_QUEUE (io); GskPacketQueueFd *packet_queue_fd = GSK_PACKET_QUEUE_FD (io); if (packet_queue_fd->fd < 0) { g_set_error (error, GSK_G_ERROR_DOMAIN, GSK_ERROR_OPEN_FAILED, _("must specify valid file-descriptor")); return FALSE; } g_return_val_if_fail (packet_queue_fd->source == NULL, FALSE); add_poll (packet_queue_fd); GSK_HOOK_SET_FLAG (GSK_IO_WRITE_HOOK (queue), IS_AVAILABLE); GSK_HOOK_SET_FLAG (GSK_IO_READ_HOOK (queue), IS_AVAILABLE); return TRUE; }
int main(int argc, char **argv) { int ret = EX_OK; int i; struct group *grgid; struct passwd *pwuid; timer_t timerid_mld, timerid_pim; struct pollfd fds[4]; nfds_t nfds = 0; struct sockaddr_storage from, to; socklen_t addrlen = sizeof(struct sockaddr_storage); unsigned int from_ifindex; char *buf; ret = parse_args(argc, argv); if (ret) return -ret; if (getuid()) { fprintf(stderr, "need to run as root\n"); return EX_NOPERM; } if (!nofork) { pid_t pid = fork(); if (pid < 0) { perror("fork()"); return EX_OSERR; } else if (pid > 0) return EX_OK; if (setsid() < 0) { perror("setsid()"); return EX_OSERR; } if (chdir("/") < 0) { perror("chdir(\"/\")"); return EX_OSERR; } openlog(basename(argv[0]), LOG_PID, LOG_DAEMON); } else openlog(basename(argv[0]), LOG_PID | LOG_PERROR, LOG_DAEMON); setlogmask(LOG_UPTO(debug)); logger(LOG_NOTICE, 0, "started"); errno = 0; mroute4 = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP); if (mroute4 >= 0) { if (pktinfo(mroute4) < 0) { close(mroute4); mroute4 = -1; } else { pim4 = pim_init(mroute4); if (pim4 < 0) { close(mroute4); mroute4 = -1; } else { add_poll(fds, &nfds, mroute4); add_poll(fds, &nfds, pim4); } } } if (mroute4 < 0) logger(LOG_WARNING, errno, "no IPv4 support"); errno = 0; mroute6 = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); if (mroute6 >= 0) { if (pktinfo(mroute6) < 0) { close(mroute6); mroute6 = -1; } else { pim6 = pim_init(mroute6); if (pim6 < 0) { close(mroute6); mroute6 = -1; } else { add_poll(fds, &nfds, mroute6); add_poll(fds, &nfds, pim6); } } } if (mroute6 < 0) logger(LOG_WARNING, errno, "no IPv6 support"); if (mroute4 < 0 && mroute6 < 0) { logger(LOG_ERR, 0, "multicast routing unavailable"); ret = -EX_OSERR; goto exit; } ret = route_init(); if (ret) goto mroute; errno = 0; grgid = getgrnam(gid); if (grgid) { if (setgid(grgid->gr_gid)) logger(LOG_WARNING, errno, "unable to drop group privileges"); } else logger(LOG_WARNING, errno, "unable to find group '%s' to drop privileges to", gid); errno = 0; pwuid = getpwnam(uid); if (pwuid) { if (setuid(pwuid->pw_uid)) logger(LOG_WARNING, errno, "unable to drop user privileges"); } else logger(LOG_WARNING, errno, "unable to find user '%s' to drop privileges to", uid); ret = signals(&sig_handler); if (ret) goto route; ret = prime_timers(&timerid_mld, &timerid_pim); if (ret) goto signal; buf = malloc(SOCK_BUFLEN); if (buf == NULL) { logger(LOG_ERR, 0, "malloc()"); ret = -EX_OSERR; goto timer; } while (running) { ret = poll(fds, nfds, -1); if (ret == -1) { if (errno == EINTR) continue; logger(LOG_ERR, errno, "poll()"); ret = -EX_OSERR; running = 0; continue; } for (i = 0; i < nfds; i++) { /* TODO handle errors */ assert(!(fds[i].revents & (POLLERR | POLLHUP))); /* either a non-event or there is something to read */ assert(!fds[i].revents || fds[i].revents & POLLIN); if (!fds[i].revents) continue; if (fds[i].revents & POLLIN) { ret = recvfromto(fds[i].fd, buf, SOCK_BUFLEN, 0, (struct sockaddr *)&from, (struct sockaddr *)&to, &addrlen, &from_ifindex); if (ret == -1) continue; if (fds[i].fd == pim4 || fds[i].fd == pim6) pim_recv(fds[i].fd, buf, ret, &from, &to, addrlen, from_ifindex); else mld_recv(fds[i].fd, buf, ret, &from, &to, addrlen, from_ifindex); } } } free(buf); timer: timer_delete(timerid_mld); timer_delete(timerid_pim); signal: signals(SIG_IGN); route: route_shutdown(); mroute: if (mroute4 > 0) { close(pim4); pim_shutdown(mroute4); } if (mroute6 > 0) { close(pim6); pim_shutdown(mroute6); } exit: logger(LOG_NOTICE, 0, "exiting"); closelog(); assert(ret <= 0); return -ret; }
int main(int argc, char *argv[]) { char buffer[MAX_PACKET_SIZE]; static struct sockaddr_in addr; int rc, i, count; struct timeval t1, t2, dt; KICK(argc < 3, "incorrect usage\n" "Usage:\n" "./linphone_proxy <local_ip> <remote_ip>\n"); local_ip = argv[1]; remote_ip = argv[2]; add_poll(&fds[0], STDIN_FILENO); proxy_to_linphone_socket = create_socket(SOCK_DGRAM, SIP_LINPHONE); init_sockaddr(&proxy_to_linphone_addr, "127.0.0.1", SIP_PORT); add_poll(&fds[1], proxy_to_linphone_socket); eprintf("created proxy_to_linphone SIP socket SRC:localhost:%d - DST:localhost:%d\n", SIP_LINPHONE, SIP_PORT); proxy_to_proxy_socket = create_socket(SOCK_DGRAM, SIP_PROXY); init_sockaddr(&proxy_to_proxy_addr, remote_ip, SIP_PROXY); add_poll(&fds[2], proxy_to_proxy_socket); eprintf("created proxy_to_sip SIP socket SRC:localhost:%d - DST:%s:%d\n", SIP_PROXY, remote_ip, SIP_PROXY); proxy_to_linphone_data_socket = create_socket(SOCK_DGRAM, DATA_LINPHONE); init_sockaddr(&proxy_to_linphone_data_addr, "127.0.0.1", DATA_PORT); add_poll(&fds[3], proxy_to_linphone_data_socket); eprintf("created proxy_to_linphone DATA socket SRC:localhost:%d - DST:localhost:%d\n", DATA_LINPHONE, DATA_PORT); proxy_to_proxy_data_socket = create_socket(SOCK_DGRAM, DATA_PROXY); init_sockaddr(&proxy_to_proxy_data_addr, remote_ip, DATA_PROXY); add_poll(&fds[4], proxy_to_proxy_data_socket); eprintf("created proxy_to_proxy DATA socket SRC:localhost:%d - DST:%s:%d\n", DATA_PROXY, remote_ip, DATA_PROXY); manager_socket = create_socket(SOCK_DGRAM, MANAGER_PORT); init_sockaddr(&manager_addr, "0.0.0.0", MANAGER_PORT); add_poll(&fds[5], manager_socket); eprintf("created manager socket SRC:localhost:%d - DST:0.0.0.0:0\n", MANAGER_PORT); while (1) { rc = poll(fds, NUM_FDS, -1); DIE(-1 == rc, "poll"); for (i = 0; i < NUM_FDS; i++) { if (0 == fds[i].revents) { continue; } switch(i) { /* receive line from console */ case 0: break; /* receive SIP packet from linphone */ case 1: gettimeofday_safe(&t1); count = recv_msg(fds[i].fd, &proxy_to_linphone_addr, buffer); if (begins_with(buffer, "INVITE")) { copy_packet(&out_invite, buffer, count); //printf("captured INVITE packet:\n%s\n", out_invite.buffer); } else if (begins_with(buffer, "ACK")) { copy_packet(&out_ack, buffer, count); //printf("captured ACK packet:\n%s\n", out_ack.buffer); } else if (strstr(buffer, "200 OK") && strstr(buffer, "OPTIONS" )) { copy_packet(&out_op_ok, buffer, count); //printf("captured OPTIONS OK packet:\n%s\n", out_op_ok.buffer); } send_msg(proxy_to_proxy_socket, &proxy_to_proxy_addr, buffer, count); gettimeofday_safe(&t2); time_diff(&t1, &t2, &dt); printf("time case 1: %lu.%06lu\n", dt.tv_sec, dt.tv_usec); break; /* receive SIP packet from proxy */ case 2: gettimeofday_safe(&t1); count = recv_msg(fds[i].fd, &addr, buffer); send_msg(proxy_to_linphone_socket, &proxy_to_linphone_addr, buffer, count); gettimeofday_safe(&t2); time_diff(&t1, &t2, &dt); printf("time case 2: %lu.%06lu\n", dt.tv_sec, dt.tv_usec); break; /* receive data packet from linphone */ case 3: gettimeofday_safe(&t1); count = recv_msg(fds[i].fd, &proxy_to_linphone_data_addr, buffer); send_msg(proxy_to_proxy_data_socket, &proxy_to_proxy_data_addr, buffer, count); gettimeofday_safe(&t2); time_diff(&t1, &t2, &dt); printf("time case 3: %lu.%06lu\n", dt.tv_sec, dt.tv_usec); break; /* receive data packet from proxy */ case 4: gettimeofday_safe(&t1); count = recv_msg(fds[i].fd, &addr, buffer); send_msg(proxy_to_linphone_data_socket, &proxy_to_linphone_data_addr, buffer, count); gettimeofday_safe(&t2); time_diff(&t1, &t2, &dt); printf("time case 4: %lu.%06lu\n", dt.tv_sec, dt.tv_usec); break; /* receive command from manager */ case 5: count = recv_msg(fds[i].fd, &manager_addr, buffer); if (begins_with(buffer, "IP: ")) { while (!isdigit(buffer[count - 1])) { buffer[--count] = 0; } strcpy(migrate_ip, buffer + 4); migrate_init(); } else if (begins_with(buffer, "establish")) { migrate_establish(); } else if (begins_with(buffer, "redirect: ")) { while (!isdigit(buffer[count - 1])) { buffer[--count] = 0; } strcpy(redirect_ip, buffer + 10); migrate_redirect(); } break; /* error */ default: break; } } } return EXIT_SUCCESS; }
static inline void migrate_establish() { char buffer[MAX_PACKET_SIZE]; struct pollfd fd; int count, rc; char *p, *q; add_poll(&fd, manager_socket); rc = poll(&fd, 1, -1); DIE(-1 == rc, "poll"); count = recv_msg(manager_socket, &manager_addr, buffer); copy_packet(&useful_invite, buffer, count); eprintf("%s\n\n", useful_invite.buffer); rc = poll(&fd, 1, -1); DIE(-1 == rc, "poll"); count = recv_msg(manager_socket, &manager_addr, buffer); copy_packet(&useful_ack, buffer, count); eprintf("%s\n\n", useful_ack.buffer); rc = poll(&fd, 1, -1); DIE(-1 == rc, "poll"); count = recv_msg(manager_socket, &manager_addr, buffer); copy_packet(&useful_op_ok, buffer, count); eprintf("%s\n\n", buffer); get_data(useful_ack.buffer, &ack_data); printf("\nACK data\n"); printf("branch: %s\n", ack_data.branch); printf("From: %s\n", ack_data.from); printf("tag: %s\n", ack_data.from_tag); printf("To: %s\n", ack_data.to); printf("tag: %s\n", ack_data.to_tag); printf("Call-id: %s\n", ack_data.call_id); printf("\n"); get_data(useful_op_ok.buffer, &op_ok_data); printf("\nOPTIONS OK data\n"); printf("branch: %s\n", op_ok_data.branch); printf("From: %s\n", op_ok_data.from); printf("tag: %s\n", op_ok_data.from_tag); printf("To: %s\n", op_ok_data.to); printf("tag: %s\n", op_ok_data.to_tag); printf("Call-id: %s\n", op_ok_data.call_id); printf("\n"); p = strstr(ack_data.from, "@") + 1; q = strstr(p, ">"); count = q - p; strncpy(initial_ip, p, count); initial_ip[count] = 0; printf("initial_ip: %s\n", initial_ip); replace_all(useful_invite.buffer, remote_ip, local_ip); replace_all(useful_invite.buffer, initial_ip, remote_ip); replace_all(useful_invite.buffer, ack_data.from_tag, ack_data.to_tag); useful_invite.size = strlen(useful_invite.buffer); printf("INVITE:\n%s\n", useful_invite.buffer); send_msg(proxy_to_linphone_socket, &proxy_to_linphone_addr, useful_invite.buffer, strlen(useful_invite.buffer)); add_poll(&fd, proxy_to_linphone_socket); rc = poll(&fd, 1, -1); DIE(-1 == rc, "poll"); recv_msg(proxy_to_linphone_socket, &proxy_to_linphone_addr, buffer); printf("\nreceived:\n%s\n", buffer); rc = poll(&fd, 1, -1); DIE(-1 == rc, "poll"); recv_msg(proxy_to_linphone_socket, &proxy_to_linphone_addr, buffer); printf("\nreceived:\n%s\n", buffer); get_data(buffer, &data_101); rc = poll(&fd, 1, -1); DIE(-1 == rc, "poll"); count = recv_msg(proxy_to_linphone_socket, &proxy_to_linphone_addr, buffer); copy_packet(&options, buffer, count); printf("\nreceived:\n%s\n", buffer); get_data(options.buffer, &options_data); printf("\nOPTIONS data\n"); printf("branch: %s\n", options_data.branch); printf("From: %s\n", options_data.from); printf("tag: %s\n", options_data.from_tag); printf("To: %s\n", options_data.to); printf("tag: %s\n", options_data.to_tag); printf("Call-id: %s\n", options_data.call_id); printf("\n"); replace_all(useful_op_ok.buffer, remote_ip, local_ip); replace_all(useful_op_ok.buffer, initial_ip, remote_ip); replace_all(useful_op_ok.buffer, op_ok_data.from_tag, options_data.from_tag); replace_all(useful_op_ok.buffer, op_ok_data.to_tag, options_data.to_tag); replace_all(useful_op_ok.buffer, op_ok_data.branch, options_data.branch); replace_all(useful_op_ok.buffer, op_ok_data.call_id, options_data.call_id); printf("\nforged OPTIONS OK: \n%s\n", useful_op_ok.buffer); send_msg(proxy_to_linphone_socket, &proxy_to_linphone_addr, useful_op_ok.buffer, strlen(useful_op_ok.buffer)); replace_all(useful_ack.buffer, remote_ip, local_ip); replace_all(useful_ack.buffer, initial_ip, remote_ip); replace_all(useful_ack.buffer, ack_data.from_tag, "magic"); replace_all(useful_ack.buffer, ack_data.to_tag, data_101.to_tag); replace_all(useful_ack.buffer, "magic", ack_data.to_tag); printf("\nforged ACK: \n%s\n", useful_ack.buffer); printf("1\n"); rc = poll(&fd, 1, -1); DIE(-1 == rc, "poll"); recv_msg(proxy_to_linphone_socket, &proxy_to_linphone_addr, buffer); printf("\nreceived:\n%s\n", buffer); printf("2\n"); rc = poll(&fd, 1, -1); DIE(-1 == rc, "poll"); recv_msg(proxy_to_linphone_socket, &proxy_to_linphone_addr, buffer); printf("\nreceived:\n%s\n", buffer); printf("3\n"); send_msg(proxy_to_linphone_socket, &proxy_to_linphone_addr, useful_ack.buffer, strlen(useful_ack.buffer)); printf("4\n"); strcpy(buffer, "done"); send_msg(manager_socket, &manager_addr, buffer, 4); printf("5\n"); }