763c85b0d3757f6244eef499a30492de291cc39d
[mono.git] / mono / utils / networking-missing.c
1 /*
2  * networking-missing.c: Implements missing standard socket functions.
3  *
4  * Author:
5  *      Rodrigo Kumpera (kumpera@gmail.com)
6  *
7  * (C) 2015 Xamarin
8  */
9
10 #include <mono/utils/networking.h>
11 #include <glib.h>
12
13 #ifdef HAVE_NETDB_H
14 #include <netdb.h>
15 #endif
16
17 #ifndef HAVE_INET_PTON
18
19 int
20 inet_pton (int family, const char *address, void *inaddrp)
21 {
22         if (family == AF_INET) {
23 #ifdef HAVE_INET_ATON
24                 struct in_addr inaddr;
25                 
26                 if (!inet_aton (address, &inaddr))
27                         return 0;
28                 
29                 memcpy (inaddrp, &inaddr, sizeof (struct in_addr));
30                 return 1;
31 #else
32                 /* assume the system has inet_addr(), if it doesn't
33                    have that we're pretty much screwed... */
34                 guint32 inaddr;
35                 
36                 if (!strcmp (address, "255.255.255.255")) {
37                         /* special-case hack */
38                         inaddr = 0xffffffff;
39                 } else {
40                         inaddr = inet_addr (address);
41 #ifndef INADDR_NONE
42 #define INADDR_NONE ((in_addr_t) -1)
43 #endif
44                         if (inaddr == INADDR_NONE)
45                                 return 0;
46                 }
47                 
48                 memcpy (inaddrp, &inaddr, sizeof (guint32));
49                 return 1;
50 #endif /* HAVE_INET_ATON */
51         }
52         
53         return -1;
54 }
55
56 #else /* !HAVE_INET_PTON */
57
58 #ifdef _MSC_VER
59 // Quiet Visual Studio linker warning, LNK4221, in cases when this source file intentional ends up empty.
60 void __mono_win32_networking_missing_lnk4221(void) {}
61 #endif
62 #endif /* !HAVE_INET_PTON */