Do not use on any
shared computer
August 29, 2008 11:48pm pdt
 
[x]
Attachment Details

C Makefile error on Solaris

Tags: C
Hi!

I'm trying to compile my server written in C under solaris by using a Makefile...

This is my makefile:

# default

OS = cygwin

CASESENSITIVE = yes



# messaggi di informazione

ifeq ($(TYPE),)

CFLAGS = -O -Wall -Werror

LDFLAGS = -s $(EXTRALIBS)

endif

ifeq ($(TYPE),trace)

CFLAGS =-Wall -Werror -DTRACE -g

LDFLAGS = $(EXTRALIBS)

endif

ifeq ($(TYPE),verbose)

CFLAGS =-Wall -Werror -DTRACE -DVERBOSE -g

LDFLAGS = $(EXTRALIBS)

endif



# non case sensitive

ifeq ($(CASESENSITIVE),no)

CFLAGS += -DNO_CASE_SENSITIVE

endif



# opzioni dipendenti dal SO

ifeq ($(OS),solaris)

EXTRALIBS = -lsocket -lnsl -lresolv

endif

ifeq ($(OS),cygwin)

EXTRALIBS =

endif



# general def's

CC = gcc

LIBS = hw1lib.a

SOURCES = server.c

PROG = server



.PHONY: all clean veryclean



all: $(PROG)



hw1lib.a: errlib.o sockwrap.o

      ar -r hw1lib.a errlib.o sockwrap.o

      ranlib hw1lib.a



server: server.o $(LIBS)



sockwrap:

      gcc -c sockwrap.c

      

errlib:

      gcc -c errlib.c



clean:

      /bin/rm -f *.o



veryclean:

      /bin/rm -f *.o *.a *~






and i always get this error:


user@cclix2$ make                                                      
gcc -O -Wall -Werror   -c -o server.o server.c
gcc -O -Wall -Werror   -c -o errlib.o errlib.c
gcc -O -Wall -Werror   -c -o sockwrap.o sockwrap.c
ar -r hw1lib.a errlib.o sockwrap.o
ar: creating hw1lib.a
ranlib hw1lib.a
gcc -s   server.o hw1lib.a   -o server
Undefined                       first referenced
 symbol                             in file
bind                                server.o
getsockname                         hw1lib.a(sockwrap.o)
accept                              server.o
listen                              server.o
gethostbyname                       hw1lib.a(sockwrap.o)
sendto                              hw1lib.a(sockwrap.o)
socket                              server.o
setsockopt                          server.o
connect                             hw1lib.a(sockwrap.o)
hstrerror                           hw1lib.a(sockwrap.o)
recvfrom                            hw1lib.a(sockwrap.o)
inet_pton                           hw1lib.a(sockwrap.o)
inet_ntoa                           server.o
inet_ntop                           hw1lib.a(sockwrap.o)
h_errno                             hw1lib.a(sockwrap.o)
shutdown                            hw1lib.a(sockwrap.o)
ld: fatal: Symbol referencing errors. No output written to server
collect2: ld returned 1 exit status
make: *** [server] Error 1

Can you explain me why i continue to get this error?
My makefile works fine under Max and cygwin... but it gives me some problems under Solaris...

My sources files are here: http://lnx.mangaitalia.net/server/hw1.zip
and i'm trying to post them below, too

what do you think i have to fix?
thanks!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529:
530:
531:
532:
533:
534:
535:
536:
537:
538:
539:
540:
541:
542:
543:
544:
545:
546:
547:
548:
549:
550:
551:
552:
553:
554:
555:
556:
557:
558:
559:
560:
561:
562:
563:
564:
565:
566:
567:
568:
569:
570:
571:
572:
573:
574:
575:
576:
577:
578:
579:
580:
581:
582:
583:
584:
585:
586:
587:
588:
589:
590:
591:
592:
593:
594:
595:
596:
597:
598:
599:
600:
601:
602:
603:
604:
605:
606:
607:
608:
609:
610:
611:
612:
613:
614:
615:
616:
617:
618:
619:
620:
621:
622:
623:
624:
625:
626:
627:
628:
629:
630:
631:
632:
633:
634:
635:
636:
637:
638:
639:
640:
641:
642:
643:
644:
645:
646:
647:
648:
649:
650:
651:
652:
653:
654:
655:
656:
657:
658:
659:
660:
661:
662:
663:
664:
665:
666:
667:
668:
669:
670:
671:
672:
673:
674:
675:
676:
677:
678:
679:
680:
681:
682:
683:
684:
685:
686:
687:
688:
689:
690:
691:
692:
693:
694:
695:
696:
697:
698:
699:
700:
701:
702:
703:
704:
705:
706:
707:
708:
709:
710:
711:
712:
713:
714:
715:
716:
717:
718:
719:
720:
721:
722:
723:
724:
725:
726:
727:
728:
729:
730:
731:
732:
733:
734:
735:
736:
737:
738:
739:
740:
741:
742:
743:
744:
745:
746:
747:
748:
749:
750:
751:
752:
753:
754:
755:
756:
757:
758:
759:
760:
761:
762:
763:
764:
765:
766:
767:
768:
769:
770:
771:
772:
773:
774:
775:
776:
777:
778:
779:
780:
781:
782:
783:
784:
785:
786:
787:
788:
789:
790:
791:
792:
793:
794:
795:
796:
797:
798:
799:
800:
801:
802:
803:
804:
805:
806:
807:
808:
809:
810:
811:
812:
813:
814:
815:
816:
817:
818:
819:
820:
821:
822:
823:
824:
825:
826:
827:
828:
829:
830:
831:
832:
833:
834:
835:
836:
837:
838:
839:
840:
841:
842:
843:
844:
845:
846:
847:
848:
849:
850:
851:
852:
853:
854:
855:
856:
857:
858:
859:
860:
861:
862:
863:
864:
865:
866:
867:
868:
869:
870:
871:
872:
873:
874:
875:
876:
877:
878:
879:
880:
881:
882:
883:
884:
885:
886:
887:
888:
889:
890:
891:
892:
893:
894:
895:
896:
897:
898:
899:
900:
901:
902:
903:
904:
905:
906:
907:
908:
909:
910:
911:
912:
913:
914:
915:
916:
917:
918:
919:
920:
921:
922:
923:
924:
925:
926:
927:
928:
929:
930:
931:
932:
933:
934:
935:
936:
937:
938:
939:
940:
941:
942:
943:
944:
945:
946:
947:
948:
949:
950:
951:
952:
953:
954:
955:
956:
957:
958:
959:
960:
961:
962:
963:
964:
965:
966:
967:
968:
969:
970:
971:
972:
973:
974:
975:
976:
977:
978:
979:
980:
981:
982:
983:
984:
985:
986:
987:
988:
989:
990:
991:
992:
993:
994:
995:
996:
997:
998:
999:
1000:
1001:
1002:
1003:
1004:
1005:
1006:
1007:
1008:
1009:
1010:
1011:
1012:
1013:
1014:
1015:
1016:
1017:
1018:
1019:
1020:
1021:
1022:
1023:
1024:
1025:
1026:
1027:
1028:
1029:
1030:
1031:
1032:
1033:
1034:
1035:
1036:
1037:
1038:
1039:
1040:
1041:
1042:
1043:
1044:
1045:
1046:
1047:
1048:
1049:
1050:
1051:
1052:
1053:
1054:
1055:
1056:
1057:
1058:
1059:
1060:
1061:
1062:
1063:
1064:
1065:
1066:
1067:
1068:
1069:
1070:
1071:
1072:
1073:
1074:
1075:
1076:
1077:
1078:
1079:
1080:
1081:
1082:
1083:
1084:
1085:
1086:
1087:
1088:
1089:
1090:
1091:
1092:
1093:
1094:
1095:
1096:
1097:
1098:
1099:
1100:
1101:
1102:
1103:
1104:
1105:
1106:
1107:
1108:
1109:
1110:
1111:
1112:
1113:
1114:
1115:
1116:
1117:
1118:
1119:
1120:
1121:
1122:
1123:
1124:
1125:
1126:
1127:
1128:
1129:
1130:
1131:
1132:
1133:
1134:
1135:
1136:
1137:
1138:
1139:
1140:
1141:
1142:
1143:
1144:
1145:
1146:
1147:
1148:
1149:
1150:
1151:
1152:
1153:
1154:
1155:
1156:
1157:
1158:
1159:
1160:
1161:
1162:
1163:
1164:
1165:
1166:
1167:
1168:
1169:
1170:
1171:
1172:
1173:
1174:
1175:
1176:
1177:
1178:
1179:
1180:
1181:
1182:
1183:
1184:
1185:
1186:
1187:
1188:
1189:
1190:
1191:
1192:
1193:
1194:
1195:
1196:
1197:
1198:
1199:
1200:
1201:
1202:
1203:
1204:
1205:
1206:
1207:
1208:
1209:
1210:
1211:
1212:
1213:
1214:
1215:
1216:
1217:
1218:
1219:
1220:
1221:
1222:
1223:
1224:
1225:
1226:
1227:
1228:
1229:
1230:
1231:
1232:
1233:
1234:
1235:
1236:
1237:
1238:
1239:
1240:
1241:
1242:
1243:
1244:
1245:
1246:
1247:
1248:
1249:
1250:
1251:
1252:
1253:
1254:
1255:
1256:
1257:
1258:
1259:
1260:
1261:
1262:
1263:
1264:
1265:
1266:
1267:
1268:
1269:
1270:
1271:
1272:
1273:
1274:
1275:
1276:
1277:
1278:
1279:
1280:
1281:
1282:
1283:
1284:
1285:
1286:
1287:
1288:
1289:
1290:
1291:
1292:
1293:
1294:
1295:
1296:
1297:
1298:
1299:
1300:
1301:
1302:
1303:
1304:
1305:
1306:
1307:
1308:
1309:
1310:
1311:
1312:
1313:
1314:
1315:
1316:
1317:
1318:
1319:
1320:
1321:
1322:
1323:
1324:
1325:
1326:
1327:
1328:
1329:
1330:
1331:
1332:
1333:
1334:
1335:
1336:
1337:
1338:
1339:
1340:
1341:
1342:
1343:
1344:
1345:
1346:
1347:
1348:
1349:
1350:
1351:
1352:
1353:
1354:
1355:
1356:
1357:
1358:
1359:
1360:
1361:
1362:
1363:
1364:
1365:
1366:
1367:
1368:
1369:
1370:
1371:
1372:
1373:
1374:
1375:
1376:
1377:
1378:
1379:
1380:
1381:
1382:
1383:
1384:
1385:
1386:
1387:
1388:
1389:
1390:
1391:
1392:
1393:
1394:
1395:
1396:
1397:
1398:
1399:
1400:
1401:
1402:
1403:
1404:
1405:
1406:
1407:
1408:
1409:
1410:
1411:
1412:
1413:
1414:
1415:
1416:
1417:
1418:
1419:
1420:
1421:
1422:
1423:
1424:
1425:
1426:
1427:
1428:
1429:
1430:
1431:
1432:
1433:
1434:
1435:
1436:
1437:
1438:
1439:
1440:
1441:
1442:
1443:
1444:
1445:
1446:
1447:
1448:
1449:
1450:
1451:
1452:
1453:
1454:
1455:
1456:
1457:
1458:
1459:
1460:
1461:
1462:
1463:
1464:
1465:
1466:
1467:
1468:
1469:
1470:
1471:
1472:
1473:
1474:
1475:
1476:
1477:
1478:
1479:
1480:
1481:
1482:
1483:
1484:
1485:
1486:
1487:
1488:
1489:
1490:
1491:
1492:
1493:
1494:
1495:
1496:
1497:
1498:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>       
#include <unistd.h>           
#include <ctype.h>
#include <sys/wait.h>
#include "sockwrap.h"
#include "errlib.h"
#if 0
#include "sockwrap_hex.gh" // in order to do debug
#endif
 
