int main(int argc, char**argv) { time_t ti; struct tm *foo; time( &ti); foo = localtime( &ti ); strftime(file_time, 20, "%Y-%m-%d-%H:%M:%S", foo); // Register SIGINT handler struct sigaction action; memset(&action, 0, sizeof(struct sigaction)); action.sa_handler = term; sigaction(SIGINT, &action, NULL); sigaction(SIGUSR1, &action, NULL); arg = parseArguments(argc, argv); gettimeofday(&startTime, NULL); sockfd = socket(AF_INET,SOCK_DGRAM, 0); int res = hostnameToIp(arg.server, arg.server); if(res != 0) { fprintf(stderr, "Error: Could not translate hostname to IP."); exit(1); } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr=inet_addr(arg.server); servaddr.sin_port=htons(arg.port); arg.pid = fork(); if(arg.pid == 0) { receiveData(arg); } else if(arg.pid > 0) { sendData(arg); } else { fprintf(stderr, "Error: Fork error.\n"); exit(1); } term(SIGINT); return 0; }
int parseConfig(configuration *config) { FILE *configFile; char buffert[PATH_MAX], realPathBuff[PATH_MAX]; char *line = NULL; char value[256] = {'\0'}; size_t lineIndex = 0, len = sizeof(value); sprintf(buffert, "%s/%s", config->rootDir, config->configPath); realpath(buffert, realPathBuff); if ((configFile = fopen(realPathBuff, "r")) == NULL) { printf("CRITICAL: Unable to open config file %s, %s\n", config->accLogPath, strerror(errno)); return -1; } while( getline(&line, &len, configFile) != EOF ) { lineIndex = 0; // Ignore spaces while (line[lineIndex] == ' ') lineIndex++; // Ignore commented and empty lines if( line[lineIndex] != '#' && line[lineIndex] != '\n' ) { // Parse the line if (startsWith("Servername", line + lineIndex)) { // Move pass variable name lineIndex += strlen("Servername:"); readValue(lineIndex, line, value, sizeof(value)); // Check if value is an valid ip address if (isValidIpAddress(value)) { strncpy(config->servername, value, BUF_CFG); } // Otherwise check if it's an valid domain name and resolve it else if(hostnameToIp(value)) { strncpy(config->servername, value, BUF_CFG); } // Else this is not a valid config else printf("Invalid config, hostname\n"); } else if (startsWith("Listen", line + lineIndex)) { int port = 0; // Move pass variable name lineIndex += strlen("Listen:"); readValue(lineIndex, line, value, sizeof(value)); // Check if valid port number if((port = atoi(value)) != 0 && port < 65536) { config->listenPort = port; } // Else invalid else { printf("Invalid config, port\n"); } } else if (startsWith("Basedir", line + lineIndex)) { // Move pass variable name lineIndex += strlen("Basedir:"); readValue(lineIndex, line, value, sizeof(value)); // relative the root dir sprintf(buffert, "%s/%s", config->rootDir, value); // Get the realpath if(realpath(buffert, realPathBuff) == NULL){ printf("Invalid config, basedirectory: %s, %s\n", realPathBuff, strerror(errno)); exit(-1); } // Change basedirectory to the realpath strncpy(config->basedir, realPathBuff, BUF_CFG); } else if (startsWith("Method", line + lineIndex)) { // Move pass variable name lineIndex += strlen("Method:"); readValue(lineIndex, line, value, sizeof(value)); // If selected logging method is to a file if((strcmp(value, "file")) == 0) { config->syslog = false; if (startsWith("Access_logpath", line + lineIndex)) { // Move pass variable name lineIndex += strlen("Access_logpath:"); readValue(lineIndex, line, value, sizeof(value)); // relative the root dir sprintf(buffert, "%s/%s", config->rootDir, value); // Get the realpath realpath(buffert, realPathBuff); // Change accLogPath to the realpath strncpy(config->accLogPath, realPathBuff, BUF_CFG); } else if (startsWith("Server_logpath", line + lineIndex)) { // Move pass variable name lineIndex += strlen("Server_logpath:"); readValue(lineIndex, line, value, sizeof(value)); // relative the root dir sprintf(buffert, "%s/%s", config->rootDir, value); // Get the realpath realpath(buffert, realPathBuff); // Change srvLogPath to the realpath strncpy(config->srvLogPath, realPathBuff, BUF_CFG); } } } else if (startsWith("Fifo_path", line + lineIndex)) { // Move pass variable name lineIndex += strlen("Fifo_path:"); readValue(lineIndex, line, value, sizeof(value)); // relative the root dir sprintf(buffert, "%s/%s", config->rootDir, value); // Get the realpath realpath(buffert, realPathBuff); // Change srvLogPath to the realpath strncpy(config->fifoPath, realPathBuff, BUF_CFG); } } } fclose(configFile); return 0; }
int main() { WSADATA wsa; SOCKET s; SOCKADDR_IN server_s; const char * hostname = "pb-homework.appspot.com"; const char * link = "/var/11?secret=totalrecall"; int status; int recv_size = 0; char * ip; char * content; char * arrayelem; char * result; char buffer[20480]; int sum=0; HOSTENT * host_info; status = WSAStartup(MAKEWORD(2,2),&wsa); if(status!=0){ puts("WSA Startup error"); return 1; } hostnameToIp(hostname,host_info,(char**)&ip); printf("IP address is: %s.\n",ip); setServerSockInfo((SOCKADDR_IN *)&server_s,ip,PB_PORT,AF_INET); s = socket(AF_INET,SOCK_STREAM,NOT_SPECIFIED_PROTOCOL); if(s == INVALID_SOCKET){ puts("socket creation problem"); WSACleanup(); return 1; } if(connect(s,(SOCKADDR*)&server_s,sizeof(SOCKADDR_IN)) == SOCKET_ERROR){ printf("CONNECTION ERROR"); closesocket(s); WSACleanup(); return 1; } char request[200]; sprintf(request, "GET %s HTTP/1.1\nHost:%s\r\n\r\n",link, hostname); printf("request is : %s\n\r\n",request); send(s, request, strlen(request), NO_FLAGS); recv_size = recv(s, buffer, MAX_BUFFER_SIZE, NO_FLAGS); recvCheckForError(recv_size,s); //content = strtok(buffer,"\n"); // //for(int i = 0; i < 6; i++){ // content = strtok(NULL,"\n"); //} httpToContent((char **)&content,buffer); printf("ARRAY : %s\n",content); arrayelem = strtok(content," "); while(arrayelem!=NULL){ sum+=atoi(arrayelem); arrayelem = strtok(NULL," "); } printf("SUM = %i",sum); sprintf(result,"result=%i",sum); sprintf(request,"POST %s HTTP/1.1\nHost:%s\r\nContent-Length:%d\r\n\r\n%s",link,hostname,strlen(result),result); send(s, request, strlen(request), NO_FLAGS); recv_size = recv(s, buffer, MAX_BUFFER_SIZE, NO_FLAGS); recvCheckForError(recv_size,s); puts(buffer); closesocket(s); WSACleanup(); return 0; }