netatalk.io

Dev Docs AFP Daemon

AFP Daemon (afpd)

Overview

The afpd daemon is the core component of Netatalk that implements the Apple Filing Protocol (AFP) server functionality. It handles client connections, processes AFP commands, manages file operations, and coordinates with other system components to provide native Mac file sharing services.

Implementation Files

Architecture

Process Model

afpd uses a master-worker process architecture:

graph TB
    subgraph "afpd Master Process"
        A[Connection Listener]
        B[Process Manager]
        C[Configuration Handler]
    end

    subgraph "Worker Processes"
        D[Client Session 1]
        E[Client Session 2]
        F[Client Session N]
    end

    subgraph "Shared Resources"
        G[Volume Configuration]
        H[Authentication Modules]
        I[CNID Database]
    end

    A --> D
    A --> E
    A --> F
    B --> D
    B --> E
    B --> F
    C --> G
    D --> H
    D --> I

Core Components

1. Network Layer

Implementation Files:

2. AFP Command Processor

Implementation Files:

3. Authentication System

Implementation Files:

4. Volume Manager

Implementation Files:

5. File Operations

Implementation Files:

6. Directory Cache

Implementation Files:

Key Data Structures

Implementation Files

AFPObj Structure

The main AFP object that maintains session state:

typedef struct AFPObj {
    void *handle;               // Protocol-specific handle (DSI/ASP)
    char *username;             // Authenticated username
    char *password;             // User password (temporary)
    struct AFPVolume *volumes;  // Available volumes
    struct auth_methods *auth;  // Authentication methods

    // Protocol information
    int proto;                  // AFPPROTO_DSI or AFPPROTO_ASP
    AFPUserBytes (*attention)();// Attention handler

    // Configuration
    struct afp_options *options;// Server options
    struct AFPConfig *config;   // Configuration data

    // Runtime state
    uid_t uid;                  // User ID
    gid_t gid;                  // Group ID
    time_t time;               // Current time
    int logfd;                 // Log file descriptor
} AFPObj;

Volume Structure

Represents a shared volume:

struct vol {
    struct vol *v_next;         // Next volume in list
    char *v_localname;          // Local filesystem path
    char *v_name;               // AFP volume name
    char *v_password;           // Volume password

    // Volume attributes
    uint16_t v_flags;           // Volume flags
    mode_t v_perm;              // Default permissions
    mode_t v_dperm;             // Default directory permissions

    // CNID information
    struct _cnid_module *v_cdb; // CNID database handle

    // Extended attributes
    char *v_cnidscheme;         // CNID scheme
    char *v_dbpath;             // Database path

    // Spotlight support
    int v_qfd;                  // Query file descriptor
};

Directory Entry Cache

Optimizes directory operations:

struct dir {
    struct dir *d_parent;       // Parent directory
    struct dir *d_child;        // First child directory
    struct dir *d_next;         // Next sibling directory

    char *d_m_name;             // Mac filename
    char *d_u_name;             // Unix filename

    cnid_t d_did;               // Directory ID (CNID)
    dev_t d_dev;                // Device number
    ino_t d_inode;              // Inode number

    time_t d_ctime;             // Creation time
    time_t d_mtime;             // Modification time

    uint16_t d_rights;          // Access rights cache
    struct dir *d_fullpath;     // Full path cache
};

AFP Command Implementation

Implementation Files

Command Processing Flow

sequenceDiagram
    participant Client as Mac Client
    participant DSI as DSI Handler
    participant Switch as Command Switch
    participant Handler as AFP Handler
    participant Volume as Volume Manager
    participant CNID as CNID System

    Client->>DSI: AFP Command (FPOpenVol)
    DSI->>Switch: Parse command
    Switch->>Handler: Route to handler
    Handler->>Volume: Validate volume access
    Volume->>CNID: Initialize CNID DB
    CNID-->>Volume: Database ready
    Volume-->>Handler: Volume opened
    Handler-->>Switch: Success response
    Switch-->>DSI: Format response
    DSI-->>Client: AFP Response

Key AFP Commands

Session Management

Volume Operations

File Operations

Directory Operations

Integration Points

Implementation Files

CNID Database Integration

// CNID operations in afpd
cnid_t cnid_add(struct cnid_db *cdb, 
                const struct stat *st,
                cnid_t did, 
                const char *name, 
                size_t len, 
                char *hint);

cnid_t cnid_get(struct cnid_db *cdb, 
                cnid_t did, 
                const char *name, 
                size_t len);

int cnid_update(struct cnid_db *cdb, 
                cnid_t id, 
                const struct stat *st,
                cnid_t did, 
                const char *name, 
                size_t len);

Authentication Module Interface

// UAM registration and usage
struct uam_export {
    int uam_setup;
    int uam_checkuser;
    int uam_login;
    int uam_logincont;
    int uam_logout;
};

// UAM module loading
void *uam_load(const char *path, const char *name);
struct uam_export *uam_attach(void *handle, const char *name);

Volume Configuration

// Volume initialization
struct vol *getvolbyvid(const uint16_t vid);
struct vol *getvolbypath(const char *path);
int load_volumes(struct AFPObj *obj);
void unload_volumes(struct AFPObj *obj);

Performance Optimizations

Implementation Files

Connection Management

I/O Optimizations

Directory Caching

Configuration Options

Implementation Files

Network Configuration

Authentication Configuration

Volume Configuration

Debugging and Monitoring

Implementation Files

Log Categories

Diagnostic Tools

Common Issues

The afpd daemon serves as the foundation of Netatalk’s AFP functionality, providing a robust and efficient implementation of the Apple Filing Protocol while maintaining compatibility across different Mac generations and network configurations.

Footnotes

This is a mirror of the Netatalk GitHub Wiki

Last updated 2025-12-27