udp.c
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:7k
- /*----------------------------------------------------------------------*
- udp.c
-
- Version: 1.0 10/97
- 1.1 4/99 - tweaked for use in tpjack software
- 1.2 6/10/99 - changed the license to LGPL
-
- This module provides functions to use that read and write UDP
- packets to and from the network.
- Copyright (c) 1999 Quicknet Technologies, Inc.
- Written by Greg Herlein <gherlein@quicknet.net> - donated to
- Quicknet 6/10/99 by Herlein Engineering.
- * This is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License (LGPL) as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA. It is also available online at
- * http://www.gnu.org/copyleft/lesser.html
-
- ----------------------------------------------------------------------*/
- /*-------------------------------< RCS >--------------------------------*/
- static char RCS_ID[] = "$Id: udp.c,v 1.1.1.1 1999/07/16 01:26:02 bogawa Exp $";
- /*----------------------------< Defines >-------------------------------*/
- /*----------------------------< Includes >------------------------------*/
- /* stdlib */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <time.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <errno.h>
- #include <fcntl.h>
- /* other lib */
- #include "udp.h"
- /*---------------------------< Definitions >----------------------------*/
- /*--------------------------< Declarations >----------------------------*/
- /*------------------------< Global Variables >--------------------------*/
- char szClientName[50];
- char szClientAddr[50];
- /*-------------------------< Local Variables >--------------------------*/
- static struct sockaddr_in their_addr;
- static int their_addr_len;
- static struct sockaddr_in my_addr;
- static int my_addr_len;
- /*----------------------------------------------------------------------*/
- int
- GetSendSocket(char *host,int nPort)
- {
- int n=0,on=1,nSock=-1,s,type;
- struct hostent* pHost;
- their_addr_len = sizeof(struct sockaddr);
- bzero((char *)&their_addr,their_addr_len);
- their_addr.sin_family=AF_INET;
- /* first see if host is already a dotted decimal address */
- their_addr.sin_addr.s_addr=inet_addr(host);
- if(their_addr.sin_addr.s_addr==-1)
- {
- /* see if we can resolve it as a hostname via bind */
- if((pHost=gethostbyname(host)) == NULL)
- {
- switch(h_errno)
- {
- case HOST_NOT_FOUND:
- printf("gethostbyname() failed - HOST_NOT_FOUND (h_errno=%d)",
- h_errno);
- break;
- case NO_ADDRESS:
- printf("gethostbyname() failed - NO_ADDRESS (h_errno=%d)",h_errno);
- break;
- case NO_RECOVERY:
- printf("gethostbyname() failed - NO_RECOVERY (h_errno=%d)",h_errno);
- break;
- case TRY_AGAIN:
- printf("gethostbyname() failed - TRY_AGAIN (h_errno=%d)",h_errno);
- break;
- default:
- printf("gethostbyname() failed - %s (errno=%d)",strerror(errno),
- errno);
- break;
- }
- return -1;
- } else
- {
- memcpy(&their_addr.sin_addr.s_addr,pHost->h_addr,pHost->h_length);
- their_addr.sin_port = htons(nPort);
- } /* end if((pHost= ... */
- } else
- {
- their_addr.sin_port = htons(nPort);
- }
- /* create a datagram socket */
- if ((nSock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
- {
- perror("sock: ");
- return -1;
- }
- /* bind that port to this porocess */
- if(connect(nSock,(struct sockaddr *)&their_addr,their_addr_len)==-1) {
- perror("bind");
- return -1;
- }
-
- /* zero the rest of the struct */
- bzero(&(their_addr.sin_zero), 8);
- return nSock;
- /* make it non-blocking */
- fcntl(nSock,F_SETFL,O_NONBLOCK);
- }
- /*----------------------------------------------------------------------*/
- int
- GetRecvSocket(int nPort)
- {
- int nSock;
- int n;
- if ((nSock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
- perror("socket: ");
- return -1;
- }
- my_addr.sin_family = AF_INET; /* host byte order */
- my_addr.sin_port = htons(nPort); /* short, network byte order */
- my_addr.sin_addr.s_addr = INADDR_ANY; /* automatically fill with my IP */
- bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */
- my_addr_len = sizeof(struct sockaddr);
- /* bind that port to this porocess */
- if(bind(nSock,(struct sockaddr *)&my_addr,my_addr_len)==-1) {
- perror("bind");
- return -1;
- }
- /* make it non-blocking */
- fcntl(nSock,F_SETFL,O_NONBLOCK);
- return nSock;
- }
- /*----------------------------------------------------------------------*/
- int
- CloseSocket(int nSock)
- {
- close(nSock);
- return 1;
- }
- /*----------------------------------------------------------------------*/
- int
- ReadUDP(int nSock,char* pBuf,int nLen)
- {
- /* reads the UDP packet that has arrived and places
- the incoming string into pBuf. Reads at most nLen bytes
- essentially, this encapsulates recvfrom to look like read
- Returns -1 on error, number of bytes read on success */
- int n=0;
-
- if((n=recvfrom(nSock,pBuf,nLen,0,(struct sockaddr *)&their_addr,
- &their_addr_len)) == -1) {
- // perror("recvfrom");
- return -1;
- }
- return n;
- }
- /*----------------------------------------------------------------------*/
- int
- IdentifyPeer(int nSock)
- {
- struct sockaddr_in sPeer;
- int nSize,n;
- char* p;
- /* get the name and address of the client */
- nSize=sizeof(sPeer);
- n=getpeername(nSock,(struct sockaddr*)&sPeer,&nSize);
- if(n==-1) {
- printf("Unable to get peer name infon");
- return -1;
- }
- p=inet_ntoa(sPeer.sin_addr);
- strncpy(szClientAddr,p,sizeof(szClientAddr));
- GetNameFromAddr(szClientAddr,szClientName,sizeof(szClientName));
- return 1;
- }
- /*----------------------------------------------------------------------*/
- int
- GetNameFromAddr(char* szIP, char* szName, int nLenName)
- {
- struct sockaddr_in sPeer;
- int n;
- struct hostent* pHe;
- /* get the name and address of the IP address passed in */
- n=inet_aton(szIP,&sPeer.sin_addr);
- if ((pHe=gethostbyaddr((char*)&sPeer.sin_addr,sizeof(sPeer.sin_addr),
- AF_INET)) == NULL) {
- printf("Unable to get peer address infon");
- return -1;
- }
- memset(szName,0x00,nLenName);
- strncpy(szName,pHe->h_name,nLenName-1);
- return 1;
- }
- /*----------------------------------------------------------------------*/
- /*-------------------------------< End >--------------------------------*/