/***********************************************************************************************
**                       List of Defines used for the messages                               **
***********************************************************************************************/
 
#define MAX_LINE 1000 /* mainly for array size */
#define HERE "HERE"
#define WHO "WHO?"
#define NOOB "NOOB"
#define PASS "PASS"
#define BYE "BYE!"
#define FAIL "FAIL"
#define NAUT "NAUT"
#define FULL "FULL"
#define GOOD "GOOD"
#define ALERT "ALERT"
#define WHAT "????"
 
/***********************************************************************************************
**                       Linked List Struct                                                  **
***********************************************************************************************/
 
struct NODE {
	int code; 
	char name[1000];
	struct NODE *next;
	int size;
	int line;
};
 
 
/***********************************************************************************************
**                      Global Variables                                                     **
***********************************************************************************************/
 
struct NODE *llist;
 
int size = 0;
int auth = 0;
int linea = 0;
char *prog;
 
 
/***********************************************************************************************
**                       Function prototypes                                                  **
***********************************************************************************************/
 
int  search_value(struct NODE *llist, int text_line, int fd);
void append_node(struct NODE *llist, char* name, int num, int linea);
void display_list(struct NODE *llist, int fd);
int verify_login(char *, char *);
void clean_list(struct NODE *llist);
void listen_client_requests(int fd);
void load_file_in_list(int fd);
 
 
/***********************************************************************************************
**                       Strings which server sends                                           **
***********************************************************************************************/
 
void sendHERE(int fd){
	Writen(fd, HERE, strlen(HERE));
}
 
void sendWHO(int fd){
	Writen(fd, WHO, strlen(WHO));
}
 
void sendNOOB(int fd){
	Writen(fd, NOOB, strlen(NOOB));
}
 
void sendPASS(int fd){
	Writen(fd, PASS, strlen(PASS));
}
 
void sendFAIL(int fd){
	Writen(fd, FAIL, strlen(FAIL));
}
 
void sendGOOD(int fd){
	Writen(fd, GOOD, strlen(GOOD));
}
 
void sendNAUT(int fd){
	Writen(fd, NAUT, strlen(NAUT));
}
 
void sendALERT(int fd){
	Writen(fd, ALERT, strlen(ALERT));
}
 
void sendFULL(int fd){
	Writen(fd, FULL, strlen(FULL));
}
 
void sendBYE(int fd){
	Writen(fd, BYE, strlen(BYE));
}
 
void sendWHAT(int fd){
	Writen(fd, WHAT, strlen(WHAT));
}
 
/***********************************************************************************************
**                    In order to display the list when server receives LIST                  **
***********************************************************************************************/
 
