Debian Codegraph @

share/libc/netdb.c

raw
1 /**************************************************************************/
2 /* */
3 /* This file is part of Frama-C. */
4 /* */
5 /* Copyright (C) 2007-2022 */
6 /* CEA (Commissariat à l'énergie atomique et aux énergies */
7 /* alternatives) */
8 /* */
9 /* you can redistribute it and/or modify it under the terms of the GNU */
10 /* Lesser General Public License as published by the Free Software */
11 /* Foundation, version 2.1. */
12 /* */
13 /* It is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU Lesser General Public License for more details. */
17 /* */
18 /* See the GNU Lesser General Public License version 2.1 */
19 /* for more details (enclosed in the file licenses/LGPLv2.1). */
20 /* */
21 /**************************************************************************/
22
23 #include "netdb.h"
24 #include "sys/socket.h"
25 #include "netinet/in.h"
26 #include "stdlib.h"
27 #include "stddef.h"
28 #include "string.h"
29 #include "errno.h"
30 #include "__fc_builtin.h"
31 __PUSH_FC_STDLIB
32
33 /* dummy implementation of getaddrinfo. Returns at most one addrinfo.
34 */
35 int getaddrinfo(
36 const char *restrict nodename,
37 const char *restrict servname,
38 const struct addrinfo *restrict hints,
39 struct addrinfo **restrict res) {
40 static unsigned int volatile net_state;
41 if (nodename == NULL && servname == NULL) return EAI_NONAME;
42 // very crude modelling: except for AGAIN, FAIL, and SYSTEM, failures
43 // could be inferred from the arguments.
44 switch (net_state) {
45 case 0: return EAI_BADFLAGS;
46 case 1: return EAI_AGAIN;
47 case 2: return EAI_FAIL;
48 case 3: return EAI_FAMILY;
49 case 5: return EAI_SERVICE;
50 case 6: return EAI_SOCKTYPE;
51 case 7: {
52 errno = EIO; // pick a semi-plausible errcode.
53 return EAI_SYSTEM;
54 }
55 default: {
56 struct addrinfo* ai = malloc(sizeof(*ai));
57 if (!ai) return EAI_MEMORY;
58 struct sockaddr* sa = malloc(sizeof(*sa));
59 if (!sa) return EAI_MEMORY;
60 sa -> sa_family = Frama_C_interval(0,AF_MAX);
61 //@ slevel 15;
62 for (int i = 0; i < 14; i++) {
63 sa -> sa_data[i] = Frama_C_interval(CHAR_MIN,CHAR_MAX);
64 }
65 //@ slevel default;
66 ai -> ai_flags = 0;
67 ai -> ai_family = sa -> sa_family;
68 ai -> ai_socktype = Frama_C_interval(0,SOCK_SEQPACKET);
69 ai -> ai_protocol = Frama_C_interval(0,IPPROTO_MAX);
70 ai -> ai_addrlen = sizeof(*sa) ;
71 ai -> ai_addr = sa ;
72 ai -> ai_canonname = malloc(6);
73 if (!ai -> ai_canonname) return EAI_MEMORY;
74 strcpy(ai -> ai_canonname, "dummy");
75 ai -> ai_next = NULL;
76 *res = ai;
77 return 0;
78 }
79 }
80 }
81
82 #define __FC_MAX_HOST_ADDRS 2
83 #define __FC_MAX_HOST_ALIASES 2
84 #define __FC_HOSTBUF_SIZE 128
85 #define __FC_QUERYBUF_SIZE 128
86 struct __fc_gethostbyname {
87 struct hostent host;
88 unsigned char host_addr[sizeof(struct in_addr)];
89 char *h_addr_ptrs[__FC_MAX_HOST_ADDRS + 1];
90 char *host_aliases[__FC_MAX_HOST_ALIASES];
91 char hostbuf[__FC_HOSTBUF_SIZE];
92 };
93
94 struct __fc_gethostbyname __fc_ghbn;
95
96 static int res_search(const char *dname, int rec_class, int type,
97 char *answer, int anslen) {
98 for (int i = 0; i < anslen-1; i++) {
99 answer[i] = Frama_C_char_interval(CHAR_MIN, CHAR_MAX);
100 }
101 answer[anslen-1] = 0;
102 return Frama_C_interval(-1, anslen);
103 }
104
105 struct hostent *gethostbyname(const char *name) {
106 char buf[__FC_QUERYBUF_SIZE];
107 const char *cp;
108 int n;
109 __fc_ghbn.host.h_addrtype = AF_INET;
110 __fc_ghbn.host.h_length = sizeof(struct in_addr);
111
112 // Disallow names consisting only of digits/dots, unless they end in a dot
113 if (*name >= '0' && *name <= '9') {
114 for (cp = name;; ++cp) {
115 if (!*cp) {
116 struct in_addr addr;
117
118 if (*--cp == '.') break;
119
120 // All-numeric, no dot at the end. Fake up a hostent as if we'd actually done a lookup.
121 addr.s_addr = inet_addr(name);
122 if (addr.s_addr == INADDR_NONE) return NULL;
123
124 memcpy(__fc_ghbn.host_addr, &addr, __fc_ghbn.host.h_length);
125 strncpy(__fc_ghbn.hostbuf, name, __FC_HOSTBUF_SIZE - 1);
126 __fc_ghbn.hostbuf[__FC_HOSTBUF_SIZE - 1] = '\0';
127 __fc_ghbn.host.h_name = __fc_ghbn.hostbuf;
128 __fc_ghbn.host.h_aliases = __fc_ghbn.host_aliases;
129 __fc_ghbn.host_aliases[0] = NULL;
130 __fc_ghbn.h_addr_ptrs[0] = (char *) __fc_ghbn.host_addr;
131 __fc_ghbn.h_addr_ptrs[1] = NULL;
132 __fc_ghbn.host.h_addr_list = __fc_ghbn.h_addr_ptrs;
133
134 return &__fc_ghbn.host;
135 }
136
137 if (*cp < '0' || *cp > '9' || *cp != '.') break;
138 }
139 }
140
141 n = res_search(name, 1, 1, buf, sizeof(buf));
142 if (n < 0) return NULL;
143
144 if (Frama_C_nondet(0, 1)) return NULL;
145 else {
146 struct in_addr addr;
147 addr.s_addr = inet_addr(name);
148 memcpy(__fc_ghbn.host_addr, &addr, __fc_ghbn.host.h_length);
149 strncpy(__fc_ghbn.hostbuf, name, __FC_HOSTBUF_SIZE - 1);
150 __fc_ghbn.hostbuf[__FC_HOSTBUF_SIZE - 1] = '\0';
151 __fc_ghbn.host.h_name = __fc_ghbn.hostbuf;
152 __fc_ghbn.host.h_aliases = __fc_ghbn.host_aliases;
153 __fc_ghbn.host_aliases[0] = NULL;
154 __fc_ghbn.h_addr_ptrs[0] = (char *) __fc_ghbn.host_addr;
155 __fc_ghbn.h_addr_ptrs[1] = NULL;
156 __fc_ghbn.host.h_addr_list = __fc_ghbn.h_addr_ptrs;
157 return &__fc_ghbn.host;
158 }
159 }
160
161
162 __POP_FC_STDLIB