/* commentserver This program provides the webserver with the comment file data and accepts new data to append to comment files. First word is the command. Everything after that is the value. LIST commentfilename APPEND commentfilename Data */ #include #include #include #include #include #include #include #include #include #include #define DATA "You got it from me" struct cookiedata { char host[20]; char name[40]; int pref1; int pref2; int pref3; int pref4; int pref5; }; int getcookie(int number, char *name, char *host) { int x,y,z; int size; int handle; struct cookiedata cookie; printf("Getcookie routine.\n"); if(number > 0) { printf("Number is greater than 0.\n"); handle = open("cookies.dat",O_RDONLY); if(handle == -1) { handle = open("cookies.dat",O_CREAT); close(handle); handle = open("cookies.dat",O_RDONLY); } size=lseek(handle,0,2) / sizeof cookie; lseek(handle,number * sizeof cookie,0); read(handle, &cookie, sizeof cookie); strcpy(name,cookie.name); strcpy(host,cookie.host); close(handle); return(number); } else { printf("Less than 0.\n"); handle = open("cookies.dat",O_RDWR); size=lseek(handle,0,2) / sizeof cookie; sprintf(cookie.name,"Anonymous_User%d",size); strcpy(cookie.host,host); write(handle,&cookie, sizeof cookie); close(handle); strcpy(name,cookie.name); return(size); } } main(argc, argv) int argc; char *argv[]; { int x,y,z; int sock; int length; struct sockaddr_in server; int msgsock; char buf[10010]; char s[10000],t[1024]; char fn[100]; char datestr[30]; int rval; int i; time_t lt; struct tm *tim; FILE *output; int number; char command[20]; char filename[256]; char data[10000]; sock = socket(AF_INET, SOCK_STREAM, 0); if(sock < 0) { perror("Opening steram socket"); exit(1); } server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons((u_short )5203); if (bind(sock, &server, sizeof(server))) { perror("binding stream socket"); exit(1); } length = sizeof (server); if(getsockname(sock, &server, &length)) { perror("getting socket name"); exit(1); } printf("Socket has port #%d\n",ntohs(server.sin_port)); listen(sock, 5); do { msgsock = accept(sock, 0,0); printf("Got a connection.\n"); if(msgsock == -1) perror("accept"); else do { bzero(buf, sizeof(buf)); x = 0; do { if(( rval = read(sock, &buf[x], 1460)) < 0) perror("Reading from stream socket"); x = x + rval; } while(rval >= 1460); if((rval = read(msgsock, buf, 10000)) < 0) perror("Reading stream message"); i = 0; buf[10000] = 0; printf("rval=%d. Buf = .%s.\n",rval,buf); if(rval != 0) { for(x=0;buf[x] !=' ' && buf[x] != 0 && buf[x] != '\n'; x++); if(x > 10) { rval=0; break; } strncpy(command,buf,x); command[x] = 0; for(y=x+1;buf[y] != ' ' && buf[y] != 0 && buf[y] != '\n' && buf[y] != 13;y++); strncpy(filename,&buf[x+1],y-(x)); filename[y-(x+1)] = 0; strcpy(s,""); if(strcmp(command,"APPEND") == 0) { for(x=y+1;buf[x] != 0; x++); strncpy(data,&buf[y+1],x-y); data[x-(y+1)] = 0; } // printf("command: .%s. filename: .%s.\n",command,filename); // for(z=0;z %s",filename); system(s); output = fopen(filename,"a+"); } fprintf(output,"%s",data); fclose(output); } if(strcmp(command,"NUMBER") == 0) { if((output = fopen(filename,"r")) == NULL) { sprintf(s,"0"); } else { x=0; fgets(t,1000,output); t[1000]=0; while(!feof(output)) { if(strncmp(t,"<.>",3) == 0) x++; fgets(t,1000,output); t[1000]=0; } sprintf(s,"%d",x); fclose(output); } } write(msgsock,s,strlen(s)+1); rval=0; } } while (rval != 0); close(msgsock); } while (1); }