int MenuSubnetEingabe::buttonOk() { if (state == IPEINGABESTATEOK) { cout << "Sichere neue Subnetz-Masken-Einstellungen..." << std::endl; // Alte Einstellungen laden std::ifstream ifs("/etc/network/interfaces"); std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); cout << "[1/4] Aktuelle Einstellungen geladen." << std::endl; // alte IP durch neue IP ersetzen size_t start_pos = content.find(originalIp); if(start_pos == std::string::npos) { return false; } content.replace(start_pos, originalIp.length(), getIpString()); cout << "[2/4] Neue Einstellungen erstellt." << std::endl; // Neue Einstellungen in Datei schreiben std::ofstream out("/etc/network/interfaces"); out << content; out.close(); cout << "[3/4] Neue Einstellungen gespeichert." << std::endl; cout << "[4/4] Setzen der neuen Subnetz-Maske." << std::endl; system(("ifconfig eth0 netmask " + getIpString()).c_str()); this->settingsSaved(); } else if (state == IPEINGABESTATEBACK) { return MENURETURN; } return MENUSTAY; }
int addParticipant(participantsDb_t db, struct sockaddr_in *addr, int capabilities, unsigned int rcvbuf, int pointopoint) { int i; if((i = lookupParticipant(db, addr)) >= 0) return i; for (i=0; i < MAX_CLIENTS; i++) { if (!db->clientTable[i].used) { char ipBuffer[16]; db->clientTable[i].addr = *addr; db->clientTable[i].used = 1; db->clientTable[i].capabilities = capabilities; db->clientTable[i].rcvbuf = rcvbuf; db->nrParticipants++; flprintf("New connection from %s (#%d) %08x\n", getIpString(addr, ipBuffer), i, capabilities); #ifdef USE_SYSLOG syslog(LOG_INFO, "New connection from %s (#%d)\n", getIpString(addr, ipBuffer), i); #endif return i; } else if(pointopoint) return -1; } return -1; /* no space left in participant's table */ }
int removeParticipant(struct participantsDb *db, int i) { if(db->clientTable[i].used) { char ipBuffer[16]; flprintf("Disconnecting #%d (%s)\n", i, getIpString(&db->clientTable[i].addr, ipBuffer)); #ifdef USE_SYSLOG syslog(LOG_INFO, "Disconnecting #%d (%s)\n", i, getIpString(&db->clientTable[i].addr, ipBuffer)); #endif db->clientTable[i].used = 0; db->nrParticipants--; } return 0; }
int MenuDnsEingabe::buttonOk() { if (state == IPEINGABESTATEOK) { cout << "Sichere neue DNS-Einstellungen..." << std::endl; // Alte Einstellungen laden std::ifstream ifs("/etc/network/interfaces"); std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); cout << "[1/4] Aktuelle Einstellungen geladen." << std::endl; // alte IP durch neue IP ersetzen size_t start_pos = content.find(originalIp); if(start_pos == std::string::npos) { return false; } content.replace(start_pos, originalIp.length(), getIpString()); cout << "[2/4] Neue Einstellungen erstellt." << std::endl; // Neue Einstellungen in Datei schreiben std::ofstream out("/etc/network/interfaces"); out << content; out.close(); cout << "[3/4] Neue Einstellungen gespeichert." << std::endl; cout << "[4/4] Starte Netzwerk-Service neu..." << std::endl; cout << "[4/4] ACHTUNG: Dieser Vorgang wird unter Umstaenden die SSH-Verbindung trennen!" << std::endl; system("ifconfig down eth0 && ifconfig up eth0"); this->settingsSaved(); } else if (state == IPEINGABESTATEBACK) { return MENURETURN; } return MENUSTAY; }
static int sendRawData(int sock, struct net_config *config, char *header, int headerSize, unsigned char *data, int dataSize) { struct iovec iov[2]; struct msghdr hdr; int packetSize; int ret; iov[0].iov_base = header; iov[0].iov_len = headerSize; iov[1].iov_base = data; iov[1].iov_len = dataSize; hdr.msg_name = &config->dataMcastAddr; hdr.msg_namelen = sizeof(struct sockaddr_in); hdr.msg_iov = iov; hdr.msg_iovlen = 2; initMsgHdr(&hdr); packetSize = dataSize + headerSize; rgWaitAll(config, sock, config->dataMcastAddr.sin_addr.s_addr, packetSize); ret = sendmsg(sock, &hdr, 0); if (ret < 0) { char ipBuffer[16]; udpc_fatal(1, "Could not broadcast data packet to %s:%d (%s)\n", getIpString(&config->dataMcastAddr, ipBuffer), getPort(&config->dataMcastAddr), strerror(errno)); } return 0; }
static int sendRawData(int sock, struct net_config *config, char *header, int headerSize, unsigned char *data, int dataSize) { struct iovec iov[2]; struct msghdr hdr; int packetSize; int ret; iov[0].iov_base = header; iov[0].iov_len = headerSize; iov[1].iov_base = data; iov[1].iov_len = dataSize; hdr.msg_name = &config->dataMcastAddr; hdr.msg_namelen = sizeof(struct sockaddr_in); hdr.msg_iov = iov; hdr.msg_iovlen = 2; initMsgHdr(&hdr); packetSize = dataSize + headerSize; doRateLimit(config->rateLimit, packetSize); #ifdef FLAG_AUTORATE if(config->flags & FLAG_AUTORATE) doAutoRateLimit(sock, config->dir, config->sendbuf, packetSize); #endif ret = sendmsg(sock, &hdr, 0); if (ret < 0) { char ipBuffer[16]; udpc_fatal(1, "Could not broadcast data packet to %s:%d (%s)\n", getIpString(&config->dataMcastAddr, ipBuffer), getPort(&config->dataMcastAddr), strerror(errno)); } return 0; }
int doReceive(int s, void *message, size_t len, struct sockaddr_in *from, int portBase) { socklen_t slen; int r; unsigned short port; char ipBuffer[16]; slen = sizeof(*from); #ifdef LOSSTEST loseRecvPacket(s); #endif r = recvfrom(s, message, len, 0, (struct sockaddr *)from, &slen); if (r < 0) return r; port = ntohs(from->sin_port); if(port != RECEIVER_PORT(portBase) && port != SENDER_PORT(portBase)) { udpc_flprintf("Bad message from port %s.%d\n", getIpString(from, ipBuffer), ntohs(((struct sockaddr_in *)from)->sin_port)); return -1; } /* flprintf("recv: %08x %d\n", *(int*) message, r);*/ return r; }
int startReceiver(int doWarn, struct disk_config *disk_config, struct net_config *net_config, struct stat_config *stat_config, const char *ifName) { char ipBuffer[16]; union serverControlMsg Msg; int connectReqSent=0; struct client_config client_config; int outFile=1; int pipedOutFile; struct sockaddr_in myIp; int pipePid = 0; int origOutFile; int haveServerAddress; client_config.sender_is_newgen = 0; net_config->net_if = getNetIf(ifName); zeroSockArray(client_config.socks, NR_CLIENT_SOCKS); client_config.S_UCAST = makeSocket(ADDR_TYPE_UCAST, net_config->net_if, 0, RECEIVER_PORT(net_config->portBase)); client_config.S_BCAST = makeSocket(ADDR_TYPE_BCAST, net_config->net_if, 0, RECEIVER_PORT(net_config->portBase)); if(net_config->ttl == 1 && net_config->mcastRdv == NULL) { getBroadCastAddress(net_config->net_if, &net_config->controlMcastAddr, SENDER_PORT(net_config->portBase)); setSocketToBroadcast(client_config.S_UCAST); } else { getMcastAllAddress(&net_config->controlMcastAddr, net_config->mcastRdv, SENDER_PORT(net_config->portBase)); if(isMcastAddress(&net_config->controlMcastAddr)) { setMcastDestination(client_config.S_UCAST, net_config->net_if, &net_config->controlMcastAddr); setTtl(client_config.S_UCAST, net_config->ttl); client_config.S_MCAST_CTRL = makeSocket(ADDR_TYPE_MCAST, net_config->net_if, &net_config->controlMcastAddr, RECEIVER_PORT(net_config->portBase)); // TODO: subscribe address as receiver to! } } clearIp(&net_config->dataMcastAddr); udpc_flprintf("%sUDP receiver for %s at ", disk_config->pipeName == NULL ? "" : "Compressed ", disk_config->fileName == NULL ? "(stdout)":disk_config->fileName); printMyIp(net_config->net_if); udpc_flprintf(" on %s\n", net_config->net_if->name); connectReqSent = 0; haveServerAddress = 0; client_config.clientNumber= 0; /*default number for asynchronous transfer*/ while(1) { // int len; int msglen; int sock; if (!connectReqSent) { if (sendConnectReq(&client_config, net_config, haveServerAddress) < 0) { perror("sendto to locate server"); } connectReqSent = 1; } haveServerAddress=0; sock = udpc_selectSock(client_config.socks, NR_CLIENT_SOCKS, net_config->startTimeout); if(sock < 0) { return -1; } // len = sizeof(server); msglen=RECV(sock, Msg, client_config.serverAddr, net_config->portBase); if (msglen < 0) { perror("recvfrom to locate server"); exit(1); } if(getPort(&client_config.serverAddr) != SENDER_PORT(net_config->portBase)) /* not from the right port */ continue; switch(ntohs(Msg.opCode)) { case CMD_CONNECT_REPLY: client_config.clientNumber = ntohl(Msg.connectReply.clNr); net_config->blockSize = ntohl(Msg.connectReply.blockSize); udpc_flprintf("received message, cap=%08lx\n", (long) ntohl(Msg.connectReply.capabilities)); if(ntohl(Msg.connectReply.capabilities) & CAP_NEW_GEN) { client_config.sender_is_newgen = 1; copyFromMessage(&net_config->dataMcastAddr, Msg.connectReply.mcastAddr); } if (client_config.clientNumber == -1) { udpc_fatal(1, "Too many clients already connected\n"); } goto break_loop; case CMD_HELLO_STREAMING: case CMD_HELLO_NEW: case CMD_HELLO: connectReqSent = 0; if(ntohs(Msg.opCode) == CMD_HELLO_STREAMING) net_config->flags |= FLAG_STREAMING; if(ntohl(Msg.hello.capabilities) & CAP_NEW_GEN) { client_config.sender_is_newgen = 1; copyFromMessage(&net_config->dataMcastAddr, Msg.hello.mcastAddr); net_config->blockSize = ntohs(Msg.hello.blockSize); if(ntohl(Msg.hello.capabilities) & CAP_ASYNC) net_config->flags |= FLAG_PASSIVE; if(net_config->flags & FLAG_PASSIVE) goto break_loop; } haveServerAddress=1; continue; case CMD_CONNECT_REQ: case CMD_DATA: case CMD_FEC: continue; default: break; } udpc_fatal(1, "Bad server reply %04x. Other transfer in progress?\n", (unsigned short) ntohs(Msg.opCode)); } break_loop: udpc_flprintf("Connected as #%d to %s\n", client_config.clientNumber, getIpString(&client_config.serverAddr, ipBuffer)); getMyAddress(net_config->net_if, &myIp); if(!ipIsZero(&net_config->dataMcastAddr) && !ipIsEqual(&net_config->dataMcastAddr, &myIp) && (ipIsZero(&net_config->controlMcastAddr) || !ipIsEqual(&net_config->dataMcastAddr, &net_config->controlMcastAddr) )) { udpc_flprintf("Listening to multicast on %s\n", getIpString(&net_config->dataMcastAddr, ipBuffer)); client_config.S_MCAST_DATA = makeSocket(ADDR_TYPE_MCAST, net_config->net_if, &net_config->dataMcastAddr, RECEIVER_PORT(net_config->portBase)); } if(net_config->requestedBufSize) { int i; for(i=0; i<NR_CLIENT_SOCKS; i++) if(client_config.socks[i] != -1) setRcvBuf(client_config.socks[i],net_config->requestedBufSize); } outFile=openOutFile(disk_config); origOutFile = outFile; pipedOutFile = openPipe(outFile, disk_config, &pipePid); global_client_config= &client_config; atexit(sendDisconnectWrapper); { struct fifo fifo; int printUncompressedPos = udpc_shouldPrintUncompressedPos(stat_config->printUncompressedPos, origOutFile, pipedOutFile); receiver_stats_t stats = allocReadStats(origOutFile, stat_config->statPeriod, printUncompressedPos); udpc_initFifo(&fifo, net_config->blockSize); fifo.data = pc_makeProduconsum(fifo.dataBufSize, "receive"); client_config.isStarted = 0; if((net_config->flags & (FLAG_PASSIVE|FLAG_NOKBD))) { /* No console used */ client_config.console = NULL; } else { if(doWarn) udpc_flprintf("WARNING: This will overwrite the hard disk of this machine\n"); client_config.console = prepareConsole(0); atexit(fixConsole); } spawnNetReceiver(&fifo,&client_config, net_config, stats); writer(&fifo, pipedOutFile); if(pipePid) { close(pipedOutFile); } pthread_join(client_config.thread, NULL); /* if we have a pipe, now wait for that too */ if(pipePid) { udpc_waitForProcess(pipePid, "Pipe"); } #ifndef __MINGW32__ fsync(origOutFile); #endif /* __MINGW32__ */ displayReceiverStats(stats, 1); } fixConsole(); sendDisconnectWrapper(); global_client_config= NULL; return 0; }