/* IBM_PROLOG_BEGIN_TAG */ /* This is an automatically generated prolog. */ /* */ /* bos720 src/bos/usr/samples/tcpip/dynload/rnd_sv.c 1.2 */ /* */ /* Licensed Materials - Property of IBM */ /* */ /* Restricted Materials of IBM */ /* */ /* COPYRIGHT International Business Machines Corp. 1999 */ /* All Rights Reserved */ /* */ /* US Government Users Restricted Rights - Use, duplication or */ /* disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ /* */ /* IBM_PROLOG_END_TAG */ /* NOTICE TO USERS OF THE SOURCE CODE EXAMPLES INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THE SOURCE CODE EXAMPLES, BOTH INDIVIDUALLY AND AS ONE OR MORE GROUPS, "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOURCE CODE EXAMPLES, BOTH INDIVIDUALLY AND AS ONE OR MORE GROUPS, IS WITH YOU. SHOULD ANY PART OF THE SOURCE CODE EXAMPLES PROVE DEFECTIVE, YOU (AND NOT IBM OR AN AUTHORIZED RISC System/6000* WORKSTATION DEALER) ASSUME THE ENTIRE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. RISC System/6000 is a trademark of International Business Machines Corporation. */ /* INCLUDE for structure definitions */ #include #include #include #include #include #include #include #include #include typedef struct _sv_pvt { pthread_t pvt_thd; struct servent pvt_servent; /* servent for return */ u_char pvt_addr[16]; /* buffer for addr return */ char pvt_sname[1025]; /* buffer for name */ } sv_pvt; void *sv_pvtinit(); void sv_close(); void sv_rewind(); void sv_minimize(); struct servent *sv_next(); struct servent *sv_byaddr(); struct servent *sv_byname(); struct servent *rnd_makeservice(); void * sv_pvtinit() { /* Step 1: Allocate memory and clear it */ sv_pvt *pvt = (sv_pvt *)malloc(sizeof(*pvt)); bzero(pvt,sizeof(*pvt)); /* Step 2: Initialize whatever needs be done per-process */ pvt->pvt_thd = pthread_self(); return pvt; } /* * sv_close() - inverse of sv_pvtinit(); */ void sv_close(void *this) { if (!this) return; free(this); } void sv_rewind(void *this) { sv_pvt *pvt = NULL; if (!this) return; pvt = (sv_pvt *)this; } void sv_minimize(void *this) { /* Nothing to do */ } /* sv_next is not supported in this example, since * we are not sequentially searching... */ /* * sv_byname() return a servent struct, given a service name * and protocol name */ struct servent * sv_byname(void *this, const char *name, const char *proto) { sv_pvt *pvt = NULL; if (!this) { errno = EINVAL; return(NULL); } pvt = (sv_pvt *)this; /* build up our servent */ return(rnd_makeservice(this, name, proto,-1)); } /* * sv_byport() - return a servent struct, given an port number * and protocol */ struct servent * sv_byport(void *this, int port, const char *proto) { sv_pvt *pvt = NULL; /* Get private struct */ if (!this) { errno = EINVAL; return(NULL); } pvt = (sv_pvt *)this; /* make and return servent from data */ return(rnd_makeservice(this,NULL, proto, port)); } /* * rnd_makeservice() - internal function to build servent from private data */ struct servent * rnd_makeservice(void *this, char *service_name, char *proto, int port) { sv_pvt *pvt = (sv_pvt *)this; /* getservbyname case */ if ( port == -1 ) { pvt->pvt_servent.s_name = service_name; pvt->pvt_servent.s_aliases = NULL; pvt->pvt_servent.s_port = strlen(service_name) + strlen(proto); pvt->pvt_servent.s_proto = proto; } /* getservbyport case */ else if ( service_name == NULL ) { char format[1024]; bzero(format, sizeof format); pvt->pvt_servent.s_aliases = NULL; pvt->pvt_servent.s_port = port; sprintf(format, "%d", port); pvt->pvt_servent.s_proto = proto; pvt->pvt_servent.s_name = (char *) malloc(sizeof(proto) + strlen(format) + 1); bzero(pvt->pvt_servent.s_name, sizeof pvt->pvt_servent.s_name); strcpy(pvt->pvt_servent.s_name, proto); strcat(pvt->pvt_servent.s_name, format); } /* Return */ return(&(pvt->pvt_servent)); }