aboutsummaryrefslogtreecommitdiffstats
path: root/qtlongterm/qtlongterm.cpp
blob: 8bd6a7334e7732fccf8a7b254e004b463953252a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (C) 2022 The Qt Company Ltd.
//

#include "qtlongterm.h"

int main(int argc, char *argv[]) {
    if (argc < 2) helpAndExit("Too few arguments");

    std::string userId = "";
    std::string licenseId = "";
    std::string daemonAddr = "";
    uint16_t daemonPort = 0;
    std::string serverURL = "";
    std::string operation = utils::trimStr((std::string)argv[1]);
    if (operation == "--help" || operation == "-h") {
        printf("%s\n", helpText().c_str());
        exit(EXIT_SUCCESS);
    }
    if (operation != "add" && operation != "remove") {
        std::string error = "Invalid operation \"" + operation;
        error += "\"";
        helpAndExit(error);
    }
    // TODO check if user wants to use [default] settings

    // Pick up the switches
    uint8_t swCount = 0;
    // Start looping for switches from index 2, first two are command itself and the operation
    for (int i = 2; i < argc; i++) {
        std::string sw = utils::trimStr((std::string)argv[i]);
        std::string val = "";
        // Check if given switch has a value
        if (sw[0] == '-' && argc > i+1) {
            val = utils::trimStr((std::string)argv[i+1]);
            if (val[0] == '-')
                val = "";
            else
                i++;
        }
        if (val.empty()) {
            std::string error = "No value for argument " + sw;
            helpAndExit(error);
        }

        // Pick up values
        if (sw == "-i") {
            licenseId = val;
            swCount++;
        }
        else if (sw == "-u") {
            userId = val;
            swCount++;
        }
        else if (sw == "-d") {
            size_t colPos = val.find(':');
            if (colPos == std::string::npos) {
                printf("Invalid service address: Must be in form <url>:<port>\n");
                exit(EXIT_FAILURE);
            }
            daemonAddr = val.substr(0, colPos);
            daemonPort = utils::strToInt(val.substr(colPos+1));
            if (daemonPort < 1) {
                printf("Invalid service port: Must be an integer\n");
                exit(EXIT_FAILURE);
            }
            swCount++;
        }
        else if (sw == "-l") {
            serverURL = val;
            size_t colPos = val.find(':');
            if (colPos == std::string::npos) {
                printf("Invalid license server address: Must be in form <url>:<port>\n");
                exit(EXIT_FAILURE);
            }
            uint16_t port =  utils::strToInt(val.substr(colPos+1));
            if (daemonPort < 1) {
                printf("Invalid license server port: Must be an integer\n");
                exit(EXIT_FAILURE);
            }
            swCount++;
        }
        else {
            std::string error = "Invalid argument:" + sw;
            helpAndExit(error);
        }
        // printf("%s : %s\n", sw.c_str(), val.c_str());
    }

    // Now we check if all needed arguments are there
    if (swCount < 3) {
        helpAndExit("Too few arguments");
    }
    std::stringstream request;
    request << "longterm " << operation << " -u " << userId << " -i " << licenseId;
    if (serverURL.length() > 0) {
        // Add optional license server URL if specified
        request << " -l " << serverURL;
    }
    std::string msg = request.str();

    // Prepare for connection
    struct sockaddr_in server;
#if __APPLE__ || __MACH__  || __linux__
    const int inetSuccess = inet_aton(daemonAddr.c_str(), &server.sin_addr);
#else
    const int inetSuccess = inet_pton(AF_INET, daemonAddr.c_str(), &server.sin_addr.s_addr);
#endif
    if (!inetSuccess) {
        // Try to resolve hostname
        struct hostent *host;
        struct in_addr **addrList;
        if ( (host = gethostbyname( daemonAddr.c_str() ) ) == nullptr){
            printf("Failed to resolve hostname: %s\n", daemonAddr.c_str());
            exit(EXIT_FAILURE);;
        }
        addrList = (struct in_addr **) host->h_addr_list;
        server.sin_addr = *addrList[0];
    }
    server.sin_family = AF_INET;
    server.sin_port = htons(daemonPort);
    int sockFD;
    sockFD = (socket(AF_INET , SOCK_STREAM , 0));
    if (sockFD < 0) {
        throw std::runtime_error(strerror(errno));
    }

    // Connect to daemon
    int connectR = connect(sockFD, (struct sockaddr *)&server, sizeof(server));
    if (connectR == -1) {
        close(sockFD);
        printf("Error while connecting socket: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }

    // Send request
    const size_t bytesSent = send(sockFD, msg.c_str(), msg.length(), 0);

    if (bytesSent < 0 ) { // send failed
        printf("ERROR in sending to daemon: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }


    // Listen for daemon reply
    std::string reply(BUFFER_SIZE, ' ');
    ssize_t bytes_recv = recv(sockFD, &reply.front(), reply.size(), 0);
    if (bytes_recv == -1) {
        printf("Error while receiving bytes\n");
        exit(EXIT_FAILURE);
    }
    close(sockFD);

    reply = utils::trimStr(reply);
    // Show the reply and we're done
    printf("%s\n", reply.c_str());
    if (reply.find("No") != std::string::npos ||
        reply.find("ERROR") != std::string::npos) {
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}

void helpAndExit(const std::string &reason) {
    printf("\nERROR: %s\n\n", reason.c_str());
    printf("%s\n", helpText().c_str());
    exit(EXIT_FAILURE);
}

std::string helpText() {
    std::stringstream ss;
    ss << "Usage:\n";
    ss << "  qtlongterm add|remove -a daemon_url:port -u username -i license_id [-l <server_url>:<server_port>]\n";
    ss << "  Or:\n  qtlongterm --help to get this help (-h also works)\n";
    ss << "Where:\n";
    ss << "  -d = Full license daemon address with port separated by ':'.\n";
    ss << "  -u = Your Qt license username\n";
    ss << "  -i = Your Qt license ID number (must be an int)\n";
    ss << "  -l = (Optional, not required) License server address and port \n";
    ss << "Use \"add\" to request a long-term license reservation and \"remove\" to remove it\n";
    ss << "\nExample:\n";
    ss << "   qtlongterm add -d localhost:60000 -u foobar -i 12345670\n";
    return ss.str();
}