Guest User

Untitled

a guest
Nov 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<sys/socket.h>
  3. #include<arpa/inet.h> //inet_addr
  4. #include<unistd.h>
  5. #include<string.h>
  6. #include<stdlib.h>
  7. #include<string.h>
  8.  
  9.  
  10.  
  11. int main(int argc , char *argv[])
  12. {
  13. int socket_desc , new_socket , c, valread;
  14. struct sockaddr_in server, client ;
  15. char *message;
  16. char buffer[1024] = {0};
  17. char *hello = "Hello from server";
  18. int opt = 1;
  19. char *string;
  20. const char s[2] = ";";
  21. char *token;
  22. char *sid;
  23. char *name;
  24. char *host;
  25. char *port;
  26. char *proto;
  27. char *user;
  28. char *password;
  29. char *key;
  30. char *companyName;
  31. char *officeAddress;
  32. char *state;
  33. char *country;
  34. char *ladmin;
  35. char *phone;
  36. char *mobile;
  37. char *email;
  38. char *designation;
  39. char *rmanager;
  40. char *sip;
  41. char *polname;
  42. char *device;
  43. char *status;
  44. char *srczone;
  45. char *dstzone;
  46. char *srcaddr;
  47. char *dstaddr;
  48. char *srcuser;
  49. char *app;
  50. char *service;
  51. char *urlcategory;
  52. char *action;
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. //Create socket
  60. socket_desc = socket(AF_INET , SOCK_STREAM , 0);
  61. if (socket_desc == 0)
  62. {
  63. printf("Could not create socket");
  64. }
  65.  
  66.  
  67. if (setsockopt(socket_desc, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
  68. {
  69. perror("setsockopt");
  70. exit(EXIT_FAILURE);
  71. }
  72.  
  73.  
  74. //Prepare the sockaddr_in structure
  75. server.sin_family = AF_INET;
  76. server.sin_addr.s_addr = INADDR_ANY;
  77. server.sin_port = htons( 8888 );
  78.  
  79.  
  80.  
  81. //Bind
  82. if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
  83. {
  84. puts("bind failed");
  85. }
  86.  
  87.  
  88. puts("bind done");
  89.  
  90.  
  91.  
  92.  
  93. //Listen
  94. listen(socket_desc , 3);
  95. while(1){
  96. //Accept and incoming connection
  97. puts("Waiting for incoming connections...");
  98. c = sizeof(struct sockaddr_in);
  99. new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
  100. if (new_socket<0)
  101. {
  102. perror("accept failed");
  103. }
  104.  
  105. puts("Connection acceptedn");
  106.  
  107.  
  108.  
  109.  
  110. valread = read(new_socket, buffer, 1024);
  111. printf("%sn", buffer);
  112. string = buffer;
  113.  
  114. //Macro that I am using to distinguish between clients and the work to be done
  115. token = strtok(string,s);
  116.  
  117. //If server is passed as the first token, following code to be executed
  118. printf("%s", token);
  119. if( token == "server" ){
  120. sid = strtok(NULL, s);
  121. printf("nSERVER-ID: %sn", sid);
  122. name = strtok(NULL, s);
  123. printf("nName: %sn", name);
  124. host = strtok(NULL,s);
  125. printf("nHost: %sn", host);
  126. port = strtok(NULL,s);
  127. printf("nPort: %sn", port);
  128. proto = strtok(NULL,s);
  129. printf("nProto: %sn", proto);
  130. user = strtok(NULL,s);
  131. printf("nUser: %sn", user);
  132. password = strtok(NULL, s);
  133. printf("nPassword: %sn", password);
  134. key = strtok(NULL,s);
  135. printf("nKey: %sn", key);
  136. companyName = strtok(NULL,s);
  137. printf("nCompanyName: %sn", companyName);
  138. officeAddress = strtok(NULL,s);
  139. printf("nOfficeAddress: %sn", officeAddress);
  140. state = strtok(NULL,s);
  141. printf("nState: %sn", state);
  142. country = strtok(NULL,s);
  143. printf("nCountry: %sn", country);
  144. email = strtok(NULL,s);
  145. printf("/nEmail: %sn", email);
  146. ladmin = strtok(NULL,s);
  147. printf("nLAdmin: %sn", ladmin);
  148. phone = strtok(NULL,s);
  149. printf("nPhone: %sn", phone);
  150. mobile = strtok(NULL,s);
  151. printf("nMobile: %sn", mobile);
  152. designation = strtok(NULL, s);
  153. printf("nDesignation: %sn", designation);
  154. rmanager = strtok(NULL, s);
  155. printf("nReportingManager: %sn", rmanager);
  156.  
  157. FILE *xml_file;
  158. xml_file = fopen("/var/www/db/db_server_list.xml", "a+");
  159. fseek(xml_file, -8, SEEK_END);
  160.  
  161. ftruncate(fileno(xml_file),ftello(xml_file));
  162.  
  163.  
  164.  
  165.  
  166.  
  167. fprintf(xml_file, "n<server id='%s'>n", sid);
  168. fprintf(xml_file, " <name>%s</name>n", name);
  169. fprintf(xml_file, " <host>%s</host>n", host);
  170. fprintf(xml_file, " <port>%s</port>n", port);
  171. fprintf(xml_file, " <proto>%s</proto>n", proto);
  172. fprintf(xml_file, " <user>%s</user>n", user);
  173. fprintf(xml_file, " <password>%s</password>n", password);
  174. fprintf(xml_file, " <key>%s</key>n", key);
  175. fprintf(xml_file, " <companyName>%s</companyName>n", companyName);
  176. fprintf(xml_file, " <address>n");
  177. fprintf(xml_file, " <officeAddr>%s</officeAddr>n", officeAddress);
  178. fprintf(xml_file, " <state>%s</state>n", state);
  179. fprintf(xml_file, " <country>%s</country>n", country);
  180. fprintf(xml_file, " </address>n");
  181. fprintf(xml_file, " <contacts>n");
  182. fprintf(xml_file, " <ladmin>%s</ladmin>n", ladmin);
  183. fprintf(xml_file, " <phone>%s</phone>n", phone);
  184. fprintf(xml_file, " <mobile>%s</mobile>n", mobile);
  185. fprintf(xml_file, " <email>%s</email>n", email);
  186. fprintf(xml_file, " <designation>%s</designation>n", designation);
  187. fprintf(xml_file, " <rManager>%s</rManager>n", rmanager);
  188. fprintf(xml_file, " </contacts>n");
  189. fprintf(xml_file, " </server>n", key);
  190. fprintf(xml_file, "</data>");
  191.  
  192. fclose(xml_file);
  193. send(new_socket, hello, 17, 0);
  194. printf("nSocket: Sent data to the client!n");
  195. }
  196. else if (token == "policy"){
  197. sip = strtok(NULL, s);
  198. printf("nserverip: %sn", sip);
  199. polname = strtok(NULL, s);
  200. printf("nPolicy Name: %sn", polname);
  201. device = strtok(NULL,s);
  202. printf("nDevice Name: %sn", device);
  203. status = strtok(NULL,s);
  204. printf("nStatus: %sn", status);
  205. srczone = strtok(NULL,s);
  206. printf("nSrcZone: %sn", srczone);
  207. dstzone = strtok(NULL,s);
  208. printf("nDstZone: %sn", dstzone);
  209. srcaddr = strtok(NULL, s);
  210. printf("nSrcAddr: %sn", srcAddr);
  211. dstaddr = strtok(NULL,s);
  212. printf("nDstAddr: %sn", dstAddr);
  213. srcuser = strtok(NULL,s);
  214. printf("nSrcUser: %sn", srcuser);
  215. app = strtok(NULL,s);
  216. printf("nApplication: %sn", app);
  217. service = strtok(NULL,s);
  218. printf("nService: %sn", service);
  219. urlcategory = strtok(NULL,s);
  220. printf("nUrlCategory: %sn", urlcategory);
  221. action = strtok(NULL,s);
  222. printf("/nAction: %sn", action);
  223.  
  224. FILE *xml_file;
  225. xml_file = fopen("/var/www/db/db_policy.xml", "a+");
  226. fseek(xml_file, -14, SEEK_END);
  227.  
  228. ftruncate(fileno(xml_file),ftello(xml_file));
  229.  
  230.  
  231.  
  232.  
  233.  
  234. fprintf(xml_file, "n<policyDecryption>n",);
  235. fprintf(xml_file, " <policyName>%s</policyName>n", polname);
  236. fprintf(xml_file, " <deviceName>%s</deviceName>n", device);
  237. fprintf(xml_file, " <status>%s</status>n", status);
  238. fprintf(xml_file, " <srcZone>%s</srcZone>n", srczone);
  239. fprintf(xml_file, " <dstZone>%s</dstZone>n", dstzone);
  240. fprintf(xml_file, " <srcAddr>%s</srcAddr>n", srcaddr);
  241. fprintf(xml_file, " <dstAddr>%s</dstAddr>n", dstaddr);
  242. fprintf(xml_file, " <srcUser>%s</srcUser>n", srcuser);
  243. fprintf(xml_file, " <application>%s</application>n", app);
  244. fprintf(xml_file, " <service>%s</service>n", service);
  245. fprintf(xml_file, " <urlCategory>%s</urlCategory>n", urlcategory);
  246. fprintf(xml_file, " <action>%s</action>n", action);
  247. fprintf(xml_file, " </policyDecryption>n");
  248. fprintf(xml_file, "</policyList>");
  249.  
  250. fclose(xml_file);
  251. send(new_socket, hello, 17, 0);
  252. printf("nSocket: Sent data to the client!n");
  253.  
  254.  
  255. }
  256. }
  257.  
  258. return 0;
  259. }
Add Comment
Please, Sign In to add comment