From 4fe0afac2fab25866d4fcf746a5204d57ded6854 Mon Sep 17 00:00:00 2001 From: hidnplayr Date: Thu, 31 Oct 2013 18:40:41 +0000 Subject: [PATCH] Liza 0.9, works on new network stack. git-svn-id: svn://kolibrios.org@4139 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/cmm/lib/socket_new.h | 96 ++++++++++++++++++++++++++++++ programs/cmm/liza/liza.c | 44 +++++--------- programs/cmm/liza/login.c | 106 +++++++++++++++++----------------- programs/cmm/liza/mail_box.c | 91 ++++++++++++++--------------- programs/cmm/liza/parselist.c | 8 ++- 5 files changed, 211 insertions(+), 134 deletions(-) create mode 100644 programs/cmm/lib/socket_new.h diff --git a/programs/cmm/lib/socket_new.h b/programs/cmm/lib/socket_new.h new file mode 100644 index 000000000..c18f9d7d4 --- /dev/null +++ b/programs/cmm/lib/socket_new.h @@ -0,0 +1,96 @@ +#define SOCK_STREAM 1 +#define SOCK_DGRAM 2 + +#define AF_INET4 2 + +#define MSG_PEEK 0x02 +#define MSG_DONTWAIT 0x40 + +dword errorcode; + +struct sockaddr_in{ + word sin_family; + word sin_port; + dword sin_addr; + char padding[8]; +}; + +inline fastcall dword Socket(ECX, EDX, ESI) +{ + $push ebx + $mov eax, 75 + $mov ebx, 0 + $int 0x40 + errorcode = EBX; + $pop ebx +} + +inline fastcall dword Close(ECX) +{ + $push ebx + $mov eax, 75 + $mov ebx, 1 + $int 0x40 + errorcode = EBX; + $pop ebx +} + +inline fastcall dword Bind(ECX, EDX, ESI) +{ + $push ebx + $mov eax, 75 + $mov ebx, 2 + $int 0x40 + errorcode = EBX; + $pop ebx +} + +inline fastcall dword Listen(ECX, EDX) +{ + $push ebx + $mov eax, 75 + $mov ebx, 3 + $int 0x40 + errorcode = EBX; + $pop ebx +} + +inline fastcall dword Connect(ECX, EDX, ESI) +{ + $push ebx + $mov eax, 75 + $mov ebx, 4 + $int 0x40 + errorcode = EBX; + $pop ebx +} + +inline fastcall dword Accept(ECX, EDX, ESI) +{ + $push ebx + $mov eax, 75 + $mov ebx, 5 + $int 0x40 + errorcode = EBX; + $pop ebx +} + +inline fastcall dword Send(ECX, EDX, ESI, EDI) +{ + $push ebx + $mov eax, 75 + $mov ebx, 6 + $int 0x40 + errorcode = EBX; + $pop ebx +} + +inline fastcall dword Receive(ECX, EDX, ESI, EDI) +{ + $push ebx + $mov eax, 75 + $mov ebx, 7 + $int 0x40 + errorcode = EBX; + $pop ebx +} diff --git a/programs/cmm/liza/liza.c b/programs/cmm/liza/liza.c index 792a9f386..43579a7af 100644 --- a/programs/cmm/liza/liza.c +++ b/programs/cmm/liza/liza.c @@ -10,7 +10,7 @@ #include "..\lib\figures.h" #include "..\lib\file_system.h" #include "..\lib\list_box.h" -#include "..\lib\socket.h" +#include "..\lib\socket_new.h" //*.obj libraries #include "..\lib\lib.obj\box_lib.h" #include "..\lib\lib.obj\network.h" @@ -24,10 +24,8 @@ byte in_out_mail[18*36] = FROM "in_out_mail.raw"; //connection algorithm enum { STOP, - GET_PORT, - GET_SERVER_IP, - GET_SOCKET, - CONNECT, + RESOLVE, + OPEN_CONNECTION, GET_ANSWER_CONNECT, SEND_USER, GET_ANSWER_USER, @@ -38,7 +36,8 @@ enum { SEND_NSTAT, GET_ANSWER_NSTAT, SEND_RETR, - GET_ANSWER_RETR + GET_ANSWER_RETR, + FAILED }; //WindowDefinitions @@ -46,9 +45,10 @@ enum { #define WIN_H 440 #define WIN_MIN_W 500 #define WIN_MIN_H 380 -#define LOGIN_HEADER "Login - Email client Liza 0.8a" -#define OPTIONS_HEADER "Options - Email client Liza 0.8a" -#define MAILBOX_HEADER "Mail Box - Email client Liza 0.8a" +#define LOGIN_HEADER "Login - Email client Liza 0.9" +#define OPTIONS_HEADER "Options - Email client Liza 0.9" +#define MAILBOX_HEADER "Mail Box - Email client Liza 0.9" +#define BUFFERSIZE 512 proc_info Form; system_colors sc; #define LBUMP 0xFFFfff @@ -59,34 +59,20 @@ dword cur_st_text; //connection data #define DEFAULT_POP_PORT 110; -dword local_port=1000; char POP_server_path[128]; -dword POP_server_IP; dword POP_server_port; char login[128]; char request[256+22]; int request_len; char connection_status; -dword socket; +dword socketnum; + +sockaddr_in sockaddr; int aim; int ticks; -//global data for server response -char immbuffer[512]; -int immpointer; - -void immfree(){ - immpointer=0; - immbuffer[immpointer]='\0'; -} - -void immputc(char agot_char){ - immbuffer[immpointer]=agot_char; - immpointer++; - immbuffer[immpointer]='\0'; - if (immpointer>511) {immpointer=0; debug ("IMM BUFFER OVERFLOW ERROR"); aim=NULL;} -} +char immbuffer[BUFFERSIZE]; #include "settings.c" #include "login.c" @@ -127,9 +113,7 @@ void OpenMailDat() ReadFile(0, 512, #read_data, "/sys/network/mail.dat"); if (!read_data) { - //strcpy(#email_text, "eiroglif@yandex.ru"); //temporarily, for testing strcpy(#email_text, "example@mail.com"); - //strcpy(#pass_text, "rostov"); } else { @@ -147,7 +131,7 @@ void OpenMailDat() void SaveAndExit() { char write_data[512], pass_b64[256]; - CloseSocket(socket); + Close(socketnum); strcpy(#write_data, #email_text); strcat(#write_data, "\n"); base64_encode stdcall (#pass_text, #pass_b64, strlen(#pass_text)); diff --git a/programs/cmm/liza/login.c b/programs/cmm/liza/login.c index 3e967f475..47ffc9d2b 100644 --- a/programs/cmm/liza/login.c +++ b/programs/cmm/liza/login.c @@ -38,7 +38,7 @@ void LoginBoxLoop() if (id==11) OptionsLoop(); if (id==12) { - if (!aim) aim=GET_PORT; else aim=NULL; + if (!aim) aim=RESOLVE; else aim=NULL; GetSettings(); SetLoginStatus(NULL, NULL); DrawLoginScreen(); @@ -60,7 +60,7 @@ void LoginBoxLoop() if (key==13) { if (aim) break; - aim=GET_PORT; + aim=RESOLVE; GetSettings(); SetLoginStatus(NULL, NULL); DrawLoginScreen(); @@ -81,70 +81,62 @@ void LoginBoxLoop() if (!pass_text) { notify("Enter password!"); aim=NULL; } if ((!login) || (!POP_server_path)) { notify("Email should be such as username@somesite.com"); aim=NULL; } - if (aim == GET_PORT) + if (aim == RESOLVE) { - SetLoginStatus(5, "Search for free local port..."); - local_port = GetFreePort(1000); - if (!local_port) { notify("Error: There is no free local ports"); aim=NULL; break;} - SetLoginStatus(12, "Obtain server IP..."); - aim = GET_SERVER_IP; - } + SetLoginStatus(1, "Resolving server address..."); + + sockaddr.sin_family = AF_INET4; + AX = POP_server_port; + $xchg al, ah + sockaddr.sin_port = AX; + sockaddr.sin_addr = GetIPfromAdress(#POP_server_path); + if (!sockaddr.sin_addr) { SetLoginStatus(12, "Can't obtain server IP."); aim = FAILED; break;} - if (aim == GET_SERVER_IP) - { - POP_server_IP = GetIPfromAdress(#POP_server_path); - if (!POP_server_IP) { SetLoginStatus(12, "Can't obtain server IP. Retry..."); break; } - SetLoginStatus(25, "Obtain to open socket..."w); - aim = GET_SOCKET; - } - - if (aim == GET_SOCKET) - { - socket = OpenSocket(local_port, POP_server_port, POP_server_IP, SOCKET_ACTIVE); - if (socket == 0xffffffff) { SetLoginStatus(25, "Error obtaining socket. Retry..."); break;} - SetLoginStatus(40, "Establish a connection..."); - aim = CONNECT; - } - - if (aim == CONNECT) - { - connection_status=StatusSocket(socket); - if (connection_status==0) {notify("Connection to server isn't possible"); aim=NULL; break; }; - if (connection_status==7) {SetLoginStatus(40, "Server disconnected. Retry..."); break; }; - if (connection_status!=4) break; //0-connection isn't possible, 2-connecting, 4-connected, 7-server disconnected - SetLoginStatus(55, "Connection established. Reading answer..."); - immfree(); - aim = GET_ANSWER_CONNECT; + aim = OPEN_CONNECTION; } + if (aim == OPEN_CONNECTION) + { + SetLoginStatus(1, "Connecting to server..."); + + socketnum = Socket(AF_INET4, SOCK_STREAM, 0); + if (socketnum == 0xffffffff) { SetLoginStatus(13, "Cannot open socket."); aim = FAILED; break;} + Connect(socketnum, #sockaddr, 16); + SetLoginStatus(55, "Connection established. Waiting for answer..."); + aim = GET_ANSWER_CONNECT; + } + + if (aim == GET_ANSWER_CONNECT) { - if (!PollSocket(socket)) break; - socket_char=ReadSocket(socket); - immputc(socket_char); + ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0); + if ((ticks == 0xffffff) || (ticks < 2)) { SetLoginStatus(61, "Connection failed"); aim = FAILED; break;} + immbuffer[ticks]='\0'; - if (socket_char=='\n') + if (immbuffer[ticks-2]=='\n') { debug(#immbuffer); if (strstr(#immbuffer,"+OK")) { SetLoginStatus(60, "Verifying username..."); aim = SEND_USER; - immfree(); } else { - immfree(); //aim=NULL; //may don't need retry? SetLoginStatus(55, "Failed to connect to server. Retry..."); } } + else + { + SetLoginStatus(103, "Connection failed"); + } } if (aim == SEND_USER) { request_len = GetRequest("USER", #login); - WriteSocket(socket,request_len,#request); + Send(socketnum, #request, request_len, 0); if (EAX == 0xffffffff) { SetLoginStatus(60, "Failed to send USER. Retry..."); break;} SetLoginStatus(70, "Login verifying..."); debug("Send USER, awaiting answer..."); @@ -153,18 +145,22 @@ void LoginBoxLoop() if (aim == GET_ANSWER_USER) { - if (!PollSocket(socket)) break; - socket_char=ReadSocket(socket); - immputc(socket_char); + ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0); + if ((ticks == 0xffffffff) || (ticks < 2)) { SetLoginStatus(81, "Connection failed"); break;} + immbuffer[ticks]='\0'; - if (socket_char=='\n') + if (immbuffer[ticks-2]=='\n') { debug("GOT::"); debug(#immbuffer); if (strstr(#immbuffer,"+OK")) - { aim = SEND_PASS; SetLoginStatus(80, "Verifying password..."); immfree(); } + { aim = SEND_PASS; SetLoginStatus(80, "Verifying password...");} else - { notify("Wrong username"); immfree(); aim=NULL;} + { notify("Wrong username"); aim=NULL;} + } + else + { + SetLoginStatus(103, "Connection failed"); } } @@ -172,18 +168,18 @@ void LoginBoxLoop() { debug("\n Send PASS, awaiting answer..."); request_len = GetRequest("PASS", #pass_text); - WriteSocket(socket,request_len,#request); + Send(socketnum, #request, request_len, 0); if (EAX == 0xffffffff) { SetLoginStatus(80, "Failed to send PASS. Retry..."); break;} aim = GET_ANSWER_PASS; } if (aim == GET_ANSWER_PASS) { - if (!PollSocket(socket)) break; - socket_char=ReadSocket(socket); - immputc(socket_char); + ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0); + if ((ticks == 0xffffff) || (ticks < 2)) { SetLoginStatus(101, "Server disconnected"); break;} + immbuffer[ticks]='\0'; - if (socket_char=='\n') + if (immbuffer[ticks-2]=='\n') { debug("GOT::"); debug(#immbuffer); @@ -191,16 +187,18 @@ void LoginBoxLoop() { SetLoginStatus(100, "Entering mailbox..."); aim=SEND_NSTAT; - immfree(); MailBoxLoop(); } else { notify("Wrong password"); aim=NULL; - immfree(); } } + else + { + SetLoginStatus(103, "Connection failed"); + } } diff --git a/programs/cmm/liza/mail_box.c b/programs/cmm/liza/mail_box.c index bec93ddb1..1b0020aa9 100644 --- a/programs/cmm/liza/mail_box.c +++ b/programs/cmm/liza/mail_box.c @@ -132,7 +132,7 @@ void MailBoxLoop() if (id==EXIT_MAIL) { StopLoading(); - CloseSocket(socket); + Close(socketnum); LoginBoxLoop(); } if (id==CHANGE_CHARSET) @@ -208,18 +208,17 @@ void MailBoxLoop() { debug("Counting mail, awaiting answer..."); request_len = GetRequest("STAT", NULL); - WriteSocket(socket,request_len,#request); + Send(socketnum, #request, request_len, 0); if (EAX == 0xffffffff) { debug("Error sending STAT. Retry..."w); break;} aim = GET_ANSWER_NSTAT; } if (aim == GET_ANSWER_NSTAT) { - if (!PollSocket(socket)) break; - socket_char=ReadSocket(socket); - immputc(socket_char); - - if (socket_char=='\n') + ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0); + if ((ticks == 0xffffff) || (ticks < 2)) break; + + if (immbuffer[ticks-2]=='\n') { debug("GOT::"); debug(#immbuffer); @@ -232,13 +231,11 @@ void MailBoxLoop() listbuffer = mem_Alloc(30*mail_list.count); //24* original listpointer = listbuffer; aim = SEND_NLIST; - debug("Recieving mail list..."); - immfree(); + debug("Receiving mail list..."); } else { notify("Sorry, can't recieve your mail"); - immfree(); aim=NULL; //aim = SEND_NLIST; } } @@ -248,21 +245,19 @@ void MailBoxLoop() { WriteText(5, Form.cheight-11, 0x80, sc.work_text, "Send LIST, awaiting answer..."); request_len = GetRequest("LIST", NULL); - WriteSocket(socket,request_len,#request); + Send(socketnum, #request, request_len, 0); if (EAX == 0xffffffff) {debug("Error while sending LIST. Retry..."); break;} aim = GET_ANSWER_NLIST; } if (aim == GET_ANSWER_NLIST) - { - ticks = PollSocket(socket); - if (!ticks) break; - for (;ticks>0;ticks--) - { - socket_char=ReadSocket(socket); - listputc(socket_char); - - if (socket_char=='.') //this way of checking end of message IS BAD + { + ticks = Receive(socketnum, #immbuffer, BUFFERSIZE, 0); + if ((ticks == 0xffffffff) || (ticks < 3)) break; + + //for (;ticks>0;ticks--) + //{ + if (immbuffer[ticks-3]=='.') //this way of checking end of message IS BAD { aim = SEND_RETR; debug("Got mail list"); @@ -271,8 +266,9 @@ void MailBoxLoop() atr.CreateArray(); atr.SetSizes(); } - } + //} } + if (aim == SEND_RETR) { from = to = date = subj = cur_charset = NULL; @@ -280,46 +276,46 @@ void MailBoxLoop() DrawMailBox(); debug("Send RETR, awaiting answer..."); request_len = GetRequest("RETR", itoa(mail_list.current+1)); - WriteSocket(socket,request_len,#request); + Send(socketnum, #request, request_len, 0); if (EAX == 0xffffffff) { notify("Error while trying to get letter from server"); aim=NULL; break;} mailbuffer = free(mailbuffer); letter_size = atr.GetSize(mail_list.current+1) + 1024; mailbuffer = malloc(letter_size); + if (!mailbuffer) {debug("alloc error!"); aim=NULL; break;} mailpointer = mailbuffer; aim = GET_ANSWER_RETR; } + if (aim == GET_ANSWER_RETR) { - ticks=PollSocket(socket); - if (!ticks) break; + ticks = Receive(socketnum, mailpointer, letter_size + mailbuffer - mailpointer , MSG_DONTWAIT); + if (ticks == 0xffffffff) break; + if (ticks == 0) break; + //debugi(EAX); + + mailpointer = mailpointer + ticks; + //*mailpointer='\0'; + //debug(mailbuffer); - for (;ticks>0;ticks--) + if (!aim) continue; + + if (letter_size + mailbuffer - mailpointer - 2 < 0) { - socket_char=ReadSocket(socket); - //debugch(socket_char); - *mailpointer=socket_char; - mailpointer++; - *mailpointer='\0'; - if (!aim) continue; + debug("Resizing buffer"); + letter_size += 4096; + mailbuffer = realloc(mailbuffer, letter_size); + if (!mailbuffer) {debug("Realloc error!"); aim=NULL; break;} + } - if (letter_size + mailbuffer - mailpointer - 2 < 0) - { - debug("Buffer overflow!!1 Realloc..."w); - letter_size += 4096; - mailbuffer = realloc(mailbuffer, letter_size); - if (!mailbuffer) {debug("Relloc error!"); aim=NULL; break;} - } + if (letter_size>9000) + { + id = mailpointer - mailbuffer * 100 ; + id /= letter_size - 1024; + if (id!=cur_st_percent) SetMailBoxStatus( id , NULL); + } - if (letter_size>9000) - { - id = mailpointer - mailbuffer * 100 ; - id /= letter_size - 1024; - if (id!=cur_st_percent) SetMailBoxStatus( id , NULL); - } - - ParceMail(); - } + ParseMail(); } } @@ -491,7 +487,6 @@ void StopLoading() aim = NULL; mailbuffer = free(mailbuffer); to = from = date = subj = cur_charset = NULL; - while (PollSocket(socket)) ReadSocket(socket); } int GetMailCount(){ diff --git a/programs/cmm/liza/parselist.c b/programs/cmm/liza/parselist.c index c8ae912c1..43fd8cfce 100644 --- a/programs/cmm/liza/parselist.c +++ b/programs/cmm/liza/parselist.c @@ -1,12 +1,16 @@ //Leency & SoUrcerer, LGPL -void ParceMail() +void ParseMail() { dword line_off, new_buf; char tline[256]; - if ( mailpointer-mailbuffer>9 ) if ( (strncmp(mailpointer-5,"\r\n.\r\n",5)==0) || (strncmp(mailpointer-3,"\n.\n",3)==0) ) + if ( mailpointer-mailbuffer>9 ) if (strncmp(mailpointer-5,"\n.\n",5)==0) // note that c-- assembles "\n.\n" to 0x0d, 0x0a, 0x2e, 0x0d, 0x0a { + debug("End of mail detected"); + mailpointer = mailpointer - 5; + *mailpointer='\0'; + if (strstr(mailbuffer, "+OK")!=mailbuffer) { aim = GET_ANSWER_RETR;