1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 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 #include "usage_stats.hh"
17 #include <arpa/inet.h>
20 #include <netinet/in.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
30 #include <sys/utsname.h>
34 #define SA struct sockaddr
40 std::string
UsageData::id
;
41 std::string
UsageData::host
;
42 std::string
UsageData::platform
;
45 extern int gethostname( char *name
, int namelen
);
48 static int countDots (const char *name
)
51 for (n
=0; *name
; ++name
) if (*name
=='.') ++n
;
55 std::string
gethostnameFullyQualified ( void )
63 if (-1 == gethostname(host
, sizeof(host
))) {
64 //perror("warning - getting local hostname");
65 strcpy(host
,"localhost");
68 nd
= countDots(fqname
);
70 hp
= gethostbyname(host
);
72 //fprintf(stderr, "warning - can't gethostbyname(%s): %s\n",host, hstrerror(h_errno));
76 if (nd
<= countDots(hp
->h_name
)) {
77 fqname
= const_cast<char*>(hp
->h_name
);
78 nd
= countDots(fqname
);
81 for (nm
= hp
->h_aliases
; *nm
; ++nm
) {
82 int d
= countDots(*nm
);
83 if (d
> nd
) {fqname
= *nm
; nd
=d
;}
88 /* still need to find a domain, look through the usual suspects:
89 * LOCALDOMAIN env variable
90 * domain defn from /etc/resolv.conf
91 * /etc/defaultdomain (sun only?)
95 char *e
= getenv("LOCALDOMAIN");
96 if (e
) strcpy(domain
, e
);
99 if( !domain
[0] && NULL
!= (fp
=fopen("/etc/resolv.conf","r")) ) {
101 while( fgets(domain
, sizeof(domain
), fp
) ) {
102 if( 0==strncmp("domain ",domain
,7) ) {
103 nd
= strlen(domain
) - 7;
104 memmove(domain
, domain
+7, nd
);
107 domain
[nd
] = 0; /* nul terminate (or reset empty) */
111 if( !domain
[0] && NULL
!= (fp
=fopen("/etc/defaultdomain","r")) ) {
112 fgets(domain
, sizeof(domain
), fp
);
119 nd
= strlen(domain
) - 1;
120 while (first
<= nd
&& isspace(domain
[first
])) ++first
;
121 while (nd
> first
&& isspace(domain
[nd
])) domain
[nd
--] = 0;
124 if (fqname
!= host
) strcpy(host
,fqname
);
125 if ('.'!=domain
[first
]) strcat(host
,".");
126 strcat(host
,domain
+first
);
131 return(std::string(fqname
));
134 UsageData::UsageData() {
137 TCHAR user_name
[UNLEN
+ 1], computer_name
[MAX_COMPUTERNAME_LENGTH
+ 1];
138 DWORD buffer_size
= sizeof(user_name
);
139 if (GetUserName(user_name
, &buffer_size
)) id
=user_name
;
142 buffer_size
= sizeof(computer_name
);
143 if (GetComputerName(computer_name
, &buffer_size
))
149 p
= getpwuid(getuid());
156 host
= gethostnameFullyQualified();
160 int result
= uname(&name
);
162 platform
= std::string(name
.sysname
) + " " + std::string(name
.release
) + " " + std::string(name
.machine
);
171 fp
= popen("lsb_release -a 2>/dev/null", "r");
173 while (fgets(path
, sizeof(path
), fp
) != NULL
)
174 if (strncmp ("Description",path
,11) == 0) dist
.append(path
);
175 //printf("\n'%s'\n", dist.c_str());
179 platform
.append(dist
);
185 UsageData::~UsageData() {
194 void UsageData::sendDataThreaded(std::string msg
, Sender
* sender
) {
195 thread_data
* data
= new thread_data
; // will be deleted by sendData
196 data
->msg
= "id="+ id
+ "&host=" + host
+ "&platform=" + platform
+ "&gccv=" + C_COMPILER_VERSION
+ "&titanv=" + PRODUCT_NUMBER
+ "&msg="+ msg
+ "\r";
200 pthread_create(&thread
, NULL
, sendData
, data
);
203 void* UsageData::sendData(void* m
) {
204 thread_data
* my_data
;
205 my_data
= (thread_data
*)m
;
208 my_data
->sndr
->send(my_data
->msg
.c_str());
209 delete my_data
->sndr
;
212 // delete the data after use
219 //**************** HttpSender *******************
221 ssize_t
process_http(int sockfd
, const char *host
, const char *page
, const char *poststr
)
223 char sendline
[MAXLINE
+ 1];//, recvline[MAXLINE + 1];
225 //printf("\n ##### \n%s\n ##### \n", poststr);
226 snprintf(sendline
, MAXSUB
,
227 "POST %s HTTP/1.0\r\n"
229 "Content-type: application/x-www-form-urlencoded\r\n"
230 "Content-length: %lu\r\n\r\n"
231 "%s", page
, host
, (unsigned long)strlen(poststr
), poststr
);
233 write(sockfd
, sendline
, strlen(sendline
));
234 /*while ((n = read(sockfd, recvline, MAXLINE)) > 0) {
236 printf("%s", recvline);
241 // msg must be in the right format to process!
242 // id=x&host=y&platform=z&gccv=v&titanv=t&msg=m\r
243 void HttpSender::send(const char* msg
) {
245 struct sockaddr_in servaddr
, clientaddr
;
248 const char *hname
= "ttcn.ericsson.se";
249 const char *page
= "/download/usage_stats/usage_stats.php";
250 const char *poststr
= msg
;//.c_str();
251 //*******************************************************
254 struct hostent
*hptr
;
255 if ((hptr
= gethostbyname(hname
)) == NULL
) {
256 /*fprintf(stderr, " gethostbyname error for host: %s: %s",
257 hname, hstrerror(h_errno));*/
260 //printf("hostname: %s\n", hptr->h_name);
261 if (hptr
->h_addrtype
== AF_INET
262 && (pptr
= hptr
->h_addr_list
) != NULL
) {
263 //printf("address: %s\n",
264 inet_ntop(hptr
->h_addrtype
, *pptr
, str
,sizeof(str
));
266 //fprintf(stderr, "Error call inet_ntop \n");
270 sockfd
= socket(AF_INET
, SOCK_STREAM
, 0);
272 memset(&clientaddr
, 0, sizeof(clientaddr
));
273 clientaddr
.sin_family
= AF_INET
;
274 clientaddr
.sin_port
= htons(49555);
275 inet_pton(AF_INET
, "0.0.0.0", &clientaddr
.sin_addr
);
276 if (bind(sockfd
, (SA
*) & clientaddr
, sizeof(clientaddr
)) < 0) {
277 clientaddr
.sin_port
= htons(59555);
278 if (bind(sockfd
, (SA
*) & clientaddr
, sizeof(clientaddr
)) < 0) {
279 clientaddr
.sin_port
= htons(61555);
280 if (bind(sockfd
, (SA
*) & clientaddr
, sizeof(clientaddr
)) < 0) {
281 // last ditch effort, use an automatically generated port
282 clientaddr
.sin_port
= htons(0);
283 bind(sockfd
, (SA
*) & clientaddr
, sizeof(clientaddr
));
288 memset(&servaddr
, 0, sizeof(servaddr
));
289 servaddr
.sin_family
= AF_INET
;
290 servaddr
.sin_port
= htons(80);
291 inet_pton(AF_INET
, str
, &servaddr
.sin_addr
);
293 connect(sockfd
, (SA
*) & servaddr
, sizeof(servaddr
));
295 process_http(sockfd
, hname
, page
, poststr
);
299 //**************** DNSSender *******************
301 DNSSender::DNSSender() : nameserver("172.31.21.9"), domain("domain.net") { }
303 void DNSSender::send(const char* /* msg */) {
304 //send_over_dns(msg, "TXT", "pass", nameserver, domain);
This page took 0.037704 seconds and 5 git commands to generate.