Example #1
0
void test_recv_client(cyg_addrword_t pnetdata)
{
	int s, readlen;
	struct sockaddr_in sa, r_sa;
	struct hostent *hp;
	
	int threadid;
		
	int port = ((TEST_RECV_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_RECV_DATA_T*)pnetdata)->pbuf;
	char *precvbuf = ((TEST_RECV_DATA_T*)pnetdata)->precvbuf;
	
	threadid = cyg_thread_self();
	
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		test_printf_error("test_recv_client");
		cyg_thread_exit();
	}
	
	memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	
	memcpy(&(sa.sin_addr), hp->h_addr_list0, hp->h_length);
	sa.sin_family = AF_INET;
	sa.sin_port = htons(IPPORT_USERRESERVED + port - TEST_RECV_SERVER_NUM);
	
	if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_recv_client");
		cyg_thread_exit();
	}
	
	if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_recv_client");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		netclose(s, pbuf, RNT_BUFFER_LEN);
		test_printf_error("test_recv_client");
		cyg_thread_exit();
	}

	if(connect(s, (struct sockaddr*)&sa, sizeof(sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		netclose(s, pbuf, RNT_BUFFER_LEN);
		test_printf_error("test_recv_client");
		cyg_thread_exit();
	}
	
	readlen = recv(s, precvbuf, TEST_RECV_MSG_LEN, 0, pbuf, RNT_BUFFER_LEN);
	netclose(s, pbuf, RNT_BUFFER_LEN);
	cyg_thread_exit();
}
Example #2
0
int kftp_reconnect(knetFile *ftp)
{
	if (ftp->ctrl_fd != -1) {
		netclose(ftp->ctrl_fd);
		ftp->ctrl_fd = -1;
	}
	netclose(ftp->fd);
	ftp->fd = -1;
	return kftp_connect(ftp);
}
Example #3
0
int khttp_connect_file(knetFile *fp)
{
	int ret, l = 0;
	char *buf, *p;
	if (fp->fd != -1) netclose(fp->fd);
	fp->fd = socket_connect(fp->host, fp->port);
	buf = (char*)calloc(0x10000, 1); // FIXME: I am lazy... But in principle, 64KB should be large enough.
	l += sprintf(buf + l, "GET %s HTTP/1.0\r\nHost: %s\r\n", fp->path, fp->http_host);
    l += sprintf(buf + l, "Range: bytes=%lld-\r\n", (long long)fp->offset);
	l += sprintf(buf + l, "\r\n");
	if ( netwrite(fp->fd, buf, l) != l ) { free(buf); return -1; }
	l = 0;
	while (netread(fp->fd, buf + l, 1)) { // read HTTP header; FIXME: bad efficiency
		if (buf[l] == '\n' && l >= 3)
			if (strncmp(buf + l - 3, "\r\n\r\n", 4) == 0) break;
		++l;
	}
	buf[l] = 0;
	if (l < 14) { // prematured header
		free(buf);
		netclose(fp->fd);
		fp->fd = -1;
		return -1;
	}
	ret = strtol(buf + 8, &p, 0); // HTTP return code
	if (ret == 200 && fp->offset>0) { // 200 (complete result); then skip beginning of the file
		off_t rest = fp->offset;
		while (rest) {
			off_t l = rest < 0x10000? rest : 0x10000;
			rest -= my_netread(fp->fd, buf, l);
		}
	} else if (ret != 206 && ret != 200) {
		// failed to open file
		free(buf);
		netclose(fp->fd);
		switch (ret) {
		case 401: errno = EPERM; break;
		case 403: errno = EACCES; break;
		case 404: errno = ENOENT; break;
		case 407: errno = EPERM; break;
		case 408: errno = ETIMEDOUT; break;
		case 410: errno = ENOENT; break;
		case 503: errno = EAGAIN; break;
		case 504: errno = ETIMEDOUT; break;
		default:  errno = (ret >= 400 && ret < 500)? EINVAL : EIO; break;
		}
		fp->fd = -1;
		return -1;
	}
	free(buf);
	fp->is_ready = 1;
	return 0;
}
Example #4
0
// place ->fd at offset off
int kftp_connect_file(knetFile *fp)
{
	int ret;
	long long file_size;
	if (fp->fd != -1) {
		netclose(fp->fd);
		if (fp->no_reconnect) kftp_get_response(fp);
	}
	kftp_pasv_prep(fp);
    kftp_send_cmd(fp, fp->size_cmd, 1);
#ifndef _WIN32
    if ( sscanf(fp->response,"%*d %lld", &file_size) != 1 )
    {
        if(!knetsilent)
        {
            fprintf(stderr,"[kftp_connect_file] %s\n", fp->response);
        }
        return -1;
    }
#else
	const char *p = fp->response;
	while (*p != ' ') ++p;
	while (*p < '0' || *p > '9') ++p;
	file_size = strtoint64(p);
#endif
	fp->file_size = file_size;
	if (fp->offset>=0) {
		char tmp[32];
#ifndef _WIN32
		sprintf(tmp, "REST %lld\r\n", (long long)fp->offset);
#else
		strcpy(tmp, "REST ");
		int64tostr(tmp + 5, fp->offset);
		strcat(tmp, "\r\n");
#endif
		kftp_send_cmd(fp, tmp, 1);
	}
	kftp_send_cmd(fp, fp->retr, 0);
	kftp_pasv_connect(fp);
	ret = kftp_get_response(fp);
	if (ret != 150) {
            if(!knetsilent)
            {
		fprintf(stderr, "[kftp_connect_file] %s\n", fp->response);
            }
		netclose(fp->fd);
		fp->fd = -1;
		return -1;
	}
	fp->is_ready = 1;
	return 0;
}
Example #5
0
int khttp_connect_file(knetFile *fp)
{
	int ret, l = 0;
	char *buf, *p;
	ssize_t byteswritten;
	if (fp->fd != -1) netclose(fp->fd);
	fp->fd = socket_connect(fp->host, fp->port);
	buf = calloc(0x10000, 1); /* FIXME: I am lazy... But in principle, 64KB should be large enough. */
	l += sprintf(buf + l, "GET %s HTTP/1.0\r\nHost: %s\r\n", fp->path, fp->http_host);
    l += sprintf(buf + l, "Range: bytes=%" PRId64 "-\r\n", fp->offset);
	l += sprintf(buf + l, "\r\n");
	byteswritten = netwrite(fp->fd, buf, l);	/* @ubw fixed unused result warning */
	if( 0 >= byteswritten )			/* do sth with the result */
	{
		PRINT_ERROR( "[khttp_connect_file] no (%d) bytes written in GET request!\n", byteswritten);
		/* return -1;  */ /* cppcheck : [dev/src/tabix/knetfile.c:440]: (error) Memory leak: buf ||| free(buf) the buf=calloc() from line 432 ! */
		free( buf );
		return -1;
	}
	l = 0;
	while (netread(fp->fd, buf + l, 1)) { /* read HTTP header; FIXME: bad efficiency */
		if (buf[l] == '\n' && l >= 3)
			if (strncmp(buf + l - 3, "\r\n\r\n", 4) == 0) break;
		++l;
	}
	buf[l] = 0;
	if (l < 14) { /* prematured header */
		netclose(fp->fd);
		fp->fd = -1;
		return -1;
	}
	ret = strtol(buf + 8, &p, 0); /* HTTP return code */
	if (ret == 200 && fp->offset>0) { /* 200 (complete result); then skip beginning of the file */
		off_t rest = fp->offset;
		while (rest) {
			off_t l = rest < 0x10000? rest : 0x10000;
			rest -= my_netread(fp->fd, buf, l);
		}
	} else if (ret != 206 && ret != 200) {
		free(buf);
		PRINT_ERROR( "[khttp_connect_file] fail to open file (HTTP code: %d).\n", ret);
		netclose(fp->fd);
		fp->fd = -1;
		return -1;
	}
	free(buf);
	fp->is_ready = 1;
	return 0;
}
Example #6
0
BOOL InitNetInterface(const char *pcInterface)
{
	struct ifreq ifr;
	int fd;
	diag_printf("Initializing %s...\n", pcInterface);
#ifndef WLAN
	if ((fd = socket(AF_INET,SOCK_DGRAM,0,g_StreamServer_Buf, System_BUFFER_LEN)) < 0)
#else
	if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
#endif
	{
		fprintf(stderr, "errno = %d\n", errno);
		return FALSE;
	}
	//strcpy(ifr.ifr_name, pcInterface);
	snprintf(ifr.ifr_name,sizeof(ifr.ifr_name),pcInterface);

	clr_flag(fd, ifr.ifr_name, IFF_UP);
	set_flag(fd, ifr.ifr_name, (IFF_UP | IFF_RUNNING));
#ifndef WLAN
	netclose(fd, g_StreamServer_Buf, System_BUFFER_LEN);
#else
	close(fd);
#endif
	diag_printf("Initialize %s... over \n", pcInterface);
	return TRUE;
}
Example #7
0
BOOL GetWlanChannel(const char *pcInterface, int *piChannel)
{
	int rt;
	int fd;
	struct iwreq wrq;

	if (pcInterface == NULL || piChannel == NULL)
	{
		fprintf(stderr, "illegal call function GetWlanChannel!\n");
		return FALSE;
	}
#ifndef WLAN 
	if ((fd = socket(AF_INET,SOCK_DGRAM,0, g_StreamServer_Buf, System_BUFFER_LEN)) < 0) 
		return FALSE;
#else
	if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
		return FALSE;
#endif
	memset(&wrq, 0, sizeof(wrq));
	strcpy(wrq.ifr_name, pcInterface);
#ifndef WLAN 
	rt = netioctl(fd, SIOCGIWFREQ, &wrq,sizeof(wrq), g_StreamServer_Buf, System_BUFFER_LEN);
	netclose(fd, g_StreamServer_Buf, System_BUFFER_LEN);
#else
	rt = ioctl(fd, SIOCGIWFREQ, &wrq);
	close(fd);
#endif	

	if (rt >= 0)
	{
		*piChannel = wrq.u.freq.m;
		return TRUE;
	}
	else return FALSE;
}
Example #8
0
int mc_tx_socket(char *p, int plen, unsigned long addr)
{
    int st, sd;
    struct sockaddr_in sap;
#ifndef WLAN
    sd = socket(AF_INET, SOCK_DGRAM, 0, g_Mctest_Buf, MCTEST_BUFFER_LEN);   /* UDO/IP */
#else
    sd = socket(AF_INET, SOCK_DGRAM, 0);   /* UDO/IP */
#endif
    if (sd < 0) 
        return sd;

	if(addr==INADDR_BROADCAST)
	{
		int i1 = 1;
		setsockopt(sd, SOL_SOCKET, SO_BROADCAST, (char *)&i1, sizeof(i1));
	}

    memset(&sap, 0, sizeof(sap));
    sap.sin_family = AF_INET;
    sap.sin_port = htons(MCTESTPORT);
    sap.sin_addr.s_addr = addr;

    /* Transmit packet */
#ifndef WLAN
    st = sendto(sd, p, plen, 0, (struct sockaddr *)&sap, sizeof(sap), g_Mctest_Buf, MCTEST_BUFFER_LEN);
    netclose(sd, g_Mctest_Buf, MCTEST_BUFFER_LEN);
#else
    st = sendto(sd, p, plen, 0, (struct sockaddr *)&sap, sizeof(sap));
    close(sd);
#endif
    return st;
}
Example #9
0
/*
 * Name: GetWlanOperationMode
 * Description: Get wireless operation mode.
 *
 * Parameter:
 *  pcInterface[in]: Net interface for wireless lan.
 *  piMode[out]: Operation mode.
 * Return value:
 *  TRUE: Success.
 *  FALSE: Failed.
 */
BOOL GetWlanOperationMode(const char *pcInterface, int *piMode)
{
	int fd;
	int rt;
	struct iwreq wrq;
	

	if (pcInterface == NULL) return FALSE;

	//if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) return FALSE;
#ifndef WLAN	
	if ((fd = socket(AF_INET,SOCK_DGRAM,0, g_StreamServer_Buf, System_BUFFER_LEN)) < 0) 
		return FALSE;
#else
	if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) 
		return FALSE;

#endif
	memset(&wrq, 0, sizeof(wrq));

	strcpy(wrq.ifr_name, pcInterface);
#ifndef WLAN
	rt = netioctl(fd, SIOCGIWMODE, &wrq,sizeof(wrq), g_StreamServer_Buf, System_BUFFER_LEN);
    netclose(fd, g_StreamServer_Buf, System_BUFFER_LEN);
#else
	rt = ioctl(fd, SIOCGIWMODE, &wrq);
    close(fd);
#endif
    if (rt < 0
    	|| !(wrq.u.mode >= 0 && wrq.u.mode < sizeof(g_apcWlanOperationMode) / sizeof(const char *)))
    	return FALSE;

	if (piMode != NULL) *piMode = wrq.u.mode;
	return TRUE;
}
Example #10
0
int knet_close(knetFile *fp)
{
	if (fp == 0) return 0;
	if (fp->ctrl_fd != -1) netclose(fp->ctrl_fd); // FTP specific
	if (fp->fd != -1) {
		/* On Linux/Mac, netclose() is an alias of close(), but on
		 * Windows, it is an alias of closesocket(). */
		if (fp->type == KNF_TYPE_LOCAL) close(fp->fd);
		else netclose(fp->fd);
	}
	free(fp->host); free(fp->port);
	free(fp->response); free(fp->retr); // FTP specific
	free(fp->path); free(fp->http_host); // HTTP specific
	free(fp);
	return 0;
}
Example #11
0
void test_tcp_create_socket(char *pcBuf, int iBufLen)
{
	int iPortBegin = TEST_TCP_SOCKET_PORT_BEGIN;
	int iPortEnd = TEST_TCP_SOCKET_PORT_END;
	int port;
	int sockfd[TEST_TCP_SOCKET_PORT_END];
	int i;
	
	port = iPortBegin;
	while(port <= iPortEnd)
	{
		if((sockfd[port-1] = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pcBuf, iBufLen)) == -1)
		{
			printf("test_tcp_create_socket(): socket error\n");
			goto fail;
		}
		port++;
	}
	
