⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 TINI的pop3的c代码
💻 C
字号:
/* ---------------------------------------------------------------------------
 *  Copyright (C) 2003 Dallas Semiconductor Corporation, All Rights Reserved.
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 * 
 *  The above copyright notice and this permission notice shall be included
 *  in all copies or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *  IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
 *  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *  OTHER DEALINGS IN THE SOFTWARE.
 * 
 *  Except as contained in this notice, the name of Dallas Semiconductor
 *  shall not be used except as stated in the Dallas Semiconductor
 *  Branding Policy.
 * ---------------------------------------------------------------------------
 */

/***************************************************************************************
 *	Module Name : TINI Mail Client Library Test Application
 *	Description : This application demonstrates pop3 library functionality
 * 	   Filename : main.c
 *	   Compiler : keil C51 Compiler V7.06
 *		Version : Version 1.0
 *		Created : 
 *Modifications :
 *		  Notes :
 *************************************************************************************/

//include files
#include <reg400.h>
#include <rom400_init.h>
#include <rom400_task.h>
#include <rom400_sock.h>
#include <rom400_kmem.h>
#include <rom400_mem.h>
#include "tini400_pop3.h"
#include "stdio.h"
#include "tini400_mime.h"
#include "tini400_ntlm.h"
#include <string.h>

//leave 4000h bytes for function parameters
#define RAM_START             0x20000
#define RAM_END               0x5FFFF

#define FS_START              0x70000
#define FS_BLOCKS             256

//Static IP Address (used if you are selecting a static IP)
#define STATIC_IP_MSB             192
#define STATIC_IP_2               168
#define STATIC_IP_3               110
#define STATIC_IP_LSB              57

//Static Subnet Mask (used if you are selecting a static IP)
#define STATIC_SUBNETMASK_MSB     255
#define STATIC_SUBNETMASK_2       255
#define STATIC_SUBNETMASK_3       255  
#define STATIC_SUBNETMASK_LSB       0

#define STATIC_IPV4_PREFIX         16

//Static Gateway (used if you are selecting a static IP)
#define STATIC_GATEWAY_MSB        180
#define STATIC_GATEWAY_2          0  
#define STATIC_GATEWAY_3          110
#define STATIC_GATEWAY_LSB        24

//display mailnode object content
void print_mailnode(struct _mail *newmailnode);
void printfile(char *filename);

//function declaration for pop3 authentication call back
int ntlm_authentication(pop3_session *pop3_handle);

//variable declarations for handling ntlm messages
type1msg t1_msg;
type2msg t2_msg;
type3msg t3_msg;

/****************************************************************************************
*   Function Name : do_static
*     Description : configures IP address of TINI
*        Input(s) : nothing
*       Output(s) : nothing
*   	Destroyed :
*    		Notes :
*****************************************************************************************/
void do_static()
{
    unsigned int result;
    unsigned char xdata config[56];
    unsigned int i;
    for (i=0;i<56;i++)
        config[i] = 0;

    //set the ip address
    config[12] = STATIC_IP_MSB;
    config[13] = STATIC_IP_2;
    config[14] = STATIC_IP_3;
    config[15] = STATIC_IP_LSB;

    //set the subnet mask
    config[16] = STATIC_SUBNETMASK_MSB;
    config[17] = STATIC_SUBNETMASK_2;
    config[18] = STATIC_SUBNETMASK_3;
    config[19] = STATIC_SUBNETMASK_LSB;

    //set the iP4 prefix
    config[20] = STATIC_IPV4_PREFIX;

    //set the gateway
    config[33] = STATIC_GATEWAY_MSB;
    config[34] = STATIC_GATEWAY_2;
    config[35] = STATIC_GATEWAY_3;
    config[36] = STATIC_GATEWAY_LSB;

    result = setnetworkparams(config);
}

//main entry point of application
void main()
{
	int status, count;
    unsigned int temp;
	struct _mail *readmail;
	struct _maillist *ml;

	long pop3_ip;

	//for file system.
	FILE* file;
    void* start;

	//user header object
	userheader u_hdr;
	char user[30], pass[30];

	//install updated network stack
    xnetstack_install();

    //install updated memory library
    kmem_install(ROM400_KMEM_MODEL_SMALLEST + 2);

	//initialize ROM
	init_rom(RAM_START, RAM_END);

   	//initialize pop3 library
	pop3_init();

    clear_param_buffers();

	//configure IP address
    do_static();

    start = (void*)FS_START;
    temp = finit(FOPEN_MAX, FS_BLOCKS, start);
	printf("Result of FS init: %d \r\n", temp);

	//pop3 server ip address
	pop3_ip=inet_addr("192.168.110.38");

	printf("\n Enter user name:");
	scanf("%s",user);
	printf("\n Enter password:");
	scanf("%s",pass);

	//login to pop3 server
	status=pop3_login(pop3_ip,user,pass);
	printf("\nthe return value of pop3_login is:%d",status);

	//get mail box status
	pop3_getmailboxstate(&status,&count);
	printf("\nMAILBOX STATUS: Number of mails %d and size %d",status,count);

	//read mail list and value
	ml=pop3_getmaillist(&status);
	if(ml!=0)
	{
		printf("\n the number of mails in mailbox : %d", ml->numberofmails);
		for(count=0;count<ml->numberofmails;count++)
		{
			printf("\n mailnumber: %d, size: %d",ml->mailnumberlist[count],ml->mailsizelist[count]);
		}
	}
    else
		printf("\n pop3_getmaillist failed, the returned status value is : %d",status);

	
	printf("\nEnter mail number to readmail:");
	scanf("%d",&count);
	readmail=pop3_receivemail(count,&status);
	
	if(readmail==0)
		printf("\n receivemail failed, the returned status value is : %d",status);
	else
	{
		printf("\nthe received mail content:");
		print_mailnode(readmail);
	}

	//logout from pop3 server
	status=pop3_logout();
	printf("\n status of of pop3_logout is : %d",status);

	//register ntlmauthentication callback function
	pop3_registerauthcallback(&ntlm_authentication);

	//login to pop3 server
	status=pop3_login(pop3_ip,user,pass);
	printf("\nthe return value of pop3_login after registering ntlm auth callback function :%d",status);
	
	//logout from pop3 server
	status=pop3_logout();
	printf("\n status of of pop3_logout is : %d",status);

	while(1)
	{
	}
}


/*****************************************************************************************
*   Function Name : print_mailnode
*     Description : this function displays content of mailnode structure object
*        Input(s) : newmailnode
*       Output(s) : nothing
*	  Return value: nothing
*    		Notes :
*****************************************************************************************/
void print_mailnode(struct _mail *newmailnode)
{
	int count;
	printf("\nfrom id : %s",newmailnode->mailhdr.from_id);
	printf("\nsender name : %s",newmailnode->mailhdr.sendername);
	printf("\nto id : %s",newmailnode->mailhdr.to_id);
	printf("\nrecipient name : %s",newmailnode->mailhdr.recipientname);
	printf("\nsubject : %s",newmailnode->mailhdr.subject);
	printf("\nreply_to_id : %s",newmailnode->mailhdr.reply_to_id);
	printf("\ncc id : %s",newmailnode->mailhdr.cc_id);
	printf("\nbcc id : %s",newmailnode->mailhdr.bcc_id);
	printf("\nerrors to id : %s",newmailnode->mailhdr.errors_to_id);
	printf("\ndate : %s",newmailnode->mailhdr.date);

	count=0;
	while(newmailnode->userhdr.headernamelist[count]!=0)
	{
		printf("\nuser header name  : %s",newmailnode->userhdr.headernamelist[count]);
		printf("\nuser header value : %s",newmailnode->userhdr.headervaluelist[count]);
		count++;
	}

	printf("\nmessage : %s",newmailnode->msg);

	count=0;
	while(newmailnode->attachmentlist[count]!=0)
	{
		printf("\nattachment file names  : %s",newmailnode->attachmentlist[count]);
		printfile(newmailnode->attachmentlist[count]);
		count++;
	}
	return ; 
}

//print file content
void printfile(char *filename)
{

    int temp;
	FILE *file;
	file = fopen(filename, "r");

	if (file==NULL)
	{
		printf("\n file not found");
		return;
	}
	printf("\nfile content:");

    temp = fgetc(file);

    while (temp != -1)
    {
        printf("%c", (char)temp);
        temp = fgetc(file);
    }
	printf("\r\n");
	fclose(file);
}


int ntlm_authentication(pop3_session *pop3_handle)
{
	char buf[MAX_LINE_SIZE];
	char *mimebuf;
	int size;

    sprintf(buf, "AUTH NTLM\r\n");

	if(send(pop3_handle->handle, buf, strlen(buf),0)!=0) 
	{
		return POP3_SOCKET_ERROR;
	}

	if((size=recv(pop3_handle->handle,buf,MAX_LINE_SIZE,0))==0x0FFFF)
	{
		return POP3_SOCKET_ERROR;
	}

	buf[size]='\0';

	if(strncmp(buf,"+",1)!=0)
	{
		return POP3_RECEIVEMAIL_ERROR;
	}

	//generate type1 ntlm message
	generate_type1_msg(&t1_msg, pop3_handle->user);
	mimebuf=mime_encode((unsigned char *)&t1_msg,(sizeof(type1msghdr)+t1_msg.buf_index), BASE64);
	strcpy(buf,mimebuf);
	strcat(buf,"\r\n");

	if(send(pop3_handle->handle, buf, strlen(buf),0)!=0)
		return POP3_SOCKET_ERROR;

	//receive server response
	if((size=recv(pop3_handle->handle,buf,MAX_LINE_SIZE,0))==0x0FFFF)
		return POP3_SOCKET_ERROR;

	//ignore server response status mark
	if(buf[0]=='+' && buf[1] == ' ')
		mimebuf=mime_decode(buf+2, BASE64);
	else
		mimebuf=mime_decode(buf, BASE64);

	memcpy((char *)&t2_msg,mimebuf,mem_sizeof(mimebuf));

	//generate type3 ntlm message
    generate_type3_msg(&t2_msg, &t3_msg, pop3_handle->user,pop3_handle->pass);

	mimebuf=mime_encode((unsigned char *)&t3_msg,(sizeof(type3msghdr)+t3_msg.buf_index), BASE64);

	strcpy(buf,mimebuf);
	strcat(buf,"\r\n");

	if(send(pop3_handle->handle, buf, strlen(buf),0)!=0) 
		return POP3_SOCKET_ERROR;

	if((size=recv(pop3_handle->handle,buf,MAX_LINE_SIZE,0))==0x0FFFF)
		return POP3_SOCKET_ERROR;

	buf[size]='\0';

	if(strncmp(buf,"+",1)!=0)
	{
		return POP3_INVALID_USER_PASSWORD;
	}
    return POP3_STATUS_SUCCESS;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -