#include <cstdlib>
#include <iostream>
#include <winsock.h>
#include <windows.h>
//using namespace std;
const char *IP = "10.28.100.254";
const char *NAME = "username";
const char *PASS = "password";
const int PORT = 900;
const int BUF_LEN = 4096;
const int SLEEP_TIME = 500;
int send_recv(struct sockaddr_in *server, char *buf)
{
SOCKET client;
int len = 0;
// connect
memset(&client, 0, sizeof(client));
client = socket(AF_INET, SOCK_STREAM, 0);
if (client < 0) {
perror("socket failed");
return -1;
}
if (connect(client, (struct sockaddr *)server, sizeof(struct sockaddr_in)) < 0) {
perror("connect failed");
return -2;
}
// send msg
printf("%s/n/n", buf);
if (send(client, buf, strlen(buf)+2, 0) < 0) {
perror("send() failed");
return -3;
}
printf("===Send Successful===/n");
// recv msg
memset(buf, 0, BUF_LEN);
if ((len = recv(client, buf, BUF_LEN, 0)) < 0) {
perror("recv() failed");
return -4;
}
printf("===Recv Successful=== len:%d/n", len);
printf("%s/n/n", buf);
closesocket(client);
return 0;
}
int main(int argc, char *argv[])
{
int ret = 0;
int len = 0;
char *buf;
char id[32];
char *c1, *c2;
struct sockaddr_in server;
WSADATA wsd;
if (WSAStartup(MAKEWORD(2,2),&wsd) != 0) {
perror("wsastartup() failed");
return -1;
}
memset(&server, 0, sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr=inet_addr(IP);
buf = (char*)malloc(BUF_LEN);
if (buf == NULL) {
goto ERROR_OUT;
}
// 1.
memset(buf, 0, BUF_LEN);
sprintf(buf, "%s",
"GET / HTTP/1.1/r/n"
"Host: 10.28.100.254:900/r/n"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3/r/n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8/r/n"
"Accept-Language: en-us,en;q=0.5/r/n"
"Accept-Encoding: gzip,deflate/r/n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7/r/n"
"Keep-Alive: 300/r/n"
"Connection: keep-alive/r/n");
ret = send_recv(&server, buf);
if ( ret < 0 ) {
goto ERROR_OUT;
}
// get id
memset(id, 0, sizeof(id));
c1 = strstr(buf, "NAME=/"ID/" VALUE=/"");
if (c1) {
c2 = strstr(c1, "/">");
if (c2) {
strncpy(id, c1 + 17, c2 - c1 - 17);
}
}
if (strlen(id) < 12) {
printf("get id error/n");
goto ERROR_OUT;
}
printf("id=%s/n", id);
// 2.
Sleep(SLEEP_TIME);
memset(buf, 0, BUF_LEN);
sprintf(buf, "%s%s%s%s",
"POST / HTTP/1.1/r/n"
"Host: 10.28.100.254:900/r/n"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3/r/n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8/r/n"
"Accept-Language: en-us,en;q=0.5/r/n"
"Accept-Encoding: gzip,deflate/r/n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7/r/n"
"Keep-Alive: 300/r/n"
"Connection: keep-alive/r/n"
"Referer: http://10.28.100.254:900//r/n"
"Content-Type: application/x-www-form-urlencoded/r/n"
"Content-Length: 39/r/n/r/n"
"ID=", id, "&STATE=1&DATA=", NAME
);
ret = send_recv(&server, buf);
if ( ret < 0 ) {
goto ERROR_OUT;
}
// 3.
Sleep(SLEEP_TIME);
memset(buf, 0, BUF_LEN);
sprintf(buf, "%s%s%s%s",
"POST / HTTP/1.1/r/n"
"Host: 10.28.100.254:900/r/n"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3/r/n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8/r/n"
"Accept-Language: en-us,en;q=0.5/r/n"
"Accept-Encoding: gzip,deflate/r/n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7/r/n"
"Keep-Alive: 300/r/n"
"Connection: keep-alive/r/n"
"Referer: http://10.28.100.254:900//r/n"
"Content-Type: application/x-www-form-urlencoded/r/n"
"Content-Length: 32/r/n"
"/r/n"
"ID=", id, "&STATE=2&DATA=", PASS
);
ret = send_recv(&server, buf);
if ( ret < 0 ) {
goto ERROR_OUT;
}
// 4.
Sleep(SLEEP_TIME);
memset(buf, 0, BUF_LEN);
sprintf(buf, "%s%s%s",
"POST / HTTP/1.1/r/n"
"Host: 10.28.100.254:900/r/n"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3/r/n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8/r/n"
"Accept-Language: en-us,en;q=0.5/r/n"
"Accept-Encoding: gzip,deflate/r/n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7/r/n"
"Keep-Alive: 300/r/n"
"Connection: keep-alive/r/n"
"Referer: http://10.28.100.254:900//r/n"
"Content-Type: application/x-www-form-urlencoded/r/n"
"Content-Length: 30/r/n"
"/r/n"
"ID=", id, "&STATE=3&DATA=1"
);
ret = send_recv(&server, buf);
if ( ret < 0 ) {
goto ERROR_OUT;
}
if (!strstr(buf, "User authorized for standard services (1 rules)")) {
ret = -9;
goto ERROR_OUT;
}
if (buf)
free(buf);
printf("Login successfully!/n");
WSACleanup();
Sleep(500);
return EXIT_SUCCESS;
ERROR_OUT:
if (buf)
free(buf);
printf("Login failed: %d/n", ret);
WSACleanup();
system("PAUSE");
return -2;
}
// vim: ts=4 sw=4 expandtab
552004974