summaryrefslogtreecommitdiff
blob: 16cbe4ed6bce1b0ee539853bae2ccde8075306d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
--- wmpop3lb2.4.2/wmpop3/Pop3Client.c	2002-06-27 16:04:42.000000000 +0200
+++ wmpop3lb2.4.2-pathed/wmpop3/Pop3Client.c	2007-01-11 14:39:12.000000000 +0200
@@ -27,6 +27,49 @@
 
 #include "Pop3Client.h"
 
+/* receive full responce */
+int do_recv(int s, void *ibuf, size_t len, int flags)
+{
+  size_t ret, total;
+  char *p, *buf = ibuf;
+
+  total = 0;
+  while (1)
+  {
+    /* left one byte for null termination */
+    ret = recv(s, buf + total, len - 1 - total, flags);
+    /* if we got error or close, then brea */
+    if (ret <= 0)
+    {
+      break;
+    }
+    /* increase size of received data */
+    total += ret;
+    /* null terminating received data */
+    buf[total] = 0;
+    /* left one byte for null termination
+     * if out of buffer, return */
+    if (len - total <= 1)
+    {
+      break;
+    }
+    /* if we found end of line signal, then stop */
+    p = strstr(buf, "\r\n");
+    printf("p == %p\n", p);
+    if (p != 0)
+    {
+      break;
+    }
+  }
+
+  /* if there wasn't any data, then return error code */
+  if (total == 0)
+  {
+    return ret;
+  }
+  return total;
+}
+
 /* return size if all goes well, -1 if not expected answer */
 int	send_command(char *exp_answer, char **retour, Pop3 pc) 
 {
@@ -61,18 +104,29 @@
     return (pc);
 }
 int pop3MakeConnection(Pop3 pc, char *serverName, int port){
+    struct timeval t;
 
     pc->s = socket(AF_INET, SOCK_STREAM, 0 );
     memset( &(pc->server), 0 , sizeof(pc->server));
     pc->server.sin_family = AF_INET;
     pc->hp = gethostbyname(serverName);
     if( pc->hp == 0)
+    {
+        close(pc->s);
         return -1;
+    }
     memcpy( &(pc->server.sin_addr), pc->hp->h_addr, pc->hp->h_length);
     pc->server.sin_port = htons(port);
     if ( connect(pc->s, (struct sockaddr *)&(pc->server)
                                , sizeof(pc->server)) < 0 )
+    {
+        close(pc->s);
         return -1;
+    }
+    t.tv_sec = 60;
+    t.tv_usec = 0;
+    setsockopt(pc->s, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t));
+    setsockopt(pc->s, SOL_SOCKET, SO_SNDTIMEO, &t, sizeof(t));
     pc->connected = CONNECTED;
    return 0;
 }
@@ -94,7 +148,7 @@
           return -1;
       }
 
