-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy paththreadsafe-gethostbyaddr.c
115 lines (106 loc) · 3.02 KB
/
threadsafe-gethostbyaddr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// THIS IS THE THREAD-SAFE IMPLEMENTATION OF GETHOSTBYADDR FUNCTION, USED IN tcp SOCKET I/O
//Implemented using mutex locks. semaphores and dynamic allocation for return types of gethostbyaddr to avoid re-entrancy issue
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <semaphore.h>
#include <errno.h>
#include <pthread.h>
static sem_t mutex; /* protects calls to gethostbyname */
/* $begin unixerror */
void unix_error(char *msg) /* unix-style error */
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(0);
}
void *Malloc(size_t size)
{
void *p;
if ((p = malloc(size)) == NULL)
unix_error("Malloc error");
return p;
}
//self defined wrapper for sem_init
void Sem_init(sem_t *sem, int pshared, unsigned int value)
{
if (sem_init(sem, pshared, value) < 0)
unix_error("Sem_init error");
}
//P() is a wrapper defined for sem_wait()
void P(sem_t *sem)
{
if (sem_wait(sem) < 0)
unix_error("P error");
}
//P() is a wrapper defined for sem_post()
void V(sem_t *sem)
{
if (sem_post(sem) < 0)
unix_error("V error");
}
static void init_gethostbyaddr_ts(void)
{
Sem_init(&mutex, 0, 1);
}
/* *Gethostbyaddr_ts - thread safe implementation of gethostbyaddr*/
struct hostent *Gethostbyaddr_ts(const char *addr, int len, int type)
{
struct hostent *sharedp, *unsharedp;
unsharedp = Malloc(sizeof(struct hostent));
P(&mutex);
sharedp = gethostbyaddr(addr,len,type);
*unsharedp = *sharedp;
V(&mutex);
return unsharedp;
}
void main()
{
pthread_t t1, t2; /* two threads */
void *getta(void *);
// Each thread calls same function of "getta"
//"getta" performs as gethostbyaddr() with argument passed is the IP address
pthread_create(&t1, NULL, getta, (void *)"127.0.0.1");
//Thread 1 queries with IP = 127.0.0.1
pthread_create(&t2, NULL, getta, (void *)"10.0.2.15");
//Thread 2 queries with IP= 10.0.2.15
pthread_join(t1, NULL);
pthread_join(t2, NULL);
}
void *getta (void *m)
{ char **cp;
char *buffer= (char *) m;
struct in_addr *addr;
struct hostent *hp;
char **p;
init_gethostbyaddr_ts();
if ((int)(addr = inet_addr(m)) == -1) {
(void) printf("IP-address must be of the form a.b.c.d\n");
exit (2);
}
hp = Gethostbyaddr_ts((char *)&addr, sizeof (addr), AF_INET);
if (hp == NULL) {
(void) printf("host information for %s not found\n", m);
exit (3);
}
printf("nom = %s\n", hp->h_name);
printf("type = %d, long = %d\n", hp->h_addrtype,hp->h_length);
for (cp=hp->h_aliases;cp != NULL && *cp != NULL;){
if (cp == hp->h_aliases)
printf("aliases = ");
printf ("%s",*cp);
if (*(++cp) != NULL) printf (",");
}
if (cp != hp->h_aliases) printf("\n");
for (cp=hp->h_addr_list;cp != NULL && *cp != NULL;){
if (cp == hp->h_addr_list)
printf("addresses = ");
printf ("%s",inet_ntoa(*((struct in_addr *)*cp)));
if (*(++cp) != NULL)
printf (",");
}
if (cp != hp->h_addr_list)
printf("\n");}