void display_list(struct NODE *llist, int fd)
{
	
	char stringa[MAX_LINE];
	
	if (llist->next == NULL )
		sendNOOB(fd);  
	
	if(llist->next != NULL)
	{
		
		sendHERE(fd);
		int i;
		
		for(i=0; i < (size-1); i++)
		{
			
			llist = llist->next;
			snprintf(stringa, sizeof(stringa), "%s\r\n", llist->name);
			Writen(fd, stringa, strlen(stringa));
			
		}
		
		llist = llist->next;
		snprintf(stringa, sizeof(stringa), "%s\r\n\r\n", llist->name);
		Writen(fd, stringa, strlen(stringa));
		
	}  
	
	
}
 
/***********************************************************************************************
**                  I store the content of text file in my Linked List                        **
***********************************************************************************************/
 
void append_node(struct NODE *llist, char * name, int num, int line)
{
	
	while(llist->next != NULL) 
	llist = llist->next; 
	llist->next = (struct NODE *)malloc(sizeof(struct NODE));
	llist->next->code = num;
	llist->next->line = line;
	strcpy((char*)llist->next->name, (char*)name);
	llist->next->next = NULL;
	
}
 
 
/***********************************************************************************************
**                  Search for Values present at a given text line                           **
***********************************************************************************************/
 
int search_value(struct NODE *llist, int text_line, int fd)
{
		
	
	while(llist != NULL)
	{
		
		if(llist->line == text_line)
		{ 			
			
			return llist->code;
			break;		                	
		}	 
		 
		llist = llist->next;
		
	}
	
	return 65535;
	
}
 
 
/***********************************************************************************************
**                                 Free all the list                                          **
***********************************************************************************************/
 
void clean_list(struct NODE *llist)
{
		
	
	while(llist != NULL)
	{	
		
		struct NODE *tmp = llist->next;
            free(llist);
            llist = tmp;
		
	}
	
	
}
 
 
/***********************************************************************************************
**                                  Children Handling                                         **
***********************************************************************************************/
 
int count = 0; 
 
void signal_handler(int sig)
{
	pid_t pid;
	int status;
	
	/* for solaris and unix system */
	signal(sig, signal_handler);
	
	while( (pid = waitpid(-1,&status,WNOHANG)) > 0)
	{		    
		count--; /* child processes counter */
	}
	
}
 
 
/***********************************************************************************************
**                  Variables for parameters and user control                                **
***********************************************************************************************/
 
int your_choise, use_port = 1, use_child_max = 1, child_max;
char *objs_file = NULL;
char *usrs_file = NULL;
unsigned char msg[1];
 
 
/***********************************************************************************************
**          The main() program: opens and receives new connections                          **
***********************************************************************************************/
 
 
int main (int argc, char *argv[])
{
	int port = 0, sock, fd, value;
	unsigned int client_len;
	struct sockaddr_in server, client;
	char host[40];  
	
	
	int c;
	
	opterr = 0;
	
	while ((c = getopt (argc, argv, "p:n:f:")) != -1)
		switch (c)
	{
		case 'p':
			port = atoi(optarg);
			use_port = 0;
			if ( port <= 0){
				printf("Errore: Numero di porta errato!\n");
				exit(3);
			}
			break;
			case 'n':
			child_max = atoi(optarg);
			use_child_max = 0;
			if( child_max <= 0){
				printf("Errore: Numero massimo di processi errato!\n");
				exit(3);
			}
			break;
			case 'o':
			objs_file = optarg;
			break;
			case 'u':
			usrs_file = optarg;
			break;
			case '?':
			if (optopt == 'o')
				fprintf (stderr, "il parametro -%c richiede come argomento il nome di un file di testo.\n", optopt);
			if (optopt == 'u')
				fprintf (stderr, "il parametro -%c richiede come argomento il nome di un file di testo.\n", optopt);
			if (optopt == 'p')
				fprintf (stderr, "il parametro -%c per la scelta della porta richiede come argomento un numero intero.\n", optopt);
			if (optopt == 'n')
				fprintf (stderr, "il parametro -%c per la scelta del numero massimo di processi richiede come argomento un numero intero.\n", optopt);
			else if (isprint (optopt)){
				fprintf (stderr, "\nComando sconosciuto `-%c'.\n", optopt);
				printf("\nUsage:\n%s -pPORT -nCHILD_MAX -uTEXT_FILE -oTEXT_FILE\n\n", argv[0]);
				exit(1);
			}
			else
				fprintf (stderr,
						 "Opzione carattere non riconosciuta `\\x%x'.\n",
						 optopt);
			return 1;
			default:
			abort ();
	}          
	
	
	
	if (objs_file == NULL){
		objs_file = "obj.txt";
	}
	
	if (usrs_file == NULL){
		usrs_file = "usr.dat";
	}
	
	if (use_port == 1){ 
		port = 1234;
	}
	
	if (use_child_max == 1){
		child_max = 2;
	}	  	  
	
	setbuf(stdout,NULL);		/* serve per settare a unbuffered stdout */
	
	prog = argv[0];
	
	sock = socket(AF_INET, SOCK_STREAM, 0);  /* SOCK_STREAM per TCP; */
	
	if (sock < 0)
	{
		perror("Creating a stream socket");
		exit(1);
	}
	
	
	server.sin_family = AF_INET; 
	server.sin_addr.s_addr = htonl(INADDR_ANY); 
	
	server.sin_port = htons(port);
	
	/* call SO_REUSEADDR before bind() */
	value = 1;
	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&value, sizeof(value)) < 0)
	{
		perror("during setsockopt");
		exit(5);
	}
	
	
	if (bind(sock, (struct sockaddr *) &server, sizeof(server)) < 0) 
	{
		perror("binding socket");
		exit(2); 
	}
	
	listen(sock, 10); 
	
	
	//signal(SIGTSTP, signal_handler); /* trap CTRL-Z signal */ 
	signal(SIGCHLD, signal_handler); 
	
	pid_t pid;
	
	while (1) { 
		
		client_len = sizeof(client);
		printf("\nServer in attesa\n");
		
		if ((fd = accept(sock, (struct sockaddr *) &client, &client_len)) < 0) {
			
			//perror("accepting connection");	per eventuali debug/test
			continue;			
			//exit(3);
		}
		
		
		if ( count < child_max)
		{
		
			count ++;
			sendWHO(fd);
			
			if ( (pid = fork()) == 0 )
			{		
				
				gethostname(host, 40);
				printf("Accepted client: IP= %s, port=%d, hostname= %s\n",
				inet_ntoa(client.sin_addr),
				ntohs(client.sin_port), host);
				close(sock);
				listen_client_requests(fd); 
				close(fd);
				
				exit(0);	
						/* when a child ends */
			} 
			
			else
				close(fd);
				
		}
		
		else if (count >= child_max)
		{
			sendFULL(fd);
			close(fd);
		}
		
		
	}
	
	return 0;
}
 
 
 
