diff options
author | tradke <tradke@9697cf00-7f2a-4e1b-af3c-314b8e4b499e> | 2000-07-15 07:30:51 +0000 |
---|---|---|
committer | tradke <tradke@9697cf00-7f2a-4e1b-af3c-314b8e4b499e> | 2000-07-15 07:30:51 +0000 |
commit | 5da0e0ca4f786d031884a83f6a9dc9d0d7114a18 (patch) | |
tree | 7b6b72e4fb1500a2fdd9783159a820a09f391f91 | |
parent | 95f9ecb754da74ec0fa6c53ad9fb6c29128ecef2 (diff) |
Close an already opened socket if the following bind/setsockopt/accept/connect
failed. Replaced bzero/bcopy by memset/memcpy. Fixed minor compiler warnings.
git-svn-id: http://svn.cactuscode.org/arrangements/CactusConnect/Socket/trunk@9 9697cf00-7f2a-4e1b-af3c-314b8e4b499e
-rw-r--r-- | src/Utils.c | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/src/Utils.c b/src/Utils.c index 78ecca9..561f7c6 100644 --- a/src/Utils.c +++ b/src/Utils.c @@ -78,20 +78,20 @@ int Socket_TCPOpenClientSock(const char *hostname,int port) struct sockaddr_in sin; int s /*,type*/; - bzero((char *)&sin, sizeof(sin)); + memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr=INADDR_ANY; sin.sin_port = htons((u_short)port); - if(phe=gethostbyname(hostname)) + if((phe=gethostbyname(hostname)) != NULL) { - bcopy(phe->h_addr, (char *)&sin.sin_addr, phe->h_length); + memcpy(&sin.sin_addr, phe->h_addr, phe->h_length); } else if((sin.sin_addr.s_addr = inet_addr(hostname)) == INADDR_NONE) { fprintf(stderr,"can\'t find host %s \n",hostname); } - if((ppe=getprotobyname("tcp")) == 0) + if((ppe=getprotobyname("tcp")) == NULL) { perror("can\'t find tcp protocol\n"); return -1; @@ -107,6 +107,7 @@ int Socket_TCPOpenClientSock(const char *hostname,int port) { perror(""); fprintf(stderr,"couldn\'t connect to host %s: port %u\n",hostname,port); + close (s); return -1; } return s; @@ -134,14 +135,14 @@ int Socket_UDPOpenClientSock(const char *hostname,int port) struct sockaddr_in sin; int sock/*,type*/; - bzero((char *)&sin, sizeof(sin)); + memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr=INADDR_ANY; sin.sin_port = htons((u_short)port); - if(phe=gethostbyname(hostname)) + if((phe=gethostbyname(hostname)) != NULL) { - bcopy(phe->h_addr, (char *)&sin.sin_addr, phe->h_length); + memcpy(&sin.sin_addr, phe->h_addr, phe->h_length); } else if((sin.sin_addr.s_addr = inet_addr(hostname)) == INADDR_NONE) { @@ -198,12 +199,12 @@ int Socket_TCPOpenServerSock(int port) struct sockaddr_in sin; int s/*,type*/; const int on = 1; - bzero((char *)&sin, sizeof(sin)); + memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons((u_short)port); - if((ppe=getprotobyname("tcp")) == 0) + if((ppe=getprotobyname("tcp")) == NULL) { perror("can\'t find tcp protocol\n"); return -1; @@ -217,12 +218,14 @@ int Socket_TCPOpenServerSock(int port) if(setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)) < 0) { fprintf(stderr,"couldn\'t set SO_REUSEADDR to port %u\n",port); - return s; + close(s); + return -1; } if(bind(s,(struct sockaddr *)&sin, sizeof(sin)) < 0) { fprintf(stderr,"couldn\'t bind to port %u\n",port); - return s; + close(s); + return -1; } if(listen(s,5) < 0) /* note, server connection qlen fixed to 5 */ { @@ -251,7 +254,7 @@ int Socket_UDPOpenServerSock(int port) struct protoent *ppe; struct sockaddr_in sin; int s/*,type*/; - bzero((char *)&sin, sizeof(sin)); + memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons((u_short)port); @@ -270,7 +273,8 @@ int Socket_UDPOpenServerSock(int port) if(bind(s,(struct sockaddr *)&sin, sizeof(sin)) < 0) { fprintf(stderr,"couldn\'t bind to port %u\n",port); - return s; + close(s); + return -1; } return s; } |