v / vlib / net
Raw file | 783 loc (774 sloc) | 15.36 KB | Latest commit hash 267e26ba1
1module net
2
3const is_windows = true
4
5// WsaError is all of the socket errors that WSA provides from WSAGetLastError
6pub enum WsaError {
7 //
8 // MessageId: WSAEINTR
9 //
10 // MessageText:
11 //
12 // A blocking operation was interrupted by a call to WSACancelBlockingCall.
13 //
14 wsaeintr = 10004
15 //
16 // MessageId: WSAEBADF
17 //
18 // MessageText:
19 //
20 // The file handle supplied is not valid.
21 //
22 wsaebadf = 10009
23 //
24 // MessageId: WSAEACCES
25 //
26 // MessageText:
27 //
28 // An attempt was made to access a socket in a way forbidden by its access permissions.
29 //
30 wsaeacces = 10013
31 //
32 // MessageId: WSAEFAULT
33 //
34 // MessageText:
35 //
36 // The system detected an invalid pointer address in attempting to use a pointer argument in a call.
37 //
38 wsaefault = 10014
39 //
40 // MessageId: WSAEINVAL
41 //
42 // MessageText:
43 //
44 // An invalid argument was supplied.
45 //
46 wsaeinval = 10022
47 //
48 // MessageId: WSAEMFILE
49 //
50 // MessageText:
51 //
52 // Too many open sockets.
53 //
54 wsaemfile = 10024
55 //
56 // MessageId: WSAEWOULDBLOCK
57 //
58 // MessageText:
59 //
60 // A non-blocking socket operation could not be completed immediately.
61 //
62 wsaewouldblock = 10035
63 //
64 // MessageId: WSAEINPROGRESS
65 //
66 // MessageText:
67 //
68 // A blocking operation is currently executing.
69 //
70 wsaeinprogress = 10036
71 //
72 // MessageId: WSAEALREADY
73 //
74 // MessageText:
75 //
76 // An operation was attempted on a non-blocking socket that already had an operation in progress.
77 //
78 wsaealready = 10037
79 //
80 // MessageId: WSAENOTSOCK
81 //
82 // MessageText:
83 //
84 // An operation was attempted on something that is not a socket.
85 //
86 wsaenotsock = 10038
87 //
88 // MessageId: WSAEDESTADDRREQ
89 //
90 // MessageText:
91 //
92 // A required address was omitted from an operation on a socket.
93 //
94 wsaedestaddrreq = 10039
95 //
96 // MessageId: WSAEMSGSIZE
97 //
98 // MessageText:
99 //
100 // A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself.
101 //
102 wsaemsgsize = 10040
103 //
104 // MessageId: WSAEPROTOTYPE
105 //
106 // MessageText:
107 //
108 // A protocol was specified in the socket function call that does not support the semantics of the socket type requested.
109 //
110 wsaeprototype = 10041
111 //
112 // MessageId: WSAENOPROTOOPT
113 //
114 // MessageText:
115 //
116 // An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call.
117 //
118 wsaenoprotoopt = 10042
119 //
120 // MessageId: WSAEPROTONOSUPPORT
121 //
122 // MessageText:
123 //
124 // The requested protocol has not been configured into the system, or no implementation for it exists.
125 //
126 wsaeprotonosupport = 10043
127 //
128 // MessageId: WSAESOCKTNOSUPPORT
129 //
130 // MessageText:
131 //
132 // The support for the specified socket type does not exist in this address family.
133 //
134 wsaesocktnosupport = 10044
135 //
136 // MessageId: WSAEOPNOTSUPP
137 //
138 // MessageText:
139 //
140 // The attempted operation is not supported for the type of object referenced.
141 //
142 wsaeopnotsupp = 10045
143 //
144 // MessageId: WSAEPFNOSUPPORT
145 //
146 // MessageText:
147 //
148 // The protocol family has not been configured into the system or no implementation for it exists.
149 //
150 wsaepfnosupport = 10046
151 //
152 // MessageId: WSAEAFNOSUPPORT
153 //
154 // MessageText:
155 //
156 // An address incompatible with the requested protocol was used.
157 //
158 wsaeafnosupport = 10047
159 //
160 // MessageId: WSAEADDRINUSE
161 //
162 // MessageText:
163 //
164 // Only one usage of each socket address (protocol/network address/port) is normally permitted.
165 //
166 wsaeaddrinuse = 10048
167 //
168 // MessageId: WSAEADDRNOTAVAIL
169 //
170 // MessageText:
171 //
172 // The requested address is not valid in its context.
173 //
174 wsaeaddrnotavail = 10049
175 //
176 // MessageId: WSAENETDOWN
177 //
178 // MessageText:
179 //
180 // A socket operation encountered a dead network.
181 //
182 wsaenetdown = 10050
183 //
184 // MessageId: WSAENETUNREACH
185 //
186 // MessageText:
187 //
188 // A socket operation was attempted to an unreachable network.
189 //
190 wsaenetunreach = 10051
191 //
192 // MessageId: WSAENETRESET
193 //
194 // MessageText:
195 //
196 // The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress.
197 //
198 wsaenetreset = 10052
199 //
200 // MessageId: WSAECONNABORTED
201 //
202 // MessageText:
203 //
204 // An established connection was aborted by the software in your host machine.
205 //
206 wsaeconnaborted = 10053
207 //
208 // MessageId: WSAECONNRESET
209 //
210 // MessageText:
211 //
212 // An existing connection was forcibly closed by the remote host.
213 //
214 wsaeconnreset = 10054
215 //
216 // MessageId: WSAENOBUFS
217 //
218 // MessageText:
219 //
220 // An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.
221 //
222 wsaenobufs = 10055
223 //
224 // MessageId: WSAEISCONN
225 //
226 // MessageText:
227 //
228 // A connect request was made on an already connected socket.
229 //
230 wsaeisconn = 10056
231 //
232 // MessageId: WSAENOTCONN
233 //
234 // MessageText:
235 //
236 // A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.
237 //
238 wsaenotconn = 10057
239 //
240 // MessageId: WSAESHUTDOWN
241 //
242 // MessageText:
243 //
244 // A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call.
245 //
246 wsaeshutdown = 10058
247 //
248 // MessageId: WSAETOOMANYREFS
249 //
250 // MessageText:
251 //
252 // Too many references to some kernel object.
253 //
254 wsaetoomanyrefs = 10059
255 //
256 // MessageId: WSAETIMEDOUT
257 //
258 // MessageText:
259 //
260 // A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
261 //
262 wsaetimedout = 10060
263 //
264 // MessageId: WSAECONNREFUSED
265 //
266 // MessageText:
267 //
268 // No connection could be made because the target machine actively refused it.
269 //
270 wsaeconnrefused = 10061
271 //
272 // MessageId: WSAELOOP
273 //
274 // MessageText:
275 //
276 // Cannot translate name.
277 //
278 wsaeloop = 10062
279 //
280 // MessageId: WSAENAMETOOLONG
281 //
282 // MessageText:
283 //
284 // Name component or name was too long.
285 //
286 wsaenametoolong = 10063
287 //
288 // MessageId: WSAEHOSTDOWN
289 //
290 // MessageText:
291 //
292 // A socket operation failed because the destination host was down.
293 //
294 wsaehostdown = 10064
295 //
296 // MessageId: WSAEHOSTUNREACH
297 //
298 // MessageText:
299 //
300 // A socket operation was attempted to an unreachable host.
301 //
302 wsaehostunreach = 10065
303 //
304 // MessageId: WSAENOTEMPTY
305 //
306 // MessageText:
307 //
308 // Cannot remove a directory that is not empty.
309 //
310 wsaenotempty = 10066
311 //
312 // MessageId: WSAEPROCLIM
313 //
314 // MessageText:
315 //
316 // A Windows Sockets implementation may have a limit on the number of applications that may use it simultaneously.
317 //
318 wsaeproclim = 10067
319 //
320 // MessageId: WSAEUSERS
321 //
322 // MessageText:
323 //
324 // Ran out of quota.
325 //
326 wsaeusers = 10068
327 //
328 // MessageId: WSAEDQUOT
329 //
330 // MessageText:
331 //
332 // Ran out of disk quota.
333 //
334 wsaedquot = 10069
335 //
336 // MessageId: WSAESTALE
337 //
338 // MessageText:
339 //
340 // File handle reference is no longer available.
341 //
342 wsaestale = 10070
343 //
344 // MessageId: WSAEREMOTE
345 //
346 // MessageText:
347 //
348 // Item is not available locally.
349 //
350 wsaeremote = 10071
351 //
352 // MessageId: WSASYSNOTREADY
353 //
354 // MessageText:
355 //
356 // WSAStartup cannot function at this time because the underlying system it uses to provide network services is currently unavailable.
357 //
358 wsasysnotready = 10091
359 //
360 // MessageId: WSAVERNOTSUPPORTED
361 //
362 // MessageText:
363 //
364 // The Windows Sockets version requested is not supported.
365 //
366 wsavernotsupported = 10092
367 //
368 // MessageId: WSANOTINITIALISED
369 //
370 // MessageText:
371 //
372 // Either the application has not called WSAStartup, or WSAStartup failed.
373 //
374 wsanotinitialised = 10093
375 //
376 // MessageId: WSAEDISCON
377 //
378 // MessageText:
379 //
380 // Returned by WSARecv or WSARecvFrom to indicate the remote party has initiated a graceful shutdown sequence.
381 //
382 wsaediscon = 10101
383 //
384 // MessageId: WSAENOMORE
385 //
386 // MessageText:
387 //
388 // No more results can be returned by WSALookupServiceNext.
389 //
390 wsaenomore = 10102
391 //
392 // MessageId: WSAECANCELLED
393 //
394 // MessageText:
395 //
396 // A call to WSALookupServiceEnd was made while this call was still processing. The call has been canceled.
397 //
398 wsaecancelled = 10103
399 //
400 // MessageId: WSAEINVALIDPROCTABLE
401 //
402 // MessageText:
403 //
404 // The procedure call table is invalid.
405 //
406 wsaeinvalidproctable = 10104
407 //
408 // MessageId: WSAEINVALIDPROVIDER
409 //
410 // MessageText:
411 //
412 // The requested service provider is invalid.
413 //
414 wsaeinvalidprovider = 10105
415 //
416 // MessageId: WSAEPROVIDERFAILEDINIT
417 //
418 // MessageText:
419 //
420 // The requested service provider could not be loaded or initialized.
421 //
422 wsaeproviderfailedinit = 10106
423 //
424 // MessageId: WSASYSCALLFAILURE
425 //
426 // MessageText:
427 //
428 // A system call has failed.
429 //
430 wsasyscallfailure = 10107
431 //
432 // MessageId: WSASERVICE_NOT_FOUND
433 //
434 // MessageText:
435 //
436 // No such service is known. The service cannot be found in the specified name space.
437 //
438 wsaservice_not_found = 10108
439 //
440 // MessageId: WSATYPE_NOT_FOUND
441 //
442 // MessageText:
443 //
444 // The specified class was not found.
445 //
446 wsatype_not_found = 10109
447 //
448 // MessageId: WSA_E_NO_MORE
449 //
450 // MessageText:
451 //
452 // No more results can be returned by WSALookupServiceNext.
453 //
454 wsa_e_no_more = 10110
455 //
456 // MessageId: WSA_E_CANCELLED
457 //
458 // MessageText:
459 //
460 // A call to WSALookupServiceEnd was made while this call was still processing. The call has been canceled.
461 //
462 wsa_e_cancelled = 10111
463 //
464 // MessageId: WSAEREFUSED
465 //
466 // MessageText:
467 //
468 // A database query failed because it was actively refused.
469 //
470 wsaerefused = 10112
471 //
472 // MessageId: WSAHOST_NOT_FOUND
473 //
474 // MessageText:
475 //
476 // No such host is known.
477 //
478 wsahost_not_found = 11001
479 //
480 // MessageId: WSATRY_AGAIN
481 //
482 // MessageText:
483 //
484 // This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server.
485 //
486 wsatry_again = 11002
487 //
488 // MessageId: WSANO_RECOVERY
489 //
490 // MessageText:
491 //
492 // A non-recoverable error occurred during a database lookup.
493 //
494 wsano_recovery = 11003
495 //
496 // MessageId: WSANO_DATA
497 //
498 // MessageText:
499 //
500 // The requested name is valid, but no data of the requested type was found.
501 //
502 wsano_data = 11004
503 //
504 // MessageId: WSA_QOS_RECEIVERS
505 //
506 // MessageText:
507 //
508 // At least one reserve has arrived.
509 //
510 wsa_qos_receivers = 11005
511 //
512 // MessageId: WSA_QOS_SENDERS
513 //
514 // MessageText:
515 //
516 // At least one path has arrived.
517 //
518 wsa_qos_senders = 11006
519 //
520 // MessageId: WSA_QOS_NO_SENDERS
521 //
522 // MessageText:
523 //
524 // There are no senders.
525 //
526 wsa_qos_no_senders = 11007
527 //
528 // MessageId: WSA_QOS_NO_RECEIVERS
529 //
530 // MessageText:
531 //
532 // There are no receivers.
533 //
534 wsa_qos_no_receivers = 11008
535 //
536 // MessageId: WSA_QOS_REQUEST_CONFIRMED
537 //
538 // MessageText:
539 //
540 // Reserve has been confirmed.
541 //
542 wsa_qos_request_confirmed = 11009
543 //
544 // MessageId: WSA_QOS_ADMISSION_FAILURE
545 //
546 // MessageText:
547 //
548 // Error due to lack of resources.
549 //
550 wsa_qos_admission_failure = 11010
551 //
552 // MessageId: WSA_QOS_POLICY_FAILURE
553 //
554 // MessageText:
555 //
556 // Rejected for administrative reasons - bad credentials.
557 //
558 wsa_qos_policy_failure = 11011
559 //
560 // MessageId: WSA_QOS_BAD_STYLE
561 //
562 // MessageText:
563 //
564 // Unknown or conflicting style.
565 //
566 wsa_qos_bad_style = 11012
567 //
568 // MessageId: WSA_QOS_BAD_OBJECT
569 //
570 // MessageText:
571 //
572 // Problem with some part of the filterspec or providerspecific buffer in general.
573 //
574 wsa_qos_bad_object = 11013
575 //
576 // MessageId: WSA_QOS_TRAFFIC_CTRL_ERROR
577 //
578 // MessageText:
579 //
580 // Problem with some part of the flowspec.
581 //
582 wsa_qos_traffic_ctrl_error = 11014
583 //
584 // MessageId: WSA_QOS_GENERIC_ERROR
585 //
586 // MessageText:
587 //
588 // General QOS error.
589 //
590 wsa_qos_generic_error = 11015
591 //
592 // MessageId: WSA_QOS_ESERVICETYPE
593 //
594 // MessageText:
595 //
596 // An invalid or unrecognized service type was found in the flowspec.
597 //
598 wsa_qos_eservicetype = 11016
599 //
600 // MessageId: WSA_QOS_EFLOWSPEC
601 //
602 // MessageText:
603 //
604 // An invalid or inconsistent flowspec was found in the QOS structure.
605 //
606 wsa_qos_eflowspec = 11017
607 //
608 // MessageId: WSA_QOS_EPROVSPECBUF
609 //
610 // MessageText:
611 //
612 // Invalid QOS provider-specific buffer.
613 //
614 wsa_qos_eprovspecbuf = 11018
615 //
616 // MessageId: WSA_QOS_EFILTERSTYLE
617 //
618 // MessageText:
619 //
620 // An invalid QOS filter style was used.
621 //
622 wsa_qos_efilterstyle = 11019
623 //
624 // MessageId: WSA_QOS_EFILTERTYPE
625 //
626 // MessageText:
627 //
628 // An invalid QOS filter type was used.
629 //
630 wsa_qos_efiltertype = 11020
631 //
632 // MessageId: WSA_QOS_EFILTERCOUNT
633 //
634 // MessageText:
635 //
636 // An incorrect number of QOS FILTERSPECs were specified in the FLOWDESCRIPTOR.
637 //
638 wsa_qos_efiltercount = 11021
639 //
640 // MessageId: WSA_QOS_EOBJLENGTH
641 //
642 // MessageText:
643 //
644 // An object with an invalid ObjectLength field was specified in the QOS provider-specific buffer.
645 //
646 wsa_qos_eobjlength = 11022
647 //
648 // MessageId: WSA_QOS_EFLOWCOUNT
649 //
650 // MessageText:
651 //
652 // An incorrect number of flow descriptors was specified in the QOS structure.
653 //
654 wsa_qos_eflowcount = 11023
655 //
656 // MessageId: WSA_QOS_EUNKOWNPSOBJ
657 //
658 // MessageText:
659 //
660 // An unrecognized object was found in the QOS provider-specific buffer.
661 //
662 wsa_qos_eunkownpsobj = 11024
663 //
664 // MessageId: WSA_QOS_EPOLICYOBJ
665 //
666 // MessageText:
667 //
668 // An invalid policy object was found in the QOS provider-specific buffer.
669 //
670 wsa_qos_epolicyobj = 11025
671 //
672 // MessageId: WSA_QOS_EFLOWDESC
673 //
674 // MessageText:
675 //
676 // An invalid QOS flow descriptor was found in the flow descriptor list.
677 //
678 wsa_qos_eflowdesc = 11026
679 //
680 // MessageId: WSA_QOS_EPSFLOWSPEC
681 //
682 // MessageText:
683 //
684 // An invalid or inconsistent flowspec was found in the QOS provider specific buffer.
685 //
686 wsa_qos_epsflowspec = 11027
687 //
688 // MessageId: WSA_QOS_EPSFILTERSPEC
689 //
690 // MessageText:
691 //
692 // An invalid FILTERSPEC was found in the QOS provider-specific buffer.
693 //
694 wsa_qos_epsfilterspec = 11028
695 //
696 // MessageId: WSA_QOS_ESDMODEOBJ
697 //
698 // MessageText:
699 //
700 // An invalid shape discard mode object was found in the QOS provider specific buffer.
701 //
702 wsa_qos_esdmodeobj = 11029
703 //
704 // MessageId: WSA_QOS_ESHAPERATEOBJ
705 //
706 // MessageText:
707 //
708 // An invalid shaping rate object was found in the QOS provider-specific buffer.
709 //
710 wsa_qos_eshaperateobj = 11030
711 //
712 // MessageId: WSA_QOS_RESERVED_PETYPE
713 //
714 // MessageText:
715 //
716 // A reserved policy element was found in the QOS provider-specific buffer.
717 //
718 wsa_qos_reserved_petype = 11031
719 //
720 // MessageId: WSA_SECURE_HOST_NOT_FOUND
721 //
722 // MessageText:
723 //
724 // No such host is known securely.
725 //
726 wsa_secure_host_not_found = 11032
727 //
728 // MessageId: WSA_IPSEC_NAME_POLICY_ERROR
729 //
730 // MessageText:
731 //
732 // Name based IPSEC policy could not be added.
733 //
734 wsa_ipsec_name_policy_error = 11033
735}
736
737// wsa_error casts an int to its WsaError value
738pub fn wsa_error(code int) WsaError {
739 return unsafe { WsaError(code) }
740}
741
742const (
743 error_ewouldblock = WsaError.wsaewouldblock
744 error_einprogress = WsaError.wsaeinprogress
745)
746
747// Link to Winsock library
748#flag -lws2_32
749#include <winsock2.h>
750#include <ws2tcpip.h>
751
752// Constants that windows needs
753const (
754 fionbio = C.FIONBIO
755 msg_nosignal = 0
756 wsa_v22 = 0x202 // C.MAKEWORD(2, 2)
757)
758
759// Error code returns the last socket error
760fn error_code() int {
761 return C.WSAGetLastError()
762}
763
764struct C.WSAData {
765mut:
766 wVersion u16
767 wHighVersion u16
768 szDescription [257]u8
769 szSystemStatus [129]u8
770 iMaxSockets u16
771 iMaxUdpDg u16
772 lpVendorInfo &u8
773}
774
775fn init() {
776 mut wsadata := C.WSAData{
777 lpVendorInfo: 0
778 }
779 res := C.WSAStartup(net.wsa_v22, &wsadata)
780 if res != 0 {
781 panic('socket: WSAStartup failed')
782 }
783}