/***********************************************************************************************
**                         Add and Manage a Client                                            **
***********************************************************************************************/
 
 
void listen_client_requests(int fd)
{
	load_file_in_list(fd);
	char buffer[5];
	int i = 0;	
	char *username = (char *)malloc(18*sizeof(char));
	char *password = (char *)malloc(18*sizeof(char));
	char *user = (char *)malloc(18*sizeof(char));
	char *passwd = (char *)malloc(18*sizeof(char));
		
	
	while(1)
    {
		Readn(fd,buffer,4*sizeof(char));
		
		
		
			  if ( strncmp(buffer, "HELO", 4*sizeof(char)) == 0)
				{ 					
					memset(username,0,18*sizeof(char));
                    memset(password,0,18*sizeof(char));
					
					for(i=1;i<=18;i++)
					{                      
					    Readn(fd,&(username[i-1]),1*sizeof(char));
                     
					    if( i > 1)					  
					    {
                      
					    if(username[i-1]=='\n' && username[i-2]=='\r') i=19;
                      
					    }
					  
                    }
					
					user = strtok(username, "\r\n");
					
					sendPASS(fd);					
					
					for(i=1;i<=18;i++)
					{
					
                       Readn(fd,&(password[i-1]),1*sizeof(char));
                      
					  if(i > 1)
					  {
					  
                       if(password[i-1]=='\n' && password[i-2]=='\r') i=19;
					   
                      }
					  
                    }					
					
					passwd = strtok(password, "\r\n");
					
				
					if (verify_login(username, password)==1)
					{
						auth=1;
						sendGOOD(fd);
					}
					
					else if(verify_login(username, password)==-1)
					{
						auth=0;
						sendFAIL(fd);
					}  
					
					
					
				}				
				
				else if ( strncmp(buffer, "LIST", 4) == 0)
				{  
					display_list(llist, fd);
					
				}
				
				else if ( strncmp(buffer, "QUIT", 4) == 0)
				{  
					sendBYE(fd);
					clean_list(llist);
					
					return;
				}
				
				else if(strncmp(buffer, "COST", 4) == 0)
				{  
					
					uint16_t arr;
					unsigned int val=0;								
					int found;
					uint16_t net;   
					
					if (auth == 0) 
					{
						sendNAUT(fd);
					}
					
					
					else if (llist ->next != NULL)
					{   
					    	
						sendHERE(fd);
						while(1)
						{
                        Readn(fd, &arr, 1*sizeof(uint16_t));
                        val = ntohs(arr);
                        if ( val != 0)
						{
							Writen(fd, &arr, 2);
						}
						else if ( val == 0)
						{
							break;
						}
						
						found = search_value(llist, val, fd);       
						
						if ( found != 0 )
						{
							net = htons(found);                               
							Writen(fd, &net, sizeof(net));
						}			
		
							
						}   
						                            
					}
				}
				
			else 
			{
			sendWHAT(fd);
			}					
				
    }
		
				
}
 
 
/***********************************************************************************************
**                       Open and Load data into the Linked List                              **
***********************************************************************************************/
 
