| 1 |
/* Smkskt.c: this function makes a Socket |
| 2 |
* Authors: Charles E. Campbell, Jr. |
| 3 |
* Terry McRoberts |
| 4 |
* Copyright: Copyright (C) 1999-2005 Charles E. Campbell, Jr. {{{1 |
| 5 |
* Permission is hereby granted to use and distribute this code, |
| 6 |
* with or without modifications, provided that this copyright |
| 7 |
* notice is copied with it. Like anything else that's free, |
| 8 |
* Smkskt.c is provided *as is* and comes with no warranty |
| 9 |
* of any kind, either expressed or implied. By using this |
| 10 |
* software, you agree that in no event will the copyright |
| 11 |
* holder be liable for any damages resulting from the use |
| 12 |
* of this software. |
| 13 |
* Date: Aug 22, 2005 |
| 14 |
*/ |
| 15 |
#include <stdio.h> |
| 16 |
#include "sockets.h" |
| 17 |
|
| 18 |
/* --------------------------------------------------------------------- */ |
| 19 |
|
| 20 |
/* makeSocket: this function allocates a Socket */ |
| 21 |
#ifdef __PROTOTYPE__ |
| 22 |
Socket *makeSocket( |
| 23 |
char *hostname, |
| 24 |
char *sktname, |
| 25 |
int sktType) |
| 26 |
#else |
| 27 |
Socket *makeSocket(hostname, sktname, sktType) |
| 28 |
char *hostname; |
| 29 |
char *sktname; |
| 30 |
int sktType; |
| 31 |
#endif |
| 32 |
{ |
| 33 |
Socket *skt= NULL; |
| 34 |
|
| 35 |
|
| 36 |
/* allocate a Socket */ |
| 37 |
skt= (Socket *) malloc(sizeof(Socket)); |
| 38 |
outofmem((void *) skt, |
| 39 |
"makeSocket: while allocating a Socket for <%s:%s>\n", |
| 40 |
hostname,sktname); |
| 41 |
|
| 42 |
/* initialize Socket */ |
| 43 |
skt->hostname= (char *) calloc((size_t) strlen(hostname)+1,sizeof(char)); |
| 44 |
outofmem((void *) skt->hostname, |
| 45 |
"makeSocket: while allocating string for <%s>\n",hostname); |
| 46 |
strcpy(skt->hostname,hostname); |
| 47 |
|
| 48 |
if(!sktname) sktname= ""; |
| 49 |
skt->sktname= (char *) calloc((size_t) strlen(sktname)+1,sizeof(char)); |
| 50 |
outofmem((void *) skt->sktname,"makeSocket: while allocating string for <%s>\n",sktname); |
| 51 |
strcpy(skt->sktname,sktname); |
| 52 |
|
| 53 |
/* set port and skt to zero */ |
| 54 |
skt->port= skt->skt= 0; |
| 55 |
skt->type= sktType; |
| 56 |
|
| 57 |
/* return Socket */ |
| 58 |
return skt; |
| 59 |
} |
| 60 |
|
| 61 |
/* ------------------------------------------------------------------------ */ |
| 62 |
|
| 63 |
/* freeSocket: this function frees up a Socket */ |
| 64 |
#ifdef __PROTOTYPE__ |
| 65 |
void freeSocket(Socket *skt) |
| 66 |
#else |
| 67 |
void freeSocket(skt) |
| 68 |
Socket *skt; |
| 69 |
#endif |
| 70 |
{ |
| 71 |
|
| 72 |
if(skt) { |
| 73 |
if(skt->hostname) free(skt->hostname); |
| 74 |
if(skt->sktname) free(skt->sktname); |
| 75 |
skt->skt = 0; |
| 76 |
skt->port = 0; |
| 77 |
skt->type = 0; |
| 78 |
skt->hostname = NULL; |
| 79 |
skt->sktname = NULL; |
| 80 |
free((char *) skt); |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
/* --------------------------------------------------------------------- |
| 86 |
* vim: ts=4 |
| 87 |
*/ |