(file) Return to addr.c CVS log (file) (dir) Up to [OMI] / omi / protocol

  1 mike  1.1 /*
  2           **==============================================================================
  3           **
  4           ** Open Management Infrastructure (OMI)
  5           **
  6           ** Copyright (c) Microsoft Corporation
  7           ** 
  8           ** Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  9           ** use this file except in compliance with the License. You may obtain a copy 
 10           ** of the License at 
 11           **
 12           **     http://www.apache.org/licenses/LICENSE-2.0 
 13           **
 14           ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15           ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
 16           ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
 17           ** MERCHANTABLITY OR NON-INFRINGEMENT. 
 18           **
 19           ** See the Apache 2 License for the specific language governing permissions 
 20           ** and limitations under the License.
 21           **
 22 mike  1.1 **==============================================================================
 23           */
 24           
 25           #include "addr.h"
 26           #include <ctype.h>
 27           #include <string.h>
 28           
 29           /* Include network headers */
 30           #if defined(CONFIG_OS_WINDOWS)
 31           # include <winsock2.h>
 32           #elif defined(CONFIG_POSIX)
 33           # include <unistd.h>
 34           # include <errno.h>
 35           # include <sys/socket.h>
 36           # include <netinet/tcp.h>
 37           # include <netinet/in.h>
 38           # include <sys/time.h>
 39           # include <sys/types.h>
 40           # include <netdb.h>
 41           # include <fcntl.h>
 42           # include <arpa/inet.h>
 43 mike  1.1 #endif
 44           
 45           #if defined(CONFIG_OS_WINDOWS)
 46           typedef unsigned long InAddr;
 47           #else
 48           typedef in_addr_t InAddr;
 49           #endif
 50           
 51           MI_Result Addr_Init(
 52               Addr* self_,
 53               const char* host,
 54               unsigned short port)
 55           {
 56               struct sockaddr_in* self = (struct sockaddr_in*)self_;
 57               struct sockaddr_in* addr;
 58           
 59               /* Clear address */
 60               memset((char*)self, 0, sizeof(*self));
 61               addr = (struct sockaddr_in*)self;
 62           
 63               if (isalpha((unsigned char)host[0]))
 64 mike  1.1     {
 65                   /* Handle host name */
 66           	struct hostent* p = gethostbyname((char*)host);
 67           
 68           	if (!p)
 69           	    return MI_RESULT_FAILED;
 70           
 71           	addr->sin_family = p->h_addrtype;
 72           	memcpy(&addr->sin_addr, p->h_addr, p->h_length);
 73               }
 74               else
 75               {
 76                   /* Handle dotted notation */
 77           	InAddr ip = inet_addr((char*)host);
 78           
 79           	if (ip == (InAddr)-1)
 80           	    return MI_RESULT_FAILED;
 81           
 82           	addr->sin_addr.s_addr = ip;
 83           	addr->sin_family = AF_INET;
 84               }
 85 mike  1.1 
 86               /* Set the port */
 87               self->sin_port = htons(port);
 88               return MI_RESULT_OK;
 89           }
 90           
 91           void Addr_InitAny(
 92               Addr* self_,
 93               unsigned short port)
 94           {
 95               struct sockaddr_in* self = (struct sockaddr_in*)self_;
 96           
 97               memset((char*)self, 0, sizeof(*self));
 98               self->sin_family = AF_INET;
 99               self->sin_addr.s_addr = htonl(INADDR_ANY);
100               self->sin_port = htons(port);
101           }

ViewCVS 0.9.2