Created
November 9, 2012 03:55
-
-
Save listnukira/4043595 to your computer and use it in GitHub Desktop.
simple bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#define SERVER_ADDR "140.113.216.151" | |
#define SERVER_PORT 12345 | |
int main() | |
{ | |
char host_addr[16]; | |
char hostname[128]; | |
char send_data[1024]; | |
char recv_data[1024]; | |
struct sockaddr_in server_addr, cli_addr; | |
int sockfd, rc; | |
// Connect to server | |
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | |
perror("Can't open stream socket."); | |
exit(-1); | |
} | |
// Set server_addr | |
bzero(&server_addr, sizeof(server_addr)); | |
server_addr.sin_family = AF_INET; | |
server_addr.sin_addr.s_addr = inet_addr(SERVER_ADDR); | |
server_addr.sin_port = htons(SERVER_PORT); | |
// Connect to server | |
if((connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr))) < 0) { | |
perror("Connect server error."); | |
close(sockfd); | |
exit(-1); | |
} | |
printf("Connect OK\n"); | |
printf("[initialAuthentication] Start Initial\n"); | |
// Get host ip address | |
bzero(&cli_addr, sizeof(cli_addr)); | |
int len = sizeof(cli_addr); | |
getsockname(sockfd, (struct sockaddr *)&cli_addr, &len); | |
inet_ntop(AF_INET, &cli_addr.sin_addr, host_addr, sizeof(host_addr)); | |
// Step 0 | |
printf("[Authentication] Authentication Step 0\n"); | |
send_data[0] = 0x00; | |
send_data[1] = 0x00; | |
send_data[2] = 0x00; | |
send_data[3] = 0x06; | |
sprintf(&send_data[4], "/hello"); | |
if ((rc = write(sockfd, send_data, 10)) < 0) { | |
close(sockfd); | |
exit(-1); | |
} | |
// Step 1 | |
if ((rc = read(sockfd, &recv_data, 1024)) < 0) { | |
close(sockfd); | |
exit(-1); | |
} | |
recv_data[rc] = '\0'; | |
printf("[Authentication] Authentication Step 1\n"); | |
// Step 2 | |
printf("[Authentication] Authentication Step 2\n"); | |
send_data[3] = 0x11; | |
sprintf(&send_data[4], "/sendIP %s", host_addr); | |
if ((rc = write(sockfd, send_data, strlen(&send_data[4]) + 4)) < 0) { | |
close(sockfd); | |
exit(-1); | |
} | |
// Step 3 | |
if ((rc = read(sockfd, &recv_data, 1024)) < 0) { | |
close(sockfd); | |
exit(-1); | |
} | |
recv_data[rc] = '\0'; | |
printf("[Authentication] Authentication Step 3\n"); | |
printf("[Authentication] Authentication OK\n"); | |
printf("Authentication OK!\n"); | |
close(sockfd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment