📄 sockutil.c
字号:
/*
###############################################################################
File Name : sockutil.c
Version : 1.0
Programmer(s) : Kim Woo Youl
Created : 2002/11/05
Description : Implementation of useful function of W3100A (I2C MODE)
Modified History :
Modified :
Programmer :
Description :
###############################################################################
*/
/*
###############################################################################
Include Part
###############################################################################
*/
#include "reg51.h"
#include "socket.h"
#include "serial.h"
#include "util.h"
#include "sockutil.h"
/*
###############################################################################
Define Part
###############################################################################
*/
/*
###############################################################################
Grobal Variable Definition Part
###############################################################################
*/
/*
###############################################################################
Local Variable Definition Part
###############################################################################
*/
/*
Description : Convert 32bit Address into Dotted Decimal Format
Argument : addr - Pointer variable to store converted value(INPUT)
addr_str - Character string to store Decimal Dotted Notation Format.(INPUT,OUTPUT)
Return Value :
Note :
*/
void inet_ntoa(u_char* addr,char* addr_str)
{
char i ;
int d;
for(i = 0; i < 4; i++)
{
d = *(addr+i);
d = d & 0x00FF;
/* Convert to decimal number */
*addr_str++ = D2C(d/100);
*addr_str++ = D2C(( d / 10 )%10);
*addr_str++ = D2C(D2C(d%10));
*addr_str++ = '.';
}
*(--addr_str) = 0;
}
/*
Description : Get Source IP Address of W3100A.
Argument : addr - Pointer to store Source IP Address(32bit Address)(INPUT, OUTPUT)
Return Value :
Note :
*/
void GetIPAddress(u_char* addr)
{
IndirectReadBuf(SRC_IP_PTR,addr,4);
}
/*
Description : Get Source IP Address of W3100A.
Argument : addr - Pointer to store Gateway IP Address(32bit Address)(INPUT, OUTPUT)
Return Value :
Note :
*/
void GetGWAddress(u_char* addr)
{
IndirectReadBuf(GATEWAY_PTR,addr,4);
}
/*
Description : Get Source Subnet mask of W3100A.
Argument : addr - Pointer to store Subnet Mask(32bit Address)(INPUT, OUTPUT)
Return Value :
Note :
*/
void GetSubMask(u_char* addr)
{
IndirectReadBuf(SUBNET_MASK_PTR,addr,4);
}
/*
Description : To be input Dotted Notation string from RS232C and convert 32-bit or 48bit Decimal Address
Argument : addr - Pointer variable to be stored Converted value (INPUT, OUTPUT)
base - binary,decimal,hexa-decimal (INPUT)
len - number of Dot Character string to be input(INPUT)
Return Value : Length of source string
Note : If converting has finished, then '1'.Else if user cancel the input or input wrong valued, then '-1'.
*/
char GetDotNotationAddr(u_char* addr, u_int base, u_int len)
{
u_char xdata str[9]; // Consider the case of binary numberr
u_char i,c;
u_char j = 0;
if(base == 0x10) i = 2; // max number
else if (base == 10) i = 3;
else i = 8;
while(1)
{
c = GetByte(); // Read 1 Character
switch(c)
{
case 0x0D : // If New line
str[j++] = '\0';
*addr++ = (u_char) ATOI(str,base); // Convert to Decimal and Store
len--;
PutStringLn("");
if( len <= 0 ) return 1;
else return -1;
case '.' : // If Dot
str[j++] = '\0';
*addr++ = (u_char) ATOI(str,base);
len--;
PutByte('.');
j = 0;
break;
case 0x1B:
return -1; // Cancel
case 0x08:
if(j !=0)
{
PutByte(0x08);
PutByte(' ');
PutByte(0x08);
j--;
}
break;
default:
if( C2D(c) != c && j < i) // If Value to be input is not character and not above avaiable max input number.
{
PutByte(c); // echo.
str[j++] = c;
}
break;
}
}
}
/*
********************************************************************************
* Description: Read established network information(G/W, IP, S/N, Mac) of W3100A and Output that through Serial.
* Arguments :
* Returns :
* Note : Mac Address is output into format of Dotted HexaDecimal.Others are output into format of Dotted Decimal Format.
********************************************************************************
*/
void GetNetConfig()
{
char xdata str[16];
u_char xdata addr[6];
u_char i;
PutStringLn("\r\n====================================");
PutStringLn(" Net Config Information");
PutStringLn("====================================");
PutString("MAC ADDRESS : ");
for(i = 0; i < 5; i++) // HexaDecimal
{
PutHTOA(IndirectReadByte(SRC_HA_PTR+i));
PutByte('.');
}
PutHTOA(IndirectReadByte(SRC_HA_PTR+i));
GetSubMask(addr);
inet_ntoa(addr,str); // Dotted Decimal Format convert
PutString("\r\nSUBNET MASK : "); PutStringLn(str);
GetGWAddress(addr);
inet_ntoa(addr,str);
PutString("G/W IP ADDRESS : ");PutStringLn(str);
GetIPAddress(addr);
inet_ntoa(addr,str);
PutString("LOCAL IP ADDRESS : "); PutStringLn(str);
PutStringLn("====================================");
}
/*
###############################################################################
Unused Function Implementation Part
###############################################################################
*/
#ifndef __UNUSED_SOCK_UTIL__
/*
********************************************************************************
* Output destination IP address of appropriate channel
*
* Description : Output destination IP address of appropriate channel
* Arguments : s - Channel number which try to get destination IP Address
* addr - Buffer address to store destination IP address
* Returns : None
* Note : API Function
* Output format is written in Hexadecimal.
********************************************************************************
*/
char* GetDestAddr(SOCKET s,u_char* addr)
{
IndirectReadBuf(DST_IP_PTR(s),addr,4);
return addr;
}
/*
Description : Converts a string containing an (Ipv4) Internet Protocol decimal dotted address into a 32bit address
Argument : addr - dotted notation address string.
Return Value : 32bit address
Note :
*/
u_long inet_addr(u_char* addr)
{
char i;
char Num[4];
u_long inetaddr;
u_char * paddr = (char*)&inetaddr;
char * NextTok = addr;
for(i = 0; i < 4; i++)
{
NextTok = StrTok(NextTok,'.',Num);
*(paddr+i) = ATOI(Num,10);
}
return inetaddr;
}
/*
Description : Calculate checksum of a stream
Argument : src - pointer to stream
len - size of stream
Return Value : checksum
Note :
*/
u_int checksum(u_char * src, u_int len)
{
u_int sum, tsum, i, j;
u_long lsum;
j = len >> 1;
lsum = 0;
for (i = 0; i < j; i++)
{
tsum = src[i * 2];
tsum = tsum << 8;
tsum += src[i * 2 + 1];
lsum += tsum;
}
if (len % 2)
{
tsum = src[i * 2];
lsum += tsum;
}
sum = lsum;
sum = ~(sum + (lsum >> 16));
return sum;
}
/*
Description : Get handle of socket which status is same to 'status'
Argument : status - socket's status to be found
start - base of socket to be found
Return Value : socket number
Note :
*/
SOCKET getSocket(u_char status, SOCKET start)
{
SOCKET i;
if(start == -1 || start > 3) start = 0;
for(i = start; i < start + MAX_SOCK_NUM ; i++)
if(select(i%MAX_SOCK_NUM,SEL_CONTROL)==status) return i%MAX_SOCK_NUM;
return -1;
}
/*
Description : Verify decimal dotted notation IP address string
Argument : src - pointer to IP address string
Return Value : success - 1, fail - -1
Note :
*/
char VerifyIPAddress(char* src)
{
char i;
int tnum;
char Num[4];
char * NextTok = src;
for(i = 0; i < 4; i++)
{
NextTok = StrTok(NextTok,'.',Num);
if (NextTok == 0 && i != 3) return -1;
if(ValidATOI(Num,10,&tnum)==-1) return -1;
if(tnum < 0 && tnum > 255) return -1;
}
return 1;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -