Scripts on ip addresses should work since ES 5.2.0, see
and the example:
Alternatively you could also categorize the IP address at index time and then use a simple keyword field to store the class c information, and then use a term aggregation on that field to do the count.