fail:
	if(port > iPortEnd)
		test_printf_success("test_tcp_create_socket");
	for(i = iPortBegin; i <= port; i++)
	{
		netclose(sockfd[i-1], pcBuf, iBufLen);
	}
	return;
}
Example #12
0
int khttp_connect_file(knetFile *fp)
{
	int ret, l = 0;
	char *buf, *p;
	if (fp->fd != -1) netclose(fp->fd);
	fp->fd = socket_connect(fp->host, fp->port);
	buf = (char*)calloc(0x10000, 1); // FIXME: I am lazy... But in principle, 64KB should be large enough.
	l += sprintf(buf + l, "GET %s HTTP/1.0\r\nHost: %s\r\n", fp->path, fp->http_host);
    l += sprintf(buf + l, "Range: bytes=%lld-\r\n", (long long)fp->offset);
	l += sprintf(buf + l, "\r\n");
	if(netwrite(fp->fd, buf, l) != l)
	{
	}
	l = 0;
	while (netread(fp->fd, buf + l, 1)) { // read HTTP header; FIXME: bad efficiency
		if (buf[l] == '\n' && l >= 3)
			if (strncmp(buf + l - 3, "\r\n\r\n", 4) == 0) break;
		++l;
	}
	buf[l] = 0;
	if (l < 14) { // prematured header
		netclose(fp->fd);
		fp->fd = -1;
		return -1;
	}
	ret = strtol(buf + 8, &p, 0); // HTTP return code
	if (ret == 200 && fp->offset>0) { // 200 (complete result); then skip beginning of the file
		off_t rest = fp->offset;
		while (rest) {
			off_t l = rest < 0x10000? rest : 0x10000;
			rest -= my_netread(fp->fd, buf, l);
		}
	} else if (ret != 206 && ret != 200) {
		free(buf);
                if(!knetsilent)
                {
                    fprintf(stderr, "[khttp_connect_file] fail to open file (HTTP code: %d).\n", ret);
                }
		netclose(fp->fd);
		fp->fd = -1;
		return -1;
	}
	free(buf);
	fp->is_ready = 1;
	return 0;
}
Example #13
0
// place ->fd at offset off
int kftp_connect_file(knetFile *fp)
{
	int ret;
	long long file_size;
	if (fp->fd != -1) {
		netclose(fp->fd);
		if (fp->no_reconnect) kftp_get_response(fp);
	}
	kftp_pasv_prep(fp);
    kftp_send_cmd(fp, fp->size_cmd, 1);
#ifndef _WIN32
    // If the file does not exist, the response will be "550 Could not get file
    // size". Be silent on failure, hts_idx_load can be trying the existence of .csi or .tbi.
    if ( sscanf(fp->response,"%*d %lld", &file_size) != 1 ) return -1;
#else
	const char *p = fp->response;
	while (*p != ' ') ++p;
	while (*p < '0' || *p > '9') ++p;
	file_size = strtoint64(p);
#endif
	fp->file_size = file_size;
	if (fp->offset>=0) {
		char tmp[32];
#ifndef _WIN32
		sprintf(tmp, "REST %lld\r\n", (long long)fp->offset);
#else
		strcpy(tmp, "REST ");
		int64tostr(tmp + 5, fp->offset);
		strcat(tmp, "\r\n");
#endif
		kftp_send_cmd(fp, tmp, 1);
	}
	kftp_send_cmd(fp, fp->retr, 0);
	kftp_pasv_connect(fp);
	ret = kftp_get_response(fp);
	if (ret != 150) {
		fprintf(stderr, "[kftp_connect_file] %s\n", fp->response);
		netclose(fp->fd);
		fp->fd = -1;
		return -1;
	}
	fp->is_ready = 1;
	return 0;
}
Example #14
0
File: plan9.c Project: npe9/harvey
int
netopen(char *e)
{
	int r;

	if((r = netopen0(e)) >= 0)
		return r;
	perror("netopen");
	netclose();
	return -1;
}
Example #15
0
/*
 * Name: GetWlanWepKey
 * Description: Get wireless WEP key settings.
 *
 * Parameter:
 *  pcInterface[in]: Net interface for wireless lan.
 *  pAscKey[out]: Buffer length of which >= 14.
 *  piAscKeyLen[out]: The Length of valid buffer pointed by "pAscKey".
 *                    -1 if WEP key is disabled.
 *  piKeyIndex[out]: Index of WEP keys.
 * Return value:
 *  TRUE: Success.
 *  FALSE: Failed.
 */
