/* ** JNetLib ** Copyright (C) 2000-2001 Nullsoft, Inc. ** Author: Justin Frankel ** File: test.cpp - JNL test code ** License: see jnetlib.h */ #ifdef _WIN32 #include #else #define Sleep(x) usleep((x)*1000) #endif #include #include "jnetlib.h" #include "ircclient.h" #include #include #include #define TEST_ASYNCDNS 0 #define TEST_CONNECTION 0 #define TEST_LISTEN 0 #define TEST_TELNET_GATEWAY 0 #define TEST_HTTPGET 0 #define TEST_WEBSERVER 0 #define TEST_IRCCLIENT 0 #define TEST_HTTPSGET 0 #define TEST_SSLLISTEN 0 #define TEST_HTTPPOST 1 #define TEST_UDP 0 //udp is not done yet tho :) #if (TEST_WEBSERVER) #include "webserver.h" class MemPageGenerator : public IPageGenerator { public: virtual ~MemPageGenerator() { free(m_buf); } MemPageGenerator(char *buf, int buf_len=-1) { m_buf=buf; if (buf_len >= 0) m_buf_size=buf_len; else m_buf_size=strlen(buf); m_buf_pos=0; } virtual int GetData(char *buf, int size) // return 0 when done { int a=m_buf_size-m_buf_pos; if (a < size) size=a; memcpy(buf,m_buf+m_buf_pos,size); m_buf_pos+=size; return size; } private: char *m_buf; int m_buf_size; int m_buf_pos; }; class wwwServer : public WebServerBaseClass { public: wwwServer() { } virtual IPageGenerator *onConnection(JNL_HTTPServ *serv, int port) { serv->set_reply_header("Server:jnetlib_test/0.0"); if (!strcmp(serv->get_request_file(),"/")) { serv->set_reply_string("HTTP/1.1 200 OK"); serv->set_reply_header("Content-Type:text/html"); serv->send_reply(); return new MemPageGenerator(strdup("Test Web Server v0.0")); } else { serv->set_reply_string("HTTP/1.1 404 NOT FOUND"); serv->send_reply(); return 0; // no data } } }; int main(int argc, char **argv) { JNL::open_socketlib(); { wwwServer foo; foo.addListenPort(8080); JNL_IRC_ERROR e; while (1) { e = foo.run(); if(e != JNL_IRC_ERROR_NONE) { return 0; } Sleep(10); } } JNL::close_socketlib(); return 0; } #endif #if (TEST_HTTPGET) int main(int argc, char **argv) { if (argc != 3) { printf("usage: httpget \n"); exit(0); } JNL_HTTPGet get; JNL::open_socketlib(); get.addheader("User-Agent:PooHead (Mozilla)"); get.addheader("Accept:*/*"); get.addheader("Host:www.stashbox.org"); get.connect(argv[1]); FILE *fp=fopen(argv[2],"wb"); int headerstate=0; int has_printed_headers=0; int has_printed_reply=0; while (1) { int st=get.run(); if (st<0) { printf("HTTPGet error: %s\n",get.geterrorstr()); break; } if (get.get_status()>0) { if (!has_printed_reply) { has_printed_reply=1; printf("reply: %s (code:%d)\n",get.getreply(),get.getreplycode()); } if (get.get_status()==2) { int len; if (!has_printed_headers) { has_printed_headers=1; printf("headers:\n"); char *p=get.getallheaders(); while (p&&*p) { printf("%s\n",p); p+=strlen(p)+1; } } while ((len=get.bytes_available()) > 0) { char buf[4096]; if (len > 4096) len=4096; len=get.get_bytes(buf,len); if (len>0)fwrite(buf,len,1,fp); } } } if (st==1) // 1 means connection closed { printf("HTTPGet done!\n"); break; } } if (fp) fclose(fp); JNL::close_socketlib(); return 0; } #endif #if (TEST_HTTPPOST) int main(int argc, char **argv) { if (argc != 3) { printf("usage: httppost \n"); exit(0); } JNL::open_socketlib(); JNL_HTTPPost post; post.addheader("User-Agent: 1014 Army Agent"); post.addheader("Accept: text/html, */*"); post.addheader("Accept-Language: en-us, en;q=0.5"); post.addheader("Accept-Encoding: gzip,deflate"); post.addheader("Accept-Charset: ISO-8859-1, utf-8;q=0.7,*;q=0.7"); post.addheader("Connection: close"); post.addfield("t","1"); post.addfield("pof",""); post.addfield("rss","on"); post.addfield("tags",""); post.addfile("file","application/octet-stream","poopoo.txt","c:/temp/poopoo.txt"); post.connect(argv[1]); int has_printed_reply = 0; int has_printed_headers = 0; FILE *fp=fopen(argv[2],"wb"); while (1) { int st=post.run(); if (st<0) { printf("HTTPPost error: %s\n",post.geterrorstr()); break; } if (post.get_status()>0) { if (!has_printed_reply) { has_printed_reply=1; printf("reply: %s (code:%d)\n",post.getreply(),post.getreplycode()); } if (post.get_status()==2) { int len; if (!has_printed_headers) { has_printed_headers=1; printf("headers:\n"); char *p=post.getallheaders(); while (p&&*p) { printf("%s\n",p); p+=strlen(p)+1; } } while ((len=post.bytes_available()) > 0) { char buf[4096]; if (len > 4096) len=4096; len=post.get_bytes(buf,len); if (len>0)fwrite(buf,len,1,fp); } } } if (st==1) // 1 means connection closed { printf("HTTPGet done!\n"); break; } } if (fp) fclose(fp); JNL::close_socketlib(); return 0; } #endif #if (TEST_TELNET_GATEWAY) int main() { JNL_Connection *cons[32]={0,}; JNL_Connection *outcons[32]={0,}; char textpos[32][256]; int n_cons=0; int states[32]={0,}; JNL::open_socketlib(); JNL_AsyncDNS dns; JNL_Listen l(23); while (!l.is_error()) { Sleep(30); if (n_cons<32) { JNL_Connection *con=l.get_connect(); if (con) { int x; for (x = 0; x < 32; x ++) { if (!cons[x]) { cons[x]=con; outcons[x]=0; states[x]=0; n_cons++; break; } } } } int x; for (x = 0; x < 32; x ++) { if (cons[x]) { cons[x]->run(); if (outcons[x]) outcons[x]->run(); if (cons[x]->get_state() == JNL_Connection::STATE_ERROR || cons[x]->get_state()==JNL_Connection::STATE_CLOSED || (outcons[x] && (cons[x]->get_state() == JNL_Connection::STATE_ERROR || cons[x]->get_state()==JNL_Connection::STATE_CLOSED))) { delete cons[x]; if (outcons[x]) delete outcons[x]; outcons[x]=0; cons[x]=0; states[x]=0; n_cons--; } else { if (states[x]==0) { cons[x]->send_string("\r\nwelcome "); states[x]++; } if (states[x]==1) { char hoststr[256]; int ret=dns.reverse(cons[x]->get_remote(),hoststr); if (ret==0) { cons[x]->send_string(hoststr); cons[x]->send_string(". host: "); states[x]++; textpos[x][0]=0; } if (ret==-1) { JNL::addr_to_ipstr(cons[x]->get_remote(),hoststr,256); cons[x]->send_string(hoststr); cons[x]->send_string(". host: "); states[x]++; textpos[x][0]=0; } } if (states[x]==2) { char b; while (cons[x]->recv_bytes(&b,1) && states[x]==2) { if (b == '\r' || b == '\n') { if (strlen(textpos[x])) { char *p=strstr(textpos[x],":"); int port=23; if (p) { *p++=0; if (atoi(p)) port=atoi(p); } outcons[x]=new JNL_Connection(&dns); outcons[x]->connect(textpos[x],port); char str[512]; sprintf(str,"\r\nconnecting to port %d of %s\r\n",port,textpos[x]); cons[x]->send_string(str); states[x]++; } else states[x]=0; } else if (b == '\b') { if (textpos[x][0]) { textpos[x][strlen(textpos[x])-1]=0; cons[x]->send_string("\b \b"); } } else { textpos[x][strlen(textpos[x])+1]=0; textpos[x][strlen(textpos[x])]=b; cons[x]->send(&b,1); } } } if (states[x]==3) { char buf[1024]; outcons[x]->run(); int l=cons[x]->recv_bytes(buf,1024); if (l) outcons[x]->send(buf,l); l=outcons[x]->recv_bytes(buf,1024); if (l) cons[x]->send(buf,l); } } } } } JNL::close_socketlib(); return 0; } #endif #if (TEST_LISTEN) int main() { JNL_HTTPServ *cons[32]={0,}; char *contents[32]={0,}; int n_cons=0; JNL::open_socketlib(); JNL_AsyncDNS dns; JNL_Listen l(8000); while (!l.is_error()) { Sleep(100); if (n_cons<32) { JNL_Connection *con=l.get_connect(); if (con) { int x; for (x = 0; x < 32; x ++) { if (!cons[x]) { cons[x]=new JNL_HTTPServ(con); n_cons++; break; } } } } int x; for (x = 0; x < 32; x ++) { if (cons[x]) { int r=cons[x]->run(); if (r == -1 || r == 4) { if (r == -1) printf("error:%s\n",cons[x]->geterrorstr()); delete cons[x]; cons[x]=0; free(contents[x]); contents[x]=0; n_cons--; } if (r == 2) { cons[x]->set_reply_string("HTTP/1.1 200 OK"); cons[x]->set_reply_header("Content-type:text/plain"); cons[x]->set_reply_header("Server:JNLTest"); contents[x]=(char*)malloc(32768); char *poop=cons[x]->get_request_parm("poop"); sprintf(contents[x],"test, sucka\r\n%s\r\n\r\n",poop?poop:"no poop"); cons[x]->send_reply(); } if (r == 3) { if (contents[x] && cons[x]->bytes_cansend()>strlen(contents[x])) { cons[x]->write_bytes(contents[x],strlen(contents[x])); cons[x]->close(0); free(contents[x]); contents[x]=0; } } } } } JNL::close_socketlib(); return 0; } #endif #if (TEST_SSLLISTEN) /* ** ARGS: ** 0 Program ** 1 Port ** 2 Certificate File ** 3 PrivateKey File */ int main(int argc, char **argv) { JNL_HTTPServ *cons[32]={0,}; char *contents[32]={0,}; int n_cons=0; unsigned short nport; char *szprivkeyfile; char *szcertfile; JNL::open_socketlib(); JNL_AsyncDNS dns; /* ** Two key things for listening via SSL ** 1) You need a private key ** 2) You need a certificate ** ** For the impatient: ** Create a private key ** openssl genrsa -des3 -out privkey.pem 2048 ** Create a self signed cert ** openssl req -new -x509 -key privkey.pem -out cacert.pem -days 1095 ** ** If you created your private key with a passphrase, ** you better have access to STDIN to unlock the private key. */ if(argc < 2) { nport = 8443; szcertfile = "c:/openssl/etc/cacert.pem"; szprivkeyfile = "c:/openssl/etc/privkey.pem"; } else if(argc < 3) { nport = atoi(argv[1]); if(nport <= 0) { nport = 8443; } szcertfile = "c:/openssl/etc/cacert.pem"; szprivkeyfile = "c:/openssl/etc/privkey.pem"; } else if(argc < 4) { struct stat buf; nport = atoi(argv[1]); if(nport <= 0) { nport = 8443; } szcertfile = argv[2]; if(stat(szcertfile,&buf)<0) { fprintf(stderr,"Could not open %s\n",szcertfile); exit(-1); } szprivkeyfile = "c:/openssl/etc/privkey.pem"; } else { struct stat buf; nport = atoi(argv[1]); if(nport <= 0) { nport = 8443; } szcertfile = argv[2]; if(stat(szcertfile,&buf)<0) { fprintf(stderr,"Could not open %s\n",szcertfile); exit(-1); } szprivkeyfile = argv[3]; if(stat(szprivkeyfile,&buf)<0) { fprintf(stderr,"Could not open %s\n",szprivkeyfile); exit(-1); } } JNL_SSL_Listen l(nport,0,szcertfile,szprivkeyfile); while (!l.is_error()) { Sleep(100); if (n_cons<32) { JNL_Connection *con=l.get_connect(); if (con) { int x; for (x = 0; x < 32; x ++) { if (!cons[x]) { cons[x]=new JNL_HTTPServ(con); n_cons++; break; } } } } int x; for (x = 0; x < 32; x ++) { if (cons[x]) { int r=cons[x]->run(); if (r == -1 || r == 4) { if (r == -1) printf("error:%s\n",cons[x]->geterrorstr()); delete cons[x]; cons[x]=0; free(contents[x]); contents[x]=0; n_cons--; } if (r == 2) { cons[x]->set_reply_string("HTTP/1.1 200 OK"); cons[x]->set_reply_header("Content-type:text/plain"); cons[x]->set_reply_header("Server:JNLTest"); contents[x]=(char*)malloc(32768); char *poop=cons[x]->get_request_parm("poop"); sprintf(contents[x],"test, sucka\r\n%s\r\n\r\n",poop?poop:"no poop"); cons[x]->send_reply(); } if (r == 3) { if (contents[x] && cons[x]->bytes_cansend()>strlen(contents[x])) { cons[x]->write_bytes(contents[x],strlen(contents[x])); cons[x]->close(0); free(contents[x]); contents[x]=0; } } } } } JNL::close_socketlib(); return 0; } #endif #if (TEST_CONNECTION) int main() { JNL::open_socketlib(); { JNL_AsyncDNS dns; JNL_Connection con(&dns); con.connect("localhost",80); FILE *fp=fopen("c:\\hi.raw","wb"); while (1) { con.run(); if (con.get_state()==JNL_Connection::STATE_ERROR) { printf("error %s\n",con.get_errstr()); } if (con.get_state()==JNL_Connection::STATE_CLOSED) { } while (con.recv_bytes_available()>0) { char buf[1024]; int a=con.recv_bytes_available(); if (a > 1024) a=1024; con.recv_bytes(buf,a); fwrite(buf,a,1,fp); } } if (fp) fclose(fp); } JNL::close_socketlib(); return 0; } #endif #if (TEST_ASYNCDNS) int main() { JNL_AsyncDNS dns; char *hosts[]= { "www.firehose.net", "gnutella.com", "207.48.52.200", "www.slashdot.org", "www.google.com", "www.winamp.com", "www.genekan.com", }; char *reverses[]= { "64.0.160.98", "205.188.245.120", "207.48.52.222", "207.48.52.200", }; int n=0; int pass=0; while (n\n"); printf("mode: 0 for client, 1 for server\n"); exit(0); } int mode=atoi(argv[1]); JNL::open_socketlib(); switch(mode) { case 0: // client mode { JNL_AsyncDNS dns; JNL_UDPConnection con(0,&dns); // 0 chooses a random port con.setpeer("localhost",80); printf("Sending message...\n"); con.send("blah",5); while (1) { con.run(); if (con.get_state()==JNL_UDPConnection::STATE_ERROR) { printf("error %s\n",con.get_errstr()); } while (con.recv_bytes_available()>0) { char buf[1024]; int s=min(con.recv_bytes_available(), sizeof(buf)); con.recv_bytes(buf,s); printf("received message: %s\n", buf); } } } break; case 1: // server (listening) mode { JNL_UDPConnection con(80); printf("Waiting for messages...\n"); while(1) { con.run(); if (con.get_state()==JNL_UDPConnection::STATE_ERROR) { printf("error %s\n",con.get_errstr()); } while (con.recv_bytes_available()>0) { char buf[1024]; int s=min(con.recv_bytes_available(), sizeof(buf)); con.recv_bytes(buf,s); printf("message received: %s. Replying...\n", buf); // reply on the addr:port from sender struct sockaddr from; con.get_last_recv_msg_addr(&from); con.setpeer(&from); con.send("blorp",6); } } } break; } JNL::close_socketlib(); return 0; } #endif #if (TEST_IRCCLIENT) int main(int argc, char **argv) { if (argc != 3) { printf("usage: ircclient host port\n"); exit(0); } JNL_IRCClient irccli; JNL::open_socketlib(); if(irccli.connect(argv[1],atoi(argv[2])) != true) { printf("%s could not connect\n",argv[0]); return 0; } irccli.nick("poopoo2"); irccli.userregistration(); irccli.join("#poopoo"); while (1) { bool st=irccli.run(); if (st != true) { break; } Sleep(200); } return 0; } #endif #if (TEST_HTTPSGET) int main(int argc, char **argv) { if (argc != 3) { printf("usage: httpsget \n"); exit(0); } JNL_HTTPGet get; JNL::open_socketlib(); get.addheader("User-Agent:PooHead (Mozilla)"); get.addheader("Accept:*/*"); get.addheader("Host:www.stashbox.org"); get.connect(argv[1]); FILE *fp=fopen(argv[2],"wb"); int headerstate=0; int has_printed_headers=0; int has_printed_reply=0; while (1) { int st=get.run(); if (st<0) { printf("HTTPGet error: %s\n",get.geterrorstr()); break; } if (get.get_status()>0) { if (!has_printed_reply) { has_printed_reply=1; printf("reply: %s (code:%d)\n",get.getreply(),get.getreplycode()); } if (get.get_status()==2) { int len; if (!has_printed_headers) { has_printed_headers=1; printf("headers:\n"); char *p=get.getallheaders(); while (p&&*p) { printf("%s\n",p); p+=strlen(p)+1; } } while ((len=get.bytes_available()) > 0) { char buf[4096]; if (len > 4096) len=4096; len=get.get_bytes(buf,len); if (len>0)fwrite(buf,len,1,fp); } } } if (st==1) // 1 means connection closed { printf("HTTPGet done!\n"); break; } } if (fp) fclose(fp); JNL::close_socketlib(); return 0; } #endif