Sunteți pe pagina 1din 8

Ministerul Educaiei al Republicii Moldova Universitatea Tehnic a Moldovei Facultatea Calculatoare Informatica i Microelectronic Catedra Automatic i Tehnologii Informaionale

Disciplina: Administrarea Reelelor

RAPORT Lucrare de laborator Nr.1 Tema: Subnetarea

A efectuat : A verificat :

studentul grupei TI-101 Dovgaliuc Victor lector superior Peca Ludmila

Chiinu 2013

1. Scopul lucrrii Studierea practic a subnetrii i elaborarea unui program care are calcula ip disponibile.

2. Indicaii teoretice Adresele IPv4 au o lungime de 32 de bii (4 octei). Fiecare adres identific o reea (network) i o staie de lucru (work station) din cadrul reelei. Notaia obinuit este obinut prin scrierea fiecrui octet n form zecimal, separai ntre ei prin puncte. De exemplu, 192.168.0.1(10) este notaia folosit pentru adresa
Clase de adrese[modificare]

La nceputurile Internetului, adresele IPv4 se mpreau n 5 clase de adrese, notate de la A la E. mprirea se fcea n funcie de configuraia binar a primului octet al adresei, astfel:
Clasa Primul octet n binar Prima adres Ultima adres Observaii

0xxxxxxx

0.0.0.1

127.255.255.255

folosete 8 bii pentru reea i 24 pentru staia de lucru

10xxxxxx

128.0.0.0

191.255.255.255 folosete 16 bii pentru reea i 16 pentru staie

110xxxxx

192.0.0.0

223.255.255.255 folosete 24 bii pentru reea i 8 pentru staie

1110xxxx

224.0.0.0

239.255.255.255 folosit pentru adresarea de tipmulticast

11110xxx

240.0.0.0

255.255.255.255 utilizat n scopuri experimentale

Subreele Att adresele IPv4 ct i cele IPv6 folosesc subnetarea, care const n mprirea adresei IP n dou pri: adresa de reea i adresa de staie. Folosind o masc de reea, calculatorul poate determina unde s mpart adresa IP (conform standardului RFC 950).

Subnetarea a aprut ca soluie pentru problema epuizrii spaiului de adrese IP. Odat cu subreelele a aprut distincia ntre adresarea "classfull" (care ine cont de clasele de adrese) i adresarea "classless" (care ofer suportul pentru cmpul de subreea). n 1992 au fost introduse i mecanismele de rutare pentru adresa rea classless. Aceste mecanisme vizau att protocoalele de rutare (CIDR), ct i protocoalele rutate (VLSM). 3.Realizarea Sarcinii Datele despre adres si masc sunt transformate in binar i prelucrate i reprezentate in 2 moduri: binar si zecimal. Subnetul se calculeaz n urmtorul mod 172.16.17.30 - 10101100.00010000.00010001.00011110 255.255.240.0 - 11111111.11111111.11110000.00000000 -----------------| sub|-----------subnet = 10101100.00010000.00010000.00000000 = 172.16.16.0

Fig. 1

Conculzie n aceast lucrare de laborator am elaborat un program care permite utilizarea mai eficient a Ip-urilor, reducnd astfel epuizarea adreselor ip cu ajutorul mtii de reea.

Anexa 1.
package md.victordov.lab; import md.victordov.lab.exceptions.IpException; import java.util.ArrayList; import java.util.Formatter; import java.util.List; /** * Created with IntelliJ IDEA. * User: victor * Date: 9/15/13 * Time: 7:34 PM * To change this template use File | Settings | File Templates. */ public class IpV4 { public IpV4(String ip, String mask) throws IpException { if (ip.indexOf("/") > -1) { String substrIp = ip.substring(0, ip.lastIndexOf("/")); this.ip = qToString(substrIp, 'i'); this.mask = maskFromSlash(ip.substring(ip.lastIndexOf("/") + 1)); } else { this.ip = qToString(ip, 'i'); this.mask = maskToString(mask); } } public String qToString(String q, char im) throws IpException { String[] ipArr = q.split("\\."); StringBuilder sb = new StringBuilder(); if (ipArr.length == 4) { for (int i = 0; i < ipArr.length; i++) { Long value = null; try{ value = Long.parseLong(ipArr[i]); } catch (NumberFormatException nfe){ throw new IpException("invalid ip, ex: 192.168.0.1 or 11.22.33.44/28"); } if (value != (value & 0xff)) { if(im == 'i') { throw new IpException("Invalid Ip address: " + ipArr[i]); } else{ throw new IpException("Invalid mask value: " + ipArr[i]); } } sb.append(String.format("%8s", Long.toBinaryString(value)).replace(' ', '0')); } } else { if(im == 'i'){ throw new IpException("invalid ip, ex: 192.168.0.1 or 11.22.33.44/28"); } else{

throw new IpException("Invalid mask, ex: 255.255.255.0"); } } return sb.toString(); } public String maskToString(String st) throws IpException { char[] a = qToString(st, 'm').toCharArray(); boolean existsOne = true; for (int i = 0; i < 32; i++) { if (a[i] == '1') { if (existsOne == false) { throw new IpException("Invalid mask, ex: 255.255.255.0"); } } else { existsOne = false; } } return String.copyValueOf(a); } public String maskFromSlash(String mask) throws IpException { Long ones = null; try{ ones = Long.parseLong(mask); } catch (NumberFormatException nfe){ throw new IpException("Invalid mask or empty, ex: 255.255.255.0"); } if (ones > 32 || ones <1) { throw new IpException("Invalid mask: 32 >= mask > 0"); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < 32; i++) { if (ones-- > 0) { sb.append("1"); } else { sb.append("0"); } } return sb.toString(); }

public Integer countMask() { char[] a = this.mask.toCharArray(); Integer count = 0; for (int i = 0; i < 32; i++) { if (a[i] == '1') { count++; } } return count; } public String getNetwork() { Integer from = 32 - countMask();

String formatPattern = "%" + from + "s"; Long ipToConvert = Long.parseLong(this.ip.substring(this.ip.length() - ((int) (long) from)), 2); Long maskToConvert = Long.parseLong(this.mask.substring(this.mask.length() - ((int) (long) from)), 2); Long result = ipToConvert & maskToConvert; StringBuilder sb = new StringBuilder(); sb.append(this.ip.substring(0, this.ip.length() - ((int) (long) from))); sb.append(String.format(formatPattern, Long.toBinaryString(result)).replace(' ', '0')); return sb.toString(); } public String getHostMin() { Long nr = Long.parseLong(getNetwork(), 2); return String.format("%32s", Long.toBinaryString(nr + 1)).replace(' ', '0'); } public String getBroadcast() { Integer from = 32 - countMask(); String formatPattern = "%" + from + "s"; Integer ipToConvert = Integer.parseInt(this.ip.substring(this.ip.length() - from), 2); Integer maskToConvert = Integer.parseInt(this.mask.substring(this.mask.length() - from), 2); StringBuilder sb = new StringBuilder(); String s = ""; for (int i = 0; i < from; i++) { s += "1"; } int result = Integer.parseInt(s, 2); sb.append(this.ip.substring(0, this.ip.length() - ((int) (long) from))); sb.append(String.format(formatPattern, Integer.toBinaryString(result)).replace(' ', '0')); return sb.toString(); } public String getHostMax() { Long nr = Long.parseLong(getBroadcast(), 2); return String.format("%32s", Long.toBinaryString(nr - 1)).replace(' ', '0'); } public static String getHex(String s) throws NumberFormatException { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 32; i += 8) { sb.append(Integer.parseInt(s.substring(i, i + 8), 2)); if (i < 23) { sb.append("."); } } return sb.toString(); } public static String putDots(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { if (i != 0 && i % 8 == 0) { sb.append("."); sb.append(s.charAt(i)); } else {

sb.append(s.charAt(i)); } } return sb.toString(); }

public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getMask() { return mask; } public void setMask(String mask) { this.mask = mask; } String ip; String mask; }