พอดีเอา VS 2010 มาเขียน C++ เพื่อที่จะให้มันทำงานบน Server ใครพอจะแนะแนวเกี่ยวกับ Code ตัวนี้ได้มั่งครับ และผมอยากรู้ว่า เขียนใน VS 2010 แล้วมันขึ้นตัวเส้นแดงว่า identifier นี่คือ อะไรอะครับ
/*##### auth.c listing*/
/*##### Authentication Script Example -- Start --*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<netinet/in.h>
#include<getopt.h>
char *command, *ipaddr, *macaddr;
char *name, *password, *role;
char *tout, *secret;
char *auth, *key, enchashbuf[41];
unsigned char hashbuf[20];
char *version;
char post[4096], cmdbuf[512], encbuf[1024];
#define DEBUG
#ifdef DEBUG
#define debug(x...)fprintf(stderr, x)
#else
#define debug(x...)
#endif
extern int cgi_escape_url(char *t, int tl, char *s, int sl, int b_newline);
static void encode_message_digest (unsigned char *md, int mdlen, char *output);
static void usage (void)
{
fprintf(stderr, "Usage: ecp [options] <switch> <command> [<secret>]\n");
fprintf(stderr, " \n");
fprintf(stderr, " <switch> Switch IP address.\n");
fprintf(stderr, " <command> One of add, del, or authenticate.\n");
fprintf(stderr, " <secret> Shared secret.\n");
fprintf(stderr, " \n");
fprintf(stderr, " -i ipaddr User IP address in A.B.C.D format.\n");
fprintf(stderr, " -m macaddr User MAC address in aa:bb:cc:dd:ee:ff format.\n");
fprintf(stderr, " -n name User name.\n");
fprintf(stderr, " -p passwd User password.\n");
fprintf(stderr, " -r role User role.\n");
fprintf(stderr, " -t timeout User session timeout.\n");
fprintf(stderr, " -v version API version number. Default is 1.0\n");
fprintf(stderr, " -a method one of md5, sha-1 or cleartext.\n");
exit(1);
}
main(int argc, char **argv)
{
char c, *p;
int fd, len, postlen;
struct sockaddr_in sa;
while ((c = getopt(argc, argv, "a:i:m:n:p:r:t:v:")) != EOF) switch(c) {
case 'i':/* ipaddr */
ipaddr = optarg;
break;
case 'm':/* macaddr */
macaddr = optarg;
break;
case 'n':/* name */
name = optarg;
break;
case 'p':/* password */
password = optarg;
break;
case 'r':/* role */
role = optarg;
break;
case 't':/* session timeout */
tout = optarg;
break;
case 'v':/* version */
version = optarg;
break;
case 'a':/* authentication */
auth = optarg;
if (!strcasecmp(auth, "sha-1") &&
!strcasecmp(auth, "md5"))
usage();
break;
default:
usage();
break;
}
argc -= (optind - 1);
argv += (optind - 1);
if ((argc < 3)) {
usage();
}
if (version == NULL)
version = "1.0";
debug("server=%s, command=%s, version=%s, secret=%s\n",
argv[1], argv[2], version, argv[3]?argv[3]:"<>");
if (argv[3]) secret = argv[3];
p = cmdbuf;
sprintf(p, "xml=<aruba command=‘%s’>", argv[2]);
p += strlen(p);
if (ipaddr) {
sprintf(p, "<ipaddr>%s</ipaddr>", ipaddr);
p += strlen(p);
}
if (macaddr) {
sprintf(p, "<macaddr>%s</macaddr>", macaddr);
p += strlen(p);
}
if (name) {
sprintf(p, "<name>%s</name>", name);
p += strlen(p);
}
if (password) {
sprintf(p, "<password>%s</password>", password);
p += strlen(p);
}
if (role) {
sprintf(p, "<role>%s</role>", role);
p += strlen(p);
}
if (tout) {
sprintf(p, "<session timeout>%s</session timeout>", tout);
p += strlen(p);
}
if (secret) {
if (auth == NULL) {
key = secret;
auth = "cleartext";
#ifndef OPENSSL_NO_SHA1
} else if (!strcasecmp(auth, "sha-1")) {
key = enchashbuf;
SHA1(secret, strlen(secret), hashbuf);
encode_message_digest(hashbuf, 20, enchashbuf);
#endif
} else if (!strcasecmp(auth, "md5")) {
key = enchashbuf;
md5_calc(hashbuf, secret, strlen(secret));
encode_message_digest(hashbuf, 16, enchashbuf);
}
debug("Message authentication is %s (%s)\n", auth, key);
sprintf(p, "<authentication>%s</authentication><key>%s</key>",
auth, key);
p += strlen(p);
}
debug("\n");
sprintf(p, "<version>%s</version>", version);
sprintf(p, "</authresponse>");
cgi_escape_url(encbuf, sizeof(encbuf), cmdbuf, strlen(cmdbuf), 0);
postlen = sprintf(post,
"POST /auth/command.xml HTTP/1.0\r\n"
"User-Agent: ecp\r\n"
"Host: %s\r\n"
"Pragma: no-cache\r\n"
"Content-Length: %d\r\n"
/* "Content-Type: application/x-www-form-urlencoded\r\n" */
"Content-Type: application/xml\r\n"
"\r\n"
"%s",
argv[1], strlen(encbuf), encbuf);
inet_aton(argv[1], &sa.sin_addr);
sa.sin_family = AF_INET;
sa.sin_port = htons(80);
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
exit(1);
}
if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
perror("connect");
exit(1);
}
if (write(fd, post, postlen) != postlen) {
perror("write");
exit(1);
}
while ((len = read(fd, post, sizeof(post))) > 0)
write(1, post, len);
close(fd);
exit(0);
}
static void encode_message_digest (unsigned char *md, int mdlen, char *output)
{
int i;
for (i=0; i<mdlen; i++) {
sprintf(output, "%02x", md[i]);
output += 2;
}
}