summaryrefslogtreecommitdiffstats
path: root/socket.c
Commit message (Collapse)AuthorAgeFilesLines
* Synchronize license comments with LICENSE.Kaz Kylheku2016-10-011-16/+17
| | | | | | | | | | | | | | | | | | | | * Makefile, args.c, args.h, arith.c, arith.h, cadr.c, cadr.h, combi.c, combi.h, configure, debug.c, debug.h, eval.c, eval.h, filter.c, filter.h, ftw.c, ftw.h, gc.c, gc.h, glob.c, glob.h, hash.c, hash.h, jmp.S, lib.c, lib.h, lisplib.c, lisplib.h, match.c, match.h, parser.c, parser.h, parser.l, parser.y, rand.c, rand.h, regex.c, regex.h, share/txr/stdlib/awk.tl, share/txr/stdlib/build.tl, share/txr/stdlib/cadr.tl, share/txr/stdlib/conv.tl, share/txr/stdlib/except.tl, share/txr/stdlib/hash.tl, share/txr/stdlib/ifa.tl, share/txr/stdlib/path-test.tl, share/txr/stdlib/place.tl, share/txr/stdlib/socket.tl, share/txr/stdlib/struct.tl, share/txr/stdlib/termios.tl, share/txr/stdlib/txr-case.tl, share/txr/stdlib/type.tl, share/txr/stdlib/with-resources.tl, share/txr/stdlib/with-stream.tl, share/txr/stdlib/yield.tl, signal.c, signal.h, socket.c, socket.h, stream.c, stream.h, struct.c, struct.h, sysif.c, sysif.h, syslog.c, syslog.h, termios.c, termios.h, txr.1, txr.c, txr.h, unwind.c, unwind.h, utf8.c, utf8.h: Revert to verbatim 2-Clause BSD.
* Fix crash in getaddrinfo.Kaz Kylheku2016-07-031-2/+4
| | | | | | | * socket.c (getaddrinfo_wrap): Initialize alist to null, because getaddrinfo doesn't set it in the failure case! Then, avoid calling freeaddrinfo for null pointer; POSIX doesn't require that to work.
* Fix memory leak in dgram socket streams.Kaz Kylheku2016-06-081-0/+1
| | | | | * socket.c (dgram_destroy): Free the dgram_stream structure too, not just its buffers.
* Use width of 8 for struct dgram boolean bitfields.Kaz Kylheku2016-05-291-2/+2
| | | | | | | | * socket.c (struct dgram): Members is_connected and is_byte_oriented widened from 1 to 8 bits. This allows byte-access instructions to be used on architectures like x86. All the flags still fit into a word, so the structure doesn't get bigger.
* Support byte oriented mode in dgram sockets.Kaz Kylheku2016-05-291-3/+19
| | | | | | | | | * socket.c (struct dgram_stream): New member, is_byte_oriented. (dgram_get_char): In byte oriented mode, just get one byte and convert to a character just like stdio_get_char: zero goes to U+DC00, and 0x01-0xFF go to U+0001 to U+FFFF. (dgram_get_prop, dgram_set_prop): Handle :byte-oriented property for getting and setting the is_byte_oriented flag.
* Rename struct dgram_stream member.Kaz Kylheku2016-05-291-5/+5
| | | | | | | * socket.c (struct dgram_stream): Member sock_connected renamed to is_connected. (make_dgram_sock_stream, dgram_flush, sock_connect, sock_mark_connected): Follow rename.
* Strengthen against resource leaks upon exceptions.Kaz Kylheku2016-04-211-3/+9
| | | | | | | | | | | | | | | | | | | | | | | * glob.c (glob_wrap): Perform argument conversions that might throw before allocating UTF-8 string. * parser.y (text): In the action for SPACE, the lexeme is not needed so free($1) right away. If regex_compile were to throw an exception, that lexeme will leak. * socket.c (getaddrinfo_wrap): Harden against leakage of node_u8 and service_u8 strings with an unwind block. For instance, the hints structure could contain bad values which cause addrinfo_in to throw. * stream.c (make_string_byte_input_stream): Perform possibly throwing argument conversions before allocating resources. * sysif.c (mkdir_wrap, mknod_wrap, chmod_wrap, symlink_wrap, link_wrap, setenv_wrap, crypt_wrap): Likewise. * syslog.c (openlog_wrap, syslog_wrapv): Likewise.
* Recycle conses in unget-char and read-until-match.Kaz Kylheku2016-04-201-1/+1
| | | | | | | | | | | | * regex.c (ead_until_match): Use rcyc_pop instead of pop to move the conses to the recycle list. We know these are not shared with anything. Adding additional logic to completely recycle the stack. * socket.c (dgram_get_char): Use rcyc_pop to get the character from the push-back list. * stream.c (stdio_get_char): Likewise.
* Allow unlimited character pushback in unget-char.Kaz Kylheku2016-04-191-11/+4
| | | | | | | | | | | | | | | | Fixing read_until_match will require this feature. * socket.c (dgram_get_char): Treat unget_c as a cons-based stack; pop a character from it if available. (dgram_unget_char): Push the character onto unget_c rather than storing the characer into unget_c. * stream.c (stdio_get_char, stdio_unget_char): Closely analogous changes to the ones in dgram_get_char and dgram_unget_char, respectively. * txr.1: Documentation improved and updated.
* Make open_socket static and register in socket.c.Kaz Kylheku2016-04-141-1/+2
| | | | | | | | | | | | | | * lisplib.c (sock_set_entries): Add auto-load entry for open-socket. * socket.c (open_socket): Change to static. (sock_load_init): Register open-socket intrinsic here rather than in stream.c. * stream.c (stream_init): Remove registration of open-socket intrinsic. * stream.h (open_socket): Declaration removed.
* Make open_sockfd static.Kaz Kylheku2016-04-141-21/+21
| | | | | | | | | The open_sockfd function is only called within socket.c * socket.c (open_sockfd): More function up and change to static. * stream.h (open_sockfd): Declaration removed.
* New open-socket-pair function.Kaz Kylheku2016-04-141-1/+59
| | | | | | | | | | | * lisplib.c (sock_set_entries): Add open-socket-pair to autoload list. * socket.c (sock_mark_connected, socketpair_wrap): New static functions. (sock_load_init): Register open-socket-pair intrinsic. * txr.1: Documented.
* Consolidate repeated address unpacking code.Kaz Kylheku2016-04-141-14/+16
| | | | | | * socket.c (sockaddr_unpack): New static function. (sock_accept): Use sockaddr_unpack instead of two copies of if/else code.
* Fix error handling in dgram case of sock-accept.Kaz Kylheku2016-04-141-10/+11
| | | | | | | * socket.c (sock_accept): Reduce scope of unwind catch around recvfrom call. Add missing check nbytes == -1. Branch to failed label in dup failed case, instead of throwing exception with truncated "unable to" message.
* Bugfix: support abstract UNIX socket addresses on Linux.Kaz Kylheku2016-03-311-2/+5
| | | | | | | | | | | | | Making it work as already documented. * socket.c (MIN): New macro. (sockaddr_pack): Use utf8_dup_to_buf to convert Unix socket path to a buffer of UTF-8 bytes, possibly with one or more embedded null bytes. Copy as much of this as fits into the sun_path member of struct sockaddr_un. * txr.1: Improve documentation about the abstract names on Linux.
* sock-set-peer shouldn't mark dgram sockets connected.Kaz Kylheku2016-03-311-1/+5
| | | | | | | | | | | | Merely setting a peer doesn't actually connect the socket, so it must not be marked connected. If it is wrongly marked connected, then dgram_flush will wrongly use send rather than sendto. * socket.c (dgram_set_sock_peer): Don't set sock_connected flag. (sock_connect): Set the sock_connected flag here, for dgram sockets.
* Rename badly named socket-related internal funs.Kaz Kylheku2016-03-311-16/+16
| | | | | | | | | | | | | | | Also rename its related functions for consistency. * socket.c (sockaddr_in_out): Renamed to sockaddr_in_unpack. The "in" stands for internet, and is juxtaposed to "out". (sockaddr_in6_out): Renamed to sockaddr_in6_unpack. (unix_sockaddr_out): Renamed to sockaddr_un_unpack. (getaddrinfo_wrap): Calls to *_out functions renamed to *_unpack. (sockaddr_in): Renamed to sockaddr_pack. Original name is confusing against the struct tag name in struct sockaddr_in. (dgram_set_sock_peer, sock_bind, sock_connect, sock_accept): Calls updated to sockaddr_pack.
* Permissive stream open mode strings.Kaz Kylheku2016-03-191-9/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | There is more to this patch than just more permissive mode strings. Now if a socket can be opened with mode "l2" for instance, and these options are effectively applied to the socket-specific "r+b" default, not to "r". * stream.c (parse_mode): New argument specifying a default mode. The syntax is relaxed, allowing previously required elements to be omitted. (normalize_mode): New argument specifying a default mode. Format mode is always called now, because an input string is no longer necessarily a valid fopen string even in cases when it doesn't specify any extensions. (open_file, open_fileno, open_tail, open_command, open_process): Use new normalize_mode argument for defaulting; normalize_mode no longer defaults to "r". * stream.h (stdio_mode_init_trivial): Macro removed. (stdio_mode_init_blank, stdio_mode_init_r, stdio_mode_init_rpb): New initializer macros. (parse_mode, normalize_mode): Declarations updated. * socket.c (sock_accept): In datagram socket case, use new parse_mode argument for defaulting using stdio_mode_init_rpb, rather than overriding a missing string with "r+b". (open_sockfd): Likewise, and use new normalize_mode argument similarly for defaulting the mode on a stream socket. * txr.1: Documented mode string permissiveness.
* Size mode meaningful in datagram sockets.Kaz Kylheku2016-03-191-11/+20
| | | | | | | | | | | | | | | | | | | | | | * socket.c (struct dgram_stream): new rx_max member. (make_dgram_sock_stream): New arguments: a struct stdio_mode, and pointer to prototype dgram socket. If a size is specified in the mode, then use that as rx_max. Otherwise if a prototype socket is specified, use its rx_max as the new socket's rx_max. Otherwise default on 65536. (dgram_get_byte_callback): Use d->rx_max as the capture size, rather than a hard-coded 65536. (sock_accept): Use d->rx_max as capture size for datagram. Parse the mode. Pass the parsed mode to make_dgram_sock_stream, as well as the accepting socket, so it can set up the rx_max for the new socket. (open_sockfd): Parse the mode and pass to make_dgram_sock_stream. * stream.c (parse_mode): Static function becomes extern. * stream.h (parse_mode): Declared. * txr.1: Documented.
* Nuke accidental tabs.Kaz Kylheku2016-03-191-9/+9
| | | | | * socket.c: Inadvertently introduced tabs recently while working on Solaris. Grr!
* Sockets are r+b by default, not r+.Kaz Kylheku2016-03-181-1/+1
| | | | | | * socket.c (open_sockfd): Default mode string is "r+b". * txr.1: Documented.
* Timeout parameter in sock-accept.Kaz Kylheku2016-03-151-7/+38
| | | | | | | | | | * socket.c (sock_accept): New third parameter specifying timeout. If a timeout is specified, use the select function to select socket for reading, datagram or stream. (sock_load_init): Update registration of sock-accept intrinsic to three arguments, one optional. * txr.1: Documented sock-accept's timeout parameter.
* Make connect interruptible and support timeout.Kaz Kylheku2016-03-151-3/+65
| | | | | | | | | | * socket.c (to_connect): New static function. (sock_connect): Use to_connect instead of calling connect directly. (lock_load_init): sock-connect intrinsic registration updated to three arguments, one optional. * txr.1: Documented sock-connect timeout.
* Implement socket timeouts.Kaz Kylheku2016-03-151-0/+35
| | | | | | | | | | | | | | | | | * lib.c (timeout_error_s): New symbol variable. (obj_init): Intern timeout-error, init new variable. * lib.h (timeout_error_s): Declared. * socket.c (sock_timeout, sock_send_timeout, sock_recv_timeout): New static functions. (sock_load_init): Register sock-send-timeout and sock-recv-timeout intrinsics. * stream.c (stdio_maybe_read_error, stdio_maybe_error): Convert EAGAIN into timeout_error_s. * txr.1: Documented.
* sock-connect return value change.Kaz Kylheku2016-03-141-1/+1
| | | | | | | * socket.c (sock_connect): return a useful value rather than t, namely the socket. * txr.1: Documented.
* All stdio streams get line buffering with i mode.Kaz Kylheku2016-03-141-1/+0
| | | | | | | | | | | * socket.c (open_sockfd): We no longer need to set stream sockets to line buffered mode here; it's done in set_mode_props. * stream.c (set_mode_props): If the mode specifies interactive, streams open for writing are also switched to line buffering. * txr.1: Documented under open-file, and open-socket.
* Mac OS X: must clear sockaddr in order to bind.Kaz Kylheku2016-03-101-0/+2
| | | | | | | | | | Undocumented members in struct sockaddr_in, and possibly sockaddr_in6 also, must be cleared to all zero bits, otherwise bind fails for the loopback address, even if the relevant addressing members are correctly set. * socket.c (sockaddr_in): memset the IPv4 or IPv6 address to zero before filing it.
* Solaris: cannot sendto on connected dgram socket.Kaz Kylheku2016-03-101-2/+8
| | | | | | | | * socket.c (struct dgram_stream): New member, sock_connected. (make_dgram_sock_stream): Initialize sock_connected to zero. (dgram_flush): Use send if sock_connected is nonzero, otherwise sendto. (dgram_set_sock_peer): Set sock_connected to 1.
* Diagnose operations on closed socket.Kaz Kylheku2016-03-071-23/+42
| | | | | * socket.c (sock_bind, sock_connect, sock_listen, sock_accept): If the socket is closed, throw an error.
* Revamped naming for socket streams.Kaz Kylheku2016-03-071-5/+32
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * socket.c (struct dgram_stream): New member, addr. (make_dgram_sock_stream): Initialize addr to nil. (dgram_print): Instead of printing the numeric fd, print the stream description, obtained from the name_k property. (dgram_mark): Mark the addr member. (dgram_get_prop): Calculate a descriptive string from the dgram stream state for the name_k property. (dgram_set_prop): New static function. (dgram_strm_ops): Name change to dgram-sock. Wire in dgram_set_prop function. (sock_bind): Set the addr_k property of the stream to the local address that was just bound. * stream.c (addr_k): New keyword symbol variable. (stdio_handle): New member, addr. (stdio_stream_print): Obtain descr by calling get_prop method rather than directly from h->descr, in case it is dynamically computed. Use ~a rather than ~s for incorporating string properties into printed form, to eliminate quotes. (stdio_stream_mark): Mark new addr member. (sock_get_prop, sock_set_prop): New static functions. (stdio_set_sock_peer): Invalidate h->descr by setting it to nil. (make_stdio_stream_common): Initialize h->addr to nil. (make_sock_stream): Specify description as nil rather than "socket". (stream_init): Intern the addr keyword, and initialize the addr_k variable. Set the name of stdio_sock_ops to "stream-sock" rather than leaving it inherited as "file-stream". Wire in the sock_get_prop and sock_set_prop functions into stdio_sock_ops. * stream.h (addr_k): Declared.
* sock_accept: uninitialized socklen_t.Kaz Kylheku2016-03-071-1/+1
| | | | | * socket.c (sock_accept): The address length passed to recvfrom was not initialized to the storage length.
* gc bugs: more instances of wrong mutations.Kaz Kylheku2016-03-071-1/+1
| | | | | | | | * socket.c (dgram_set_sock_peer): Assign to struct dgram_stream using using set macro. * stream.c (stdio_set_sock_peer): Assign to struct stdio_handle peer using set macro.
* Show failing address in sock-connect error.Kaz Kylheku2016-03-071-2/+2
| | | | | * socket.c (sock_connect): If connect fails, show socket and address in error message.
* Special implementation of dgram socket streams.Kaz Kylheku2016-03-061-28/+488
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | * socket.c (struct dgram_stream): New struct. (make_dgram_sock_stream, dgram_print, dgram_mark, dgram_destroy, dgram_overflow, dgram_put_byte_callback, dgram_put_string, dgram_put_char, dgram_put_byte, dgram_get_byte_callback, dgram_get_char, dgram_get_byte, dgram_unget_char, dgram_unget_byte, dgram_flush, dgram_close, dgram_get_prop, dgram_get_fd, dgram_get_sock_family, dgram_get_sock_type, dgram_get_sock_peer, dgram_set_sock_peer, dgram_get_error, dgram_get_error_str, dgram_clear_error): New static functions. (dgram_strm_ops): New static structure. (sock_listen): Check against datagram sockets which have a peer; otherwise a no-op for datagram sockets. (sock_accept): Special logic for dgram sockets. (open_sockfd): Function moved here from stream.c, and augmented to open dgram stream for dgram sockets. (open_socket): Function moved here from stream.c. (sock_load_init): Fill in some operations in dgram_strm_ops. * stream.c (generic_get_line, make_sock_stream, errno_to_string): Statics become external. (open_sockfd, open_socket): Functions removed from here, moved to socket.c. * stream.h (generic_get_line, make_sock_stream, errno_to_string): Declared.
* Refer to correct socket function names in errors.Kaz Kylheku2016-03-061-3/+3
| | | | | | * socket.c (sock_bind, sock_connect, sock_listen): Report function name as sock-bind, sock-connect and sock-listen, rather than bind, connect and listen.
* Socket streams know their connected peer address.Kaz Kylheku2016-02-271-0/+2
| | | | | | | | | | | | * stream.c (struct stdio_handle): New member, peer. (stdio_stream_mark): Mark the new member. (sock_peer, sock_set_peer): New functions. (stream_init): Registered sock-peer intrinsic. * stream.h (sock_peer, sock_set_peer): Declared. * socket.c (sock_connect): If the connect is successful, then store the address into the stream as the peer.
* Do the SO_REUSEADDR thing when binding socket.Kaz Kylheku2016-02-271-2/+5
| | | | | * socket.c (sock_bind): Set the SO_REUSEADDR option before binding the socket, to thwart the EADDRINUSE nuisance.
* Adding socket support: unix, ipv4, ipv6.Kaz Kylheku2016-02-261-0/+382
* socket.c, socket.h: New files. * Makefile: include new socket.o among objects if have_objects variable is defined to 'y'. * configure (have_sockets): New variable. New configure test for socket-related functionality. (HAVE_SOCKETS, HAVE_GETADDRINFO): New configuration preprocessor symbols. Also, reordering the shell probing so that /usr/xpg4/bin/sh is the last fallback. On Solaris, it chokes on some code that is needed for Solaris. * lisplib.c (sock_set_entries, sock_instantiate): New static functions. (lisplib_init): Register new functions as autoload hooks. * share/txr/stdlib/socket.tl: New file. * stream.c (socket_error_s): New symbol variable. (struct stdio_handle): New members, family and type, helping stdio streams represent sockets too. (stdio_stream_mark): Mark new members of struct stdio_handle. (make_stdio_stream_common): Initialize new members. (make_sock_stream, stream_fd, sock_family, sock_type, open_socket, open_sockfd): New functions. (stream_init): Initialize socket_error_s variable. Register sock-family, sock-type and open-socket intrinsic functions. Register socket-error subtype. * stream.h (socket_error_s, make_sock_stream, stream_fd, sock_family, sock_type, open_socket, open_sockfd): Declared.