-      size = recv(pc->s,&pc->inBuf,1024,0);
+      size = do_recv(pc->s,&pc->inBuf,1024,0);
       memset(temp,0,1024);
       memcpy(temp,pc->inBuf,size);
       if( temp[0] != '+' ){
@@ -104,10 +158,10 @@
 
       sprintf(pc->outBuf,"USER %s\r\n",name);
       send(pc->s, &pc->outBuf,strlen(pc->outBuf),0); 
-      size =recv(pc->s,pc->inBuf,1024,0);
+      size = do_recv(pc->s,pc->inBuf,1024,0);
       memset(temp,0,1024);
       memcpy(temp,pc->inBuf,size);
-      if( temp[0] != '+' ){
+      if( temp[0] != '+' && temp[0] != '\r' ){
           fprintf(stderr,"Invalid User Name\n");
           return -1;
       }
@@ -115,10 +169,10 @@
       memset(pc->outBuf,0,1024);
       sprintf(pc->outBuf,"PASS %s\r\n",pass);
       send(pc->s, pc->outBuf, strlen(pc->outBuf),0 );
-      size =recv(pc->s,&pc->inBuf,1024,0);
+      size = do_recv(pc->s,&pc->inBuf,1024,0);
       memset(temp,0,1024);
       memcpy(temp,pc->inBuf,size);
-      if( temp[0] != '+' ){
+      if( temp[0] != '+' && temp[0] != '\r'){
           fprintf(stderr,"Incorrect Password\n");
           return -1;
       }
@@ -213,7 +267,7 @@
   /* Find total number of messages in mail box */
   sprintf(pc->outBuf,"STAT\r\n");
   send(pc->s, pc->outBuf, strlen(pc->outBuf),0 );
-  size = recv(pc->s,pc->inBuf,1024,0);
+  size = do_recv(pc->s,pc->inBuf,1024,0);
   if( pc->inBuf[0] != '+' ){
     perror("Error Receiving Stats");
     return (-1);
@@ -266,7 +320,7 @@
   /* Find total number of messages in mail box */
   sprintf(pc->outBuf,"STAT\r\n");
   send(pc->s, pc->outBuf, strlen(pc->outBuf),0 );
-  size = recv(pc->s,pc->inBuf,1024,0);
+  size = do_recv(pc->s,pc->inBuf,1024,0);
   pc->inBuf[size] = '\0';
 #ifdef _DEBUG
   printf("  pop3CheckMail, stat received buf (size=%d): [%s]\n",
@@ -313,7 +367,7 @@
 
   sprintf(pc->outBuf,"LAST\r\n");
   send(pc->s, pc->outBuf, strlen(pc->outBuf),0 );
-  size = recv(pc->s,pc->inBuf,1024,0);
+  size = do_recv(pc->s,pc->inBuf,1024,0);
   pc->inBuf[size] = '\0';
 #ifdef _DEBUG
   printf("  pop3CheckMail, last received buf (size=%d): [%s]\n",
@@ -325,9 +379,25 @@
 #ifdef _DEBUG
     printf("  Error Receiving LAST: [%s]\n", temp);
 #endif
-    pc->numOfUnreadMessages = pc->numOfMessages;
+    /* TRY STAT instead LAST */
+    sprintf(pc->outBuf,"STAT\r\n");
+    send(pc->s, pc->outBuf, strlen(pc->outBuf),0 );
+    size = do_recv(pc->s,pc->inBuf,1024,0);
+    pc->inBuf[size] = '\0';
+#ifdef _DEBUG
+    printf("  pop3CheckMail, last received buf (size=%d): [%s]\n",
+		    size, pc->inBuf);
+#endif
+    memset(temp,0,1024);
+    memcpy(temp,pc->inBuf,size);
+    if( temp[0] != '+' ){
+#ifdef _DEBUG
+      printf("  Error Receiving STAT: [%s]\n", temp);
+#endif
+      pc->numOfUnreadMessages = pc->numOfMessages;
+    }
   }
-  else {
+  if( temp[0] != '+' ){
     ptr = strtok(temp, " ");
     ptr = strtok( 0," ");
     pc->numOfUnreadMessages = pc->numOfMessages - atoi(ptr);
@@ -545,7 +615,7 @@
   printf("  %s\n", pc->outBuf);
 #endif
   send(pc->s, pc->outBuf, strlen(pc->outBuf), 0);
-  size = recv(pc->s, pc->inBuf, 4096, 0);
+  size = do_recv(pc->s, pc->inBuf, 4096, 0);
   if ('+' != pc->inBuf[0]) {
     perror("error while deleting mail");
     return (1);
@@ -579,7 +649,7 @@
     if( pc->connected == NOT_CONNECTED )
         return -1;
     send(pc->s, "quit\r\n", 6,0 );
-    size =recv(pc->s,&pc->inBuf,1024,0);
+    size = do_recv(pc->s,&pc->inBuf,1024,0);
     pc->connected = NOT_CONNECTED;
     if(pc->s != 0)
         close(pc->s);