📄 smtptest.c
字号:
/*
funtion : send email
author : liyanan
data : 2006-04-08
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <errno.h>
#define ETH_NAME "eth0"
#define SD_BOTH 2
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
int s;
struct sockaddr_in remote;
unsigned short port;
int rt;
char *send_data;
char *recv_data;
char *hostname;
struct hostent *ht;
char *usersrc,*userdes,*passsrc,*passdes;
char *From,*To,*Date,*Subject,*tmpbuf;
char *Filename;
int GetLocalAddress(char *szIPAddr)
{
int sock;
struct sockaddr_in sin;
struct sockaddr sa;
struct ifreq ifr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock == -1)
{
printf("Error:get local IP socket fail!\n");
return -1;
}
strncpy(ifr.ifr_name, ETH_NAME,IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
if(ioctl(sock, SIOCGIFADDR, &ifr) < 0)
{
printf("Error:get local IP ioctl fail!");
return -1;
}
memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
sprintf(szIPAddr,"%s",inet_ntoa(sin.sin_addr));
return 0;
}
int GetLocalMac(unsigned char *vmac)
{
int sock;
int i;
struct sockaddr_in sin;
struct sockaddr sa;
struct ifreq ifr;
unsigned char mac[6];
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(mac, 0, sizeof(mac));
if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0)
{
perror("ioctl");
return -1;
}
memcpy(&sa, &ifr.ifr_addr, sizeof(sin));
memcpy(mac,sa.sa_data,6);
sprintf(vmac,"%.2X:%.2X:%.2X:%.2X:%0.2X:%0.2X\n",mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return 0;
}
void Destory()
{
if(s!=INVALID_SOCKET)
{
shutdown(s,SD_BOTH);
close(s);
//WSACleanup();
}
free(From);
free(To);
free(Date);
free(Subject);
free(Filename);
free(tmpbuf);
free(recv_data);
free(userdes);
free(passdes);
}
//---------------------------------------------------------------------------
// Base64 code table
// 0-63 : A-Z(25) a-z(51), 0-9(61), +(62), /(63)
char Base2Chr( unsigned char n )
{
n &= 0x3F;
if ( n < 26 )
return ( char )( n + 'A' );
else if ( n < 52 )
return ( char )( n - 26 + 'a' );
else if ( n < 62 )
return ( char )( n - 52 + '0' );
else if ( n == 62 )
return '+';
else
return '/';
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// aLen 为 aSrc 的长度, aDest 所指的缓冲区必须至少为 aLen 的 1.33 倍!!!
// 返回 aDest 的长度
int Base64Encode22( char * const aDest, const unsigned char * aSrc, int aLen )
{
char * p = aDest;
int i;
unsigned char t;
for ( i = 0; i < aLen; i++ )
{
switch ( i % 3 )
{
case 0 :
*p++ = Base2Chr( *aSrc >> 2 );
t = ( *aSrc++ << 4 ) & 0x3F;
break;
case 1 :
*p++ = Base2Chr( t | ( *aSrc >> 4 ) );
t = ( *aSrc++ << 2 ) & 0x3F;
break;
case 2 :
*p++ = Base2Chr( t | ( *aSrc >> 6 ) );
*p++ = Base2Chr( *aSrc++ );
break;
}
}
if ( aLen % 3 != 0 )
{
*p++ = Base2Chr( t );
if ( aLen % 3 == 1 )
*p++ = '=';
*p++ = '=';
}
*p = 0; // aDest is an ASCIIZ string
return ( p - aDest ); // exclude the end of zero
}
//---------------------------------------------------------------------------
int Base64Enc11(char *buf, char*text,int size)
{
static char *base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int buflen = 0;
while(size>0)
{
*buf++ = base64_encoding[ (text[0] >> 2 ) & 0x3f];
if(size>2)
{
*buf++ = base64_encoding[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = base64_encoding[((text[1] & 0xF) << 2) | (text[2] >> 6)];
*buf++ = base64_encoding[text[2] & 0x3F];
}
else
{
switch(size)
{
case 1:
*buf++ = base64_encoding[(text[0] & 3) << 4 ];
*buf++ = '=';
*buf++ = '=';
break;
case 2:
*buf++ = base64_encoding[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = base64_encoding[((text[1] & 0x0F) << 2) | (text[2] >> 6)];
*buf++ = '=';
break;
}
}
text +=3;
size -=3;
buflen +=4;
}
*buf = 0;
return buflen;
}
char* ch64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned char *encode(unsigned char *src,int srclen)
{
int n,buflen,i,j;
int pading=0;
unsigned char *buf;
static unsigned char *dst;
buf=src;
buflen=n=srclen;
if(n%3!=0) /* pad with "=" by using a temp buffer */
{
pading=1;
buflen=n+3-n%3;
buf=malloc(buflen+1);
memset(buf,0,buflen+1);
memcpy(buf,src,n);
for(i=0;i<3-n%3;i++)
buf[n+i]="=";
}
dst=malloc(buflen*4/3+1);
memset(dst,0,buflen*4/3+1);
for(i=0,j=0;i<buflen;i+=3,j+=4)
{
dst[j]=(buf[i]&0xFC)>>2;
dst[j+1]=((buf[i]&0x03)<<4) + ((buf[i+1]&0xF0)>>4);
dst[j+2]=((buf[i+1]&0x0F)<<2) + ((buf[i+2]&0xC0)>>6);
dst[j+3]=buf[i+2]&0x3F;
}
for(i=0;i<buflen*4/3;i++) /* map 6 bit value to base64 ASCII character */
dst[i]=ch64[dst[i]];
if(pading)
free(buf);
return dst;
}
/*
unsigned char *decode(unsigned char *src)
{
int n,i,j;
unsigned char *p;
static unsigned char *dst;
n=strlen(src);
for(i=0;i<n;i++) /* map base64 ASCII character to 6 bit value *
{
p=strchr(ch64,src[i]);
if(!p)
break;
src[i]=p-ch64;
}
dst=malloc(n*3/4+1);
memset(dst,0,n*3/4+1);
for(i=0,j=0;i<n;i+=4,j+=3)
{
dst[j]=(src[i]<<2) + ((src[i+1]&0x30)>>4);
dst[j+1]=((src[i+1]&0x0F)<<4) + ((src[i+2]&0x3C)>>2);
dst[j+2]=((src[i+2]&0x03)<<6) + src[i+3];
}
return dst;
}
*/
// base64 tables
static char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
char * base64_encode(const unsigned char *value, int vlen);
unsigned char * base64_decode(const char *value, int *rlen);
static signed char index_64[128] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
-1, 0, 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,-1, -1,-1,-1,-1,
-1,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,-1, -1,-1,-1,-1
} ;
char *base64_encode(const unsigned char *value, int vlen) {
unsigned char oval = 0 ;
char *result = (char *)malloc((vlen * 4) / 3 + 5) ;
char *out = result;
while (vlen >= 3) {
*out++ = basis_64[value[0] >> 2];
*out++ = basis_64[((value[0] << 4) & 0x30) | (value[1] >> 4)];
*out++ = basis_64[((value[1] << 2) & 0x3C) | (value[2] >> 6)];
*out++ = basis_64[value[2] & 0x3F];
value += 3;
vlen -= 3;
}
if (vlen > 0) {
*out++ = basis_64[value[0] >> 2];
oval = (value[0] << 4) & 0x30 ;
if (vlen > 1) oval |= value[1] >> 4;
*out++ = basis_64[oval];
*out++ = (vlen < 2) ? '=' : basis_64[(value[1] << 2) & 0x3C];
*out++ = '=';
}
*out = '\0';
return result;
}
unsigned char *base64_decode(const char *value, int *rlen)
{
int c1, c2, c3, c4;
int vlen = strlen(value);
unsigned char *result =(unsigned char *)malloc((vlen * 3) / 4 + 1);
unsigned char *out = result;
*rlen = 0;
while (1) {
if (value[0]==0) {
*out = '\0' ;
return result;
}
c1 = value[0];
if (CHAR64(c1) == -1) goto base64_decode_error;
c2 = value[1];
if (CHAR64(c2) == -1) goto base64_decode_error;
c3 = value[2];
if ((c3 != '=') && (CHAR64(c3) == -1)) goto base64_decode_error;
c4 = value[3];
if ((c4 != '=') && (CHAR64(c4) == -1)) goto base64_decode_error;
value += 4;
*out++ = (CHAR64(c1) << 2) | (CHAR64(c2) >> 4);
*rlen += 1;
if (c3 != '=') {
*out++ = ((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2);
*rlen += 1;
if (c4 != '=') {
*out++ = ((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4);
*rlen += 1;
}
}
}
base64_decode_error:
*result = 0;
*rlen = 0;
return result;
}
/*Base64编码*
void Base64(unsigned char chasc[3],unsigned char chuue[4])
/*
chasc:未编码的二进制代码
chuue:编码过的Base64代码
*
{
int i,k=2;
unsinged char t=NULL;
for(i=0;i<3;i++)
{
*(chuue+i)=*(chasc+i)>>k;
*(chuue+i)|=t;
t=*(chasc+i)<<(8-k);
t>>=2;
k+=2;
}
*(chuue+3)=*(chasc+2)&63;
for(i=0;i<4;i++)
if((*(chuue+i)>=0)&&(*(chuue+i)<=25)) *(chuue+i)+=65;
else if((*(chuue+i)>=26)&&(*(chuue+i)<=51)) *(chuue+i)+=71;
else if((*(chuue+i)>=52)&&(*(chuue+i)<=61)) *(chuue+i)-=4;
else if(*(chuue+i)==62) *(chuue+i)=43;
else if(*(chuue+i)==63) *(chuue+i)=47;
}
/*Base64解码*
void unBase64(unsigned char chuue[4],unsigned char chasc[3])
/*
chuue:未解码的Base64代码
chasc:解码过的二进制代码
*
{int i,k=2;
unsigned char t=NULL;
for(i=0;i<4;i++)
if((*(chuue+i)>=65)&&(*(chuue+i)<=90)) *(chuue+i)-=65;
else if((*(chuue+i)>=97)&&(*(chuue+i)<=122)) *(chuue+i)-=71;
else if((*(chuue+i)>=48)&&(*(chuue+i)<=57)) *(chuue+i)+=4;
else if(*(chuue+i)==43) *(chuue+i)=62;
else if(*(chuue+i)==47) *(chuue+i)=63;
else if(*(chuue+i)==61) *(chuue+i)=0;
for(i=0;i<3;i++)
{*(chhex+i)=*(chuue+i)<<k;
k+=2;
t=*(chuue+i+1)>>8-k;
*(chhex+i)|=t;
}
}
*/
// encode base64 string
// return encode len
//CODE
/**
* @brief 进行base64编码
*
* @param buf [out]保存编码结果
* @param text [in]要编码的串
* @param size [in]text的长度
*
* @return 编码长度
**/
int Base64Enc(unsigned char *buf,const unsigned char*text,int size)
{
static char *base64_encoding =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int buflen = 0;
while(size>0){
*buf++ = base64_encoding[ (text[0] >> 2 ) & 0x3f];
if(size>2){
*buf++ = base64_encoding[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = base64_encoding[((text[1] & 0xF) << 2) | (text[2] >> 6)];
*buf++ = base64_encoding[text[2] & 0x3F];
}else{
switch(size){
case 1:
*buf++ = base64_encoding[(text[0] & 3) << 4 ];
*buf++ = '=';
*buf++ = '=';
break;
case 2:
*buf++ = base64_encoding[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = base64_encoding[((text[1] & 0x0F) << 2) | (text[2] >> 6)];
*buf++ = '=';
break;
}
}
text +=3;
size -=3;
buflen +=4;
}
*buf = 0;
return buflen;
}
char GetBase64Value(char ch)
{
if ((ch >= 'A') && (ch <= 'Z'))
return ch - 'A';
if ((ch >= 'a') && (ch <= 'z'))
return ch - 'a' + 26;
if ((ch >= '0') && (ch <= '9'))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -