A first attempt at listening sockets in Open Transport.
[u/mdw/putty] / mac / macstore.c
index 7643c54..35f31bd 100644 (file)
@@ -506,10 +506,10 @@ void *enum_settings_start(void) {
     OSErr error;
     struct enum_settings_state *state;
 
-    state = safemalloc(sizeof(*state));
+    state = snew(struct enum_settings_state);
     error = get_session_dir(kDontCreateFolder, &state->vRefNum, &state->dirID);
     if (error != noErr) {
-       safefree(state);
+       sfree(state);
        return NULL;
     }
     state->index = 1;
@@ -602,6 +602,15 @@ void write_random_seed(void *data, int len)
     return;
 }
 
+/*
+ * This host key cache uses a file in the PuTTY Preferences folder and
+ * stores keys as individual TEXT resources in the resource fork of
+ * that file.  This has two problems.  Firstly, a resource fork can
+ * contain no more than 2727 resources.  Secondly, the Resource
+ * Manager uses a linear search to find a particular resource, which
+ * could make having lots of host keys quite slow.
+ */
+
 int verify_host_key(const char *hostname, int port,
                    const char *keytype, const char *key)
 {
@@ -674,6 +683,7 @@ void store_host_key(const char *hostname, int port,
     char *resname;
     Str255 presname;
     Handle resvalue;
+    Handle reshandle;
     int id;
 
     /* Open the host key file */
@@ -699,7 +709,12 @@ void store_host_key(const char *hostname, int port,
     UseResFile(keyrefnum);
     resname = dupprintf("%s@%d:%s", keytype, port, hostname);
     c2pstrcpy(presname, resname);
-    
+
+    reshandle = Get1NamedResource(FOUR_CHAR_CODE('TEXT'), presname);
+    if (reshandle != NULL) {
+       /* The resource exists, we're replacing a host key */
+       RemoveResource(reshandle);
+    }
     error = PtrToHand(key, &resvalue, strlen(key));
     if (error != noErr) goto out;
 
@@ -716,8 +731,6 @@ void store_host_key(const char *hostname, int port,
     sfree(resname);
 }
   
-
-
 /*
  * Emacs magic:
  * Local Variables: