session.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if 0
00012 I casted 'port' to true unsigned, now, tell me if warning continues ; #pragma warning(disable: 4761)
00013 #endif
00014
00015
00016
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 #include <string.h>
00020 #ifdef _MSC_VER
00021 #include <winsock.h>
00022 #include <io.h>
00023 #else
00024 #include <netdb.h>
00025 #include <unistd.h>
00026 #include <netinet/in.h>
00027 #include <sys/socket.h>
00028 #endif
00029 #include <sys/types.h>
00030 #define socklen_t int
00031
00032 int pop3_prepare(const char* servername, const int port, struct sockaddr_in* connection, struct hostent* server){
00033
00034 int sock;
00035 #ifdef _REENTRANT
00036 struct hostent result_buffer;
00037 char tmp[512];
00038 int my_error;
00039 #endif
00040 #ifdef _MSC_VER
00041
00042 WSADATA wsaData;
00043
00044 if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
00045 {
00046 fprintf(stderr,"WSAStartup() failed");
00047 exit(1);
00048 }
00049
00050 #endif
00051 memset((char*)connection,0,sizeof(struct sockaddr_in));
00052
00053 #ifdef _REENTRANT
00054 #if defined(__sun__)||defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
00055 server=gethostbyname_r (servername,&result_buffertmp, 512 - 1, &my_error);
00056 #else
00057 gethostbyname_r (servername,&result_buffer,tmp, 512 - 1, &server, &my_error);
00058 #endif
00059 #ifdef _MSC_VER
00060 gethostbyname_r (servername, &result_buffer, &server);
00061 #endif
00062 #else
00063 server=gethostbyname(servername);
00064 #endif
00065 if(!server){
00066 perror("pop3_prepare.gethostbyname");
00067 return(-1);
00068 }
00069 memmove((char*)&(connection->sin_addr.s_addr),server->h_addr,server->h_length);
00070 connection->sin_family=AF_INET;
00071 connection->sin_port=htons(port?(unsigned short int)port:(unsigned short int)110);
00072 sock=socket(AF_INET,SOCK_STREAM,0);
00073 if(-1==sock){
00074 perror("pop3_prepare.socket");
00075 free(server);
00076 }
00077 return(sock);
00078 }
00079
00080 char* pop3_connect(int sock, struct sockaddr_in* connection){
00081
00082 int r;
00083 char* buf;
00084
00085 #ifdef EBUG
00086 fprintf(stderr,"<pop3_connect>\n");
00087 #endif
00088 r=connect(sock,(struct sockaddr*)connection,(socklen_t)sizeof(*connection));
00089 if(r==-1){
00090 perror("pop3_connect.connect");
00091 return(NULL);
00092 }
00093 buf=(char*)malloc(512);
00094 if(!buf){
00095 perror("pop3_connect.malloc");
00096 return(NULL);
00097 }
00098 r=recv(sock,buf,512,0);
00099 buf[r]='\0';
00100 #ifdef EBUG
00101 fprintf(stderr,"recv %s",buf);
00102 fprintf(stderr,"</pop3_connect>\n");
00103 #endif
00104 return(buf);
00105 }
00106
00107 void pop3_disconnect(int sock){
00108
00109 if(sock>0){
00110 close(sock);
00111 }
00112 }