static int acceptNewClient(int fd) { FD *client; int clientFD; FD *f; FD *proxy; int proxyFD; clientFD = netAccept(fd); if (clientFD < 0) { fprintf(stderr, "netAccept failed\n"); return 0; } client = addFD(clientFD, readClientRequest); if (!client) { fprintf(stderr, "addFD failed\n"); return 0; } proxyFD = netConnect(NULL, (unsigned char *) PROXY_HOST, PROXY_PORT); if (proxyFD < 0) { fprintf(stderr, "netConnect to proxy %s:%d failed\n", PROXY_HOST, PROXY_PORT); return 0; } proxy = addFD(proxyFD, readProxyResponse); if (!proxy) { fprintf(stderr, "addFD failed\n"); return 0; } client->writeFD = proxyFD; proxy->writeFD = clientFD; f = table[fd]; client->logFile = f->logFile; proxy->logFile = f->logFile; client->id = id; proxy->id = id; id++; return 0; }
static void lectFic(char *L) { int fd; dropTrSuite(); if ((fd = open(L,O_RDONLY)) == -1) { perror(L); messErr(16); } else addFD(fd,L); }
int main(int argc, char *argv[]) { FD *f; int fd; fd_set localFDSet; int ret; if (!netInit()) { return 1; } if (!threadInit()) { return 1; } fd = netListen(NULL, NULL, &mainPort); if (fd < 0) { fprintf(stderr, "netListen failed\n"); return 1; } f = addFD(fd, acceptNewLogger); if (!f) { fprintf(stderr, "addFD failed\n"); return 1; } while (1) { localFDSet = fdSet; ret = select(maxFD + 1, &localFDSet, NULL, NULL, NULL); if (ret == -1) { perror("select"); } for (fd = 0; fd <= maxFD; fd++) { if (FD_ISSET(fd, &localFDSet)) { if ((*table[fd]->handler)(fd)) { for (fd = 0; fd <= maxFD; fd++) { removeFD(fd); } return 0; } } } } return 1; }
void ScannerThread::run() { setState(ScannerThread::STARTED); int fanotifyfd = m_fanotifyfd; int maxfd = 0; fd_set readfds; fd_set errorfds; struct timeval timeout; pid_t originalParent = getppid(); while (getppid() == originalParent && !m_stop) { FD_ZERO(&readfds); maxfd = 0; maxfd = addFD(maxfd,fanotifyfd,&readfds); errorfds = readfds; timeout.tv_sec = 4; timeout.tv_usec = 0; setState(ScannerThread::BEFORE_SELECT); int res = select(maxfd+1, &readfds, 0, &errorfds, &timeout); setState(ScannerThread::AFTER_SELECT); if (res < 0) { if (errno != EINTR) { PRINT(m_thread<<" ERROR FROM passthroughScanner select:"<<errno<<" "<<strerror(errno)); } continue; } else if (res == 0) { } else { handleFanotifyEvent(); } } }
static int acceptNewLogger(int fd) { FD *f; int newFD; newFD = netAccept(fd); if (newFD < 0) { fprintf(stderr, "netAccept failed\n"); return 0; } f = addFD(newFD, readLoggerRequest); if (!f) { fprintf(stderr, "addFD failed\n"); return 0; } return 0; }
static int readLoggerRequest(int fd) { char b[10240]; int bytesRead; int doSuspend; FD *f; FILE *file; unsigned char *host; int i; char *p; unsigned short port; int proxyListenFD; char *resume; char *str; char *suspend; int suspendPort; bytesRead = recv(fd, b, sizeof(b) - 1, 0); if (bytesRead < 0) { if (errno != ECONNRESET) { perror("recv"); } removeFD(fd); return 0; } else if (!bytesRead) { removeFD(fd); return 0; } b[bytesRead] = 0; resume = "/resume"; suspend = "/suspend"; if (strstr(b, "/exit")) { char *goodbye = "HTTP/1.0 200 OK\n" "Content-Type: text/html\n" "\n" "Bye!" ; send(fd, goodbye, strlen(goodbye), 0); removeFD(fd); return 1; } else if ((strstr(b, resume)) || (strstr(b, suspend))) { if (strstr(b, resume)) { str = resume; doSuspend = 0; } else { str = suspend; doSuspend = 1; } p = strstr(b, str); p += strlen(str); if (*p != '/') { char *notOK = "HTTP/1.0 200 OK\n" "Content-Type: text/html\n" "\n" "No backslash after command!" ; send(fd, notOK, strlen(notOK), 0); removeFD(fd); return 0; } sscanf(p + 1, "%d", &suspendPort); for (i = 0; i <= maxFD; i++) { if (table[i] && (table[i]->port == suspendPort)) { table[i]->suspend = doSuspend; break; } } if (i <= maxFD) { char *ok = "HTTP/1.0 200 OK\n" "Content-Type: text/html\n" "\n" "OK!" ; send(fd, ok, strlen(ok), 0); } else { char *notOK = "HTTP/1.0 200 OK\n" "Content-Type: text/html\n" "\n" "Cannot find port number in table!" ; send(fd, notOK, strlen(notOK), 0); } removeFD(fd); return 0; } /* XXX send(1, b, bytesRead, 0); */ file = fdopen(fd, "w"); if (!file) { char *err = "fdopen failed\n"; send(fd, err, strlen(err), 0); removeFD(fd); return 0; } table[fd]->logFile = file; port = 0; proxyListenFD = netListen(NULL, &host, &port); if (proxyListenFD < 0) { fprintf(file, "listen failed\n"); removeFD(fd); fclose(file); return 0; } f = addFD(proxyListenFD, acceptNewClient); if (!f) { fprintf(file, "addFD failed\n"); removeFD(fd); fclose(file); return 0; } fprintf(file, welcome1); fprintf(file, welcome2); fprintf(file, welcome3, host, port); fprintf(file, welcome4, host, mainPort, port, host, mainPort, port); sprintf(suspendStr, "http://%s:%d/suspend/%d", host, mainPort, port); free(host); fflush(file); f->logFile = file; table[fd]->port = port; return 0; }
void ReadSelector::addStream(Stream &stream, ReadSelector::Handler handler) { addFD(stream.fd(), handler); }
void server(char* port, int kflag){ int sockfd, new_fd, numbytes; // listen on sock_fd, new connection on new_fd struct addrinfo hints, *servinfo, *p; struct sockaddr_storage their_addr; // connector's address information socklen_t sin_size; struct sigaction sa; int yes=1; char s[INET6_ADDRSTRLEN]; int rv; char buf[MAXDATASIZE]; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // use my IP //This checks if something went wrong. addrinfo returns 0 when it suceeds if ((rv = getaddrinfo(NULL, port, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); exit(1); } // loop through all the results and bind to the first we can for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("server: socket"); continue; } if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("server: bind"); continue; } break; } freeaddrinfo(servinfo); // all done with this structure if (p == NULL) { fprintf(stderr, "server: failed to bind\n"); exit(1); } if (listen(sockfd, BACKLOG) == -1) { perror("listen"); exit(1); } if(vflag){fprintf(stderr, "Waiting for a connection...\n");} /* For multiple connections */ if(rflag) { char *message; struct Clients *conns = malloc(sizeof(struct Clients)); conns->clients = malloc(10 * sizeof(int)); conns->length = 0; //Create the sending thread that reads from STDIN struct Thread *sender_thread = createThread(send_to_all, conns); //Run it int sender_t = runThread(sender_thread, NULL); //Initialize a mutex lock pthread_mutex_init(&mutexDelete, NULL); do { new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if(new_fd == -1) { perror("accept"); continue; } inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s); /* Get por of incoming connection*/ struct sockaddr_in *sin = (struct sockaddr_in*)&their_addr; short w = ntohs(sin->sin_port); /*For debugging print when we get a connection*/ if(vflag){fprintf(stderr, "Server: got connection from %s\nAnd the port is %u\n", s, w);} /* Add connection to our struct of clients and prepare thread*/ /* Mutex lock is used as conns is a shared resource*/ pthread_mutex_lock(&mutexDelete); addFD(conns,new_fd); pthread_mutex_unlock(&mutexDelete); struct Thread *sniffer_thread = createThread(read_and_send_to_all, conns); int threazy = runThread(sniffer_thread, NULL); detachThread(sniffer_thread); } while (1); /*Clean up the listener socket and mutex*/ pthread_mutex_destroy(&mutexDelete); close(sockfd); exit(0); } /* Now wait for connections , this is for regular -l*/ do { sin_size = sizeof their_addr; new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if (new_fd == -1) { perror("accept"); continue; } inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s); /* Get por of incoming connection*/ struct sockaddr_in *sin = (struct sockaddr_in*)&their_addr; short w = ntohs(sin->sin_port); /*For debugging print when we get a connection*/ if(vflag){fprintf(stderr, "Server: got connection from %s\nAnd the port is %u\n", s, w);} /*Let's start a thread that we will use for receiving messages*/ struct Thread *receive_thread = createThread(receiving_thread, &new_fd); /* And now we send it off !*/ int rthreazy = runThread(receive_thread, NULL); /* Now we start a thread for reading fron STDIN and run it*/ struct Thread *send_thread = createThread(sending_thread, &new_fd); int sthreazy = runThread(send_thread, NULL); /* Join threads */ joinThread(receive_thread, NULL); close(new_fd); } while(kflag); //if kflag is on we keep on listening after a connection closes /*Clean the listener socket if you are leaving*/ close(sockfd); }