Hamiltonian approach to the magnetostatic equilibrium problem