📄 client.c
字号:
/*************************************************************************** client.c - description ------------------- begin : Fri Apr 11 09:08:01 EET 2003 copyright : (C) 2001-2003 by Petri Turunen email : petri.turunen@pete.fi.eu.org ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#include<stdio.h>#ifdef HAVE_SYS_TYPES_H#include<sys/types.h> // Include these for socket(), connect(), bind(), etc.#else# error no sys/types.h#endif#ifdef HAVE_SYS_SOCKET_H#include <sys/socket.h> // Include these for socket(), connect(), bind(), etc.#else# error no sys/socket.h#endif#ifdef HAVE_NETINET_IN_H#include<netinet/in.h> // Include this for htonl(), htons(), etc.#else# error no netinet/in.h#endif#ifdef HAVE_ARPA_INET_H#include<arpa/inet.h> //inet_pton#else# error no arpa/inet.h#endif#include<netdb.h> // Include this for getprotobyname()#include<string.h> // Include this for memset()#include<unistd.h>#include <signal.h> //signal#include <errno.h> //error numbers#include <stdlib.h> //for malloc and free#include "lparser.h"extern char CRYPTKEY[]; //defined in linux_mon.cint client(char *addr, char *port,char *machine, char *status, char *error, char *Amachine, char *actionid, char *Sstate, char *service){ // Variables for the client component of the application. int sockfd; // File descriptor that represents the client socket. struct addrinfo hints, *res, *ressave; char read_buf[401]; unsigned char *outbuf; int bytes_to_write; int n, len; struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; bzero(&hints, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype=SOCK_STREAM; // Code for the client component begins here. if((n = getaddrinfo(addr,port,&hints, &res))!=0) { slog(2,"client.c: getaddrinfo failed for: %s, %s",addr,gai_strerror(n)); } n=0; ressave=res; do { //connect loop starts sockfd=socket(res->ai_family, res->ai_socktype, res->ai_protocol); if(sockfd<0) { slog(2,"client.c: Error socket: %s",strerror(errno)); return -1; } // 'Call' the server. n = connect(sockfd, res->ai_addr,res->ai_addrlen); if(n < 0) { if(errno == ETIMEDOUT || errno == EINTR) { slog(2,"Error while connecting: %s. Connection timeout.",addr); return -1; } else if(errno == ECONNREFUSED) { slog(2,"Error while connecting: %s. Connection refused.", addr); return -1; } freeaddrinfo(ressave); slog(2,"Error while connecting: %s. Unknown error.", addr); return -1; } else { break; } } while( (res = res->ai_next) != NULL); //connect loop end if(res == NULL) slog(2,"client.c: connection to: %s failed",addr); freeaddrinfo(ressave); //set write timeout setsockopt(sockfd,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv)); //set read timeout setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)); /**** Send helo ***/ bytes_to_write = encrypt_stuff(CRYPTKEY, &outbuf, "HELO"); send_tcp(outbuf, bytes_to_write, sockfd); free(outbuf); len=read(sockfd, read_buf, 400); decrypt_stuff(CRYPTKEY, &outbuf, read_buf, len); if(strcmp(outbuf,"Im ready")!=0) { slog(2,"Error while talking to server."); shutdown(sockfd,SHUT_WR); free(outbuf); close(sockfd); return 2; } free(outbuf); /**** Send Version ***/ bytes_to_write = encrypt_stuff(CRYPTKEY, &outbuf, "1"); send_tcp(outbuf, bytes_to_write, sockfd); free(outbuf); len=read(sockfd, read_buf, 400); /**** Send Machine name ***/ if(machine[0]=='\0') { slog(1,"client.c: Warning no computer name provided using default."); bytes_to_write = encrypt_stuff(CRYPTKEY, &outbuf, "computer"); } else bytes_to_write = encrypt_stuff(CRYPTKEY, &outbuf, "%s", machine); send_tcp(outbuf, bytes_to_write, sockfd); free(outbuf); len=read(sockfd, read_buf, 400); /**** We are reporting event ***/ bytes_to_write = encrypt_stuff(CRYPTKEY, &outbuf, "REPORT"); send_tcp(outbuf, bytes_to_write, sockfd); free(outbuf); len=read(sockfd, read_buf, 400); /**** Send event status ***/ bytes_to_write = encrypt_stuff(CRYPTKEY, &outbuf, "%s", status); send_tcp(outbuf, bytes_to_write, sockfd); free(outbuf); len=read(sockfd, read_buf, 400); decrypt_stuff(CRYPTKEY, &outbuf, read_buf, len); if(strcmp(outbuf,"Ok")!=0) slog(2,"client.c: ERROR while sending event status to server"); free(outbuf); /**** Send event description ***/ bytes_to_write = encrypt_stuff(CRYPTKEY, &outbuf, "%s", error); send_tcp(outbuf, bytes_to_write, sockfd); free(outbuf); len=read(sockfd, read_buf, 400); decrypt_stuff(CRYPTKEY, &outbuf, read_buf, len); if(strcmp(outbuf,"Ok")!=0) slog(2,"client.c: ERROR while sending event description to server"); free(outbuf); /**** Send Amachine ***/ bytes_to_write = encrypt_stuff(CRYPTKEY, &outbuf, "%s", Amachine); send_tcp(outbuf, bytes_to_write, sockfd); free(outbuf); len=read(sockfd, read_buf, 400); decrypt_stuff(CRYPTKEY, &outbuf, read_buf, len); if(strcmp(outbuf,"Ok")!=0) slog(2,"client.c: ERROR while sending Amachine to server"); free(outbuf); /**** Send actionid ***/ bytes_to_write = encrypt_stuff(CRYPTKEY, &outbuf, "%s", actionid); send_tcp(outbuf, bytes_to_write, sockfd); free(outbuf); len=read(sockfd, read_buf, 400); decrypt_stuff(CRYPTKEY, &outbuf, read_buf, len); if(strcmp(outbuf,"Ok")!=0) slog(2,"client.c: ERROR while sending actionid to server"); free(outbuf); /**** Send Sstate ***/ bytes_to_write = encrypt_stuff(CRYPTKEY, &outbuf, "%s", Sstate); send_tcp(outbuf, bytes_to_write, sockfd); free(outbuf); len=read(sockfd, read_buf, 400); decrypt_stuff(CRYPTKEY, &outbuf, read_buf, len); if(strcmp(outbuf,"Ok")!=0) slog(2,"client.c: ERROR while sending Sstate to server"); free(outbuf); /**** Send service ***/ bytes_to_write = encrypt_stuff(CRYPTKEY, &outbuf, "%s", service); send_tcp(outbuf, bytes_to_write, sockfd); free(outbuf); len=read(sockfd, read_buf, 400); decrypt_stuff(CRYPTKEY, &outbuf, read_buf, len); if(strcmp(outbuf,"Ok")!=0) slog(2,"client.c: ERROR while sending service to server"); free(outbuf); /**** Send QUIT ***/ bytes_to_write = encrypt_stuff(CRYPTKEY, &outbuf, "QUIT"); send_tcp(outbuf, bytes_to_write, sockfd); free(outbuf); len=read(sockfd, read_buf, 400); decrypt_stuff(CRYPTKEY, &outbuf, read_buf, len); if(strcmp(outbuf,"Bye Bye")!=0) slog(2,"client.c: Failed to get correct end message from server."); free(outbuf); shutdown(sockfd,SHUT_WR); close(sockfd); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -