Guest User

Untitled

a guest
Mar 13th, 2018
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. /***************************************************************************
  2. * Connection.cc
  3. *
  4. * Fri Mar 7 16:13:20 2008
  5. * Copyright 2008 Cain
  6. * Darkdemun@gmail.com
  7. ****************************************************************************/
  8.  
  9.  
  10. #include "Connection.h"
  11. #include <time.h>
  12.  
  13. #ifndef CONNECT
  14. #define CONNECT 1
  15.  
  16. Connection::Connection()
  17. {
  18. };
  19.  
  20. Connection::Connection(Serv server)
  21. {
  22. cout << "tryin..." << endl;
  23. Connect(server);
  24. };
  25.  
  26. Connection::~Connection()
  27. {
  28. };
  29.  
  30. int Connection::Connect(Serv server) // write errors to a file
  31. {
  32. sockfd = socket(PF_INET, SOCK_STREAM, 0);
  33. cout << "trying......." << endl;
  34. he = gethostbyname(server.GetServer().c_str());
  35. dest_addr.sin_family = AF_INET;
  36. dest_addr.sin_port = htons(server.GetPort());
  37. dest_addr.sin_addr.s_addr = ((struct in_addr *) he->h_addr)->s_addr;
  38. memset(dest_addr.sin_zero, '\0', sizeof dest_addr.sin_zero);
  39.  
  40. if(connect(sockfd, (struct sockaddr *)&dest_addr, sizeof dest_addr) == -1)
  41. {
  42. perror("socket"); //write errors to a file
  43. return -1;
  44. }
  45.  
  46. string init = "PASS "+server.GetPass()+"\r\nSERVER " +server.GetSName()+ " 2 Services by Cain_";
  47. cout << init << endl;
  48. SendStr(init);
  49. connected = true;
  50. return 0;
  51. };
  52.  
  53. int Connection::SendStr(string msg)
  54. {
  55. msg = msg+"\r\n";
  56. char *buf = (char*) msg.c_str();
  57. int total = 0; // how many bytes we've sent
  58.  
  59. int bytesleft = strlen(buf); // how many we have left to send
  60. int n;
  61.  
  62. while(total < strlen(buf))
  63. {
  64. n = send(sockfd, buf+total, bytesleft, 0);
  65. if (n == -1) { break; }
  66. total += n;
  67. bytesleft -= n;
  68. }
  69. return 0;
  70. };
  71.  
  72. int Connection::RecvStr(string * buffer)
  73. {
  74.  
  75. char* buf = (char*) malloc(1024);
  76. char get[2];
  77. buf[0] = '\0';
  78. int bytesread = 0;
  79.  
  80. while(bytesread = recv(sockfd,get,1,0))
  81. {
  82.  
  83. if(get[0] == '\n')
  84. {
  85. *buffer = string(buf);
  86. return 1;
  87. }
  88. else
  89. {
  90. get[1] = '\0';
  91. strcat(buf,get);
  92.  
  93. }
  94. }
  95. return 0;
  96. };
  97.  
  98. bool Connection::IsConnected() const
  99. {
  100. return connected;
  101. };
  102.  
  103. string Connection::GetTime() const
  104. {
  105. time_t rawtime;
  106. time(&rawtime);
  107. char *time;
  108. sprintf(time, "%d", rawtime);
  109. string ttime = string(time);
  110. return ttime;
  111. };
  112. #endif
Add Comment
Please, Sign In to add comment