BOOL GetWlanWepKey(const char *pcInterface,
					void *pAscKey,
					int *piAscKeyLen,
					int *piKeyIndex)
{
	int fd;
	int rt;
	unsigned char pucKey[IW_ENCODING_TOKEN_MAX];
	struct iwreq wrq;

	if (pcInterface == NULL) return FALSE;
#ifndef WLAN
	if ((fd = socket(AF_INET,SOCK_DGRAM,0, g_StreamServer_Buf, System_BUFFER_LEN)) < 0) 
		return FALSE;
#else
	if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) 
		return FALSE;
#endif
	memset(&wrq, 0, sizeof(wrq));

	strcpy(wrq.ifr_name, pcInterface);
	wrq.u.data.pointer = (caddr_t)pucKey;
	wrq.u.data.length = 0;
	wrq.u.data.flags = 0;
#ifndef WLAN
	rt = netioctl(fd, SIOCGIWENCODE, &wrq, sizeof(wrq),g_StreamServer_Buf, System_BUFFER_LEN);
    netclose(fd, g_StreamServer_Buf, System_BUFFER_LEN);
#else
	rt = ioctl(fd, SIOCGIWENCODE, &wrq);
    close(fd);
#endif
    if (rt >= 0)
    {
    	if (wrq.u.data.flags & IW_ENCODE_DISABLED)
    	{
    		if (piAscKeyLen != NULL) *piAscKeyLen = -1;
    	}
    	else
    	{
    		if (piAscKeyLen != NULL)
    		{
				/* iKeyIndex in ioctl from 1 to 4, while SetWlanWepKey use 0 to 3 */
    			*piAscKeyLen = wrq.u.data.length - 1;
    		}
    		if (pAscKey != NULL)
    			memcpy(pAscKey, pucKey, wrq.u.data.length);
    		if (piKeyIndex != NULL)
    			*piKeyIndex = wrq.u.encoding.flags & 0x000000FF;
    	}
    }

	return (rt >= 0 ? TRUE : FALSE);
}
Example #16
0
BOOL SetWlanESSID(const char *pcInterface, const char *pcWlanID)
{
	int rt;
	int fd;
	struct iwreq wrq;
	char ac[64];

	if (pcInterface == NULL)
	{
		fprintf(stderr, "illegal call function SetWlanESSID!\n");
		return FALSE;
	}

#ifndef WLAN
	if ((fd = socket(AF_INET,SOCK_DGRAM,0, g_StreamServer_Buf, System_BUFFER_LEN)) < 0) 
		return FALSE;
#else
	if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) 
		return FALSE;
#endif	
	memset(&wrq, 0, sizeof(wrq));
	strcpy(wrq.ifr_name, pcInterface);

	if (pcWlanID == NULL)
	{//essid: any
		ac[0] = '\0';
		wrq.u.essid.flags = 0;
	}
	else
	{
		httpMyStrncpy(ac, pcWlanID, sizeof(ac));
		wrq.u.essid.flags = 1;
	}

	wrq.u.essid.pointer = (caddr_t)ac;
	wrq.u.essid.length = strlen(ac) + 1;
	
#ifndef WLAN
	rt = netioctl(fd, SIOCSIWESSID, &wrq, sizeof(wrq),g_StreamServer_Buf, System_BUFFER_LEN);

	netclose(fd, g_StreamServer_Buf, System_BUFFER_LEN);
#else
	if((rt = ioctl(fd, SIOCSIWESSID, &wrq)) < 0)
		diag_printf("scan ESSID %s failed\n",pcWlanID);	
	else
		diag_printf("set ssid finished\n");
	
	close(fd);
#endif	
	return (rt >= 0 ? TRUE : FALSE);
}
Example #17
0
char *GetWlanESSID(const char *pcInterface, char *pcWlanID, int iWlanIDMaxLen)
{
	int fd;
	struct iwreq wrq;
	char ac[64];

	if (pcInterface == NULL || pcWlanID == NULL || iWlanIDMaxLen < 0)
	{
		fprintf(stderr, "illegal call function GetWlanESSID!\n");
		return NULL;
	}
#ifndef WLAN
	if ((fd = socket(AF_INET,SOCK_DGRAM,0, g_StreamServer_Buf, System_BUFFER_LEN)) < 0) 
		return NULL;
#else
	if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) 
		return NULL;
#endif
	/* Get network ID */
	memset(&wrq, 0, sizeof(wrq));
	strcpy(wrq.ifr_name, pcInterface);
	wrq.u.essid.pointer = (caddr_t)ac;
	wrq.u.essid.length = 0;
	wrq.u.essid.flags = 0;
#ifndef WLAN  
	if(netioctl(fd, SIOCGIWESSID, &wrq, sizeof(wrq),g_StreamServer_Buf, System_BUFFER_LEN) >= 0)
#else
	if(ioctl(fd, SIOCGIWESSID, &wrq) >= 0)
#endif
	{
		if (wrq.u.essid.flags == 0) pcWlanID[0] = '\0';
		else
		{
			if (wrq.u.essid.length < sizeof(ac))
				ac[wrq.u.essid.length] = '\0';
			else ac[sizeof(ac) - 1] = '\0';
			httpMyStrncpy(pcWlanID, ac, iWlanIDMaxLen);
		}
	}
	else pcWlanID[0] = '\0';
#ifndef WLAN 
	netclose(fd, g_StreamServer_Buf, System_BUFFER_LEN);
#else
	close(fd);
#endif
	return pcWlanID;
}
Example #18
0
void test_sendto_server(cyg_addrword_t pnetdata)
{
	int s, i, len, sendtolen;
	struct sockaddr_in sa;
	
	int threadid;
		
	int port = ((TEST_SENDTO_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_SENDTO_DATA_T*)pnetdata)->pbuf;
	char *psendtobuf = ((TEST_SENDTO_DATA_T*)pnetdata)->psendtobuf;
	
	threadid = port;
	
    if(inet_aton(TEST_SENDTO_SERVER_ADDR, &sa.sin_addr, pbuf, RNT_BUFFER_LEN) == 0)
    {
		test_printf_error("test_sendto_server");
		cyg_thread_exit();
    }
    sa.sin_family = AF_INET;
    sa.sin_port = htons(IPPORT_USERRESERVED + port);
    
	if((s = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_sendto_server");
		cyg_thread_exit();
	}
	
    for(i = 0; i < TEST_SENDTO_WRITE_TIMES; i++)
	{
		len = sprintf(psendtobuf, "%s", TEST_SENDTO_MSG);
		len++;
		sendtolen = sendto(s, psendtobuf, len, 0, 
							(struct sockaddr*)&sa, sizeof(sa), pbuf, RNT_BUFFER_LEN);
		if(sendtolen < 0)
		{
			test_printf_error("test_sendto_server");
			break;
		}
	}
	
	if(i == TEST_SENDTO_WRITE_TIMES)
		test_printf_success("test_sendto_server");
	
	netclose(s, pbuf, RNT_BUFFER_LEN);
	cyg_thread_exit();
}
Example #19
0
static int onHttpReboot_Safe (HTTPCONNECTION hConnection, time_t *tLastFill, void *pParam)
{
	request *req = (request *) hConnection;
#ifndef WLAN
	netclose (req->fd, g_StreamServer_Buf, System_BUFFER_LEN);
#else
	close(req->fd);
#endif	
#ifndef WLAN	
	WebCameraSIGTERM(0);
	diag_printf("Reboot the wb740!\n");
	wb740reboot(g_StreamServer_Buf, System_BUFFER_LEN);
#else
	WebCameraSIGTERM(0);
	W99802Reboot();
#endif

	return 0;
}
Example #20
0
void test_netioctl_entry(char *pBuf, int iBufLen)
{
	int fd;
	struct ifreq ifbuf;
	
	fd = socket(AF_INET, SOCK_STREAM, 0, pBuf, iBufLen);
	if(fd < 0)
	{
		test_printf_error("test_netioctl_entry");
		return;
	}
		
	if(netioctl(-1, SIOCSIFADDR, &ifbuf, sizeof(ifbuf), pBuf, iBufLen) >= 0)
	{
		test_printf_error("test_netioctl_entry");
		netclose(fd, pBuf, iBufLen);
		return;
	}
	
	if(netioctl(fd, SIOCSIFADDR, NULL, sizeof(ifbuf), pBuf, iBufLen) >= 0)
	{
		test_printf_error("test_netioctl_entry");
		netclose(fd, pBuf, iBufLen);
		return;
	}
	
	if(netioctl(fd, SIOCSIFADDR, &ifbuf, 0, pBuf, iBufLen) >= 0)
	{
		test_printf_error("test_netioctl_entry");
		netclose(fd, pBuf, iBufLen);
		return;
	}
	
	if(netioctl(fd, SIOCSIFADDR, &ifbuf, sizeof(ifbuf), NULL, iBufLen) >= 0)
	{
		test_printf_error("test_netioctl_entry");
		netclose(fd, pBuf, iBufLen);
		return;
	}
	
	if(netioctl(fd, SIOCSIFADDR, &ifbuf, sizeof(ifbuf), pBuf, 0) >= 0)
	{
		test_printf_error("test_netioctl_entry");
		netclose(fd, pBuf, iBufLen);
		return;
	}
	
	test_printf_success("test_netioctl_entry");
	netclose(fd, pBuf, iBufLen);
}
Example #21
0
/*
 * Name: SetWlanOperationMode
 * Description: Set wireless operation mode.
 *
 * Parameter:
 *  pcInterface[in]: Net interface for wireless lan.
 *  iMode[in]: Operation mode.
 * Return value:
 *  TRUE: Success.
 *  FALSE: Failed.
 */
BOOL SetWlanOperationMode(const char *pcInterface, int iMode)
{
	int fd;
	int rt;
	struct iwreq wrq;
	//int buffer_size = 1024;
	//char buffer_size[1024];

	if (pcInterface == NULL) return FALSE;
	if (!(iMode >= 0
		&& iMode < sizeof(g_apcWlanOperationMode) / sizeof(const char *)))
		return FALSE;

	if (iMode != 1 && iMode != 2)
	{
		fprintf(stderr, "Only Ad-Hoc and Managed modes supported.\n");
		return FALSE;
	}

	//if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) return FALSE;
#ifndef WLAN
	if ((fd = socket(AF_INET,SOCK_DGRAM,0, g_StreamServer_Buf, System_BUFFER_LEN)) < 0) 
		return FALSE;
#else
	if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) 
		return FALSE;
#endif
	memset(&wrq, 0, sizeof(wrq));

	strcpy(wrq.ifr_name, pcInterface);
	wrq.u.mode = iMode;
diag_printf("mode %d\n", iMode);
#ifndef WLAN
	rt = netioctl(fd, SIOCSIWMODE, &wrq,sizeof(wrq), g_StreamServer_Buf, System_BUFFER_LEN);
    netclose(fd, g_StreamServer_Buf, System_BUFFER_LEN);
#else
	rt = ioctl(fd, SIOCSIWMODE, &wrq);
    close(fd);
#endif
	return (rt >= 0 ? TRUE : FALSE);
}
Example #22
0
BOOL GetWlanLinkState(const char *pcInterface, int *piState)
{
	int fd;
	int rt;
	int iState;
	struct iwreq wrq;

	if (pcInterface == NULL || piState == NULL) return FALSE;
#ifndef WLAN
	if ((fd = socket(AF_INET,SOCK_DGRAM,0, g_StreamServer_Buf, System_BUFFER_LEN)) < 0) 
		return FALSE;

#else
	if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) 
		return FALSE;

