/*------------------------------------------------------------------------ * main - Iterative server for DAYTIME service *------------------------------------------------------------------------ */ int main(int argc, char *argv[]) { char *service = "daytime"; /* service name or port number */ char buf[LINELEN+1]; /* buffer for one line of text */ struct sockaddr_in fsin; /* the request from address */ int alen; /* from-address length */ int tsock; /* TCP master socket */ int usock; /* UDP socket */ int nfds; fd_set rfds; /* readable file descriptors */ switch (argc) { case 1: break; case 2: service = argv[1]; break; default: errexit("usage: daytimed [port]\n"); } tsock = passiveTCP(service, QLEN); usock = passiveUDP(service); nfds = MAX(tsock, usock) + 1; /* bit number of max fd */ FD_ZERO(&rfds); while (1) { FD_SET(tsock, &rfds); FD_SET(usock, &rfds); if (select(nfds, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0) < 0) errexit("select error: %s\n", strerror(errno)); if (FD_ISSET(tsock, &rfds)) { int ssock; /* TCP slave socket */ alen = sizeof(fsin); ssock = accept(tsock, (struct sockaddr *)&fsin, &alen); if (ssock < 0) errexit("accept failed: %s\n", strerror(errno)); daytime(buf); (void) write(ssock, buf, strlen(buf)); (void) close(ssock); } if (FD_ISSET(usock, &rfds)) { alen = sizeof(fsin); if (recvfrom(usock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin, &alen) < 0) errexit("recvfrom: %s\n", strerror(errno)); daytime(buf); (void) sendto(usock, buf, strlen(buf), 0, (struct sockaddr *)&fsin, sizeof(fsin)); } } }
int main(int argc, char *argv[]) { opterr = 0; /* disable getopt error msgs */ switch (getopt(argc, argv, "ds")) { case 'd': dg_template(daytime_dg, STDIN_FILENO, NULL, 0); break; case 's': (void) safe_write(STDIN_FILENO, daytime(), TIMEBUF_SIZE); break; default: return (1); } return (0); }
int main(int argc,char **argv) { printf("Http server welcome you!\n"); tpool_create(10); if(1!=argc) getoption(argc,argv);//It's hard to learn how to use it if(NULL==_log) logfd=open(DEFAULTLOG,O_WRONLY | O_APPEND | O_CREAT); else logfd=open(_log,O_WRONLY | O_CREAT | O_APPEND); daytime(); int sockfd,sockfds; if(daemon_check) daemons(); ssl_init(ctx); signal(SIGPIPE,SIG_IGN); signal(SIGCHLD, SIG_IGN); sockfd=make_socket(sockfd); sockfds=make_socket_ssl(sockfds); if(sockfd<0||sockfds<0) errorfunc("sockfd error!"); int addrlen = 1; setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&addrlen,sizeof(addrlen));//set the port quickly reuse setsockopt(sockfds,SOL_SOCKET,SO_REUSEADDR,&addrlen,sizeof(addrlen)); struct epoll_event events[MAXEVENTS];//question int epollfd=epoll_create(MAXEVENTS); addfd(epollfd,sockfd,0); addfd(epollfd,sockfds,0); chdir("/home/wangyao/web"); while(1) { int ret=epoll_wait(epollfd,events,MAXEVENTS,-1);//epoll func should be use in here if(ret<0) errorfunc("epoll_wait error!"); lt(events,ret,epollfd,sockfd,sockfds); } close(sockfds); close(sockfd); close(epollfd); sleep(10); tpool_destroy(); SSL_CTX_free(ctx); exit(0); }
// Is given the response to give, and the socket to send on. // Creates a header and concatonates response, then sends the header+response. // Should really be renamed sendWithHeader since it now also sends response. void sendHttpHeader(int socket, char response[]){ char headerResponse[9999]; char date[256]; daytime(date); strcat(headerResponse, "Content-Type: text/html; "); strcat(headerResponse, "charset=UTF-8 \n"); strcat(headerResponse, "Server: Jeff And Charles CS436 Project \n"); strcat(headerResponse, "Date: "); strcat(headerResponse, date); strcat(headerResponse, "Content-Length: "); int length=strlen(response)+strlen(headerResponse)+3; char cLength[256]; sprintf(cLength, "%i",length); strcat(headerResponse, cLength); strcat(headerResponse, "\n"); strcat(headerResponse, response); strcat(headerResponse, "\n"); write(socket, headerResponse, strlen(headerResponse)); }
/* ARGSUSED3 */ static void daytime_dg(int s, const struct sockaddr *sap, int sa_size, const void *buf, size_t sz) { (void) safe_sendto(s, daytime(), TIMEBUF_SIZE, 0, sap, sa_size); }
int main(int argc, char*argv[]) { char *service = "daytime"; char buf[LINELEN + 1]; struct sockaddr_in fsin; unsigned int alen; int tsock; int usock; int nfds; fd_set rfds; tsock = passiveTCP("tcp", QLEN); usock = passiveUDP("udp"); nfds = MAX(tsock, usock) + 1; FD_ZERO(&rfds); while (1) { FD_SET(tsock, &rfds); (usock, &rfds); if (select(nfds, &rfds, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0) < 0) errexit("select error : %s\n", strerror(errno)); if (FD_ISSET(tsock, &rfds)) { int ssock; pthread_t thread_id; alen = sizeof(fsin); ssock = accept(tsock, (struct sockaddr *) &fsin, &alen); if (ssock < 0) errexit("accept failed: %s\n", strerror(errno)); if (pthread_create(&thread_id, NULL, connection_handler, (void*) &ssock) < 0) { perror("could not create thread"); return 1; } daytime(buf); (void) write(ssock, buf, strlen(buf)); (void) close(ssock); } if (FD_ISSET(usock, &rfds)) { alen = sizeof(fsin); pthread_t thread_id; if (recvfrom(usock, buf, sizeof(buf), 0, (struct sockaddr *) &fsin, &alen) < 0) errexit("recvfrom: %s\n", strerror(errno)); if (pthread_create(&thread_id, NULL, connection_handlerudp, (void*) &usock) < 0) { perror("could not create thread"); return 1; } daytime(buf); (void) sendto(usock, buf, strlen(buf), 0, (struct sockaddr*) &fsin, sizeof(fsin)); } } void *connection_handlerudp(void *socket_desc) { char buf[LINELEN + 1]; unsigned int alen; struct sockaddr_in fsin; int readsize; int sock = *(int*) socket_desc; alen = sizeof(fsin); while ((readsize = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &fsin, &alen)) > 0) { sendto(sock, buf, sizeof(buf), 0, (struct sockaddr *) &fsin, sizeof(fsin)); } if (readsize == 0) { puts("Client disconnected"); fflush(stdout); } else if (readsize == -1) { perror("recv failed"); } errexit("recvfrom: %s\n", strerror(errno)); }
bool due (RECYCLE * recycle) { LOGSET *logset = recycle->logset; STATE *state = &recycle->states [recycle->state]; if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "checking %s", logset->files [logset->file]); if (logset->flags & (LOGSET_FLAG_MONTHLY | LOGSET_FLAG_WEEKLY | LOGSET_FLAG_DAILY)) { error (0, 0, "last cycle %s", daytime (&state->tm)); error (0, 0, "next cycle %s", daytime (&logset->tm)); } } /* * recycle everything now to create a snapshot or starting point; this may be * at some time for legal reasons or in case of security breaches; */ if (recycle->flags & (RECYCLE_B_FORCE)) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s now because the operator requested it !", state->filename); } return (true); } /* * prevent multiple recyles in one day for whatever reason; use 'force' TO * override this feature; */ if (!datecomp (&recycle->tm, &state->tm)) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "skipping %s because it was recycled once today.", state->filename); } return (false); } /* * prevent unecessary recycling of empty files; this feature prevents useful * information being displaced by no information; */ if (logset->flags & (LOGSET_FLAG_NOTIFEMPTY)) { if (logset->statinfo.st_size == 0) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "skipping %s because it is empty.", state->filename); } return (false); } } if (logset->flags & (LOGSET_FLAG_MONTHLY)) { if (logset->threshold > 0) { if (datecomp (&recycle->tm, &logset->tm) > 0) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s because a monthly cycle is overdue.", state->filename); } return (true); } else if (datecomp (&recycle->tm, &logset->tm) == 0) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s because a monthly change is due.", state->filename); } return (true); } } else if (logset->tm.tm_year != state->tm.tm_year) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s because a monthly change is due.", state->filename); } return (true); } else if (logset->tm.tm_mon != state->tm.tm_mon) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s because a monthly change is due.", state->filename); } return (true); } } else if (logset->flags & (LOGSET_FLAG_WEEKLY)) { if (logset->threshold > 0) { if (datecomp (&recycle->tm, &logset->tm) > 0) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s because a weekly cycle is overdue.", state->filename); } return (true); } else if (datecomp (&recycle->tm, &logset->tm) == 0) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s because a weekly change is due.", state->filename); } return (true); } } else if (logset->tm.tm_wday == 0) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s because a weekly change is due.", state->filename); } return (true); } } else if (logset->flags & (LOGSET_FLAG_DAILY)) { if (logset->threshold > 0) { if (datecomp (&recycle->tm, &logset->tm) > 0) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s because a daily cycle is overdue.", state->filename); } return (true); } else if (datecomp (&recycle->tm, &logset->tm) == 0) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s because a daily change is due.", state->filename); } return (true); } } else if (logset->tm.tm_year != state->tm.tm_year) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s because a daily change is due.", state->filename); } return (true); } else if (logset->tm.tm_yday != state->tm.tm_yday) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s because a daily change is due.", state->filename); } return (true); } } else if (logset->threshold > 0) { if (logset->statinfo.st_size > logset->threshold) { if (recycle->flags & (RECYCLE_B_VERBOSE)) { error (0, 0, "recycling %s because it exceeds %lu bytes.", state->filename, logset->threshold); } return (true); } } return (false); }