Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:29

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 
0020 void getCompressedBuffer(const char* fname, Bytef** buffPtr, unsigned long& zippedSize)
0021 {
0022    FILE* pFile = fopen ( fname , "r" );
0023    if ( pFile==nullptr )  { std::cerr << "Can't open " << fname << std::endl; exit(1); }
0024 
0025    // obtain file size:
0026    fseek (pFile , 0 , SEEK_END);
0027    unsigned int lSize = ftell (pFile);
0028    rewind (pFile);
0029 
0030    // allocate memory to contain the whole file:
0031    void* buffer = malloc (sizeof(Bytef)*(lSize));
0032 
0033    size_t result = fread (buffer, 1, lSize ,pFile);
0034    fclose(pFile);
0035    if ( !result ) { std::cerr << "Failed to read " << fname <<std::endl; exit(1); }
0036 
0037    //
0038    // write a new buffer. First four bytes is integer with  
0039    // value of size of uncompressed data. Remaining content 
0040    // is compressed original buffer.
0041    //
0042    unsigned int deflatedSize =  compressBound(lSize) + 4; // estimation
0043    Bytef * deflatedBuff = (Bytef*) malloc (sizeof(Bytef)*(deflatedSize));
0044    *((unsigned int*)deflatedBuff) = htonl(lSize);
0045 
0046    //set buffer ptr
0047    *buffPtr = deflatedBuff;
0048 
0049    // compress buffer
0050    zippedSize = deflatedSize;
0051    compress(deflatedBuff+4, &zippedSize, (const Bytef *)buffer, lSize);
0052    zippedSize +=4;
0053 
0054    free(buffer);
0055    /*
0056    printf("zipped size %d \n", (int)zippedSize);
0057    FILE* pFileOut = fopen ( "myfile-compressed" , "wb" );
0058    fwrite (deflatedBuff , 1 , zippedSize , pFileOut );
0059    fclose(pFileOut);
0060    */
0061 }
0062 
0063 int main(int argc, char **argv)
0064 {
0065   if (argc != 2)
0066   {
0067       std::cerr << "Uasage: sendCrashReport <fileName>" << std::endl; exit(1);
0068   }
0069 
0070    // socket creation
0071    int sd = socket(AF_INET,SOCK_DGRAM, 0);
0072    if (sd  < 0) { return 1; }
0073 
0074    // printf("bind port\n");
0075    struct sockaddr_in cliAddr;
0076    cliAddr.sin_family = AF_INET;
0077    cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
0078    cliAddr.sin_port = htons(0);
0079 
0080    int rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr)); 
0081    if (rc < 0) {
0082       std::cerr << "Can't bind port %d " << rc << std::endl; exit(1);
0083    }
0084    
0085    // send data 
0086    struct hostent* h = gethostbyname("xrootd.t2.ucsd.edu");
0087    if (!h) {
0088       std::cerr << "Can't get gost ip \n"; exit(1);
0089    }
0090 
0091    struct sockaddr_in remoteServAddr;
0092    remoteServAddr.sin_family = h->h_addrtype;
0093    memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
0094    remoteServAddr.sin_port = htons(9699);
0095 
0096    Bytef* buff;
0097    unsigned long  buffSize;
0098    getCompressedBuffer(argv[1], &buff, buffSize);
0099 
0100    sendto(sd, buff, buffSize, 0, 
0101       (struct sockaddr *) &remoteServAddr, 
0102       sizeof(remoteServAddr));
0103 
0104    free(buff);
0105 }