Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-04 22:54:44

0001 #include <cstdio>
0002 #include <cstdlib>
0003 #include <arpa/inet.h>
0004 #include "zlib.h"
0005 #include <sys/types.h>
0006 #include <sys/socket.h>
0007 #include <unistd.h>
0008 #include <cstring>
0009 #include <netdb.h>
0010 #include <iostream>
0011 
0012 #define BUFLEN 60000
0013 
0014 // AMT: This code is a substitute of netcat command. The reason it is replaced
0015 //      is that netcat has limited buffer to 1024 bytes.
0016 //
0017 //      TODO:: waith for server echo with timeout.
0018 
0019 void getCompressedBuffer(const char* fname, Bytef** buffPtr, unsigned long& zippedSize) {
0020   FILE* pFile = fopen(fname, "r");
0021   if (pFile == nullptr) {
0022     std::cerr << "Can't open " << fname << std::endl;
0023     exit(1);
0024   }
0025 
0026   // obtain file size:
0027   fseek(pFile, 0, SEEK_END);
0028   unsigned int lSize = ftell(pFile);
0029   rewind(pFile);
0030 
0031   // allocate memory to contain the whole file:
0032   void* buffer = malloc(sizeof(Bytef) * (lSize));
0033 
0034   size_t result = fread(buffer, 1, lSize, pFile);
0035   fclose(pFile);
0036   if (!result) {
0037     std::cerr << "Failed to read " << fname << std::endl;
0038     exit(1);
0039   }
0040 
0041   //
0042   // write a new buffer. First four bytes is integer with
0043   // value of size of uncompressed data. Remaining content
0044   // is compressed original buffer.
0045   //
0046   unsigned int deflatedSize = compressBound(lSize) + 4;  // estimation
0047   Bytef* deflatedBuff = (Bytef*)malloc(sizeof(Bytef) * (deflatedSize));
0048   *((unsigned int*)deflatedBuff) = htonl(lSize);
0049 
0050   //set buffer ptr
0051   *buffPtr = deflatedBuff;
0052 
0053   // compress buffer
0054   zippedSize = deflatedSize;
0055   compress(deflatedBuff + 4, &zippedSize, (const Bytef*)buffer, lSize);
0056   zippedSize += 4;
0057 
0058   free(buffer);
0059   /*
0060    printf("zipped size %d \n", (int)zippedSize);
0061    FILE* pFileOut = fopen ( "myfile-compressed" , "wb" );
0062    fwrite (deflatedBuff , 1 , zippedSize , pFileOut );
0063    fclose(pFileOut);
0064    */
0065 }
0066 
0067 int main(int argc, char** argv) {
0068   if (argc != 2) {
0069     std::cerr << "Uasage: sendCrashReport <fileName>" << std::endl;
0070     exit(1);
0071   }
0072 
0073   // socket creation
0074   int sd = socket(AF_INET, SOCK_DGRAM, 0);
0075   if (sd < 0) {
0076     return 1;
0077   }
0078 
0079   // printf("bind port\n");
0080   struct sockaddr_in cliAddr;
0081   cliAddr.sin_family = AF_INET;
0082   cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
0083   cliAddr.sin_port = htons(0);
0084 
0085   int rc = bind(sd, (struct sockaddr*)&cliAddr, sizeof(cliAddr));
0086   if (rc < 0) {
0087     std::cerr << "Can't bind port %d " << rc << std::endl;
0088     exit(1);
0089   }
0090 
0091   // send data
0092   struct hostent* h = gethostbyname("xrootd.t2.ucsd.edu");
0093   if (!h) {
0094     std::cerr << "Can't get gost ip \n";
0095     exit(1);
0096   }
0097 
0098   struct sockaddr_in remoteServAddr;
0099   remoteServAddr.sin_family = h->h_addrtype;
0100   memcpy((char*)&remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
0101   remoteServAddr.sin_port = htons(9699);
0102 
0103   Bytef* buff;
0104   unsigned long buffSize;
0105   getCompressedBuffer(argv[1], &buff, buffSize);
0106 
0107   sendto(sd, buff, buffSize, 0, (struct sockaddr*)&remoteServAddr, sizeof(remoteServAddr));
0108 
0109   free(buff);
0110 }