/** * Creates a new httpreq to represent the http request to make. Returns * a pointer to this struct for later passing to the get/post function. * * url - url the http request will use (minus the http:// prefix) */ httpreq* newrequest(char url[]) { httpreq *req; // Create the httpreq if ((req = malloc(sizeof(httpreq))) == NULL) { fprintf(stderr,"ERROR, could not allocate memory for http request\n"); exit(1); } // Parse the url and add the info to the httpreq parse(url, req); // Lookup the host addr and validate it if ((req->serverAddr = gethostbyname(req->hostName)) == NULL) { fprintf(stderr,"ERROR, no such host: %s\n", req->hostName); exit(1); } // Setup the socket address memcpy(&(req->sock.req.sin_addr.s_addr), *(req->serverAddr->h_addr_list), sizeof(struct in_addr)); req->sock.req.sin_family = AF_INET; req->sock.req.sin_port = htons(req->portNum); // Add the required headers addheader(req, "Host", req->hostName); // Required by HTTP 1.1 addheader(req, "Connection", "close"); // Don't keep the conn open afterwards addheader(req, "User-Agent", "Gamesman/1.0"); // So we can identify ourselves addheader(req, "Content-Type", "application/octet-stream"); // Body content will be binary // Ready to go return req; }
void JNL_HTTPPost::_addformheader() { char *h; #ifdef _WIN32 srand(GetTickCount()); #else srand(time(NULL)); #endif int x; char *preamble = "content-type:multipart/form-data; boundary="; char *dashes = "----"; char *post_boundary = (char*)alloca(10+strlen(dashes)); memcpy(post_boundary,dashes,strlen(dashes)); for(x=strlen(dashes); x < (int)strlen(dashes)+9; x++) { post_boundary[x] = (char)(rand() % 9) + '0'; } post_boundary[x] = 0; h = (char *)alloca(strlen(preamble) + strlen(post_boundary) + 1); sprintf(h,"%s%s",preamble,post_boundary); addheader(h); m_boundary = post_boundary; }
bool CTar32::addheader(const CTar32FileStatus &stat) { int blocksize = 1; if(m_archive_type == ARCHIVETYPE_TAR || m_archive_type == ARCHIVETYPE_TARGZ || m_archive_type == ARCHIVETYPE_TARZ || m_archive_type == ARCHIVETYPE_TARBZ2 || m_archive_type == ARCHIVETYPE_TARLZMA || m_archive_type == ARCHIVETYPE_TARXZ){ blocksize = 512; HEADER tar_header; { HEADER *pblock = &tar_header; memset(pblock, 0, sizeof(*pblock)); const CTar32FileStatus *p = &stat; std::string fname = p->filename; if(fname.length() >= sizeof(pblock->dbuf.name)/*100*/){ CTar32FileStatus tmpstat = stat; tmpstat.filename = "././@LongLink"; tmpstat.typeflag = LONGLINK; tmpstat.original_size = fname.length(); bool bret = addheader(tmpstat); char filename[2000]; strcpy(filename, fname.c_str()); size64 writesize = ((fname.length() - 1)/512+1)*512; size64 ret = m_pfile->write(filename, writesize); if(ret != writesize){ throw CTar32Exception("LongLink filename write error", ERROR_CANNOT_WRITE); } } strncpy(pblock->dbuf.name, fname.c_str(),NAMSIZ-1); /* tarnt 0.96->0.97 */ sprintf(pblock->dbuf.mode, "%6o ", (unsigned int)p->mode); sprintf(pblock->dbuf.uid, "%06o ",p->uid); sprintf(pblock->dbuf.gid, "%06o ",p->gid); sprintf(pblock->dbuf.size, "%11I64o ", p->original_size); sprintf(pblock->dbuf.mtime, "%11lo ", p->mtime); pblock->dbuf.typeflag = p->typeflag; memcpy(pblock->dbuf.magic, p->magic_version, sizeof(p->magic_version)); strncpy(pblock->dbuf.uname, p->uname, sizeof pblock->dbuf.uname); sprintf(pblock->dbuf.chksum, "%6o ", pblock->compsum()); } size64 ret = m_pfile->write(&tar_header,sizeof(tar_header)); if(ret != sizeof(tar_header)){ throw CTar32Exception("header write error", ERROR_CANNOT_WRITE); } }else{ ; } m_currentfile_status = stat; m_currentfile_status.blocksize = blocksize; return true; }
/** * Sets the type header to the specified httpreq struct. The specified * value is copied (by value) into the struct. Thus, changes to that * character array will not have any further affect. * * req - httpreq struct to modify * value - value for the header t */ void settype(httpreq *req, char value[]) { addheader(req, "TYPE", value); }
/** * POST's the specified httpreq to it's preconfigured url with * all preconfigured headers and the body content (if any). * Returns a httpres struct representing the HTTP response data. * * req - httpreq struct to POST to it's url/addr * body - content for the body of the HTTP POST, can be NULL * bodyLength - length of the body content, can be 0 if body is NULL * returns httpres struct representing the HTTP response data */ httpres* post(httpreq *req, char body[], int bodyLength) { httpres *res; header *currHdr; header *tmpHdr; char buffer[64]; int sockFd; int n; // Add the content-length header net_itoa(bodyLength, buffer); addheader(req, "Content-Length", buffer); // Create a socket if ((sockFd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stderr,"ERROR, opening socket\n"); exit(1); } // Connect to the socket if (connect(sockFd, &(req->sock.res), sizeof(struct sockaddr_in)) < 0) { fprintf(stderr,"ERROR, opening socket\n"); exit(1); } // Submit the http request n = write(sockFd, "POST ", 5); n = write(sockFd, req->path, strlen(req->path)); n = write(sockFd, " HTTP/1.1\r\n", 11); // Add the headers currHdr = req->headers; while (currHdr != NULL) { n = write(sockFd, currHdr->name, strlen(currHdr->name)); n = write(sockFd, ": ", 2); n = write(sockFd, currHdr->value, strlen(currHdr->value)); n = write(sockFd, "\r\n", 2); currHdr = currHdr->next; } // Add the extra line to separate headers from body n = write(sockFd, "\r\n", 2); // Add the body (if any) if (bodyLength > 0) n = write(sockFd, body, bodyLength); // Create the response if ((res = malloc(sizeof(res))) == NULL) { fprintf(stderr,"ERROR, could not allocate memory for http response\n"); exit(1); } // Read the response readresponse(sockFd, res); // Free the malloc'd memory currHdr = req->headers; while (currHdr != NULL) { free(currHdr->name); free(currHdr->value); tmpHdr = currHdr; currHdr = currHdr->next; free(tmpHdr); } free(req->hostName); free(req->path); free(req); return res; }
int main(void) { // int sockfd, new_fd111, numbytes; int sockfd, numbytes; char buf[MAXDATASIZE]; struct addrinfo hints, *servinfo, *p; int rv; // int yes=1; char s[INET_ADDRSTRLEN]; char* header; struct sockaddr_in sa; //store local address int sa_len = sizeof(sa); FILE *fp; struct sockaddr_storage their_addr; // connector's address information socklen_t sin_size; int cpid; // int whatTheHell = 1; // printf("whatTheHell = %d\n", whatTheHell); struct userNode* bidderInfo; bidderInfo = malloc(sizeof(struct userNode)); memset(bidderInfo,0,sizeof(struct userNode)); /*phase 1: Authorization*/ memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; if ((rv = getaddrinfo(HOSTNAME, PORT_S_P1, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } cpid = fork(); if(cpid){ //parent process if (readBidderPass(2, "bidderPass2.txt", &bidderInfo) != 0){ //read bidderpass1.txt and load user information perror("bidderPass1.txt"); return 1; } header = "Bidder2"; sleep(2); //parent sleep 2s, wait until child finished }else{ //child process if (readBidderPass(1, "bidderPass1.txt", &bidderInfo) != 0){ //read bidderpass1.txt and load user information perror("bidderPass1.txt"); return 1;} header = "Bidder1"; } // loop through all the results and connect 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("client: socket"); continue; } if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("client: connect"); continue; } break; } if (p == NULL) { fprintf(stderr, "client: failed to connect\n"); return 2; } //Phase 1: <Bidder#>__ has TCP port ___ and IP address: ____ getsockname(sockfd, (struct sockaddr*)&sa, &sa_len); inet_ntop(AF_INET, (void*)&(sa.sin_addr), s, sizeof s); if(cpid){ //parent process printf("Phase 1: <Bidder2> has TCP port %d and IP address: %s\n", sa.sin_port, s); }else{ //child process printf("Phase 1: <Bidder1> has TCP port %d and IP address: %s\n", sa.sin_port, s); } #ifdef DEBUG inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); printf("client: connecting to %s Port:%d\n", s, ((struct sockaddr_in*)(p->ai_addr))->sin_port); #endif freeaddrinfo(servinfo); // all done with this structure //Login cammand: "Login#type userIndex username password bankaccount" sprintf(buf,"Login#%d %d %s %s %s",bidderInfo->type, bidderInfo->userIndex, bidderInfo->name, bidderInfo->password, bidderInfo->accountNum); printf("Phase 1: Login request. User:%s password:%s Bank account:%s\n", bidderInfo->name, bidderInfo->password, bidderInfo->accountNum); #ifdef DEBUG puts(buf); #endif //send Login command to server addheader(buf, header); if ((numbytes = send(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("send"); exit(1); } sleep(1); //sleep 1 second //receive Accepted# or Rejected# command from server if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = '\0'; removeheader(buf); printf("Phase 1: Login request reply: %s .\n\n", buf); // printf("client: received '%s'\n",buf); // while ((recv(sockfd, buf, MAXDATASIZE-1, 0)) != 0); //wait until server close(sockfd), phase 1 close(sockfd); /*End of phase 1*/ if(!strcmp(buf, "Rejected#")) return 0; //if rejected, bidder shouldn't appear in following phases /*************************************************************************************************/ /*phase 3: Auction*/ #ifdef DEBUG puts("Phase 3"); #endif memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; //ipv4 hints.ai_socktype = SOCK_DGRAM; //UDP socket gethostname(buf, MAXDATASIZE-1); //use buf to store hostname temporarily if ((rv = getaddrinfo(buf, (!cpid)?PORT_BD1_P3_UDP:PORT_BD2_P3_UDP, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 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("listener: socket"); continue; } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("listener: bind"); continue; } break; } if (p == NULL) { fprintf(stderr, "listener: failed to bind socket\n"); return 2; } // sleep(15); //wait for server begin phase 3 sleep(20); //Phase 3: <Bidder#>__ has UDP port ___ and IP address: ____ getsockname(sockfd, (struct sockaddr*)&sa, &sa_len); inet_ntop(AF_INET, (void*)&(sa.sin_addr), s, sizeof s); if(cpid){ //parent process sleep(5); // parent/bidder2 sleep 5s, wait until child finished printf("Phase 3: <Bidder2> has UDP port %d and IP address: %s\n", sa.sin_port, s); puts("Phase 3: <Bidder2> (Item list displayed here)"); }else{ //child process printf("Phase 3: <Bidder1> has UDP port %d and IP address: %s\n", sa.sin_port, s); puts("Phase 3: <Bidder1> (Item list displayed here)"); } freeaddrinfo(servinfo); //receive broadcastList do{ if ((numbytes = recvfrom(sockfd, buf, MAXDATASIZE-1 , 0, (struct sockaddr *)&their_addr, &sin_size)) == -1) { perror("recvfrom"); exit(1); } buf[numbytes] = '\0'; #ifdef DEBUG printf("listener: got packet from %s\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s)); printf("listener: packet is %d bytes long\n", numbytes); printf("listener: packet contains \"%s\"\n", buf); #endif removeheader(buf); if(!strcmp("ListEnd#",buf)) break; puts(buf); }while(1); sleep(5); //wait for server open UDP recv /*send bidding info*/ if(cpid){ //parent process if((fp = fopen("bidding2.txt", "r")) == NULL){ perror("bidding2.txt"); exit(1); } puts("Phase 3: <Bidder2> (Bidding information displayed here)"); }else{ //child process if((fp = fopen("bidding1.txt", "r")) == NULL){ perror("bidding1.txt"); exit(1); } puts("Phase 3: <Bidder1> (Bidding information displayed here)"); } //read one line and send per loop while(fgets(buf, sizeof(buf), fp) != NULL){ buf[strlen(buf)-1] = '\0'; //remove '\n' in the end addheader(buf, header); if ((numbytes = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr*)&their_addr, sizeof(struct sockaddr_in))) == -1) { perror("talker: sendto"); exit(1); } #ifdef DEBUG printf("talker: sent %d bytes to server\n", numbytes); puts(buf); #endif removeheader(buf); puts(buf); } //indicate end of file strcpy(buf, "ListEnd#"); addheader(buf, header); if ((numbytes = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr*)&their_addr, sizeof(struct sockaddr_in))) == -1){ perror("talker: sendto"); exit(1); } #ifdef DEBUG puts(buf); #endif close(sockfd); //receive final sold decision // int yes = 1; // int new_fd; memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; //ipv4 hints.ai_socktype = SOCK_STREAM; //TCP socket gethostname(buf, MAXDATASIZE-1); //use buf to store hostname temporarily if ((rv = getaddrinfo(buf, (cpid)?PORT_BD2_P3_TCP:PORT_BD1_P3_TCP, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 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; } sa_len = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sa_len, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("server: bind"); continue; } break; } if (p == NULL){ fprintf(stderr, "server: failed to bind\n"); return 2; } freeaddrinfo(servinfo); // all done with this structure if (listen(sockfd, BACKLOG) == -1) { perror("listen"); exit(1); } rv = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if (rv == -1) { perror("accept"); exit(1); } if(cpid){ //parent process puts("Phase 3: <Bidder2>"); }else{ //child process puts("Phase 3: <Bidder1>"); } do{ if ((numbytes = recv(rv, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); exit(1); } removeheader(buf); if(!strcmp("ListEnd#",buf)) break; buf[strlen(buf)-1] = '\0'; //remove '\n' in the end puts(buf); }while(1); close(rv); close(sockfd); if(cpid){ //parent process puts("End of Phase 3 for <Bidder2>.\n"); }else{ //child process puts("End of Phase 3 for <Bidder1>.\n"); } return 0; }
DisconnectFrame::DisconnectFrame(int receipt): StompFrame("DISCONNECT") { char buffer [10]; sprintf(buffer, "%d",receipt); addheader("receipt",buffer); }