int main(int argc, char *const argv[]) { int opt; char *srvaddr = "127.0.0.1"; int id = 11; int tag = 1; /* the id and server address don't really matter, * as we don't register to the server. */ while ((opt = getopt(argc, argv, "i:S:nf:t:h:d:")) != -1) { switch (opt) { case 'i': id = atoi(optarg); break; case 'S': srvaddr = optarg; break; case 'n': nonblock = 1; break; case 'f': rate = atoi(optarg); break; case 't': tag = atoi(optarg); break; case 'h': hold = atoi(optarg); break; case 'd': parse_sockaddr_in(&dest_addr, optarg); /* default send to the xcast's port if not set. */ if( dest_addr.sin_port == 0 ) dest_addr.sin_port = htons(MIX_CAST_PORT); break; } } open_audio_out(); set_option(OPT_ACCESS_RAW_AUDIO_PACK, 1); set_event_callback(on_event); id |= (tag<<16); //wrap tag and id client_init(id, DEVTYPE_NETPLAY, srvaddr, AUDIO_PORT); while(1) sleep(10000); return 0; }
int main(int argc, char *argv[]) { int count = 0; int interface = 0; unsigned relay_ip = 0; unsigned relay_mask = 0; const char *tun = "socks0"; const char *script = "./tun2socks_ifup.socks0"; struct sockaddr_in relay = {0}; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-h") == 0) { usage(argv[0]); return 0; } else if (strcmp(argv[i], "-s") == 0 && i + 1 < argc) { script = argv[i + 1]; i++; } else if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) { tun = argv[i + 1]; i++; } else { parse_sockaddr_in(&relay, argv[i]); continue; } } setuid(0); interface = get_interface(tun); run_config_script(tun, script); relay_ip = (relay.sin_addr.s_addr); relay_mask = htonl(0xffff); for (;;) { int ln1, xdat; u_char buf[1500], packet[1500]; int num = read(interface, packet, sizeof(packet)); if (num <= 0) { perror("interface read"); for (int i = 0; i < 10; i++) { sleep(1); fprintf(stderr, "."); } break; } ln1 = translate_ip2ip(buf, sizeof(buf), packet, num, relay_ip, relay_mask, relay.sin_port); if (ln1 > 0) { write(interface, buf, ln1); } } return 0; }
void master_mode(const char *master_port, const char *start_script) { struct event listen_event; struct sockaddr_in dst; int fd; LIST_INIT(&active_peers); LIST_INIT(&inactive_peers); LIST_INIT(&unassigned_peers); event_init(); if (parse_sockaddr_in(master_port, &dst)) errx(1, "Could not parse addr/port"); fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) err(1, "Could not create socket"); #ifdef FIOCLEX if (ioctl(fd, FIOCLEX, NULL) == -1) err(1, "Could not set close-on-exec flag"); #endif if (bind(fd, (struct sockaddr *)&dst, sizeof(dst)) == -1) err(1, "Could not bind socket"); if (listen(fd, 5) == -1) err(1, "Could not listen on socket"); if (start_script) { pid_t child; int status; if ((child = vfork()) == 0) { execlp(start_script, start_script, (char *)NULL); _exit(255); } if (child == -1) err(1, "Could not fork start script"); waitpid(child, &status, 0); if (status != 0) err(1, "Start script failed"); } event_set(&listen_event, fd, EV_READ | EV_PERSIST, listen_handler, NULL); event_add(&listen_event, NULL); event_dispatch(); (void)close(fd); }
void master_mode(const char *master_port, const char *start_script) { struct sockaddr_in dst; int fd; LIST_INIT(&active_peers); LIST_INIT(&inactive_peers); event_init(); if (parse_sockaddr_in(master_port, &dst)) errx(1, "Could not parse addr/port"); fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) err(1, "Could not create socket"); #ifdef FIOCLEX if (ioctl(fd, FIOCLEX, NULL) == -1) err(1, "Could not set close-on-exec flag"); #endif if (bind(fd, (struct sockaddr *)&dst, sizeof(dst)) == -1) err(1, "Could not bind socket"); if (listen(fd, 5) == -1) err(1, "Could not listen on socket"); event_add(&listen_event, fd, 0, 1, listen_handler, NULL); listen_event_socket = fd; if (start_script) { signal_add(&child_event, SIGCHLD, child_handler); if ((child_pid = vfork()) == 0) { execlp(start_script, start_script, (char *)NULL); _exit(255); } if (child_pid == -1) err(1, "Could not fork start script"); } else { clients_started = 1; } event_dispatch(); (void)close(fd); }
void stat_mode(const char *client_port) { struct sockaddr_in dst; ssize_t recv_bytes, sent_bytes; char buf[7 * 4]; struct build_stat st; uint32_t tmp; int fd; if (parse_sockaddr_in(client_port, &dst)) errx(1, "Could not parse addr/port"); fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) err(1, "Could not create socket"); if (connect(fd, (struct sockaddr *)&dst, sizeof(dst)) == -1) err(1, "Could not connect socket"); sent_bytes = write(fd, "S", 1); if (sent_bytes == -1) err(1, "Could not write to socket"); if (sent_bytes == 0) exit(0); if (sent_bytes != 1) errx(1, "Premature end of stream while writing to socket"); recv_bytes = atomic_read(fd, &buf, 7 * 4); if (recv_bytes == 0 || (recv_bytes == -1 && errno == ECONNRESET)) exit(0); if (recv_bytes == -1) err(1, "Could not read from socket"); if (recv_bytes != 7 * 4) errx(1, "Premature end while reading statistics from socket"); (void)memcpy(&tmp, buf, 4); st.open_jobs = ntohl(tmp); (void)memcpy(&tmp, buf + 4, 4); st.in_processing = ntohl(tmp); (void)memcpy(&tmp, buf + 8, 4); st.failed = ntohl(tmp); (void)memcpy(&tmp, buf + 12, 4); st.prefailed = ntohl(tmp); (void)memcpy(&tmp, buf + 16, 4); st.indirect_failed = ntohl(tmp); (void)memcpy(&tmp, buf + 20, 4); st.indirect_prefailed = ntohl(tmp); (void)memcpy(&tmp, buf + 24, 4); st.done = ntohl(tmp); (void)printf("Jobs not yet processed: %zu\n", st.open_jobs); (void)printf("Jobs currently in processing: %zu\n", st.in_processing); (void)printf("Successful builds: %zu\n", st.done); (void)printf("Failing builds: %zu\n", st.failed + st.prefailed + st.indirect_failed + st.indirect_prefailed); (void)printf(" Directly broken: %zu\n", st.failed); (void)printf(" Broken due to a broken dependency: %zu\n", st.indirect_failed); (void)printf(" Not build as explicitly marked broken: %zu\n", st.prefailed); (void)printf(" Broken due to an explicitly broken dependency: %zu\n", st.indirect_prefailed); exit(0); }
void client_mode(const char *client_port) { struct sockaddr_in dst; uint32_t build_info_len; ssize_t recv_bytes, sent_bytes; char *build_info; int fd; if (parse_sockaddr_in(client_port, &dst)) errx(1, "Could not parse addr/port"); fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) err(1, "Could not create socket"); if (connect(fd, (struct sockaddr *)&dst, sizeof(dst)) == -1) err(1, "Could not connect socket"); loop: sent_bytes = atomic_write(fd, "G", 1); if (sent_bytes == -1) err(1, "Could not write to socket"); if (sent_bytes == 0) exit(0); if (sent_bytes != 1) errx(1, "Premature end of stream while writing to socket"); recv_bytes = atomic_read(fd, &build_info_len, 4); if (recv_bytes == 0 || (recv_bytes == -1 && errno == ECONNRESET)) exit(0); if (recv_bytes == -1) err(1, "Could not read from socket"); if (recv_bytes != 4) errx(1, "Premature end while reading build info from socket"); build_info_len = ntohl(build_info_len); if (build_info_len < 10 || build_info_len > 0xffffff) errx(1, "Invalid build info length from master"); build_info = xmalloc(build_info_len + 1); build_info[build_info_len] = '\0'; recv_bytes = atomic_read(fd, build_info, build_info_len); if (recv_bytes == -1) err(1, "Could not read from socket"); if ((uint32_t)recv_bytes != build_info_len || strlen(build_info) != build_info_len) errx(1, "Premature end of stream while reading path from socket"); if (verbosity > 0) { const char *begin, *end; if (strncmp(build_info, "PKGNAME=", 8) != 0) err(1, "Inconsistent build info from server"); begin = build_info + 8; if ((end = strchr(begin, '\n')) == NULL) err(1, "Inconsistent build info from server"); printf("Building package %.*s\n", (int)(end - begin), begin); fflush(stdout); } if (build_package(build_info, build_info_len) == 0) sent_bytes = atomic_write(fd, "D", 1); else sent_bytes = atomic_write(fd, "F", 1); if (sent_bytes == -1) err(1, "Could not write to socket"); if (sent_bytes != 1) errx(1, "Premature end of stream while writing to socket"); free(build_info); goto loop; }