blob: 93225ea5078e407d0412a7eab72c14aac71d08be [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001#include <ctype.h>
2#include <errno.h>
3#include <unistd.h>
4#include <getopt.h>
5#include <limits.h>
6#include <netdb.h>
7#include <stdbool.h>
8#include <stdint.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <time.h>
13#include <syslog.h>
14#include <pthread.h>
15#include <sys/socket.h>
16#include <sys/types.h>
17#include <sys/un.h>
18#include <sys/ioctl.h>
19#include <fcntl.h>
20#include "linklist.h"
21#include "socket_tok.h"
22
23/*command max len 64*/
24#define SOCKET_CMD_MAX_LEN 1024
25#define BUF_SIZE 1024
26
27#define EXIT_CMD_STOP "stop\n"
28#define EXIT_CMD_Q "q\n"
29#define EXIT_CMD_EXIT "exit\n"
30
31#define SOCKET_CREATE "sock_create"
32#define SOCKET_CREATECLIENT "sock_createclient"
33#define SOCKET_CREATESERVER "sock_createserver"
34#define SOCKET_BIND "sock_bind"
35#define SOCKET_LISTEN "sock_listen"
36#define SOCKET_ACCEPT "sock_accept"
37#define SOCKET_CONN "sock_conn"
38#define SOCKET_SEND "sock_send"
39#define SOCKET_SENDTO "sock_sendto"
40#define SOCKET_WRITE "sock_write"
41#define SOCKET_RECV "sock_recv"
42#define SOCKET_RECVFROM "sock_recvfrom"
43#define SOCKET_READ "sock_read"
44#define SOCKET_CLOSE "sock_close"
45#define SOCKET_LOCALTEST "sock_localtest"
46
47#define sock_param_len 4096
48
49typedef unsigned short UINT16;
50typedef unsigned long int UINT32;
51
52static int g_sockfd = -1;
53
54static char* sms_convert_cardinfo_state(int type, int value);
55
56static void printUsage(const char *Opt)
57{
58 printf("Usage: %s\n", Opt);
59 printf("sock_create:type create a new socket(eg. sock_create:1)\n");
60 printf("sock_createclient:type,addr create a new client socket(eg. sock_createclient:1,/var/sockclient)\n");
61 printf("sock_createserver:type,addr,backlog create a new server socket(eg. sock_createserver:1,/var/sockserver,30)\n");
62 printf("sock_bind:fd,addr socket bind(eg. sock_bind:fd,/var/sockclient)\n");
63 printf("sock_listen:fd,backlog socket listen(eg. sock_listen:fd,30)\n");
64 printf("sock_accept:fd socket accept(eg. sock_accept:fd)\n");
65 printf("sock_conn:fd,srvaddr conn server socket(eg. sock_conn:fd, /var/sockserver)\n");
66 printf("sock_send:fd,message send socket message(eg. sock_send:fd,ipc test)\n");
67 printf("sock_sendto:fd,srvaddr,message sendto socket message(eg. sock_sendto:fd,/var/sockserver,ipc test)\n");
68 printf("sock_write:fd,message write socket message(eg. sock_write:fd,ipc test)\n");
69 printf("sock_recv:fd recv socket message(eg. sock_recv: fd)\n");
70 printf("sock_recvfrom:fd recvfrom socket message(eg. sock_recvfrom: fd)\n");
71 printf("sock_read:fd read socket message(eg. sock_read: fd)\n");
72 printf("sock_close:fd close socket message(eg. sock_close: fd)\n");
73 printf("sock_localtest: test local socket operation\n");
74 printf("\n");
75}
76
77static int parseOpts(int argc, char *argv[])
78{
79 int rc = 0;
80 int c;
81 while ((c = getopt(argc, argv, "?n:a:")) != -1) {
82 switch (c) {
83 case 'a':
84 break;
85 case 'n':
86 break;
87 case '?':
88 default:
89 printUsage(argv[0]);
90 return -1;
91 }
92 }
93
94 return rc;
95}
96
97/**
98enum sock_type {
99 SOCK_STREAM = 1,
100 SOCK_DGRAM = 2,
101 SOCK_RAW = 3,
102 SOCK_RDM = 4,
103 SOCK_SEQPACKET = 5,
104 SOCK_DCCP = 6,
105 SOCK_PACKET = 10,
106};
107**/
108static int socket_create(char *data, int data_len)
109{
110 int sockfd = -1;
111 int type = 0;
112 printf("socket_create test \n");
113
114 if (at_tok_start(&data) < 0) {
115 printf("socket_create:at_tok_start error\n");
116 }
117
118 if(at_tok_nextint(&data, &type) < 0) {
119 printf("socket_create:at_tok_nextstr error\n");
120 }
121 printf("socket_create:type=%d\n", type);
122
123 sockfd = socket(AF_UNIX, type, 0);
124 if(sockfd < 0){
125 printf("socket_create error: %s(errno: %d) \n", strerror(errno), errno);
126 }else{
127 printf("socket_create sucess:sockfd=%d\n", sockfd);
128 }
129 g_sockfd = sockfd;
130 return 0;
131}
132
133static int socket_test_local()
134{
135 int clientfd = -1;
136 int serverfd = -1;
137 int numRecv;
138 char data[BUF_SIZE] = {0};
139 struct sockaddr_un my_addr = {0};
140
141 if((clientfd = socket(AF_UNIX, 2, 0))<0){
142 printf("socket_test_local: create client socket error \n");
143 return -1;
144 }
145
146 if((serverfd = socket(AF_UNIX, 2, 0))<0){
147 printf("socket_test_local: create server socket error \n");
148 return -1;
149 }
150
151 memset(&my_addr, 0, sizeof(struct sockaddr_un));
152 my_addr.sun_family = AF_UNIX;
153 strncpy(my_addr.sun_path, "/var/socktest", sizeof(my_addr.sun_path)-1);
154
155 //·þÎñ¶Ë°ó¶¨µØÖ·
156 if(bind(serverfd, (struct sockaddr*)&my_addr, sizeof(my_addr)) == 0){
157 printf("server socket °ó¶¨³É¹¦ \n");
158 }
159 else{
160 printf("server socket °ó¶¨Ê§°Ü \n");
161 }
162
163 //δÁ¬½Ó£¬¿Í»§¶ËÖ±½Ó¸ø·þÎñ¶Ë·¢ËÍÊý¾Ý
164 if(sendto(clientfd, "send data to server test one", 28, 0, &my_addr, sizeof(my_addr)) < 0){
165 printf("send data to server test one failed \n");
166 }
167
168 //¶ÁÈ¡Êý¾Ý
169 if(read(serverfd, data, BUF_SIZE-1) < 0){
170 printf("recv data test one failed \n");
171 }else{
172 printf("recv data test one sucess \n");
173 }
174
175 //Á¬½Ó·þÎñ¶Ë
176 if(connect(clientfd, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0){
177 printf("connect error: %s(errno: %d) \n", strerror(errno), errno);
178 return -1;
179 }
180
181 if(send(clientfd, "send data to server test two", 28, 0) < 0){
182 printf("send data to server test two failed: %s(errno: %d) \n", strerror(errno), errno);
183
184 }else{
185 printf("send data to server test two sucess \n");
186 }
187
188 memset(data, 0, sizeof(data));
189 if(numRecv = recv(serverfd, data, sizeof(data)-1, 0) == -1){
190 printf("recv data test two failed \n");
191 }else{
192 printf("recv data test two sucess \n");
193 }
194
195 //²âÊÔ·þÎñ¶Ë¸ø¿Í»§¶Ë·¢ÏûÏ¢ÊÇ·ñÄÜͨ
196 if(send(serverfd, "send data to server test three", 30, 0) < 0){
197 printf("send data to server test three failed: %s(errno: %d) \n", strerror(errno), errno);
198 }else{
199 printf("send data to server test three sucess \n");
200 }
201 fcntl(clientfd, F_SETFL, O_NONBLOCK);
202 memset(data, 0, sizeof(data));
203 if(numRecv = recv(clientfd, data, sizeof(data)-1, 0) == -1){
204 printf("recv data test three failed \n");
205 }else{
206 printf("recv data test three sucess \n");
207 }
208
209 //¸ø¿Í»§¶Ë°ó¶¨µØÖ·
210 memset(&my_addr, 0, sizeof(struct sockaddr_un));
211 my_addr.sun_family = AF_UNIX;
212 strncpy(my_addr.sun_path, "/var/sockclient", sizeof(my_addr.sun_path)-1);
213
214 //¿Í»§¶Ë°ó¶¨µØÖ·
215 if(bind(clientfd, (struct sockaddr*)&my_addr, sizeof(my_addr)) == 0){
216 printf("client socket °ó¶¨³É¹¦ \n");
217 }
218 else{
219 printf("client socket °ó¶¨Ê§°Ü \n");
220 }
221
222 //δÁ¬½Ó£¬·þÎñ¶ËÖ±½Ó¸ø¿Í»§¶Ë·¢ËÍÊý¾Ý
223 if(sendto(serverfd, "send data to server test four", 29, 0, &my_addr, sizeof(my_addr)) < 0){
224 printf("send data to server test four failed \n");
225 }else{
226 printf("send data to server test four sucess \n");
227 }
228
229 //¶ÁÈ¡Êý¾Ý
230 if(read(clientfd, data, BUF_SIZE-1) < 0){
231 printf("recv data test four failed \n");
232 }else{
233 printf("recv data test four sucess \n");
234 }
235
236 close(clientfd);
237 close(serverfd);
238}
239
240void socket_test_dgram_data(int fd, char* paddr, char* pdata){
241 struct sockaddr_un other_addr = {0};
242 ssize_t numRead;
243 ssize_t numRecv;
244 char buf[2048] = {0};
245 int sockfd = -1;
246 sockfd = fd;
247 int ret;
248 memset(&other_addr, 0, sizeof(struct sockaddr_un));
249 other_addr.sun_family = AF_UNIX;
250 strncpy(other_addr.sun_path, paddr, sizeof(other_addr.sun_path)-1);
251
252 //Èç¹ûµÚÒ»¸ö×Ö·ûÊÇz»òZ£¬Ôò´´½¨ÄäÃûsocket
253 if(other_addr.sun_path[0] == 'z' || other_addr.sun_path[0]=='Z'){
254 printf("socket_test_dgram_data: anonymous socket\n");
255 other_addr.sun_path[0] = '\0';
256 }
257 //δÁ¬½Ó£¬¿Í»§¶ËÖ±½Ó¸ø·þÎñ¶Ë·¢ËÍÊý¾Ý
258 if(sendto(sockfd, "send dgram data to server test one", 34, 0, &other_addr, sizeof(other_addr)) < 0){
259 printf("sendto dgram data to server test one failed \n");
260 }else{
261 printf("sendto dgram data to server test one sucess \n");
262 }
263#if 0
264 memset(buf, 0, sizeof(buf));
265 if(recvfrom(sockfd, buf, sizeof(buf)-1, 0, (struct sockaddr*)&other_addr, sizeof(other_addr)) <= 0){
266 printf("recvfrom: dgram data to server test one failed \n");
267 }else{
268 printf("recvfrom: dgram data to server test one sucess \n");
269 }
270#endif
271 //Á¬½Ó·þÎñ¶Ë
272 if(ret = connect(sockfd, (struct sockaddr*)&other_addr, sizeof(other_addr)) < 0){
273 printf("socket_create_client: connect error:ret=%d, %s(errno: %d) \n", ret, strerror(errno), errno);
274
275 return -1;
276 }else{
277 printf("³É¹¦Óë·þÎñÆ÷½¨Á¢Á¬½Ó: sockfd=%d\n", sockfd);
278 }
279
280
281 printf("send msg to server: \n");
282 memset(buf, 0, sizeof(buf));
283
284 if(sendto(sockfd, "send dgram data to server test two", 34, 0, &other_addr, sizeof(other_addr)) < 0){
285 printf("sendto dgram data to server test two failed \n");
286 }else{
287 printf("sendto dgram data to server test two sucess \n");
288 }
289
290#if 0
291 if(recvfrom(sockfd, buf, sizeof(buf)-1, 0, (struct sockaddr*)&other_addr, sizeof(other_addr)) <= 0){
292 printf("recvfrom: dgram data to server two failed \n");
293 }else{
294 printf("recvfrom: dgram data to server two sucess \n");
295 }
296#endif
297
298 memset(buf, 0, sizeof(buf));
299 // send¡¢recv²âÊÔ
300 if(send(sockfd, "send dgram data to server test three", 36, 0) < 0){
301 printf("send dgram data to server test three failed \n");
302 }else{
303 printf("send dgram data to server test three sucess \n");
304 }
305
306#if 0
307 if(recv(sockfd, buf, sizeof(buf)-1, 0) <= 0){
308 printf("recvfrom: dgram data to server three failed \n");
309 }else{
310 printf("recvfrom: dgram data to server three sucess \n");
311 }
312#endif
313 memset(buf, 0, sizeof(buf));
314 // write¡¢read²âÊÔ
315 if(write(sockfd, "send dgram data to server test four", 35) < 0){
316 printf("write dgram data to server test four failed \n");
317 }else{
318 printf("write dgram data to server test four sucess \n");
319 }
320
321#if 0
322 if(read(sockfd, buf, sizeof(buf)-1) <= 0){
323 printf("recvfrom: dgram data to server four failed \n");
324 }else{
325 printf("recvfrom: dgram data to server four sucess \n");
326 }
327#endif
328
329 if(pdata != NULL){
330 if(send(sockfd, pdata, strlen(pdata), 0) < 0){
331 printf("socket_create_client: send dgram msg error: %s(errno: %d) \n", strerror(errno), errno);
332 return -1;
333 }
334 printf("send sucess: %s \n", pdata);
335 }else{
336 if(send(sockfd, "send dgram msg to server test", 29, 0) < 0){
337 printf("socket_create_client: send dgram msg error: %s(errno: %d) \n", strerror(errno), errno);
338 return -1;
339 }
340 printf("send msg to server test \n");
341 }
342
343#if 0
344 if(numRecv = recv(sockfd, buf, sizeof(buf), 0) == -1){
345 printf("socket_create_client: recv error \n");
346 return -1;
347 }
348 printf("socket_create_client Receieved: %s \n", buf);
349#endif
350 if(close(sockfd) == 0){
351 printf("clsoe sockfd:%d sucess \n", sockfd);
352 }else{
353 printf("clsoe sockfd:%d failed \n", sockfd);
354 }
355}
356
357void socket_test_stream_data(int fd, char* paddr, char* pdata){
358 struct sockaddr_un other_addr = {0};
359 ssize_t numRead;
360 ssize_t numRecv;
361 char buf[2048] = {0};
362 int sockfd = -1;
363 sockfd = fd;
364 memset(&other_addr, 0, sizeof(struct sockaddr_un));
365 other_addr.sun_family = AF_UNIX;
366 strncpy(other_addr.sun_path, paddr, sizeof(other_addr.sun_path)-1);
367
368 //Èç¹ûµÚÒ»¸ö×Ö·ûÊÇz»òZ£¬Ôò´´½¨ÄäÃûsocket
369 if(other_addr.sun_path[0] == 'z' || other_addr.sun_path[0]=='Z'){
370 printf("socket_test_stream_data: anonymous socket\n");
371 other_addr.sun_path[0] = '\0';
372 }
373 //Á¬½Ó·þÎñ¶Ë
374 if(connect(sockfd, (struct sockaddr*)&other_addr, sizeof(other_addr)) < 0){
375 printf("socket_create_client: connect error: %s(errno: %d) \n", strerror(errno), errno);
376 close(fd);
377 return -1;
378 }else{
379 printf("³É¹¦Óë·þÎñÆ÷½¨Á¢Á¬½Ó: sockfd=%d\n", sockfd);
380 }
381
382 printf("send msg to server: \n");
383
384 memset(buf, 0, sizeof(buf));
385 // send¡¢recv²âÊÔ
386 if(send(sockfd, "send stream data to server test one", 35, 0) < 0){
387 printf("send stream data to server test one failed \n");
388 }else{
389 printf("send stream data to server test one sucess \n");
390 }
391
392 if(recv(sockfd, buf, sizeof(buf)-1, 0) <= 0){
393 printf("recv: stream data to server one failed \n");
394 }else{
395 printf("recv: stream data to server one sucess \n");
396 }
397
398 memset(buf, 0, sizeof(buf));
399 // write¡¢read²âÊÔ
400 if(write(sockfd, "send stream data to server test two", 35) < 0){
401 printf("write stream data to server test two failed \n");
402 }else{
403 printf("write stream data to server test two sucess \n");
404 }
405
406 if(read(sockfd, buf, sizeof(buf)-1) <= 0){
407 printf("read: stream data to server two failed \n");
408 }else{
409 printf("read: stream data to server two sucess \n");
410 }
411
412 memset(buf, 0, sizeof(buf));
413 if(pdata != NULL){
414 if(send(sockfd, pdata, strlen(pdata), 0) < 0){
415 printf("socket_create_client: send stream msg error: %s(errno: %d) \n", strerror(errno), errno);
416 close(fd);
417 return -1;
418 }
419 printf("send sucess: %s \n", pdata);
420 }else{
421 if(send(sockfd, "send stream msg to server test", 30, 0) < 0){
422 printf("socket_create_client: send stream msg error: %s(errno: %d) \n", strerror(errno), errno);
423 close(fd);
424 return -1;
425 }
426 printf("send stream msg to server test \n");
427 }
428
429 if(numRecv = recv(sockfd, buf, sizeof(buf), 0) == -1){
430 printf("socket_create_client: recv error \n");
431 close(fd);
432 return -1;
433 }
434 printf("recv sucess: %s \n", buf);
435 if(close(sockfd) == 0){
436 printf("clsoe sockfd:%d sucess \n", sockfd);
437 }else{
438 printf("clsoe sockfd:%d failed \n", sockfd);
439 }
440}
441static int socket_create_client(char *data, int data_len)
442{
443 int sockfd = -1;
444 int type = 0;
445 char* paddr = NULL;
446 char* pdata = NULL;
447 struct sockaddr_un other_addr = {0};
448 int backlog = 0;
449 ssize_t numRead;
450 ssize_t numRecv;
451 char buf[2048] = {0};
452 int i=0;
453 printf("socket_create_client test \n");
454
455 if (at_tok_start(&data) < 0) {
456 printf("socket_create_client:at_tok_start error\n");
457 }
458
459 if(at_tok_nextint(&data, &type) < 0) {
460 printf("socket_create_client:at_tok_nextstr error\n");
461 }
462 printf("socket_create_client:type=%d\n", type);
463
464 if(at_tok_nextstr(&data, &paddr) < 0) {
465 printf("socket_create_client:at_tok_nextstr error\n");
466 }
467
468 printf("socket_create_client: addr=%s \n", paddr);
469
470 if(at_tok_nextstr(&data, &pdata) < 0) {
471 printf("socket_create_client: no test data\n");
472 }
473
474 if(strlen(paddr) > sizeof(other_addr.sun_path)-1)
475 {
476 printf("socket_create_client:server socket path too logn: %s\n", paddr);
477 return -1;
478 }
479
480 if(type == 1){
481 while(true){
482 if((sockfd = socket(AF_UNIX, type, 0))<0){
483 printf("socket_create_client: create socket error \n");
484 return -1;
485 }
486 printf("socket_create_client: socket_create:sockfd=%d(%d´Î)\n", sockfd, i);
487 socket_test_stream_data(sockfd, paddr, pdata);
488 sleep(1);
489 i++;
490 }
491 }else if(type ==2){
492 while(true){
493 if((sockfd = socket(AF_UNIX, type, 0))<0){
494 printf("socket_create_client: create socket error \n");
495 return -1;
496 }
497
498 printf("socket_create_client: socket_create:sockfd=%d(%d´Î)\n", sockfd, i);
499 socket_test_dgram_data(sockfd, paddr, pdata);
500 sleep(1);
501 i++;
502 }
503 }
504
505 return 0;
506}
507
508static void do_recv(int fd)
509{
510 int ret = -1;
511 char buf[BUF_SIZE];
512 char resp_buf[BUF_SIZE+10];
513 printf("do_recv loop enter, fd=%d \n", fd);
514 while(1)
515 {
516 memset(buf, 0, BUF_SIZE);
517 do
518 {
519 ret = read(fd,buf,BUF_SIZE-1);
520 }while(ret < 0 && errno == EINTR);//×èÈû¶Áд
521
522 if(ret < 0)
523 {
524 printf("fd %d read error, ret=%d!\n", fd, ret);
525 continue;
526 }
527 if(ret == 0)//¶Ô·½ÒѹرÕ
528 {
529 printf("client is close!\n");
530 //close(fd);
531 continue;
532 }
533
534 if(ret > 0)
535 {
536 printf("read sucess, fd:%d, recv data=%s \n", fd, buf);
537
538 //ret = write(fd, buf, ret);
539
540 //printf("write back data=%s, len=%d \n",buf, ret);
541 }
542 }
543}
544
545static void do_select(int fd)
546{
547 linklist fdlist,sun_list;
548 fdlist = create_linklist();
549 datatype sun_data = {0};
550 sun_data.fd = fd;
551 int maxfd = fd;
552 //struct timeval tout = {5,0};
553
554 insert_end_linklist(fdlist,sun_data);//½«lsten()´¦ÀíºóµÄfd¼ÓÈëÁбí
555 //show_linklist(fdlist);
556
557 fd_set fdset;
558 int newfd = -1;
559 int ret = -1;
560 char buf[BUF_SIZE];
561 char resp_buf[BUF_SIZE+10];
562 struct sockaddr_un addr_un;
563 socklen_t cun_addr_len = sizeof(addr_un);
564 /* ÓÃselect()º¯ÊýʵÏÖI/O¶à·¸´ÓÃ*/
565 while(1)
566 {
567 int i;
568 FD_ZERO(&fdset);
569 if(get_length_linklist(fdlist) >= 1)//½«ÁбíÖеÄfd¼ÓÈë¶Á¼¯ºÏ½øÐд¦Àí
570 {
571 for(i=0;i<get_length_linklist(fdlist);i++)
572 {
573 sun_list = get_list_pos_linklist(fdlist,i);
574 sun_data = sun_list->data;
575 FD_SET(sun_data.fd,&fdset);
576 maxfd = sun_data.fd > maxfd ? sun_data.fd : maxfd;
577 printf("µÚ %d ¸ö£¨fd:%d£©£¨ip:%s£©£¨port:%d£©\n",i,sun_data.fd,sun_data.ipv4_addr,sun_data.port);
578 }
579 //show_linklist(fdlist);
580 }
581 else
582 {
583 continue ;
584 }
585 //switch(select(maxfd+1,&fdset,NULL,NULL,&tout))
586 switch(ret=select(maxfd+1,&fdset,NULL,NULL,NULL))
587 {
588 case 0:
589 {
590 printf("time out!\n");
591 goto _error1;
592 }
593 case -1:
594 {
595 printf("select error\n");
596 goto _error1;
597 }
598 default:
599 {
600 printf("select ret=%d \n", ret);
601 if(FD_ISSET(fd,&fdset))//Óпͻ§¶Ë·¢ËÍÁËÁ¬½ÓÇëÇó
602 {
603 bzero(&addr_un, sizeof(addr_un));
604 //if((newfd = accept(fd,NULL,0)) < 0)
605 if((newfd = accept(fd,(struct sockaddr *)&addr_un,&cun_addr_len)) < 0)
606 {
607 printf("connect failed: newfd=%d\n", newfd);
608
609 goto _error1;
610 }
611 /* ½«·ÖÅä³É¹¦µÄÌ×½Ó×ÖnewfdÉèÖóɷÇ×èÈûģʽ*/
612 int b_on = 1;
613 ioctl(newfd, FIONBIO, &b_on);//½«·ÖÅä³É¹¦µÄÌ×½Ó×ÖnewfdÉèÖÃΪ·Ç×èÈû·½Ê½
614 sun_data.fd = newfd;
615 printf("get a new client->(ip:%s)(port:%d)(fd:%d)\n",sun_data.ipv4_addr,sun_data.port,sun_data.fd);
616
617 insert_end_linklist(fdlist,sun_data);//½«½¨Á¢¿Í»§¶ËÁ¬½ÓµÄfd¼ÓÈëÁбí
618 //show_linklist(fdlist);
619 }
620 else//ÓÐÁ¬½ÓºÃµÄ¿Í»§¶Ë·¢ËÍÁËÊý¾Ý
621 {
622 for(i=0;i<get_length_linklist(fdlist);i++)//½«Á´±íÖеÄfd¶¼´¦ÀíÒ»±é
623 {
624
625 sun_list = get_list_pos_linklist(fdlist,i);
626 sun_data = sun_list->data;
627
628 //printf("readung fd is ->(µÚ %d ¸ö)(fd:%d)(ip:%s)(port:%d)\n",i,sun_data.fd,sun_data.ipv4_addr,sun_data.port);
629 if(sun_data.fd == fd)//²»Êǽ¨Á¢Á¬½Óºó·ÖÅäµÄnewfd
630 continue ;
631
632 bzero(buf,BUF_SIZE);
633 do
634 {
635 ret = read(sun_data.fd,buf,BUF_SIZE-1);
636 }while(ret < 0 && errno == EINTR);//×èÈû¶Áд
637
638 if(ret < 0)
639 {
640 printf("fd %d read error, ret=%d, errno=%d!\n", sun_data.fd, ret, errno);
641 continue;
642 }
643 if(ret == 0)//¶Ô·½ÒѹرÕ
644 {
645 printf("client is close, errno=%d!\n", errno);
646 close(sun_data.fd);
647 delete_locate_linklist(fdlist,sun_data);
648 continue;
649 }
650
651 printf("read sucess, fd(:%d) receive data: %s \n",sun_data.fd,buf);
652 ret = write(sun_data.fd, buf, ret);
653 printf("write back data=%s, len=%d \n",buf, ret);
654 }
655 }
656 }
657 }
658 }
659
660 close(newfd);
661_error1:
662 printf("exit cause by _error1!\n");
663 close(fd);
664 clear_linklist(fdlist);
665}
666
667//"/tmp/us_xfr" "zmysocket"
668static int socket_create_server(char *data, int data_len)
669{
670 int sockfd = -1;
671 int cfd = -1;
672 int type = 0;
673 char* paddr = NULL;
674 struct sockaddr_un my_addr = {0};
675 int backlog = 0;
676 ssize_t numRead;
677 char buf[2048] = {0};
678 printf("socket_create_server test \n");
679
680 if (at_tok_start(&data) < 0) {
681 printf("socket_create_server:at_tok_start error\n");
682 }
683
684 if(at_tok_nextint(&data, &type) < 0) {
685 printf("socket_create_server:at_tok_nextstr error\n");
686 }
687 printf("socket_create_server:type=%d\n", type);
688
689 if(at_tok_nextstr(&data, &paddr) < 0) {
690 printf("socket_create_server:at_tok_nextstr error\n");
691 }
692
693 printf("socket_create_server: addr=%s \n", paddr);
694
695 if(at_tok_nextint(&data, &backlog) < 0) {
696 printf("socket_create_server:at_tok_nextstr error\n");
697 }
698
699 if((sockfd = socket(AF_UNIX, type, 0)) < 0)
700 {
701 printf("socket_create_server failed, type=%d \n", type);
702 return -1;
703 }
704 g_sockfd = sockfd;
705 printf("socket_create_server:type=%d, sockfd=%d\n", type, sockfd);
706
707 /*ÔÊÐí°ó¶¨µØÖ·¿ìËÙÖØÓà */
708 int b_reuse = 1;
709 setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&b_reuse,sizeof(int));
710
711 if(strlen(paddr) > sizeof(my_addr.sun_path)-1)
712 {
713 printf("socket_bind:server socket path too long: %s\n", paddr);
714 goto error;
715 }
716
717 if(access(paddr,F_OK))
718 {
719 unlink(paddr);//ɾ³ý¸ÃÎļþ
720 }
721
722 memset(&my_addr, 0, sizeof(struct sockaddr_un));
723 my_addr.sun_family = AF_UNIX;
724 strncpy(my_addr.sun_path, paddr, sizeof(my_addr.sun_path)-1);
725 //Èç¹ûµÚÒ»¸ö×Ö·ûÊÇz»òZ£¬Ôò´´½¨ÄäÃûsocket
726 if(my_addr.sun_path[0] == 'z' || my_addr.sun_path[0]=='Z'){
727 printf("socket_create_server: anonymous socket\n");
728 my_addr.sun_path[0] = '\0';
729 }
730 if(bind(sockfd, (struct sockaddr*)&my_addr, sizeof(my_addr)) == 0){
731 printf("socket°ó¶¨³É¹¦ \n");
732 }
733 else{
734 printf("socket°ó¶¨Ê§°Ü errno=%d, strerror=%s\n", errno, strerror(errno));
735 goto error;
736 }
737
738 if(type == 1){
739 if(listen(sockfd, backlog) == 0){
740 printf("socket¼àÌý³É¹¦ \n");
741 }else{
742 printf("socket¼àÌýʧ°Ü \n");
743 goto error;
744 }
745 do_select(sockfd);
746 }else{
747 do_recv(sockfd);
748 }
749
750
751
752#if 0
753
754 for(;;){
755 cfd = accept(sockfd, NULL, NULL);
756 if(cfd == -1){
757 printf("socket_create_server: accept failed");
758 continue;
759 }
760
761 printf("socket_create_server cfd=%d \n", cfd);
762 while((numRead = recv(cfd, buf, sizeof(buf), 0)) > 0){
763 printf("socket_create_server:read data=%s \n", buf);
764 }
765
766 if(numRead == -1){
767 printf("socket_create_server: read data failed \n");
768
769 }
770
771 if(!fork()){
772 if(send(cfd, "you are connected! \n", 19, 0) == -1)
773 {
774 printf("socket_create_server: send error \n");
775 close(cfd);
776 exit(0);
777 }
778 }
779 if(close(cfd) == -1){
780 printf("socket_create_server: cfd close failed\n");
781 }
782
783 }
784#endif
785
786error:
787 close(sockfd);
788 return 0;
789
790}
791
792static int socket_bind(char *data, int data_len)
793{
794 int sockfd = -1;
795 char* paddr = NULL;
796 struct sockaddr_un my_addr = {0};
797
798 if (at_tok_start(&data) < 0) {
799 printf("socket_bind:at_tok_start error\n");
800 }
801
802 if(at_tok_nextint(&data, &sockfd) < 0) {
803 printf("socket_bind:at_tok_nextstr error\n");
804 }
805
806 if(at_tok_nextstr(&data, &paddr) < 0) {
807 printf("socket_bind:at_tok_nextstr error\n");
808 }
809
810 printf("socket_bind: sockfd=%d, addr=%s \n", sockfd, paddr);
811
812 if(strlen(paddr) > sizeof(my_addr.sun_path)-1)
813 {
814 printf("socket_bind:server socket path too logn: %s\n", paddr);
815 return -1;
816 }
817
818 memset(&my_addr, 0, sizeof(struct sockaddr_un));
819 my_addr.sun_family = AF_UNIX;
820 strncpy(my_addr.sun_path, paddr, sizeof(my_addr.sun_path)-1);
821
822 //Èç¹ûµÚÒ»¸ö×Ö·ûÊÇz»òZ£¬Ôò´´½¨ÄäÃûsocket
823 if(my_addr.sun_path[0] == 'z' || my_addr.sun_path[0]=='Z'){
824 printf("socket_bind: anonymous socket\n");
825 my_addr.sun_path[0] = '\0';
826 }
827
828 if(bind(sockfd, (struct sockaddr*)&my_addr, sizeof(my_addr)) == 0){
829 printf("socket°ó¶¨³É¹¦ \n");
830 }
831 else{
832 printf("socket°ó¶¨Ê§°Ü errno=%d, strerror=%s\n", errno, strerror(errno));
833 }
834 return 0;
835}
836
837static int socket_listen(char *data, int data_len)
838{
839 int sockfd = -1;
840 int backlog = 0;
841 struct sockaddr_un my_addr = {0};
842
843 if (at_tok_start(&data) < 0) {
844 printf("socket_listen:at_tok_start error\n");
845 }
846
847 if(at_tok_nextint(&data, &sockfd) < 0) {
848 printf("socket_listen:at_tok_nextint error\n");
849 }
850
851 if(at_tok_nextint(&data, &backlog) < 0) {
852 printf("socket_listen:at_tok_nextint error\n");
853 }
854
855 printf("socket_bind: sockfd=%d, backlog=%d \n", sockfd, backlog);
856
857 if(listen(sockfd, backlog) == 0){
858 printf("socket¼àÌý³É¹¦ \n");
859 }else{
860 printf("socket¼àÌýʧ°Ü \n");
861 }
862 return 0;
863}
864
865static int socket_accept(char *data, int data_len)
866{
867 int sockfd = -1;
868 int newfd = -1;
869 int ret = -1;
870 struct sockaddr_un addr_un;
871 socklen_t cun_addr_len = sizeof(addr_un);
872
873 if (at_tok_start(&data) < 0) {
874 printf("socket_accept:at_tok_start error\n");
875 }
876
877 if(at_tok_nextint(&data, &sockfd) < 0) {
878 printf("socket_accept:at_tok_nextint error\n");
879 }
880
881 printf("socket_accept sockfd=%d \n", sockfd);
882
883 if((newfd = accept(sockfd,(struct sockaddr *)&addr_un,&cun_addr_len)) < 0)
884 {
885 printf("socket_accept failed: newfd=%d\n", newfd);
886 }else{
887 printf("socket_accept sucess: newfd=%d\n", newfd);
888 }
889 return 0;
890}
891
892
893static int socket_connect(char *data, int data_len)
894{
895 int sockfd = -1;
896 char* paddr = NULL;
897 struct sockaddr_un my_addr = {0};
898
899 if (at_tok_start(&data) < 0) {
900 printf("socket_connect:at_tok_start error\n");
901 }
902
903 if(at_tok_nextint(&data, &sockfd) < 0) {
904 printf("socket_connect:at_tok_nextstr error\n");
905 }
906
907 if(at_tok_nextstr(&data, &paddr) < 0) {
908 printf("socket_connect:at_tok_nextstr error\n");
909 }
910
911 printf("socket_connect: sockfd=%d, addr=%s \n", sockfd, paddr);
912
913 if(strlen(paddr) > sizeof(my_addr.sun_path)-1)
914 {
915 printf("socket_connect:server socket path too logn: %s\n", paddr);
916 return -1;
917 }
918
919 memset(&my_addr, 0, sizeof(struct sockaddr_un));
920 my_addr.sun_family = AF_UNIX;
921 strncpy(my_addr.sun_path, paddr, sizeof(my_addr.sun_path)-1);
922 printf("socket_connect: my_addr.sun_path=%s \n", my_addr.sun_path);
923
924 //Èç¹ûµÚÒ»¸ö×Ö·ûÊÇz»òZ£¬Ôò´´½¨ÄäÃûsocket
925 if(my_addr.sun_path[0] == 'z' || my_addr.sun_path[0]=='Z'){
926 printf("socket_connect: anonymous socket\n");
927 my_addr.sun_path[0] = '\0';
928 }
929
930 if(connect(sockfd, (struct sockaddr*)&my_addr, sizeof(my_addr)) == 0){
931 printf("³É¹¦Óë·þÎñÆ÷½¨Á¢Á¬½Ó \n");
932 }
933 else{
934 printf("Á¬½Óʧ°Ü\n");
935 }
936
937 return 0;
938}
939
940static int socket_send(char *data, int data_len)
941{
942 int sockfd = -1;
943 char* pdata = NULL;
944 int len = 0;
945 if (at_tok_start(&data) < 0) {
946 printf("socket_send:at_tok_start error\n");
947 }
948
949 if(at_tok_nextint(&data, &sockfd) < 0) {
950 printf("socket_send:at_tok_nextstr error\n");
951 }
952
953 if(at_tok_nextstr(&data, &pdata) < 0) {
954 printf("socket_send:at_tok_nextstr error\n");
955 }
956
957 printf("socket_send: sockfd=%d, pdata=%s \n", sockfd, pdata);
958
959 len = send(sockfd, pdata, strlen(pdata), 0);
960
961 if(len >= 0){
962 printf("socket_send sucess, len=%d \n", len);
963 }else{
964 printf("socket_send failed, error: %s(errno: %d) \n", strerror(errno), errno);
965 }
966
967 return 0;
968}
969
970static int socket_sendto(char *data, int data_len)
971{
972 int sockfd = -1;
973 char* pdata = NULL;
974 char* paddr = NULL;
975 struct sockaddr_un other_addr = {0};
976 int len = 0;
977 if (at_tok_start(&data) < 0) {
978 printf("socket_sendto:at_tok_start error\n");
979 }
980
981 if(at_tok_nextint(&data, &sockfd) < 0) {
982 printf("socket_sendto:at_tok_nextstr error\n");
983 }
984
985 if(at_tok_nextstr(&data, &paddr) < 0) {
986 printf("socket_sendto:at_tok_nextstr error\n");
987 }
988
989 if(at_tok_nextstr(&data, &pdata) < 0) {
990 printf("socket_sendto:at_tok_nextstr error\n");
991 }
992
993 printf("socket_sendto: sockfd=%d, addr=%s \n", sockfd, paddr);
994
995 if(strlen(paddr) > sizeof(other_addr.sun_path)-1)
996 {
997 printf("socket_sendto:server socket path too logn: %s\n", paddr);
998 return -1;
999 }
1000
1001 memset(&other_addr, 0, sizeof(struct sockaddr_un));
1002 other_addr.sun_family = AF_UNIX;
1003 strncpy(other_addr.sun_path, paddr, sizeof(other_addr.sun_path)-1);
1004 printf("socket_sendto: other_addr.sun_path=%s \n", other_addr.sun_path);
1005
1006 //Èç¹ûµÚÒ»¸ö×Ö·ûÊÇz»òZ£¬Ôò´´½¨ÄäÃûsocket
1007 if(other_addr.sun_path[0] == 'z' || other_addr.sun_path[0]=='Z'){
1008 printf("socket_sendto: anonymous socket\n");
1009 other_addr.sun_path[0] = '\0';
1010 }
1011
1012 len = sendto(sockfd, pdata, strlen(pdata), 0, &other_addr, sizeof(other_addr));
1013
1014 if(len >= 0){
1015 printf("socket_sendto sucess, len=%d \n", len);
1016 }else{
1017 printf("socket_sendto failed, error: %s(errno: %d) \n", strerror(errno), errno);
1018 }
1019 return 0;
1020}
1021
1022static int socket_write(char *data, int data_len)
1023{
1024 int sockfd = -1;
1025 char* pdata = NULL;
1026 int len = 0;
1027 if (at_tok_start(&data) < 0) {
1028 printf("socket_write:at_tok_start error\n");
1029 }
1030
1031 if(at_tok_nextint(&data, &sockfd) < 0) {
1032 printf("socket_write:at_tok_nextstr error\n");
1033 }
1034
1035 if(at_tok_nextstr(&data, &pdata) < 0) {
1036 printf("socket_write:at_tok_nextstr error\n");
1037 }
1038
1039 printf("socket_write: sockfd=%d, pdata=%s \n", sockfd, pdata);
1040
1041 len = write(sockfd, pdata, strlen(pdata));
1042 if(len >= 0){
1043 printf("socket_write sucess, len=%d \n", len);
1044 }else{
1045 printf("socket_write failed, error: %s(errno: %d) \n", strerror(errno), errno);
1046 }
1047
1048 return 0;
1049}
1050
1051static int socket_recv(char *data, int data_len)
1052{
1053 int sockfd = -1;
1054 char pdata[2048] = {0};
1055 int len = 0;
1056 if (at_tok_start(&data) < 0) {
1057 printf("socket_recv:at_tok_start error\n");
1058 }
1059
1060 if(at_tok_nextint(&data, &sockfd) < 0) {
1061 printf("socket_recv:at_tok_nextstr error\n");
1062 }
1063
1064 printf("socket_recv: sockfd=%d \n", sockfd);
1065
1066 len = recv(sockfd, pdata, sizeof(pdata), 0);
1067 if(len >= 0){
1068 printf("recv sucess, len=%d \n", len);
1069 }else{
1070 printf("recv failed, error: %s(errno: %d) \n", strerror(errno), errno);
1071 }
1072 return 0;
1073}
1074
1075static int socket_recvfrom(char *data, int data_len)
1076{
1077 int sockfd = -1;
1078 char pdata[2048] = {0};
1079 struct sockaddr_un other_addr = {0};
1080 int len = 0;
1081 if (at_tok_start(&data) < 0) {
1082 printf("socket_recvfrom:at_tok_start error\n");
1083 }
1084
1085 if(at_tok_nextint(&data, &sockfd) < 0) {
1086 printf("socket_recvfrom:at_tok_nextstr error\n");
1087 }
1088
1089 printf("socket_recvfrom: sockfd=%d \n", sockfd);
1090
1091 len = recvfrom(sockfd, pdata, sizeof(pdata)-1, 0, (struct sockaddr*)&other_addr, sizeof(other_addr));
1092 if(len >= 0){
1093 printf("recvfrom sucess: other_addr.sun_path=%s\n", other_addr.sun_path);
1094 }else{
1095 printf("recvfrom failed, error: %s(errno: %d) \n", strerror(errno), errno);
1096 }
1097 return 0;
1098}
1099
1100static int socket_read(char *data, int data_len)
1101{
1102 int sockfd = -1;
1103 char pdata[2048] = {0};
1104 int len = 0;
1105 if (at_tok_start(&data) < 0) {
1106 printf("socket_read:at_tok_start error\n");
1107 }
1108
1109 if(at_tok_nextint(&data, &sockfd) < 0) {
1110 printf("socket_read:at_tok_nextstr error\n");
1111 }
1112
1113 printf("socket_read: sockfd=%d \n", sockfd);
1114
1115 len = read(sockfd, pdata, sizeof(pdata));
1116 if(len >= 0){
1117 printf("read sucess, len=%d \n", len);
1118 }else{
1119 printf("read failed, error: %s(errno: %d) \n", strerror(errno), errno);
1120 }
1121 return 0;
1122}
1123
1124static int socket_close(char *data, int data_len)
1125{
1126 int sockfd = -1;
1127
1128 if (at_tok_start(&data) < 0) {
1129 printf("socket_close:at_tok_start error\n");
1130 }
1131
1132 if(at_tok_nextint(&data, &sockfd) < 0) {
1133 printf("socket_close:at_tok_nextint error\n");
1134 }
1135
1136 printf("socket_close: sockfd=%d \n", sockfd);
1137
1138 if(close(sockfd) == 0){
1139 printf("¹Ø±Õsocket³É¹¦ \n");
1140 }else{
1141 printf("¹Ø±Õsocketʧ°Ü \n");
1142 }
1143 return 0;
1144}
1145
1146
1147static void socket_cmd_proc(char *cmdstr)
1148{
1149 char *data = cmdstr;
1150 int data_len = strlen(data) - 1;/* -strlen("\r")*/
1151 char request = data[0];
1152
1153 cmdstr[data_len] = '\0';
1154 printf("socket_cmd_proc data=%s \n", data);
1155 if(0 == strncmp(data, SOCKET_CREATECLIENT, strlen(SOCKET_CREATECLIENT))) {
1156 printf("Request create client socket.\n");
1157 socket_create_client(data, data_len);
1158 }else if(0 == strncmp(data, SOCKET_CREATESERVER, strlen(SOCKET_CREATESERVER))) {
1159 printf("Request create server socket.\n");
1160 socket_create_server(data, data_len);
1161 }else if(0 == strncmp(data, SOCKET_CREATE, strlen(SOCKET_CREATE))) {
1162 printf("Request create socket.\n");
1163 socket_create(data, data_len);
1164 }else if(0 == strncmp(data, SOCKET_BIND, strlen(SOCKET_BIND))) {
1165 printf("Request bind socket.\n");
1166 socket_bind(data, data_len);
1167 }else if(0 == strncmp(data, SOCKET_LISTEN, strlen(SOCKET_LISTEN))) {
1168 printf("Request listen socket.\n");
1169 socket_listen(data, data_len);
1170 }else if(0 == strncmp(data, SOCKET_ACCEPT, strlen(SOCKET_ACCEPT))) {
1171 printf("Request accept socket.\n");
1172 socket_accept(data, data_len);
1173 }else if(0 == strncmp(data, SOCKET_CONN, strlen(SOCKET_CONN))) {
1174 printf("Request connect socket.\n");
1175 socket_connect(data, data_len);
1176 }else if(0 == strncmp(data, SOCKET_SENDTO, strlen(SOCKET_SENDTO))) {
1177 printf("Request sendto message.\n");
1178 socket_sendto(data, data_len);
1179 }else if(0 == strncmp(data, SOCKET_SEND, strlen(SOCKET_SEND))) {
1180 printf("Request send message.\n");
1181 socket_send(data, data_len);
1182 }else if(0 == strncmp(data, SOCKET_WRITE, strlen(SOCKET_WRITE))) {
1183 printf("Request write message.\n");
1184 socket_write(data, data_len);
1185 }else if(0 == strncmp(data, SOCKET_RECVFROM, strlen(SOCKET_RECVFROM))) {
1186 printf("Request recvfrom message.\n");
1187 socket_recvfrom(data, data_len);
1188 }else if(0 == strncmp(data, SOCKET_RECV, strlen(SOCKET_RECV))) {
1189 printf("Request recv message.\n");
1190 socket_recv(data, data_len);
1191 }else if(0 == strncmp(data, SOCKET_READ, strlen(SOCKET_READ))) {
1192 printf("Request read message.\n");
1193 socket_read(data, data_len);
1194 }else if(0 == strncmp(data, SOCKET_CLOSE, strlen(SOCKET_CLOSE))) {
1195 printf("Request close socket.\n");
1196 socket_close(data, data_len);
1197 }else if(0 == strncmp(data, SOCKET_LOCALTEST, strlen(SOCKET_LOCALTEST))){
1198 printf("Request local test.\n");
1199 socket_test_local();
1200 }else {
1201 printf("Request unknow.\n");
1202 printUsage(cmdstr);
1203 }
1204}
1205int main(int argc, char *argv[])
1206{
1207 char cmdstr[SOCKET_CMD_MAX_LEN] = {0};
1208
1209 printf("socket_ipc:Demo go.\n");
1210 if(parseOpts(argc,argv) == -1) {
1211 printf("socket_ipc:Arg error.\n");
1212 return -1;
1213 }
1214
1215 while(1) {
1216 memset(cmdstr,0,SOCKET_CMD_MAX_LEN);
1217
1218 printf("Please input an socket command:\n");
1219
1220 if(NULL != fgets(cmdstr,SOCKET_CMD_MAX_LEN - 1,stdin)) {
1221 if(0 == strcmp(EXIT_CMD_STOP,cmdstr) ||
1222 0 == strcmp(EXIT_CMD_Q,cmdstr) ||
1223 0 == strcmp(EXIT_CMD_EXIT,cmdstr)) {
1224 if(g_sockfd != -1){
1225 if(close(g_sockfd) == 0){
1226 printf("close g_sockfd=%d sucess \n", g_sockfd);
1227 }else{
1228 printf("close g_sockfd=%d failed \n", g_sockfd);
1229 }
1230 }
1231 break;
1232 }
1233
1234 printf("cmdstr:%d %s\n",strlen(cmdstr),cmdstr);
1235 if(1 >= strlen(cmdstr)) {
1236 continue;
1237 }
1238
1239 socket_cmd_proc(cmdstr);
1240 }
1241 }
1242
1243 printf("socket_ipc:Demo end.\n");
1244 return 0;
1245}
1246