void load_file_in_list(fd)
{
	
	llist = (struct NODE *)malloc(sizeof(struct NODE));
	llist->code = 0;
	llist->next = NULL;
	llist->size = 0;
	llist->line = 0;  
	
	FILE * pFile;
	char buffer [1000];
	int code;
	char name[1000];
	
	
	pFile = fopen (objs_file , "r");
	
	if (pFile == NULL)
	{
		perror ("Error opening file");
	}
	else
	{
		while ( fgets (buffer , 100 , pFile) != NULL )
		{
			
			sscanf(buffer,"%d %s \n", &code, name);  
			linea++;
			append_node(llist, name, code, linea); 
			size++;
		}
		
		fclose (pFile);
	}
	
}
 
int verify_login(char * usern, char * pass)
{
	
	char entry[32];
	char username[17] = { 0 }; 
	char password[17] = { 0 };
	char * temp = (char *)malloc(17*sizeof(char));;
	
	FILE * passfile = fopen(usrs_file, "r");
	
	if ( passfile == NULL)
	{
		return -1;	
	}   
	
	
	while (fread(entry, 1, 32, passfile))
	{
		
		strncpy (username, entry, 16);
		username[16]='\0';
		temp = strchr(username, '~');
		if (temp)
			*temp = '\0';
		
		strncpy (password, &entry[16], 16);
		password[16]='\0';
		temp = strchr(password, '~');
		if (temp)
			*temp = '\0';
		
		if (strcmp(usern, username) == 0 &&
			strcmp(pass, password) ==0 )
		{
			fclose(passfile);
			return 1;
		}
		
	}
	
	fclose(passfile);
	return -1;
	
}
 
 
#### sockwrap.c ###
 
#include <stdlib.h> // getenv()
 
#include <sys/types.h>
 
#include <sys/socket.h>
 
#include <sys/time.h> // timeval
 
#include <sys/select.h>
 
#include <netinet/in.h>
 
#include <arpa/inet.h> // inet_aton()
 
#include <netdb.h>
 
#include <errno.h>
 
#include <unistd.h>
 
 
 
#include "errlib.h"
 
#include "sockwrap.h"
 
 
 
#define INTERRUPTED_BY_SIGNAL (errno == EINTR || errno == ECHILD)
 
 
 
extern char *prog;
 
 
 
int Socket (int family, int type, int protocol)
 
{
 
  int n;
 
  if ( (n = socket(family,type,protocol)) < 0)
 
    err_sys ("(%s) error - socket() failed", prog);
 
  return n;
 
}
 
 
 
void Bind (int sockfd, const SA *myaddr,  socklen_t myaddrlen)
 
{
 
  if ( bind(sockfd, myaddr, myaddrlen) != 0)
 
    err_sys ("(%s) error - bind() failed", prog);
 
}
 
 
 
void Listen (int sockfd, int backlog)
 
{
 
  char *ptr;
 
  if ( (ptr = getenv("LISTENQ")) != NULL)
 
    backlog = atoi(ptr);
 
  if ( listen(sockfd,backlog) < 0 )
 
    err_sys ("(%s) error - listen() failed", prog);
 
}
 
 
 
 
 
int Accept (int listen_sockfd, SA *cliaddr, socklen_t *addrlenp)
 
{
 
  int n;
 
again:
 
  if ( (n = accept(listen_sockfd, cliaddr, addrlenp)) < 0)
 
  {
 
    if (INTERRUPTED_BY_SIGNAL)
 
      goto again;
 
    else
 
      err_sys ("(%s) error - accept() failed", prog);
 
  }
 
  return n;
 
}
 
 
 
 
 
void Connect (int sockfd, const SA *srvaddr, socklen_t addrlen)
 
{
 
  if ( connect(sockfd, srvaddr, addrlen) != 0)
 
    err_sys ("(%s) error - connect() failed", prog);
 
}
 
 
 
 
 
void Close (int fd)
 
{
 
  if (close(fd) != 0)
 
    err_sys ("(%s) error - close() failed", prog);
 
}
 
 
 
 
 
void Shutdown (int fd, int howto)
 
{
 
  if (shutdown(fd,howto) != 0)
 
    err_sys ("(%s) error - shutdown() failed", prog);
 
}
 
 
 
 
 
ssize_t Read (int fd, void *bufptr, size_t nbytes)
 
{
 
  ssize_t n;
 
again:
 
  if ( (n = read(fd,bufptr,nbytes)) < 0)
 
  {
 
    if (INTERRUPTED_BY_SIGNAL)
 
      goto again;
 
    else
 
      err_sys ("(%s) error - read() failed", prog);
 
  }
 
  return n;
 
}
 
 
 
 
 
void Write (int fd, void *bufptr, size_t nbytes)
 
{
 
  if (write(fd,bufptr,nbytes) != nbytes)
 
    err_sys ("(%s) error - write() failed", prog);
 
}
 
 
 
 
 
ssize_t Recvfrom (int fd, void *bufptr, size_t nbytes, int flags, SA *sa, socklen_t *salenptr)
 
{
 
  ssize_t n;
 
 
 
  if ( (n = recvfrom(fd,bufptr,nbytes,flags,sa,salenptr)) < 0)
 
    err_sys ("(%s) error - recvfrom() failed", prog);
 
  return n;
 
}
 
 
 
 
 
ssize_t Recvfrom_timeout (int fd, void *bufptr, size_t nbytes, int flags, SA *sa, socklen_t *salenptr, int timeout)
 
{
 
  ssize_t n;
 
again:
 
  if ( (n = recvfrom(fd,bufptr,nbytes,flags,sa,salenptr)) < 0)
 
  {
 
    if (INTERRUPTED_BY_SIGNAL)
 
    {
 
      if (errno==EINTR && timeout)
 
        return -1;
 
      else
 
        goto again;
 
    }
 
    else
 
      err_sys ("(%s) error - recvfrom() failed", prog);
 
  }
 
  return n;
 
}
 
 
 
 
 
void Sendto (int fd, void *bufptr, size_t nbytes, int flags, const SA *sa, socklen_t salen)
 
{
 
  if (sendto(fd,bufptr,nbytes,flags,sa,salen) != nbytes)
 
    err_sys ("(%s) error - sendto() failed", prog);
 
}
 
 
 
/*
 
 * deprecated and no more supported in recent systems
 
 *
 
 
 
void Inet_aton (const char *strptr, struct in_addr *addrptr)
 
{
 
  if (inet_aton(strptr, addrptr) == 0)
 
    err_quit ("(%s) error - inet_aton() failed for '%s'", prog, strptr);
 
}
 
 
 
*/
 
 
 
void Inet_pton (int af, const char *strptr, void *addrptr)
 
{
 
  int status = inet_pton(af, strptr, addrptr);
 
  if (status == 0)
 
    err_quit ("(%s) error - inet_pton() failed for '%s': invalid address", prog, strptr);
 
  if (status < 0)
 
    err_sys ("(%s) error - inet_pton() failed for '%s'", prog, strptr);
 
}
 
 
 
 
 
void Inet_ntop (int af, const void *addrptr, char *strptr, size_t length)
 
{
 
  if ( inet_ntop(af, addrptr, strptr, length) == NULL)
 
     err_quit ("(%s) error - inet_ntop() failed: invalid address", prog);
 
}
 
 
 
 
 
#ifndef MAXLINE
 
#define MAXLINE 1024
 
#endif
 
 
 
/* reads exactly "n" bytes from a descriptor */
 
 
 
ssize_t readn (int fd, void *vptr, size_t n)
 
{
 
  size_t nleft;
 
  ssize_t nread;
 
  char *ptr;
 
 
 
  ptr = vptr;
 
  nleft = n;
 
  while (nleft > 0)
 
  {
 
    if ( (nread = read(fd, ptr, nleft)) < 0)
 
    {
 
      if (INTERRUPTED_BY_SIGNAL)
 
      {
 
        nread = 0;
 
        continue; /* and call read() again */
 
      }
 
      else
 
        return -1;
 
    }
 
    else
 
      if (nread == 0)
 
        break; /* EOF */
 
 
 
    nleft -= nread;
 
    ptr   += nread;
 
  }
 
  return n - nleft;
 
}
 
 
 
 
 
ssize_t Readn (int fd, void *ptr, size_t nbytes)
 
{
 
  ssize_t n;
 
 
 
  if ( (n = readn(fd, ptr, nbytes)) < 0)
 
    err_sys ("(%s) error - readn() failed", prog);
 
  return n;
 
}
 
 
 
 
 
/*
 
 
 
read a whole buffer, for performance, and then return one char at a time
 
 
 
*/
 
static ssize_t my_read (int fd, char *ptr)
 
{
 
  static int read_cnt = 0;
 
  static char *read_ptr;
 
  static char read_buf[MAXLINE];
 
 
 
  if (read_cnt <= 0)
 
  {
 
again:
 
    if ( (read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0)
 
    {
 
      if (INTERRUPTED_BY_SIGNAL)
 
      	goto again;
 
      return -1;
 
    }
 
    else
 
      if (read_cnt == 0)
 
    	return 0;
 
    read_ptr = read_buf;
 
  }
 
  read_cnt--;
 
  *ptr = *read_ptr++;
 
  return 1;
 
}
 
 
 
 
 
ssize_t readline (int fd, void *vptr, size_t maxlen)
 
{
 
  int n, rc;
 
  char c, *ptr;
 
 
 
  ptr = vptr;
 
  for (n=1; n<maxlen; n++)
 
  {
 
    if ( (rc = my_read(fd,&c)) == 1)
 
    {
 
      *ptr++ = c;
 
      if (c == '\n')
 
      	break;	/* newline is stored, like fgets() */
 
    }
 
    else if (rc == 0)
 
    {
 
      if (n == 1)
 
      	return 0; /* EOF, no data read */
 
      else
 
      	break; /* EOF, some data was read */
 
    }
 
    else
 
      return -1; /* error, errno set by read() */
 
  }
 
  *ptr = 0; /* null terminate like fgets() */
 
  return n;
 
}
 
 
 
 
 
ssize_t Readline (int fd, void *ptr, size_t maxlen)
 
{
 
  ssize_t n;
 
 
 
  if ( (n = readline(fd, ptr, maxlen)) < 0)
 
    err_sys ("(%s) error - readline() failed", prog);
 
  return n;
 
}
 
 
 
ssize_t writen (int fd, const void *vptr, size_t n)
 
{
 
  size_t nleft;
 
  ssize_t nwritten;
 
  const char *ptr;
 
 
 
  ptr = vptr;
 
  nleft = n;
 
  while (nleft > 0)
 
  {
 
    if ( (nwritten = write(fd, ptr, nleft)) <= 0)
 
    {
 
      if (INTERRUPTED_BY_SIGNAL)
 
      {
 
      	nwritten = 0;
 
        continue; /* and call write() again */
 
      }
 
      else
 
      	return -1;
 
    }
 
    nleft -= nwritten;
 
    ptr   += nwritten;
 
  }
 
  return n;
 
}
 
 
 
 
 
void Writen (int fd, void *ptr, size_t nbytes)
 
{
 
  if (writen(fd, ptr, nbytes) != nbytes)
 
    err_sys ("(%s) error - writen() failed", prog);
 
}
 
 
 
 
 
int Select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout)
 
