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