| 1 |
/* Speeraddr.c: this function gets the name of the peer |
| 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 |
* Speeraddr.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 |
* Definitions: |
| 20 |
*/ |
| 21 |
#define PEERBUF 65 |
| 22 |
|
| 23 |
/* --------------------------------------------------------------------- */ |
| 24 |
|
| 25 |
/* Speeraddr: get peer IP address */ |
| 26 |
#ifdef __PROTOTYPE__ |
| 27 |
unsigned long Speeraddr(Socket *skt) |
| 28 |
#else |
| 29 |
unsigned long Speeraddr(skt) |
| 30 |
Socket *skt; |
| 31 |
#endif |
| 32 |
{ |
| 33 |
unsigned long ret; |
| 34 |
static struct sockaddr sktname; |
| 35 |
#if defined(_AIX) |
| 36 |
static size_t namelen = sizeof(struct sockaddr); |
| 37 |
#elif defined(__gnu_linux__) |
| 38 |
static socklen_t namelen = sizeof(struct sockaddr); |
| 39 |
#else |
| 40 |
static int namelen = sizeof(struct sockaddr); |
| 41 |
#endif |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
if(!skt) ret= 0; |
| 46 |
|
| 47 |
else if(getpeername(skt->skt,&sktname,&namelen) == -1) { |
| 48 |
ret= 0; |
| 49 |
} |
| 50 |
|
| 51 |
else { |
| 52 |
union { |
| 53 |
unsigned char byte[4]; |
| 54 |
unsigned long i; |
| 55 |
} u; |
| 56 |
u.byte[0] = sktname.sa_data[2]; |
| 57 |
u.byte[1] = sktname.sa_data[3]; |
| 58 |
u.byte[2] = sktname.sa_data[4]; |
| 59 |
u.byte[3] = sktname.sa_data[5]; |
| 60 |
ret = u.i; |
| 61 |
} |
| 62 |
|
| 63 |
return ret; |
| 64 |
} |
| 65 |
|
| 66 |
/* ===================================================================== */ |
| 67 |
#ifdef DEBUG_TEST |
| 68 |
|
| 69 |
/* main: test routine begins here... */ |
| 70 |
int main(int argc,char **argv) |
| 71 |
{ |
| 72 |
Socket *srvr = NULL; |
| 73 |
Socket *skt = NULL; |
| 74 |
|
| 75 |
rdcolor(); /* initialize color names (GREEN, RED, etc.) */ |
| 76 |
|
| 77 |
srvr= Sopen("GetPeerTest","S"); |
| 78 |
if(!srvr) error(XTDIO_ERROR,"unable to open <GetPeerTest> server\n"); |
| 79 |
|
| 80 |
skt = Saccept(srvr); |
| 81 |
|
| 82 |
printf("client<%s> accepted, peer is %lu\n",Sprtskt(skt),Speeraddr(skt)); |
| 83 |
|
| 84 |
Sclose(skt); |
| 85 |
Sclose(srvr); |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
#endif /* DEBUG_TEST */ |
| 91 |
|
| 92 |
/* ===================================================================== |
| 93 |
* vim: ts=4 |
| 94 |
*/ |