void get_ip(inetface_t *iface, bool update_hwaddr) { header_list_t *list; if (!iface->up || (iface->up && is_ip_public(iface->ip))) { if (!switch_to_private_network(iface)) { log_error("Could not switch to private network"); return; } } list = retrieve_configuration(iface); if (list == NULL) { log_error("Could not retrieve configuration information"); return; } configure_network(iface, list, update_hwaddr); if (Config.update_passwd) configure_password(list); header_list_free(list); }
int start_mongoose(int ether) { struct mg_context* ctx; // Initialize random number generator. It will be used later on for // the session identifier creation. srand((unsigned)time(0)); // Setup and start Mongoose const char* options[] = { "document_root", ODI_HOME, "listening_ports", WEB_SERVER_PORT, //"access_log_file", "/odi/log/mongoose_access", "error_log_file", "/odi/log/mongoose_error", "enable_directory_listing", "no", "ssl_certificate", SSL_KEY_FILE, "request_timeout_ms", REQUESTED_TIME_OUT, NULL }; if ((ctx = mg_start(options, event_handler, NULL)) == NULL) { logger_error("%s\n", "Cannot start REMOTEM, fatal exit"); exit(EXIT_FAILURE); } logger_remotem("Web server root directory is %s, started on ports %s", mg_get_option(ctx, "document_root"), mg_get_option(ctx, "listening_ports")); strcpy(downloading_status, "Idle"); get_config_info(); if (ether) configure_network(); DVR_ID = getSerial(); return EXIT_SUCCESS; }
/** * Function to run the tunnel */ void run_tunnel(char *dest, int server) { struct icmp_packet packet; int tun_fd, sock_fd; fd_set fs; tun_fd = tun_alloc("tun0", IFF_TUN | IFF_NO_PI); printf("[DEBUG] Starting tunnel - Dest: %s, Server: %d\n", dest, server); printf("[DEBUG] Opening ICMP socket\n"); sock_fd = open_icmp_socket(); if (server) { printf("[DEBUG] Binding ICMP socket\n"); bind_icmp_socket(sock_fd); } configure_network(server); while (1) { FD_ZERO(&fs); FD_SET(tun_fd, &fs); FD_SET(sock_fd, &fs); select(tun_fd>sock_fd?tun_fd+1:sock_fd+1, &fs, NULL, NULL, NULL); if (FD_ISSET(tun_fd, &fs)) { printf("[DEBUG] Data needs to be readed from tun device\n"); // Reading data from tun device and sending ICMP packet printf("[DEBUG] Preparing ICMP packet to be sent\n"); // Preparing ICMP packet to be sent memset(&packet, 0, sizeof(struct icmp_packet)); printf("[DEBUG] Destination address: %s\n", dest); strcpy(packet.src_addr, "0.0.0.0"); strcpy(packet.dest_addr, dest); if(server) { set_reply_type(&packet); } else { set_echo_type(&packet); } packet.payload = malloc(MTU); packet.payload_size = tun_read(tun_fd, packet.payload, MTU); if(packet.payload_size == -1) { perror("Error while reading from tun device\n"); exit(EXIT_FAILURE); } printf("[DEBUG] Sending ICMP packet with payload_size: %d, payload: %s\n", packet.payload_size, packet.payload); // Sending ICMP packet send_icmp_packet(sock_fd, &packet); free(packet.payload); } if (FD_ISSET(sock_fd, &fs)) { printf("[DEBUG] Received ICMP packet\n"); // Reading data from remote socket and sending to tun device // Getting ICMP packet memset(&packet, 0, sizeof(struct icmp_packet)); receive_icmp_packet(sock_fd, &packet); printf("[DEBUG] Read ICMP packet with src: %s, dest: %s, payload_size: %d, payload: %s\n", packet.src_addr, packet.dest_addr, packet.payload_size, packet.payload); // Writing out to tun device tun_write(tun_fd, packet.payload, packet.payload_size); printf("[DEBUG] Src address being copied: %s\n", packet.src_addr); strcpy(dest, packet.src_addr); } } }
/* sighup_handler() is invoked when carrier drops, eg. before redial. */ static void sighup_handler(int signo __unused) { if(exiting) return; if (redial_cmd == NULL) { syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); exiting", dev, unit); exit_handler(1); } again: /* invoke a shell for redial_cmd or punt. */ if (*redial_cmd) { /* Non-empty redial command */ syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); running '%s'", dev, unit, redial_cmd); acquire_line(); /* reopen dead line */ setup_line(CLOCAL); if (locked) { if (uucp_lock) uu_unlock(dvname); /* for redial */ locked = 0; } if (system(redial_cmd)) goto again; if (uucp_lock) { int res; if ((res = uu_lock(dvname)) != UU_LOCK_OK) { if (res != UU_LOCK_INUSE) syslog(LOG_ERR, "uu_lock: %s", uu_lockerr(res)); syslog(LOG_ERR, "can't relock %s after %s, aborting", dev, redial_cmd); exit_handler(1); } locked = 1; } /* Now check again for carrier (dial command is done): */ if (!(modem_control & CLOCAL)) { tty.c_cflag &= ~CLOCAL; if (tcsetattr(fd, TCSAFLUSH, &tty) < 0) { syslog(LOG_ERR, "tcsetattr(TCSAFLUSH): %m"); exit_handler(1); } ioctl(fd, TIOCMGET, &comstate); if (!(comstate & TIOCM_CD)) { /* check for carrier */ /* force a redial if no carrier */ goto again; } } else setup_line(0); } else { /* Empty redial command */ syslog(LOG_NOTICE,"SIGHUP on %s (sl%d); reestablish connection", dev, unit); acquire_line(); /* reopen dead line */ setup_line(0); /* restore ospeed from hangup (B0) */ /* If modem control, just wait for carrier before attaching. If no modem control, just fall through immediately. */ if (!(modem_control & CLOCAL)) { int carrier = 0; syslog(LOG_NOTICE, "waiting for carrier on %s (sl%d)", dev, unit); /* Now wait for carrier before attaching line. */ /* We must poll since CLOCAL prevents signal. */ while (! carrier) { sleep(2); ioctl(fd, TIOCMGET, &comstate); if (comstate & TIOCM_CD) carrier = 1; } syslog(LOG_NOTICE, "carrier now present on %s (sl%d)", dev, unit); } } slip_discipline(); configure_network(); }
int main(int argc, char **argv) { int option; while ((option = getopt(argc, argv, "ace:fhlnr:s:u:zLK:O:S:")) != -1) { switch (option) { case 'a': slflags |= IFF_LINK2; slflags &= ~IFF_LINK0; break; case 'c': slflags |= IFF_LINK0; slflags &= ~IFF_LINK2; break; case 'e': exit_cmd = (char*) strdup (optarg); break; case 'f': foreground = 1; break; case 'h': flow_control |= CRTSCTS; break; case 'l': modem_control = CLOCAL; /* clear HUPCL too */ break; case 'n': slflags |= IFF_LINK1; break; case 'r': redial_cmd = (char*) strdup (optarg); break; case 's': speed = atoi(optarg); break; case 'u': config_cmd = (char*) strdup (optarg); break; case 'z': redial_on_startup = 1; break; case 'L': uucp_lock = 1; break; case 'K': keepal = atoi(optarg); break; case 'O': outfill = atoi(optarg); break; case 'S': sl_unit = atoi(optarg); break; case '?': default: usage(); exit_handler(1); } } if (optind == argc - 1) dev = argv[optind]; if (optind < (argc - 1)) warnx("too many args, first='%s'", argv[optind]); if (optind > (argc - 1)) warnx("not enough args"); if (dev == NULL) { usage(); exit_handler(2); } if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) { strcpy(tty_path, _PATH_DEV); strcat(tty_path, "/"); strncat(tty_path, dev, 10); dev = tty_path; } dvname = strrchr(dev, '/'); /* always succeeds */ dvname++; /* trailing tty pathname component */ snprintf(pidfilename, sizeof(pidfilename), "%sslattach.%s.pid", _PATH_VARRUN, dvname); printf("%s\n",pidfilename); if (!foreground) daemon(0,0); /* fork, setsid, chdir /, and close std*. */ /* daemon() closed stderr, so log errors from here on. */ openlog("slattach",LOG_CONS|LOG_PID,LOG_DAEMON); acquire_line(); /* get tty device as controlling terminal */ setup_line(0); /* configure for slip line discipline */ slip_discipline(); /* switch to slip line discipline */ /* upon INT log a timestamp and exit. */ if (signal(SIGINT,sigint_handler) == SIG_ERR) syslog(LOG_NOTICE,"cannot install SIGINT handler: %m"); /* upon TERM log a timestamp and exit. */ if (signal(SIGTERM,sigterm_handler) == SIG_ERR) syslog(LOG_NOTICE,"cannot install SIGTERM handler: %m"); /* upon HUP redial and reconnect. */ if (signal(SIGHUP,sighup_handler) == SIG_ERR) syslog(LOG_NOTICE,"cannot install SIGHUP handler: %m"); if (redial_on_startup) sighup_handler(0); else if (!(modem_control & CLOCAL)) { if (ioctl(fd, TIOCMGET, &comstate) < 0) syslog(LOG_NOTICE,"cannot get carrier state: %m"); if (!(comstate & TIOCM_CD)) { /* check for carrier */ /* force a redial if no carrier */ kill (getpid(), SIGHUP); } else configure_network(); } else configure_network(); /* configure the network if needed. */ for (;;) { sigset_t mask; sigemptyset(&mask); sigsuspend(&mask); } }