{
 
  int n;
 
again:
 
  if ( (n = select (maxfdp1, readset, writeset, exceptset, timeout)) < 0)
 
  {
 
    if (INTERRUPTED_BY_SIGNAL)
 
      goto again;
 
    else
 
      err_sys ("(%s) error - select() failed", prog);
 
  }
 
  return n;
 
}
 
 
 
 
 
pid_t Fork (void)
 
{
 
  pid_t pid;
 
  if ((pid=fork()) < 0)
 
    err_sys ("(%s) error - fork() failed", prog);
 
  return pid;
 
}
 
 
 
#ifdef SOLARIS
 
const char * hstrerror (int err)
 
{
 
   if (err == NETDB_INTERNAL)
 
      return "internal error - see errno";
 
   if (err == NETDB_SUCCESS)
 
      return "no error";
 
   if (err == HOST_NOT_FOUND)
 
      return "unknown host";
 
   if (err == TRY_AGAIN)
 
      return "hostname lookup failure";
 
   if (err == NO_RECOVERY)
 
      return "unknown server error";
 
   if (err == NO_DATA)
 
      return "no address associated with name";
 
   return "unknown error";
 
}
 
#endif
 
 
 
struct hostent *Gethostbyname (const char *hostname)
 
{
 
  struct hostent *hp;
 
  if ((hp = gethostbyname(hostname)) == NULL)
 
    err_quit ("(%s) error - gethostbyname() failed for '%s': %s",
 
      prog, hostname, hstrerror(h_errno));
 
  return hp;
 
}
 
 
 
void Getsockname (int sockfd, struct sockaddr *localaddr, socklen_t *addrp)
 
{
 
  if ((getsockname(sockfd, localaddr, addrp)) != 0)
 
    err_quit ("(%s) error - getsockname() failed", prog);
 
}
 
######
 
errlib.c
 
/*
 
module: errlib.c
 
purpose: library of error functions
 
reference: Stevens, Unix network programming (2ed), p.922
 
*/
 
#include <stdio.h>
#include <stdlib.h> /* exit() */
#include <stdarg.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
 
#define MAXLINE 4095
 
int daemon_proc = 0; /* set to 0 if stdout/stderr available, else set to 1 */
 
static void err_doit (int errnoflag, int level, const char *fmt, va_list ap)
{
  int errno_save = errno;
  size_t n;
  char buf[MAXLINE+1];
 
  vsnprintf (buf, MAXLINE, fmt, ap);
  n = strlen(buf);
  if (errnoflag)
    snprintf (buf+n, MAXLINE-n, ": %s", strerror(errno_save));
  strcat (buf, "\n");
 
  if (daemon_proc)
    syslog (level, buf);
  else
  {
    fflush (stdout);
    fputs (buf, stderr);
    fflush (stderr);
  }
}
 
void err_ret (const char *fmt, ...)
{
  va_list ap;
 
  va_start (ap, fmt);
  err_doit (1, LOG_INFO, fmt, ap);
  va_end (ap);
  return;
}
 
void err_sys (const char *fmt, ...)
{
  va_list ap;
 
  va_start (ap, fmt);
  err_doit (1, LOG_ERR, fmt, ap);
  va_end (ap);
  exit (1);
}
 
void err_msg (const char *fmt, ...)
{
  va_list ap;
 
  va_start (ap, fmt);
  err_doit (0, LOG_INFO, fmt, ap);
  va_end (ap);
  return;
}
 
void err_quit (const char *fmt, ...)
{
  va_list ap;
 
  va_start (ap, fmt);
  err_doit (0, LOG_ERR, fmt, ap);
  va_end (ap);
  exit (1);
}
 
#####
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: roccogalati
Solution Provided By: sunnycoder
Participating Experts: 2
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Author Comment by roccogalati
Author Comment by roccogalati:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by sunnycoder

Rank: Sage

Accepted Solution by sunnycoder:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Assisted Solution by blu

Rank: Guru

Assisted Solution by blu:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628