Use LTTngUSTLogger logger plugin in logtest regression test
[deliverable/titan.core.git] / common / usage_stats.cc
CommitLineData
d44e3c4f 1/******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *
10 * Balasko, Jeno
11 * Baranyi, Botond
12 * Ormandi, Matyas
13 * Szalai, Gabor
14 *
15 ******************************************************************************/
970ed795
EL
16#include "usage_stats.hh"
17
18#ifdef MINGW
19# include <windows.h>
20# include <lmcons.h>
21#else
22# include <pwd.h>
23#endif
24
25#include <arpa/inet.h>
26#include <assert.h>
27#include <errno.h>
28#include <netinet/in.h>
29#include <signal.h>
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <sys/wait.h>
36#include <netdb.h>
37#include <unistd.h>
38#include <sys/utsname.h>
39#include <stdio.h>
40#include <time.h>
41
42#define SA struct sockaddr
43#define MAXLINE 4096
44#define MAXSUB 2000
45
46#define LISTENQ 1024
47
48std::string UsageData::id;
49std::string UsageData::host;
50std::string UsageData::platform;
51
52#ifdef sun
53extern int gethostname( char *name, int namelen );
54#endif
55
56static int countDots (const char *name )
57{
58 int n;
59 for (n=0; *name; ++name) if (*name=='.') ++n;
60 return(n);
61}
62
63std::string gethostnameFullyQualified ( void )
64{
65 char host[774];
66 struct hostent *hp;
67 char *fqname=NULL;
68 int nd;
69 host[0] = 0;
70
71 if (-1 == gethostname(host, sizeof(host))) {
72 //perror("warning - getting local hostname");
73 strcpy(host,"localhost");
74 }
75 fqname = host;
76 nd = countDots(fqname);
77
78 hp = gethostbyname(host);
79 if (!hp) {
80 //fprintf(stderr, "warning - can't gethostbyname(%s): %s\n",host, hstrerror(h_errno));
81 }
82 else {
83 char **nm;
84 if (nd <= countDots(hp->h_name)) {
85 fqname = const_cast<char*>(hp->h_name);
86 nd = countDots(fqname);
87 }
88
89 for (nm = hp->h_aliases; *nm; ++nm) {
90 int d = countDots(*nm);
91 if (d > nd) {fqname = *nm; nd=d;}
92 }
93 }
94
95 if (2 > nd) {
96 /* still need to find a domain, look through the usual suspects:
97 * LOCALDOMAIN env variable
98 * domain defn from /etc/resolv.conf
99 * /etc/defaultdomain (sun only?)
100 */
101 FILE *fp = NULL;
102 char domain[1024];
103 char *e = getenv("LOCALDOMAIN");
104 if (e) strcpy(domain, e);
105 else domain[0] = 0;
106
107 if( !domain[0] && NULL != (fp=fopen("/etc/resolv.conf","r")) ) {
108 nd = 0;
109 while( fgets(domain, sizeof(domain), fp) ) {
110 if( 0==strncmp("domain ",domain,7) ) {
111 nd = strlen(domain) - 7;
112 memmove(domain, domain+7, nd);
113 }
114 }
115 domain[nd] = 0; /* nul terminate (or reset empty) */
116 fclose(fp);
117 }
118
119 if( !domain[0] && NULL != (fp=fopen("/etc/defaultdomain","r")) ) {
86be9305 120 (void)fgets(domain, sizeof(domain), fp);
970ed795
EL
121 fclose(fp);
122 }
123
124 if( domain[0] ) {
125 /* trim blanks */
126 int first = 0;
127 nd = strlen(domain) - 1;
128 while (first <= nd && isspace(domain[first])) ++first;
129 while (nd > first && isspace(domain[nd])) domain[nd--] = 0;
130
131 if (domain[first]) {
132 if (fqname != host) strcpy(host,fqname);
133 if ('.'!=domain[first]) strcat(host,".");
134 strcat(host,domain+first);
135 fqname = host;
136 }
137 }
138 }
139 return(std::string(fqname));
140}
141
142UsageData::UsageData() {
143
144#ifdef MINGW
145 TCHAR user_name[UNLEN + 1], computer_name[MAX_COMPUTERNAME_LENGTH + 1];
146 DWORD buffer_size = sizeof(user_name);
147 if (GetUserName(user_name, &buffer_size)) id=user_name;
148 else id="unknown";
149
150 buffer_size = sizeof(computer_name);
151 if (GetComputerName(computer_name, &buffer_size))
152 host=computer_name;
153 else host="unknown";
154#else
155 struct passwd *p;
156 setpwent();
157 p = getpwuid(getuid());
158 if (p != NULL)
159 id = p->pw_name;
160 else
161 id="unknown";
162 endpwent();
163
164 host = gethostnameFullyQualified();
165
166 struct utsname name;
167
168 int result = uname(&name);
169 if (result >= 0)
170 platform= std::string(name.sysname) + " " + std::string(name.release) + " " + std::string(name.machine);
171 else
172 platform="unknown";
173
174#ifdef LINUX
175 std::string dist;
176 FILE *fp;
177 char path[128];
178
179 fp = popen("lsb_release -a 2>/dev/null", "r");
180 if (fp != NULL) {
181 while (fgets(path, sizeof(path), fp) != NULL)
182 if (strncmp ("Description",path,11) == 0) dist.append(path);
183 //printf("\n'%s'\n", dist.c_str());
184 pclose(fp);
185 }
186 dist.erase(0,12);
187 platform.append(dist);
188#endif
189
190#endif
191}
192
193UsageData::~UsageData() {
194
195}
196
197struct thread_data {
198 std::string msg;
199 Sender* sndr;
200};
201
202void UsageData::sendDataThreaded(std::string msg, Sender* sender) {
203 thread_data* data = new thread_data; // will be deleted by sendData
204 data->msg = "id="+ id + "&host=" + host + "&platform=" + platform + "&gccv=" + C_COMPILER_VERSION + "&titanv=" + PRODUCT_NUMBER + "&msg="+ msg + "\r";
205 data->sndr = sender;
206
207 pthread_t thread;
208 pthread_create(&thread, NULL, sendData, data);
209}
210
211void* UsageData::sendData(void* m) {
212 thread_data* my_data;
213 my_data = (thread_data*)m;
214
215 if(my_data->sndr) {
216 my_data->sndr->send(my_data->msg.c_str());
217 delete my_data->sndr;
218 }
219
220 // delete the data after use
221 delete my_data;
222
223 return NULL;
224}
225
226
227//**************** HttpSender *******************
228
229ssize_t process_http(int sockfd, const char *host, const char *page, const char *poststr)
230{
231 char sendline[MAXLINE + 1];//, recvline[MAXLINE + 1];
232 ssize_t n = 0;
233 //printf("\n ##### \n%s\n ##### \n", poststr);
234 snprintf(sendline, MAXSUB,
235 "POST %s HTTP/1.0\r\n"
236 "Host: %s\r\n"
237 "Content-type: application/x-www-form-urlencoded\r\n"
238 "Content-length: %lu\r\n\r\n"
239 "%s", page, host, (unsigned long)strlen(poststr), poststr);
240
86be9305 241 (void)write(sockfd, sendline, strlen(sendline));
970ed795
EL
242 /*while ((n = read(sockfd, recvline, MAXLINE)) > 0) {
243 recvline[n] = '\0';
244 printf("%s", recvline);
245 }*/
246 return n;
247}
248
249// msg must be in the right format to process!
250// id=x&host=y&platform=z&gccv=v&titanv=t&msg=m\r
251void HttpSender::send(const char* msg) {
252 int sockfd;
253 struct sockaddr_in servaddr, clientaddr;
254
255 char **pptr;
256 const char *hname = "ttcn.ericsson.se";
257 const char *page = "/download/usage_stats/usage_stats.php";
258 const char *poststr = msg;//.c_str();
259 //*******************************************************
260
261 char str[50];
262 struct hostent *hptr;
263 if ((hptr = gethostbyname(hname)) == NULL) {
264 /*fprintf(stderr, " gethostbyname error for host: %s: %s",
265 hname, hstrerror(h_errno));*/
266 return;
267 }
268 //printf("hostname: %s\n", hptr->h_name);
269 if (hptr->h_addrtype == AF_INET
270 && (pptr = hptr->h_addr_list) != NULL) {
271 //printf("address: %s\n",
272 inet_ntop(hptr->h_addrtype, *pptr, str,sizeof(str));
273 } else {
274 //fprintf(stderr, "Error call inet_ntop \n");
275 return;
276 }
277
278 sockfd = socket(AF_INET, SOCK_STREAM, 0);
279
280 memset(&clientaddr, 0, sizeof(clientaddr));
281 clientaddr.sin_family = AF_INET;
282 clientaddr.sin_port = htons(49555);
283 inet_pton(AF_INET, "0.0.0.0", &clientaddr.sin_addr);
284 if (bind(sockfd, (SA *) & clientaddr, sizeof(clientaddr)) < 0) {
285 clientaddr.sin_port = htons(59555);
286 if (bind(sockfd, (SA *) & clientaddr, sizeof(clientaddr)) < 0) {
287 clientaddr.sin_port = htons(61555);
288 if (bind(sockfd, (SA *) & clientaddr, sizeof(clientaddr)) < 0) {
289 // last ditch effort, use an automatically generated port
290 clientaddr.sin_port = htons(0);
291 bind(sockfd, (SA *) & clientaddr, sizeof(clientaddr));
292 }
293 }
294 }
295
296 memset(&servaddr, 0, sizeof(servaddr));
297 servaddr.sin_family = AF_INET;
298 servaddr.sin_port = htons(80);
299 inet_pton(AF_INET, str, &servaddr.sin_addr);
300
301 connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
302
303 process_http(sockfd, hname, page, poststr);
304 close(sockfd);
305}
306
307//**************** DNSSender *******************
308
309DNSSender::DNSSender() : nameserver("172.31.21.9"), domain("domain.net") { }
310
311void DNSSender::send(const char* /* msg */) {
312 //send_over_dns(msg, "TXT", "pass", nameserver, domain);
313}
314
This page took 0.036454 seconds and 5 git commands to generate.