Skip to content

Instantly share code, notes, and snippets.

@404neko
Last active March 3, 2016 13:31
Show Gist options
  • Save 404neko/a3b4d488a62df263376f to your computer and use it in GitHub Desktop.
Save 404neko/a3b4d488a62df263376f to your computer and use it in GitHub Desktop.
//use "gcc mon.c -lgdi32 -lIphlpapi -lws2_32 -o mon.exe"
#include <stdio.h>
#include <math.h>
#include <process.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <stdio.h>
#include <windows.h>
#define GREEN 0
#define RED -1
#define YELLOW 1
#define WHITE 2
char *ip;
int stage = WHITE;
COLORREF crColor=RGB(255,255,255);
int x = 233;
int y = 233;
HWND hDesktopWindow;
HDC hDC;
void get_color(){
switch(stage){
case WHITE: crColor=RGB(255,255,255);break;
case RED: crColor=RGB(255,0,0);break;
case YELLOW:crColor=RGB(255,255,0);break;
case GREEN: crColor=RGB(0,255,0);break;
default: break;
}
}
int check_ip(char *str){
int i=0,count = 0;
for(;i<strlen(str);i++){
if(!(str[i]>='0'&&str[i]<='9')){
if(str[i]=='.'){
count++;
continue;
}
return 0;
}
}
if(count==3)
return 1;
else
return 0;
}
char *get_host(char *host_name){
int ret;
struct hostent *host;
struct in_addr addr;
WSADATA wsaData;
ret = WSAStartup( 0x101 , &wsaData);
if( ret != 0 )
{
return "ERROR";
}
host = gethostbyname(host_name);
if( host == NULL )
{
return "ERROR";
}
addr.s_addr = *(unsigned long * )host->h_addr;
char* to_return=(char*)malloc(sizeof(inet_ntoa(addr))*sizeof(char));
strcpy(to_return,inet_ntoa(addr));
WSACleanup();
return to_return;
}
int icmp_echo(){
HANDLE hIcmpFile;
unsigned long ipaddr = INADDR_NONE;
DWORD dwRetVal = 0;
char SendData[32] = "Nyan";
LPVOID ReplyBuffer = NULL;
DWORD ReplySize = 0;
ipaddr = inet_addr(ip);
if (ipaddr == INADDR_NONE) {
return -1;
}
hIcmpFile = IcmpCreateFile();
if (hIcmpFile == INVALID_HANDLE_VALUE) {
return -1;
}
ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
ReplyBuffer = (VOID*) malloc(ReplySize);
if (ReplyBuffer == NULL) {
return -1;
}
dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData),
NULL, ReplyBuffer, ReplySize, 1000);
if (dwRetVal != 0) {
PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
struct in_addr ReplyAddr;
ReplyAddr.S_un.S_addr = pEchoReply->Address;
return (int)pEchoReply->RoundTripTime;
}
else {
return -1;
}
return -1;
}
void use_icmp_echo(){
while(1){
sleep(1);
int stage_ = icmp_echo();
if(stage_==-1){
stage = RED;
}
if(stage_>128+32){
stage = YELLOW;
}
if(stage_>0&&stage_<128+32){
stage = GREEN;
}
}
}
void use_display(){
while(1){
get_color();
SetPixel(hDC, x+0, y+0, crColor);
SetPixel(hDC, x+0, y+1, crColor);
SetPixel(hDC, x+0, y+2, crColor);
SetPixel(hDC, x+1, y+0, crColor);
SetPixel(hDC, x+1, y+1, crColor);
SetPixel(hDC, x+1, y+2, crColor);
SetPixel(hDC, x+2, y+0, crColor);
SetPixel(hDC, x+2, y+1, crColor);
SetPixel(hDC, x+2, y+2, crColor);
}
}
int main(int argc, char **argv){
if(argc<4){
printf("Please use \"command ip x y\".\n");
exit(-1);
}
x = atoi(argv[2]);
y = atoi(argv[3]);
ip=(char *)malloc(sizeof(char)*(16+4));
ip=strcpy(ip,argv[1]);
hDesktopWindow = GetDesktopWindow();
hDC = GetDC(hDesktopWindow);
HANDLE handles[2];
handles[0] = _beginthread(use_icmp_echo, 0, 0);
handles[1] = _beginthread(use_display, 0, 0);
WaitForMultipleObjects(2, handles, TRUE, INFINITE);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment