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
15 ******************************************************************************/
16 #include "usage_stats.hh"
25 #include <arpa/inet.h>
28 #include <netinet/in.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
38 #include <sys/utsname.h>
42 #define SA struct sockaddr
48 std::string
UsageData::id
;
49 std::string
UsageData::host
;
50 std::string
UsageData::platform
;
53 extern int gethostname( char *name
, int namelen
);
56 static int countDots (const char *name
)
59 for (n
=0; *name
; ++name
) if (*name
=='.') ++n
;
63 std::string
gethostnameFullyQualified ( void )
71 if (-1 == gethostname(host
, sizeof(host
))) {
72 //perror("warning - getting local hostname");
73 strcpy(host
,"localhost");
76 nd
= countDots(fqname
);
78 hp
= gethostbyname(host
);
80 //fprintf(stderr, "warning - can't gethostbyname(%s): %s\n",host, hstrerror(h_errno));
84 if (nd
<= countDots(hp
->h_name
)) {
85 fqname
= const_cast<char*>(hp
->h_name
);
86 nd
= countDots(fqname
);
89 for (nm
= hp
->h_aliases
; *nm
; ++nm
) {
90 int d
= countDots(*nm
);
91 if (d
> nd
) {fqname
= *nm
; nd
=d
;}
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?)
103 char *e
= getenv("LOCALDOMAIN");
104 if (e
) strcpy(domain
, e
);
107 if( !domain
[0] && NULL
!= (fp
=fopen("/etc/resolv.conf","r")) ) {
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
);
115 domain
[nd
] = 0; /* nul terminate (or reset empty) */
119 if( !domain
[0] && NULL
!= (fp
=fopen("/etc/defaultdomain","r")) ) {
120 (void)fgets(domain
, sizeof(domain
), fp
);
127 nd
= strlen(domain
) - 1;
128 while (first
<= nd
&& isspace(domain
[first
])) ++first
;
129 while (nd
> first
&& isspace(domain
[nd
])) domain
[nd
--] = 0;
132 if (fqname
!= host
) strcpy(host
,fqname
);
133 if ('.'!=domain
[first
]) strcat(host
,".");
134 strcat(host
,domain
+first
);
139 return(std::string(fqname
));
142 UsageData::UsageData() {
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
;
150 buffer_size
= sizeof(computer_name
);
151 if (GetComputerName(computer_name
, &buffer_size
))
157 p
= getpwuid(getuid());
164 host
= gethostnameFullyQualified();
168 int result
= uname(&name
);
170 platform
= std::string(name
.sysname
) + " " + std::string(name
.release
) + " " + std::string(name
.machine
);
179 fp
= popen("lsb_release -a 2>/dev/null", "r");
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());
187 platform
.append(dist
);
193 UsageData::~UsageData() {
202 void 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";
208 pthread_create(&thread
, NULL
, sendData
, data
);
211 void* UsageData::sendData(void* m
) {
212 thread_data
* my_data
;
213 my_data
= (thread_data
*)m
;
216 my_data
->sndr
->send(my_data
->msg
.c_str());
217 delete my_data
->sndr
;
220 // delete the data after use
227 //**************** HttpSender *******************
229 ssize_t
process_http(int sockfd
, const char *host
, const char *page
, const char *poststr
)
231 char sendline
[MAXLINE
+ 1];//, recvline[MAXLINE + 1];
233 //printf("\n ##### \n%s\n ##### \n", poststr);
234 snprintf(sendline
, MAXSUB
,
235 "POST %s HTTP/1.0\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
);
241 (void)write(sockfd
, sendline
, strlen(sendline
));
242 /*while ((n = read(sockfd, recvline, MAXLINE)) > 0) {
244 printf("%s", recvline);
249 // msg must be in the right format to process!
250 // id=x&host=y&platform=z&gccv=v&titanv=t&msg=m\r
251 void HttpSender::send(const char* msg
) {
253 struct sockaddr_in servaddr
, clientaddr
;
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 //*******************************************************
262 struct hostent
*hptr
;
263 if ((hptr
= gethostbyname(hname
)) == NULL
) {
264 /*fprintf(stderr, " gethostbyname error for host: %s: %s",
265 hname, hstrerror(h_errno));*/
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
));
274 //fprintf(stderr, "Error call inet_ntop \n");
278 sockfd
= socket(AF_INET
, SOCK_STREAM
, 0);
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
));
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
);
301 connect(sockfd
, (SA
*) & servaddr
, sizeof(servaddr
));
303 process_http(sockfd
, hname
, page
, poststr
);
307 //**************** DNSSender *******************
309 DNSSender::DNSSender() : nameserver("172.31.21.9"), domain("domain.net") { }
311 void DNSSender::send(const char* /* msg */) {
312 //send_over_dns(msg, "TXT", "pass", nameserver, domain);
This page took 0.077051 seconds and 5 git commands to generate.