More preparatory work: remove the <windows.h> include from lots of
[u/mdw/putty] / winnet.c
index 0dac18e..430a47b 100644 (file)
--- a/winnet.c
+++ b/winnet.c
@@ -368,6 +368,26 @@ void sk_getaddr(SockAddr addr, char *buf, int buflen)
 #endif
 }
 
+int sk_addrtype(SockAddr addr)
+{
+    return (addr->family == AF_INET ? ADDRTYPE_IPV4 : ADDRTYPE_IPV6);
+}
+
+void sk_addrcopy(SockAddr addr, char *buf)
+{
+#ifdef IPV6
+    if (addr->family == AF_INET) {
+#endif
+       struct in_addr a;
+       a.s_addr = htonl(addr->address);
+       memcpy(buf, (char*) &a.s_addr, 4);
+#ifdef IPV6
+    } else {
+       memcpy(buf, (char*) addr->ai, 16);
+    }
+#endif
+}
+
 void sk_addr_free(SockAddr addr)
 {
     sfree(addr);
@@ -393,6 +413,9 @@ static void sk_tcp_flush(Socket s)
 static void sk_tcp_close(Socket s);
 static int sk_tcp_write(Socket s, char *data, int len);
 static int sk_tcp_write_oob(Socket s, char *data, int len);
+static void sk_tcp_set_private_ptr(Socket s, void *ptr);
+static void *sk_tcp_get_private_ptr(Socket s);
+static void sk_tcp_set_frozen(Socket s, int is_frozen);
 static char *sk_tcp_socket_error(Socket s);
 
 extern char *do_select(SOCKET skt, int startup);
@@ -405,6 +428,9 @@ Socket sk_register(void *sock, Plug plug)
        sk_tcp_write,
        sk_tcp_write_oob,
        sk_tcp_flush,
+       sk_tcp_set_private_ptr,
+       sk_tcp_get_private_ptr,
+       sk_tcp_set_frozen,
        sk_tcp_socket_error
     };
 
@@ -459,6 +485,9 @@ Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
        sk_tcp_write,
        sk_tcp_write_oob,
        sk_tcp_flush,
+       sk_tcp_set_private_ptr,
+       sk_tcp_get_private_ptr,
+       sk_tcp_set_frozen,
        sk_tcp_socket_error
     };
 
@@ -633,6 +662,9 @@ Socket sk_newlistener(int port, Plug plug, int local_host_only)
        sk_tcp_write,
        sk_tcp_write_oob,
        sk_tcp_flush,
+       sk_tcp_set_private_ptr,
+       sk_tcp_get_private_ptr,
+       sk_tcp_set_frozen,
        sk_tcp_socket_error
     };
 
@@ -801,7 +833,10 @@ void try_send(Actual_Socket s)
                s->pending_error = err;
                return;
            } else {
-               fatalbox(winsock_error_string(err));
+               /* We're inside the Windows frontend here, so we know
+                * that the frontend handle is unnecessary. */
+               logevent(NULL, winsock_error_string(err));
+               fatalbox("%s", winsock_error_string(err));
            }
        } else {
            if (s->sending_oob) {
@@ -937,8 +972,12 @@ int select_result(WPARAM wParam, LPARAM lParam)
        ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
        noise_ultralight(ret);
        if (ret <= 0) {
-           fatalbox(ret == 0 ? "Internal networking trouble" :
-                    winsock_error_string(WSAGetLastError()));
+           char *str = (ret == 0 ? "Internal networking trouble" :
+                        winsock_error_string(WSAGetLastError()));
+           /* We're inside the Windows frontend here, so we know
+            * that the frontend handle is unnecessary. */
+           logevent(NULL, str);
+           fatalbox("%s", str);
        } else {
            return plug_receive(s->plug, 2, buf, ret);
        }
@@ -1043,13 +1082,13 @@ void net_pending_errors(void)
  * Each socket abstraction contains a `void *' private field in
  * which the client can keep state.
  */
-void sk_set_private_ptr(Socket sock, void *ptr)
+static void sk_tcp_set_private_ptr(Socket sock, void *ptr)
 {
     Actual_Socket s = (Actual_Socket) sock;
     s->private_ptr = ptr;
 }
 
-void *sk_get_private_ptr(Socket sock)
+static void *sk_tcp_get_private_ptr(Socket sock)
 {
     Actual_Socket s = (Actual_Socket) sock;
     return s->private_ptr;
@@ -1070,7 +1109,7 @@ static char *sk_tcp_socket_error(Socket sock)
     return s->error;
 }
 
-void sk_set_frozen(Socket sock, int is_frozen)
+static void sk_tcp_set_frozen(Socket sock, int is_frozen)
 {
     Actual_Socket s = (Actual_Socket) sock;
     if (s->frozen == is_frozen)
@@ -1099,3 +1138,13 @@ SOCKET next_socket(int *state)
     Actual_Socket s = index234(sktree, (*state)++);
     return s ? s->s : INVALID_SOCKET;
 }
+
+int net_service_lookup(char *service)
+{
+    struct servent *se;
+    se = getservbyname(service, NULL);
+    if (se != NULL)
+       return ntohs(se->s_port);
+    else
+       return 0;
+}