This paper analyzes and designs medium access control (MAC) protocols for wireless ad-hoc networks through the network utility maximization (NUM) framework. We first reverse-engineer the current exponential backoff (EB) type of MAC protocols such as the BEB (binary exponential backoff) in the IEEE 802.11 standard through a non-cooperative gametheoretic model. This MAC protocol is shown to be implicitly maximizing, using a stochastic subgradient, a selfish local utility at each link in the form of expected net reward for successful transmission. While the existence of a Nash equilibrium can be established, neither convergence nor social welfare optimality is guaranteed due to the inadequate feedback mechanism in the EB protocol. This motivates the forward-engineering part of the paper, where a network-wide utility maximization problem is formulated, using a collision and persistence probability model and aligning selfish utility with total social welfare. By adjusting the parameters in the utility objective functions of the NUM problem, we can also control the tradeoff between efficiency and fairness of radio resource allocation through a rigorous and systematic design. We develop two distributed algorithms to solve the MAC design NUM problem, which lead to random access protocols that have slightly more message passing overhead than the current EB protocol, but significant potential for efficiency and fairness improvement. We provide readily-verifiable sufficient conditions under which convergence of the proposed algorithms to a global optimality of network utility can be guaranteed, and through numerical examples illustrate the value of the NUM approach to the complexity-performance tradeoff in MAC design.