UsedPort.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //---------------------------------------------------------------------------
  2. // UsedPort.cpp
  3. //---------------------------------------------------------------------------
  4. // Tested with :
  5. // GCC CYGWIN 3.4.4 (May need cygwin1.dll, depending on functions used)
  6. // GCC MINGW 3.4.5
  7. // Not tested with:
  8. // VC++ 4.0.0
  9. // GCC Linux 3.4.4
  10. //---------------------------------------------------------------------------
  11. // 06/09/09 1.0 Laurent Destailleur Creation
  12. //---------------------------------------------------------------------------
  13. #define PROG "UsedPort"
  14. #define VERSION "1.0"
  15. // If GNU GCC CYGWIN: _WIN32 to defined manually, __GNUC__ is defined, _MSC_VER not defined
  16. // If GNU GCC MINGW: _WIN32 automaticaly defined, __GNUC__ is defined, _MSC_VER not defined
  17. // If VC: _WIN32 automaticaly defined, __GNUC__ is not defined, _MSC_VER defined
  18. // If on Windows and Cygwin, we can use _WIN32 WSA pre function, if we want or not.
  19. //#define _WIN32
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #ifndef _WIN32
  24. // Pour Unix
  25. #include <sys/socket.h>
  26. #include <netinet/in.h>
  27. #include <netdb.h>
  28. #include <unistd.h>
  29. #define SOCKET int // Non defini sous Unix
  30. #define INVALID_SOCKET (SOCKET)(~0) // Non defini sous Unix
  31. #define SOCKADDR_IN struct sockaddr_in // Non defini sous Unix
  32. #define LPHOSTENT struct hostent * // Non defini sous Unix
  33. #define MAXHOSTNAMELEN 256
  34. #endif
  35. #ifdef _WIN32
  36. #ifdef _MSC_VER
  37. // Pour VC++
  38. #include <direct.h>
  39. #include <winsock.h>
  40. #define MAXHOSTNAMELEN 256
  41. #endif
  42. #ifdef __GNUC__
  43. #define MAXHOSTNAMELEN 256
  44. // Pour GCC WIN32 CYGWIN
  45. #include <winsock.h>
  46. // Pour GCC WIN32 EGCS
  47. //#include <base.h>
  48. //#include <defines.h>
  49. //#include <structures.h>
  50. //#include <sockets.h>
  51. //#include <functions.h>
  52. #endif
  53. #endif
  54. #define MAX_ENTRIES 10 // SUBJECT, HOST, USER, PASSWD
  55. #define MAX_MAILS 100 // Nb max of mails managed
  56. #define SIZE_RECEIVE_BUFFER 4096 // Ko
  57. #define SIZE_BLOCK_FILELIST 256 // Size of block used to extend by alloc/realloc files list string
  58. #define UNKNOWN_ERROR 1
  59. #define BAD_ACTION 2
  60. #define BAD_PASSWORD 3
  61. #define FAILED_TO_START_SOCKETS 4
  62. #define FAILED_TO_RESOLVE_HOST 5
  63. #define FAILED_TO_OBTAIN_SOCKET_HANDLE 6
  64. #define FAILED_TO_CONNECT 7
  65. #define FAILED_TO_SEND 8
  66. #define FAILED_TO_RECEIVE 9
  67. #define SERVER_ERROR 10
  68. #define FAILED_TO_GET_HOSTNAME 11
  69. #define OUT_OF_MEMORY 12
  70. #define FAILED_TO_PARSE_CGI 13
  71. #define NB_OF_MAILS 14
  72. #define SIZE_OF_MAILS 15
  73. #define BAD_USER 16
  74. #define BAD_HOST 17
  75. #define TO_MANY_MAILS 18
  76. #define MAIL_UNKNOWN 19
  77. #define BAD_CGIEXE 20
  78. #define MAIL_DELETED 21
  79. #define BAD_FORMAT_MAIL 22
  80. #define SIZE_TEXT 23
  81. #define FROM_TEXT 24
  82. #define SUBJECT_TEXT 25
  83. #define ATTACHED_FILE_TEXT 26
  84. #define NONE_TEXT 27
  85. #define DELETE_TEXT 28
  86. // Types
  87. typedef struct {
  88. char *mail;
  89. unsigned long int size;
  90. char *received_time;
  91. char *subject;
  92. char *return_path;
  93. char *from;
  94. char *status;
  95. char *mime_version;
  96. char *files;
  97. } mailentry;
  98. typedef struct {
  99. char *name;
  100. char *val;
  101. } entry;
  102. // Variables
  103. entry entries[MAX_ENTRIES]; // Tab of CGI entries First=0
  104. mailentry tabmails[MAX_MAILS+1]; // Tab of mails entries First=1
  105. int iRet;
  106. int iNbUnread; // Nb of unread mails
  107. unsigned long int lSizeUnread; // Size of all unread mails
  108. int Port=0;
  109. char Host[MAXHOSTNAMELEN]="";
  110. #ifdef _WIN32
  111. WSADATA Data;
  112. #endif
  113. // Functions
  114. int Ack(SOCKET sc);
  115. int DoQuit(int iRet);
  116. int DoQuit(int iRet)
  117. //---------------------------------------------------------------------------
  118. // Show result
  119. //---------------------------------------------------------------------------
  120. {
  121. printf("Return code = %d\n",iRet);
  122. return(iRet);
  123. }
  124. int testConnect()
  125. //---------------------------------------------------------------------------
  126. // Init socket, get list of mails
  127. //---------------------------------------------------------------------------
  128. {
  129. SOCKET sc;
  130. char s[2048],t[256];
  131. int i;
  132. startgetmess:
  133. //***** Get mailfile
  134. #ifdef _WIN32
  135. if (WSAStartup(MAKEWORD(1, 1), &Data) != 0) return(DoQuit(FAILED_TO_START_SOCKETS));
  136. #endif
  137. //***** Create Socket
  138. printf("Create socket: socket(PF_INET,SOCK_STREAM)\n");
  139. if ((sc = socket(PF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
  140. {
  141. return(DoQuit(FAILED_TO_OBTAIN_SOCKET_HANDLE));
  142. }
  143. //***** Resolve the servers IP
  144. printf("Resolve IP address for: %s\n",Host);
  145. struct hostent *adr;
  146. adr = gethostbyname(Host);
  147. if (!adr)
  148. {
  149. return(DoQuit(FAILED_TO_RESOLVE_HOST));
  150. }
  151. //***** Connect to server
  152. SOCKADDR_IN sin;
  153. sin.sin_port = htons((u_short) Port);
  154. sin.sin_family = adr->h_addrtype;
  155. memcpy((char *) &sin.sin_addr, adr->h_addr, adr->h_length);
  156. char AddrHexa[9];
  157. sprintf(AddrHexa,"%08lX",* (unsigned long int *) &sin.sin_addr);
  158. AddrHexa[8]=0;
  159. printf("Connect socket to: %s\n",AddrHexa);
  160. #ifdef _WIN32
  161. if (connect(sc,(LPSOCKADDR) &sin,sizeof(sin)))
  162. #else
  163. if (connect(sc,(const struct sockaddr *) &sin,sizeof(sin)))
  164. #endif
  165. {
  166. printf("Failed to connect !\n");
  167. return(DoQuit(FAILED_TO_CONNECT));
  168. }
  169. //***** Server welcome message
  170. printf("Connected !\n");
  171. /*
  172. if ((iRet=Ack(sc))) {
  173. return(DoQuit(iRet));
  174. }
  175. */
  176. //***** Disconect
  177. return(DoQuit(0));
  178. }
  179. int Ack(SOCKET sc)
  180. //---------------------------------------------------------------------------
  181. // Function : Get POP response from the server.
  182. // Input : sc
  183. // Return : O = Ok, >0 = Error
  184. //---------------------------------------------------------------------------
  185. {
  186. static char *buf;
  187. unsigned long int liSizeOfMail=SIZE_RECEIVE_BUFFER;
  188. int rlen;
  189. int Received = 0;
  190. if (!buf)
  191. if ((buf = (char *) malloc(liSizeOfMail+1)) == NULL) // The first time, create buf
  192. return(OUT_OF_MEMORY);
  193. again:
  194. if ((rlen = recv(sc,buf+Received,liSizeOfMail-Received,0)) < 1) {
  195. return(FAILED_TO_RECEIVE); // Possible when pop server refuses client
  196. }
  197. buf[Received+rlen] = 0;
  198. Received += rlen;
  199. // Check for newline
  200. if ((buf[Received-2] != '\r') || (buf[Received-1] != '\n')) {
  201. goto again; // Incomplete data. Line must be terminated by CRLF
  202. }
  203. return((buf[0] == '-')?1:0);
  204. }
  205. int main(int argc, char **argv)
  206. //---------------------------------------------------------------------------
  207. // MAIN
  208. //---------------------------------------------------------------------------
  209. {
  210. // Read parameters
  211. //----------------
  212. int noarg,curseurarg,help=0,invalide=0;
  213. char option;
  214. char *endptr;
  215. for (noarg=1;noarg<argc;noarg++) {
  216. if (((argv[noarg][0])=='/') || ((argv[noarg][0])=='-')) {
  217. option=(argv[noarg][1] | 0x20);
  218. curseurarg=2;
  219. if (strlen(argv[noarg]) < 3) { ++noarg; curseurarg=0; }
  220. switch (option) {
  221. case 's': strncpy(Host,argv[noarg]+curseurarg,sizeof(Host)); break;
  222. case 'p': Port=strtol(argv[noarg] + curseurarg, &endptr, 10); break; // Get port from "-p80" (curseurarg = 2) or "-p 80" (curseurarg = 0)
  223. case '?': help=-1;break; // Help
  224. case 'h': help=-1;break; // Help
  225. case 'v': help=-1;break; // Help
  226. default: invalide=-1;break;
  227. }
  228. }
  229. }
  230. // Check for conversion errors
  231. if (*endptr != '\0') {
  232. // Handle error: Invalid input format
  233. printf("Invalid port number format\n");
  234. exit(-1);
  235. }
  236. // Check for overflow
  237. if (Port < 0 || Port > INT_MAX) {
  238. // Handle error: Port number out of range
  239. printf("Port number out of range\n");
  240. exit(-1);
  241. }
  242. help=!(Port > 0);
  243. // Show usage
  244. //-----------
  245. Usage:
  246. if (help) {
  247. printf("----- %s V%s (c)Laurent Destailleur -----\n",PROG,VERSION);
  248. printf("%s is software that allows you to know if a TCP/IP port is used\n",PROG);
  249. printf("%s sources can be compiled for WIN32 (VC++, GCC CYGWIN, MINGW) or for\n");
  250. printf("Unix/Linux (GCC)\n",PROG);
  251. printf("\n");
  252. }
  253. if (help|invalide) {
  254. if (invalide) printf("----- %s V%s (c)Laurent Destailleur 2009 -----\n",PROG,VERSION);
  255. printf("Usage: %s params [options]\n",PROG);
  256. printf("Params:\n");
  257. printf(" -s Host Server to test\n");
  258. printf(" -p Port Port to test\n");
  259. printf("Options:\n");
  260. printf(" -v Print version and help information\n");
  261. printf(" -help Print version and help information\n");
  262. printf("\n");
  263. exit(-1);
  264. }
  265. // Print input values
  266. //-------------------
  267. printf("Port=%d\n",Port);
  268. printf("Host=%s\n",Host);
  269. // Check parameters
  270. //-----------------
  271. if (Host[0]==0) {
  272. invalide=-1;
  273. goto Usage;
  274. }
  275. // Action
  276. //-------
  277. iRet=testConnect();
  278. return(iRet);
  279. }