]> sourceware.org Git - systemtap.git/blob - cscommon.cxx
Give information about online servers and which servers were specified when no suitab...
[systemtap.git] / cscommon.cxx
1 /*
2 Compile-server and client common functions
3 Copyright (C) 2011 Red Hat Inc.
4
5 This file is part of systemtap, and is free software. You can
6 redistribute it and/or modify it under the terms of the GNU General
7 Public License (GPL); either version 2, or (at your option) any
8 later version.
9 */
10 #include "util.h"
11 #include "cscommon.h"
12
13 #include <fstream>
14 #include <string>
15 #include <vector>
16 #include <cstdlib>
17 #include <cstring>
18 #include <cassert>
19 #include <iomanip>
20
21 #if HAVE_NSS
22 extern "C"
23 {
24 #include <ssl.h>
25 }
26 #endif
27
28 using namespace std;
29
30 cs_protocol_version::~cs_protocol_version ()
31 {
32 assert (this->v);
33 free ((void*)this->v);
34 }
35
36 const cs_protocol_version &
37 cs_protocol_version::operator= (const char *v)
38 {
39 if (this->v)
40 free ((void *)this->v);
41 this->v = strdup (v);
42 return *this;
43 }
44
45 bool
46 cs_protocol_version::operator< (const cs_protocol_version &that) const
47 {
48 // Compare the levels of each version in turn.
49 vector<string> these_tokens;
50 tokenize (this->v, these_tokens, ".");
51 vector<string> those_tokens;
52 tokenize (that.v, those_tokens, ".");
53
54 unsigned this_limit = these_tokens.size ();
55 unsigned that_limit = those_tokens.size ();
56 unsigned i;
57 for (i = 0; i < this_limit && i < that_limit; ++i)
58 {
59 char *e;
60 unsigned long this_level = strtoul (these_tokens[i].c_str (), & e, 0);
61 assert (! *e);
62 unsigned long that_level = strtoul (those_tokens[i].c_str (), & e, 0);
63 assert (! *e);
64 if (this_level > that_level)
65 return false;
66 if (this_level < that_level)
67 return true;
68 }
69
70 // If the other version has more components, then this one is less than that one.
71 if (i < that_limit)
72 {
73 assert (i == this_limit);
74 return true;
75 }
76 // This version is greater than or equal to that one.
77 return false;
78 }
79
80 #if HAVE_NSS
81 int
82 read_from_file (const string &fname, cs_protocol_version &data)
83 {
84 // C++ streams may not set errno in the even of a failure. However if we
85 // set it to 0 before each operation and it gets set during the operation,
86 // then we can use its value in order to determine what happened.
87 string dataStr;
88 errno = 0;
89 ifstream f (fname.c_str ());
90 if (! f.good ())
91 {
92 clog << _F("Unable to open file '%s' for reading: ", fname.c_str());
93 goto error;
94 }
95
96 // Read the data;
97 errno = 0;
98 f >> dataStr;
99 if (f.fail ())
100 {
101 clog << _F("Unable to read from file '%s': ", fname.c_str());
102 goto error;
103 }
104
105 data = dataStr.c_str ();
106
107 // NB: not necessary to f.close ();
108 return 0; // Success
109
110 error:
111 if (errno)
112 clog << strerror (errno) << endl;
113 else
114 clog << _("unknown error") << endl;
115 return 1; // Failure
116 }
117
118 string get_cert_serial_number (const CERTCertificate *cert)
119 {
120 ostringstream serialNumber;
121 serialNumber << hex << setfill('0') << right;
122 for (unsigned i = 0; i < cert->serialNumber.len; ++i)
123 {
124 if (i > 0)
125 serialNumber << ':';
126 serialNumber << setw(2) << (unsigned)cert->serialNumber.data[i];
127 }
128 return serialNumber.str ();
129 }
130 #endif /* HAVE_NSS */
This page took 0.041278 seconds and 5 git commands to generate.