#endif
	memset(&wrq, 0, sizeof(wrq));

	strcpy(wrq.ifr_name, pcInterface);
	wrq.u.data.pointer = (caddr_t)&iState;
   	wrq.u.data.length = sizeof(int);
#ifndef WLAN
   	rt = netioctl(fd, SIOCIWFIRSTPRIV + 0x9, &wrq,sizeof(wrq), g_StreamServer_Buf, System_BUFFER_LEN);
   	netclose(fd, g_StreamServer_Buf, System_BUFFER_LEN);
#else
   	rt = ioctl(fd, SIOCIWFIRSTPRIV + 0x9, &wrq);
   	close(fd);
#endif


	if (rt >= 0)
	{
		*piState = iState;
		return TRUE;
	}
	else return FALSE;
}
Example #23
0
prism_cnfDesireSSID_t *GetWlanESSIDList(const char *pcInterface, int *piWlanESSIDNum)
{
	int fd;
	//char ac[64];
	int iApNum;
	prism_cnfDesireSSID_t *pRt;

	if (pcInterface == NULL || piWlanESSIDNum == NULL)
	{
		fprintf(stderr, "illegal call function GetWlanESSIDList!\n");
		return NULL;
	}
#ifndef WLAN
	if ((fd = socket(AF_INET,SOCK_DGRAM,0, g_StreamServer_Buf, System_BUFFER_LEN)) < 0) 
		return NULL;
#else
	if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) 
		return NULL;
#endif

	if (ScanWlanAp(pcInterface, fd))
	{
		pRt = ReadWlanESSIDList(pcInterface, fd, &iApNum);

		if (pRt == NULL) *piWlanESSIDNum = 0;
		else *piWlanESSIDNum = iApNum;
	}
	else PTE;
#ifndef WLAN
	netclose(fd, g_StreamServer_Buf, System_BUFFER_LEN);
#else
	close(fd);
#endif
	

	return pRt;
}
Example #24
0
unsigned long GetGeneralIP(const char *pcInterface, int iRequest)
{
	struct ifreq ifr;
	int fd;
	unsigned long ulRt;

	if (pcInterface == NULL)
	{
		fprintf(stderr, "illegal call function GetGeneralIP!\n");
		return 0L;
	}
#ifndef WLAN
	if ((fd = socket(AF_INET,SOCK_DGRAM,0, g_StreamServer_Buf, System_BUFFER_LEN)) < 0) 
		return 0L;
#else	
	if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) 
		return 0L;
#endif
	//strcpy(ifr.ifr_name, pcInterface);
	snprintf(ifr.ifr_name,sizeof(ifr.ifr_name),pcInterface);

#ifndef WLAN
	if (netioctl(fd, iRequest, &ifr,sizeof(ifr), g_StreamServer_Buf, System_BUFFER_LEN) < 0)
#else
	if (ioctl(fd, iRequest, &ifr) < 0)
#endif
		ulRt = 0;
	else
		ulRt = (*(struct sockaddr_in *)&(ifr.ifr_addr)).sin_addr.s_addr;
#ifndef WLAN		
	netclose(fd, g_StreamServer_Buf, System_BUFFER_LEN);
#else
	close(fd);
