📄 attsock.c
字号:
static DNS_INQUERY *gpInqueryHead = NULL;
static unsigned int MakeHostent(char* hostname, long addr, char *buf, long *len);
static DNS_INQUERY *Sock_GetDNSQueryByHandle(unsigned int hAsynHandle);
//static DNS_INQUERY *Sock_GetDNSQueryByLookupID(kal_int16 lookup_id);
long WSAAsyncGetHostByName(void *hWnd, unsigned int uMsg,
char *name, char *buf, long buflen)
{
long addr = 0;
static kal_int16 sDNSHandle = 0; /* 底层请求标识 */
static unsigned int AsynHandle = 1;
int message = 0;
kal_int8 result = 0;
kal_uint8 addrlen = IN_ADDR_LEN;
kal_int32 accountid = Get_Dialer_id();
if (hWnd == NULL
|| name == NULL || name[0] == '\0'
|| buf == NULL || buflen <= 0)
{
MsgOut("[gWinSock] - fail to call WSAAsyncGetHostByName(), "
"with invalid paramters, name=%s\r\n",
name == NULL ? "<null>" : name);
SockSetError(EINVALIDPARA);
return (-1);
}
if ((addr = inet_addr(name)) != INADDR_NONE)
{
message = MakeHostent(name, addr, buf, &buflen);
if (message == NOBUF_ERROR)
message = (message << 16) | buflen;
else
message = (message << 16);
PlxPostMessage(hWnd, uMsg, AsynHandle, message);
MsgOut("[gWinSock] + WSAAsyncGetHostByName(), succeed hanle = %d", AsynHandle);
return AsynHandle++;
}
/* 调用MTK域名查询函数 */
result = soc_gethostbyname(KAL_FALSE, MOD_MMI, (kal_int16)sDNSHandle,
(const kal_char *)name, (kal_uint8 *)(&addr), &addrlen, 0, accountid);
if (result == SOC_SUCCESS)
{
MsgOut("[gWinSock] + WSAAsyncGetHostByName() call soc_gethostbyname() OK, %d\r\n", AsynHandle);
message = MakeHostent(name, addr, buf, &buflen);
if (message == NOBUF_ERROR)
message = (message << 16) | buflen;
else
message = (message << 16);
PlxPostMessage(hWnd, uMsg, (WPARAM)(AsynHandle), message);
}
else if (result == SOC_WOULDBLOCK)
{
/* a DNS query is pending, we need costruct a gpInqueryHead object */
long size = sizeof(DNS_INQUERY) + strlen(name) + 1;
DNS_INQUERY *inq = (DNS_INQUERY*)malloc(size);
SetSocketEventHandler(get_hostname_ind, MSG_ID_APP_SOC_GET_HOST_BY_NAME_IND);
if (inq == NULL)
{
SockSetError(ENOMEMORY);
return (-1);
}
memset(inq, 0, size);
inq->next = gpInqueryHead;
inq->hWnd = hWnd;
inq->uMsg = uMsg;
inq->hAsync = AsynHandle;
inq->lookup_id = sDNSHandle++;
inq->hostname = (char*)inq + sizeof(DNS_INQUERY);
inq->hostent = buf;
inq->hostentlen = buflen;
strcpy(inq->hostname, name);
gpInqueryHead = inq;
MsgOut("[gWinSock] + WSAAsyncGetHostByName() call gethostbyname(), asynchandle=%d\r\n", AsynHandle);
}
else
{
MsgOut("[gWinSock] - WSAAsyncGetHostByName() fail to call soc_gethostbyname(), errno=%d\r\n", result);
SockSetError(EUNKNOWERROR);
return (-1);
}
return AsynHandle++;
}
long WSAAsyncGetHostByAddr(void *hWnd, unsigned int uMsg,
char* addr, long len, long type,
char* buf, long buflen)
{
MsgOut("[gWinSock] - WSAAsyncGetHostByAddr() not supported %d!\r\n", -1);
SockSetError(EOPNOTSUPP);
return (-1);
}
int WSACancelAsyncRequest(unsigned int hAsynHandle)
{
DNS_INQUERY *inq = Sock_GetDNSQueryByHandle(hAsynHandle);
if (inq == NULL)
{
MsgOut("[gWinSock] - WSACancelAsyncRequest() fail to cancel a unknown task, "
"asynchandle=%d\r\n", hAsynHandle);
SockSetError(EINVAL);
return SOCKET_ERROR;
}
MsgOut("[gWinSock] + WSACancelAsyncRequest() cancel asynchandle=%d\r\n", hAsynHandle);
free(inq);
return 0;
}
/* make hostent data structure */
static unsigned int MakeHostent(char *hostname, long addr, char *buf, long *len)
{
struct hostent *pRet = NULL;
long olen, off = 0;
/* figure out the length of hostent data */
off += sizeof(struct hostent);
/* 3 = NULL + h_addr_list[0] + NULL */
off += 3 * sizeof(char*);
off += strlen(hostname) + 1 + IN_ADDR_LEN;
olen = *len;
*len = off;
if (olen < off)
{
return NOBUF_ERROR;
}
/* fill in HOSTENT structure */
pRet = (struct hostent*)buf;
memset(pRet, 0, *len);
pRet->h_addrtype = AF_INET;
pRet->h_length = IN_ADDR_LEN;
off = sizeof(struct hostent);
pRet->h_aliases = (char** )((char*)pRet + off);
/* NULL */
off += sizeof(char*);
pRet->h_addr_list = (char**)((char*)pRet + off);
/* h_addr_list[0] + NULL */
off += 2 * sizeof( char* );
pRet->h_addr_list[0] = (char*)((char*)pRet + off);
/* addr */
off += IN_ADDR_LEN;
pRet->h_name = (char*)pRet + off;
strcpy(pRet->h_name, hostname);
memcpy(pRet->h_addr_list[0], &addr, IN_ADDR_LEN);
return NO_ERROR;
}
/* Pull out a gpInqueryHead instance from gpInqueryHead queue
* by aysnchronous tasking handle. */
static DNS_INQUERY *Sock_GetDNSQueryByHandle(unsigned int hAsynHandle)
{
DNS_INQUERY *prv = NULL, *inq = gpInqueryHead;
while (inq != NULL)
{
if (inq->hAsync == hAsynHandle)
break;
prv = inq;
inq = inq->next;
}
if (inq == NULL)
{
return NULL;
}
if (prv == NULL)
gpInqueryHead = inq->next;
else
prv->next = inq->next;
return inq;
}
#if 1
static DNS_INQUERY *Sock_GetDNSQueryByLookupID(kal_int16 lookup_id)
{
DNS_INQUERY *prv = NULL, *inq = gpInqueryHead;
while (inq != NULL)
{
if (inq->lookup_id == lookup_id)
break;
prv = inq;
inq = inq->next;
}
if (inq == NULL)
return NULL;
if (prv == NULL)
gpInqueryHead = inq->next;
else
prv->next = inq->next;
return inq;
}
#endif
static void get_hostname_ind(void* inMsg)
{
int message = 0;
kal_int16 error = 0;
app_soc_get_host_by_name_ind_struct *dns_ind;
DNS_INQUERY *pdns = NULL;
#ifdef SOCKET_MSGOUT
MsgOut("[gWinSock] + get_hostname_ind\r\n");
#endif
if(!inMsg)
{
return;
}
dns_ind = (app_soc_get_host_by_name_ind_struct *) inMsg;
pdns = Sock_GetDNSQueryByLookupID((kal_int16)dns_ind->request_id);
if (pdns == NULL)
{
return;
}
#ifdef SOCKET_MSGOUT
MsgOut("[gWinSock] + get_hostname_ind(result=%d)", dns_ind->result);
#endif
/* Check if the result is OK */
if(dns_ind->result==KAL_TRUE)
{
long ipaddr = *((long* )(dns_ind->addr));
message = MakeHostent(pdns->hostname, ipaddr, pdns->hostent, &pdns->hostentlen);
#ifdef SOCKET_MSGOUT
MsgOut("[gWinSock] + get_hostname_ind(addr=%d.%d.%d.%d)",
dns_ind->addr[0],dns_ind->addr[1],dns_ind->addr[2],dns_ind->addr[3]);
#endif
}
else
{
error = SERVER_FAIL;
}
#ifdef SOCKET_MSGOUT
MsgOut("[gWinSock] + get_hostname_ind(handle=%d), message =%d",
pdns->hAsync, message);
#endif
PlxPostMessage(pdns->hWnd, pdns->uMsg, (WPARAM)(pdns->hAsync), message);
}
int LIB_GetError(void)
{
return WSAGetLastError();
}
int LIB_SetError(int error)
{
WSASetLastError(error);
return 0;
}
int LIB_GetSysClock(void)
{
return 1;
}
/************************************************************************/
/* resolve socket conflict problems */
/************************************************************************/
#define MAX_SOC_EVENTS 2
#define MAX_SOC_NOTIFY 16
typedef struct tagEventNode
{
U16 eventID;
int count;
PsFuncPtr callbacks[MAX_SOC_NOTIFY];
} EVENTNODE, *PEVENTNODE;
EVENTNODE gSockEventQueue[MAX_SOC_EVENTS];
#define ValidNotifyID(id) \
(((id >> 8) & 0xff) < MAX_SOC_EVENTS) && ((id & 0xff) <= MAX_SOC_NOTIFY) && ((id & 0xff) > 0)
#define makeNotifyID(eid,nid) ((eid<<8 & 0xff00) | ((nid & 0xff) + 1))
void global_soc_ind_hdlr (void * msg);
void global_get_host_hdlr (void * msg);
static int GetEventQueue (U16 eventID);
/************************************************************************/
/* Clear all socket event handler can be used in init proc */
/************************************************************************/
void ClearAllSocketEventHandler (void)
{
#ifdef SOCKET_MSGOUT
PlxTrace ("[ClearAllSocketEventHandler]");
#endif
memset (gSockEventQueue, 0, sizeof(gSockEventQueue));
SetProtocolEventHandler (global_soc_ind_hdlr, MSG_ID_APP_SOC_NOTIFY_IND);
SetProtocolEventHandler (global_get_host_hdlr, MSG_ID_APP_SOC_GET_HOST_BY_NAME_IND);
}
/************************************************************************/
/* Clear an specific event handler by the identification returned before */
/************************************************************************/
void ClearSocketEventHandler (U16 notifID)
{
U8 idxEvt, idxNtf;
PsFuncPtr func;
if ( !ValidNotifyID(notifID) )
return;
idxEvt = (notifID >> 8) & 0xff;
idxNtf = (notifID & 0xff) - 1;
func = gSockEventQueue[idxEvt].callbacks[idxNtf];
if ( func != NULL ) {
gSockEventQueue[idxEvt].callbacks[idxNtf] = NULL;
gSockEventQueue[idxEvt].count --;
}
}
/************************************************************************/
/* Set event handler this handler will be kept in the queue */
/************************************************************************/
U16 SetSocketEventHandler (PsFuncPtr funcPtr, U16 eventID)
{
int i, fidx = -1, idx;
PEVENTNODE node = NULL;
#ifdef SOCKET_MSGOUT
PlxTrace ("[SetSocketEventHandler] (funcPtr[%x] eventID[%d])", funcPtr, eventID);
#endif
/* no null callback allowed if want to remove eventhandler use ClearSocketEventHandler */
if ( funcPtr == NULL )
return 0;
/* we now only support these two events */
if ( eventID != MSG_ID_APP_SOC_NOTIFY_IND && eventID != MSG_ID_APP_SOC_GET_HOST_BY_NAME_IND )
return 0;
idx = GetEventQueue (eventID);
#ifdef SOCKET_MSGOUT
PlxTrace ("[SetSocketEventHandler] eventID[%d]", idx);
#endif
if ( idx < 0 )
return 0;
node = &gSockEventQueue[idx];
#ifdef SOCKET_MSGOUT
PlxTrace ("[SetSocketEventHandler] node[%x]", node);
#endif
node->eventID = eventID;
if ( node->count == 0 ) {
fidx = 0;
}
else {
for ( i = 0; i < MAX_SOC_NOTIFY; i++ ) {
if ( node->callbacks[i] == NULL && fidx < 0 )
fidx = i;
if ( node->callbacks[i] == funcPtr ) { // same notify user
SetProtocolEventHandler (global_soc_ind_hdlr, MSG_ID_APP_SOC_NOTIFY_IND);
SetProtocolEventHandler (global_get_host_hdlr, MSG_ID_APP_SOC_GET_HOST_BY_NAME_IND);
return makeNotifyID (idx,i);
}
}
}
if ( fidx >= 0 ) {
node->callbacks[fidx] = funcPtr;
node->count ++;
SetProtocolEventHandler (global_soc_ind_hdlr, MSG_ID_APP_SOC_NOTIFY_IND);
SetProtocolEventHandler (global_get_host_hdlr, MSG_ID_APP_SOC_GET_HOST_BY_NAME_IND);
return makeNotifyID (idx, fidx);
}
return 0;
}
static int GetEventQueue (U16 eventID)
{
int i, fidx = -1;
for ( i = 0; i < MAX_SOC_EVENTS; i++ ) {
if ( gSockEventQueue[i].count == 0 && fidx < 0 )
fidx = i;
if ( gSockEventQueue[i].eventID == eventID )
break;
}
if ( i < MAX_SOC_EVENTS )
return i;
if ( fidx >= 0 )
return fidx;
return -1;
}
void global_soc_ind_hdlr (void *msg)
{
int idx, i;
PEVENTNODE node;
idx = GetEventQueue (MSG_ID_APP_SOC_NOTIFY_IND);
if ( idx < 0 )
return;
node = &gSockEventQueue[idx];
for ( i = 0; i < MAX_SOC_NOTIFY; i++ ) {
if ( node->callbacks[i] != NULL ) {
#ifdef SOCKET_MSGOUT
PlxTrace ("[global_soc_ind_hdlr] notify to [%x]", node->callbacks[i]);
#endif
node->callbacks[i] (msg);
}
}
}
void global_get_host_hdlr (void * msg)
{
int idx, i;
PEVENTNODE node;
idx = GetEventQueue (MSG_ID_APP_SOC_GET_HOST_BY_NAME_IND);
if ( idx < 0 )
return;
node = &gSockEventQueue[idx];
for ( i = 0; i < MAX_SOC_NOTIFY; i++ ) {
if ( node->callbacks[i] != NULL ) {
node->callbacks[i] (msg);
}
}
}
static U16 gCnectRef;
/************************************************************************/
/* should be called after connection established */
/************************************************************************/
void NetworkConnect (void)
{
gCnectRef ++;
}
/************************************************************************/
/* should be called instead of system network disconnection func */
/************************************************************************/
#define MSG_FAKE_DEACTIVE 0x400
static PsFuncPtr gSocDeactivateBack;
static int fakeDeactiveProc (void *, int, unsigned long, unsigned long);
void NetworkDisconnect (PsFuncPtr callback)
{
if ( gCnectRef == 0 )
return;
#ifdef MMI_ON_HARDWARE_P
if ( --gCnectRef == 0 ) {
SetProtocolEventHandler (callback, MSG_ID_APP_SOC_DEACTIVATE_CNF);
soc_close_nwk_account (MOD_MMI);
}
else {
gSocDeactivateBack = callback;
PlxPostMessage ((void *)fakeDeactiveProc, MSG_FAKE_DEACTIVE, 0, 0);
}
#else
gSocDeactivateBack = callback;
PlxPostMessage ((void *)fakeDeactiveProc, MSG_FAKE_DEACTIVE, 0, 0);
#endif
}
static int fakeDeactiveProc (void * hwnd, int msg, unsigned long wparam, unsigned long lparam)
{
if ( msg == MSG_FAKE_DEACTIVE && gSocDeactivateBack != NULL ) {
app_soc_deactivate_cnf_struct cnf;
memset (&cnf, 0, sizeof(cnf));
cnf.result = 1;
gSocDeactivateBack ((void*)&cnf);
gSocDeactivateBack = NULL;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -