CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
cmsShowSendReport.cc
Go to the documentation of this file.
1 #include <cstdio>
2 #include <cstdlib>
3 #include <arpa/inet.h>
4 #include "zlib.h"
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <unistd.h>
8 #include <cstring>
9 #include <netdb.h>
10 #include <iostream>
11 
12 #define BUFLEN 60000
13 
14 // AMT: This code is a substitute of netcat command. The reason it is replaced
15 // is that netcat has limited buffer to 1024 bytes.
16 //
17 // TODO:: waith for server echo with timeout.
18 
19 void getCompressedBuffer(const char* fname, Bytef** buffPtr, unsigned long& zippedSize) {
20  FILE* pFile = fopen(fname, "r");
21  if (pFile == nullptr) {
22  std::cerr << "Can't open " << fname << std::endl;
23  exit(1);
24  }
25 
26  // obtain file size:
27  fseek(pFile, 0, SEEK_END);
28  unsigned int lSize = ftell(pFile);
29  rewind(pFile);
30 
31  // allocate memory to contain the whole file:
32  void* buffer = malloc(sizeof(Bytef) * (lSize));
33 
34  size_t result = fread(buffer, 1, lSize, pFile);
35  fclose(pFile);
36  if (!result) {
37  std::cerr << "Failed to read " << fname << std::endl;
38  exit(1);
39  }
40 
41  //
42  // write a new buffer. First four bytes is integer with
43  // value of size of uncompressed data. Remaining content
44  // is compressed original buffer.
45  //
46  unsigned int deflatedSize = compressBound(lSize) + 4; // estimation
47  Bytef* deflatedBuff = (Bytef*)malloc(sizeof(Bytef) * (deflatedSize));
48  *((unsigned int*)deflatedBuff) = htonl(lSize);
49 
50  //set buffer ptr
51  *buffPtr = deflatedBuff;
52 
53  // compress buffer
54  zippedSize = deflatedSize;
55  compress(deflatedBuff + 4, &zippedSize, (const Bytef*)buffer, lSize);
56  zippedSize += 4;
57 
58  free(buffer);
59  /*
60  printf("zipped size %d \n", (int)zippedSize);
61  FILE* pFileOut = fopen ( "myfile-compressed" , "wb" );
62  fwrite (deflatedBuff , 1 , zippedSize , pFileOut );
63  fclose(pFileOut);
64  */
65 }
66 
67 int main(int argc, char** argv) {
68  if (argc != 2) {
69  std::cerr << "Uasage: sendCrashReport <fileName>" << std::endl;
70  exit(1);
71  }
72 
73  // socket creation
74  int sd = socket(AF_INET, SOCK_DGRAM, 0);
75  if (sd < 0) {
76  return 1;
77  }
78 
79  // printf("bind port\n");
80  struct sockaddr_in cliAddr;
81  cliAddr.sin_family = AF_INET;
82  cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
83  cliAddr.sin_port = htons(0);
84 
85  int rc = bind(sd, (struct sockaddr*)&cliAddr, sizeof(cliAddr));
86  if (rc < 0) {
87  std::cerr << "Can't bind port %d " << rc << std::endl;
88  exit(1);
89  }
90 
91  // send data
92  struct hostent* h = gethostbyname("xrootd.t2.ucsd.edu");
93  if (!h) {
94  std::cerr << "Can't get gost ip \n";
95  exit(1);
96  }
97 
98  struct sockaddr_in remoteServAddr;
99  remoteServAddr.sin_family = h->h_addrtype;
100  memcpy((char*)&remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
101  remoteServAddr.sin_port = htons(9699);
102 
103  Bytef* buff;
104  unsigned long buffSize;
105  getCompressedBuffer(argv[1], &buff, buffSize);
106 
107  sendto(sd, buff, buffSize, 0, (struct sockaddr*)&remoteServAddr, sizeof(remoteServAddr));
108 
109  free(buff);
110 }
void getCompressedBuffer(const char *fname, Bytef **buffPtr, unsigned long &zippedSize)
void free(void *ptr) noexcept
int main(int argc, char **argv)
void * malloc(size_t size) noexcept
string fname
main script
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
def exit(msg="")