#endif
	return ulRt;
}
Example #25
0
void test_tcp_socket_server(cyg_addrword_t pnetdata)
{
	int s, new_s, i;
	struct sockaddr_in sa, r_sa;
	int r_sa_l = sizeof(r_sa);
	struct hostent *hp;
	
	int threadid;
		
	int port = ((TEST_TCP_SOCKET_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_TCP_SOCKET_DATA_T*)pnetdata)->pbuf;
	
	threadid = cyg_thread_self();
	
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		test_printf_error("test_tcp_socket_server");
		cyg_thread_exit();
	}
	
	memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	
	if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_tcp_socket_server");
		cyg_thread_exit();
	}
	
	if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_tcp_socket_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_tcp_socket_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}

	if(listen(s, 10, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_tcp_socket_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	for(i = 0; i < TEST_TCP_ACCEPT_SOCKET_TIMES; i++)
	{
		if((new_s = accept(s, (struct sockaddr*)&sa, (size_t*)&r_sa_l, pbuf, RNT_BUFFER_LEN)) == -1)
		{
			test_printf_error("test_tcp_socket_server");
			netclose(s, pbuf, RNT_BUFFER_LEN);
			cyg_thread_exit();
		}
		netclose(new_s, pbuf, RNT_BUFFER_LEN);
	}
	test_printf_success("test_tcp_socket_server");
	
	netclose(s, pbuf, RNT_BUFFER_LEN);
	cyg_thread_exit();
}
Example #26
0
void test_recv_server(cyg_addrword_t pnetdata)
{
	int s, new_s;
	struct sockaddr_in sa, r_sa;
	int r_sa_l = sizeof(r_sa);
	struct hostent *hp;
	
	int threadid;
		
	int port = ((TEST_RECV_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_RECV_DATA_T*)pnetdata)->pbuf;
	char *precvbuf = ((TEST_RECV_DATA_T*)pnetdata)->precvbuf;
	
	threadid = cyg_thread_self();
	
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		test_printf_error("test_recv_server");
		cyg_thread_exit();
	}
	
	memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	
	if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		test_printf_error("test_recv_server");
		cyg_thread_exit();
	}
	
	if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
	{
		test_printf_error("test_recv_server");
		netclose(s, pbuf, RNT_BUFFER_LEN);
		cyg_thread_exit();
	}
	
	if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
	{
		netclose(s, pbuf, RNT_BUFFER_LEN);
		test_printf_error("test_recv_server");
		cyg_thread_exit();
	}

	if(listen(s, 10, pbuf, RNT_BUFFER_LEN) == -1)
	{
		netclose(s, pbuf, RNT_BUFFER_LEN);
		test_printf_error("test_recv_server");
		cyg_thread_exit();
	}
	
	if((new_s = accept(s, (struct sockaddr*)&sa, (size_t*)&r_sa_l, pbuf, RNT_BUFFER_LEN)) == -1)
	{
		netclose(s, pbuf, RNT_BUFFER_LEN);
		test_printf_error("test_recv_server");
		cyg_thread_exit();
	}
	
	if(recv(-1, precvbuf, TEST_RECV_MSG_LEN, 0, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	
	if(recv(new_s, NULL, TEST_RECV_MSG_LEN, 0, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	
	// recv 0 will block and wait for client's write. (But netread wouldn't)
	/*
	if(recv(new_s, precvbuf, 0, 0, pbuf, RNT_BUFFER_LEN) != 0)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	*/
	
	if(recv(new_s, precvbuf, -1, 0, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	
	if(recv(new_s, precvbuf, TEST_RECV_MSG_LEN, -1, pbuf, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	
	if(recv(new_s, precvbuf, TEST_RECV_MSG_LEN, 0, NULL, RNT_BUFFER_LEN) != -1)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	
	if(recv(new_s, precvbuf, TEST_RECV_MSG_LEN, 0, pbuf, 0) != -1)
	{
		test_printf_error("test_recv_server");
		goto fail;
	}
	
	test_printf_success("test_recv_server");
	
fail:
	netclose(new_s, pbuf, RNT_BUFFER_LEN);
	netclose(s, pbuf, RNT_BUFFER_LEN);
	cyg_thread_exit();
}
Example #27
0
void test_netioctl_withbuf_entry(char *pBuf, int iBufLen)
{
	struct rtentry rtitem;
	int dst, mask, gateway;
    int i, fd;
    
	fd = socket(AF_INET, SOCK_DGRAM, 0, pBuf, iBufLen);
	if(fd < 0)
	{
		test_printf_error("test_netioctl_withbuf_entry");
		return;
	}
 		
	memset(&rtitem, 0, sizeof(rtitem));
	if(inet_aton(TEST_NETIOCTL_WITH_BUF_DST, (struct in_addr*)&dst, pBuf, iBufLen) == 0)
	{
		test_printf_error("test_netioctl_withbuf_entry");
 		netclose(fd, pBuf, iBufLen);
		return;
	}
	if(inet_aton(TEST_NETIOCTL_WITH_BUF_MASK, (struct in_addr*)&mask, pBuf, iBufLen) == 0)
	{
		test_printf_error("test_netioctl_withbuf_entry");
 		netclose(fd, pBuf, iBufLen);
		return;
	}
	if(inet_aton(TEST_NETIOCTL_WITH_BUF_GATEWAY, (struct in_addr*)&gateway, pBuf, iBufLen) == 0)
	{
		test_printf_error("test_netioctl_withbuf_entry");
 		netclose(fd, pBuf, iBufLen);
		return;
	}
 		
	set_sockaddr((struct sockaddr_in *) &rtitem.rt_dst, dst, 0);
	set_sockaddr((struct sockaddr_in *) &rtitem.rt_genmask, mask, 0);
	set_sockaddr((struct sockaddr_in *) &rtitem.rt_gateway, gateway, 0);

	rtitem.rt_flags = RTF_UP | RTF_GATEWAY;
	rtitem.rt_dev = "eth1";
 		
	if(netioctl_withbuf(fd, SIOCDELRT, &rtitem, sizeof(rtitem), pBuf, iBufLen) < 0)
	{
		test_printf_error("test_netioctl_withbuf_entry");
 		netclose(fd, pBuf, iBufLen);
		return;
	}
	
	/*
	printf("To check with \"route\" command in 5 seconds");
	for(i = 0; i < 10; i++)
	{
		printf(".");
		tt_msleep(1000);
	}
	printf("\n");
	*/
	
	if(netioctl_withbuf(fd, SIOCADDRT, &rtitem,sizeof(rtitem), pBuf, iBufLen) < 0)
	{
		test_printf_error("test_netioctl_withbuf_entry");
 		netclose(fd, pBuf, iBufLen);
		return;
	}
 	
	test_printf_success("test_netioctl_withbuf_entry");
 	netclose(fd, pBuf, iBufLen);
}
Example #28
0
void test_tcp_socket_client(cyg_addrword_t pnetdata)
{
	int s, i, len;
	char msg;
	struct sockaddr_in sa, r_sa;
	struct hostent *hp;
	
	int threadid;
		
	int port = ((TEST_TCP_SOCKET_DATA_T*)pnetdata)->iport;
	char *pbuf = ((TEST_TCP_SOCKET_DATA_T*)pnetdata)->pbuf;
	
	threadid = cyg_thread_self();
	
	if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL)
	{
		test_printf_error("test_tcp_socket_client");
		cyg_thread_exit();
	}
	
	memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
	r_sa.sin_family = AF_INET;
	r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
	
	memcpy(&(sa.sin_addr), hp->h_addr_list0, hp->h_length);
    sa.sin_family = AF_INET;
    sa.sin_port = htons(IPPORT_USERRESERVED + port - TEST_TCP_SOCKET_SERVER_NUM);
    
    for(i = 0; i < TEST_TCP_ACCEPT_SOCKET_TIMES; i++)
    {
		if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1)
		{
			test_printf_error("test_tcp_socket_client");
			cyg_thread_exit();
		}
		
		if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1)
		{
			test_printf_error("test_tcp_socket_client");
			netclose(s, pbuf, RNT_BUFFER_LEN);
			cyg_thread_exit();
		}
		
		if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1)
		{
			test_printf_error("test_tcp_socket_client");
			netclose(s, pbuf, RNT_BUFFER_LEN);
			cyg_thread_exit();
		}
    
	    while(connect(s, (struct sockaddr*)&sa, sizeof(sa), pbuf, RNT_BUFFER_LEN) == -1) NULL;
	    
		if((len = netread(s, &msg, sizeof(msg), pbuf, RNT_BUFFER_LEN)) == -1)
		{
			test_printf_error("test_tcp_socket_client");
			break;
		}
		if(len == 0)
		{
			netclose(s, pbuf, RNT_BUFFER_LEN);
			continue;
		}
    }
    
	test_printf_success("test_tcp_socket_client");
	cyg_thread_exit();
}
Example #29
0
void test_tcp_bind_socket(char *pcBuf, int iBufLen)
{
	struct sockaddr_in r_sa;
	struct hostent *hp;
	int i;
	
	int iPortBegin = TEST_TCP_SOCKET_PORT_BEGIN;
	int iPortEnd = TEST_TCP_SOCKET_PORT_END;
	int port;
	int sockfd[TEST_TCP_SOCKET_PORT_END];
	
	char hname[256];
	
	memset(hname, 0, 256);
	if(gethostname(hname, 256, pcBuf, iBufLen) == -1)
	{
		test_printf_error("test_tcp_bind_socket");
		return;
	}
	
	port = iPortBegin;
	while(port <= iPortEnd)
	{
		if((hp = gethostbyname(hname, pcBuf, iBufLen)) == NULL)
		{
			test_printf_error("test_tcp_bind_socket");
			goto fail;
		}
		
		memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length);
		r_sa.sin_family = AF_INET;
		r_sa.sin_port = htons(IPPORT_USERRESERVED + port);
		
		if((sockfd[port-1] = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pcBuf, iBufLen)) == -1)
		{
			test_printf_error("test_tcp_bind_socket");
			goto fail;
		}
		
		if(set_reuseaddr(sockfd[port-1], pcBuf, iBufLen) == -1)
		{
			test_printf_error("test_tcp_bind_socket");
			goto fail;
		}
	
		if(bind(sockfd[port-1], (struct sockaddr*)&r_sa, sizeof(r_sa), pcBuf, iBufLen) == -1)
		{
			test_printf_error("test_tcp_bind_socket");
			goto fail;
		}
		port++;
	}
	
fail:
	if(port > iPortEnd)
		test_printf_success("test_tcp_bind_socket");
	for(i = iPortBegin; i <= port; i++)
	{
		netclose(sockfd[i-1], pcBuf, iBufLen);
	}
	return;
}
Example #30
0
/*
 * Name: SetWlanWepKey
 * Description: Set wireless WEP key.
 *
 * Parameter:
 *  pcInterface[in]: Net interface for wireless lan.
 *  pAscKey[in]: Buffer length of which >= 14.
 *  iAscKeyLen[in]: The Length of valid buffer pointed by "pAscKey".
 *                  Set the value to -1 if WEP key wants to be
 *                  disabled.
 *  iKeyIndex[in]: Index of WEP keys.
 * No return value.
 */
BOOL SetWlanWepKey(const char *pcInterface,
					const void *pAscKey,
					int iAscKeyLen,
					int iKeyIndex,
					int WepAuthentication)
{
	int fd;
	int rt;
	unsigned char pucKey[IW_ENCODING_TOKEN_MAX];
	struct iwreq wrq;
	
	diag_printf("SetWlanWepKey=%s,%02x%02x%02x,%d,%d,%d\n", pcInterface,
		(int)((unsigned char *)pAscKey)[0],
		(int)((unsigned char *)pAscKey)[1],
		(int)((unsigned char *)pAscKey)[2],
		iAscKeyLen,
		iKeyIndex,
		WepAuthentication);
		
	
	if (pcInterface == NULL) return FALSE;
#ifndef WLAN
	if ((fd = socket(AF_INET,SOCK_DGRAM,0, g_StreamServer_Buf, System_BUFFER_LEN)) < 0)
		return FALSE;
#else
	if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) 
		return FALSE;	
#endif
	memset(&wrq, 0, sizeof(wrq));

	/* iKeyIndex in ioctl from 1 to 4, while SetWlanWepKey use 0 to 3 */
	iKeyIndex++;

	strcpy(wrq.ifr_name, pcInterface);
	if (iAscKeyLen >= 0)
	{//on
		wrq.u.data.length = iAscKeyLen;
		if(wrq.u.data.length > IW_ENCODING_TOKEN_MAX)
			wrq.u.data.length = IW_ENCODING_TOKEN_MAX;
		if (pAscKey != NULL)
		{
			memcpy(pucKey, pAscKey, wrq.u.data.length);
			wrq.u.data.pointer = (caddr_t)pucKey;
		}
		else wrq.u.data.pointer = NULL;

		if (iKeyIndex > 0 && iKeyIndex < IW_ENCODE_INDEX)
			wrq.u.encoding.flags |= iKeyIndex;

		wrq.u.data.flags |= WepAuthentication;
		//wrq.u.data.flags |= IW_ENCODE_OPEN;
	}
	else
	{//off
		wrq.u.data.length = 0;
		wrq.u.data.pointer = (caddr_t)pucKey;
		wrq.u.data.flags |= IW_ENCODE_DISABLED;
		wrq.u.data.flags |= IW_ENCODE_OPEN;
	}
#ifndef WLAN
	rt = netioctl(fd, SIOCSIWENCODE, &wrq, sizeof(wrq),g_StreamServer_Buf, System_BUFFER_LEN);
	netclose(fd, g_StreamServer_Buf, System_BUFFER_LEN);
#else
	rt = ioctl(fd, SIOCSIWENCODE, &wrq);
	close(fd);
#endif
	return (rt >= 0 ? TRUE : FALSE);
}